nostrdb/bech32: add some initial tests
since we modified this recently, let's add some tests to make sure we didn't break anything Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
committed by
Daniel D’Aquino
parent
1b09e9458c
commit
c3b06d281e
@@ -216,31 +216,31 @@ int parse_nostr_bech32_buffer(struct cursor *cur,
|
||||
|
||||
switch (obj->type) {
|
||||
case NOSTR_BECH32_NOTE:
|
||||
if (!parse_nostr_bech32_note(cur, &obj->data.note))
|
||||
if (!parse_nostr_bech32_note(cur, &obj->note))
|
||||
return 0;
|
||||
break;
|
||||
case NOSTR_BECH32_NPUB:
|
||||
if (!parse_nostr_bech32_npub(cur, &obj->data.npub))
|
||||
if (!parse_nostr_bech32_npub(cur, &obj->npub))
|
||||
return 0;
|
||||
break;
|
||||
case NOSTR_BECH32_NSEC:
|
||||
if (!parse_nostr_bech32_nsec(cur, &obj->data.nsec))
|
||||
if (!parse_nostr_bech32_nsec(cur, &obj->nsec))
|
||||
return 0;
|
||||
break;
|
||||
case NOSTR_BECH32_NEVENT:
|
||||
if (!parse_nostr_bech32_nevent(cur, &obj->data.nevent))
|
||||
if (!parse_nostr_bech32_nevent(cur, &obj->nevent))
|
||||
return 0;
|
||||
break;
|
||||
case NOSTR_BECH32_NADDR:
|
||||
if (!parse_nostr_bech32_naddr(cur, &obj->data.naddr))
|
||||
if (!parse_nostr_bech32_naddr(cur, &obj->naddr))
|
||||
return 0;
|
||||
break;
|
||||
case NOSTR_BECH32_NPROFILE:
|
||||
if (!parse_nostr_bech32_nprofile(cur, &obj->data.nprofile))
|
||||
if (!parse_nostr_bech32_nprofile(cur, &obj->nprofile))
|
||||
return 0;
|
||||
break;
|
||||
case NOSTR_BECH32_NRELAY:
|
||||
if (!parse_nostr_bech32_nrelay(cur, &obj->data.nrelay))
|
||||
if (!parse_nostr_bech32_nrelay(cur, &obj->nrelay))
|
||||
return 0;
|
||||
break;
|
||||
}
|
||||
@@ -248,36 +248,52 @@ int parse_nostr_bech32_buffer(struct cursor *cur,
|
||||
return 1;
|
||||
}
|
||||
|
||||
int parse_nostr_bech32_str(struct cursor *bech32) {
|
||||
enum nostr_bech32_type type;
|
||||
|
||||
if (!parse_nostr_bech32_type((const char *)bech32->p, &type))
|
||||
int parse_nostr_bech32_str(struct cursor *bech32, enum nostr_bech32_type *type) {
|
||||
if (!parse_nostr_bech32_type((const char *)bech32->p, type))
|
||||
return 0;
|
||||
|
||||
if (!consume_until_non_alphanumeric(bech32, 1))
|
||||
return 0;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
*parsed_len = bech32->p - start;
|
||||
|
||||
// some random sanity checking
|
||||
if (*parsed_len < 10 || *parsed_len > 10000)
|
||||
int parse_nostr_bech32(unsigned char *buf, int buflen,
|
||||
const char *bech32_str, size_t bech32_len,
|
||||
struct nostr_bech32 *obj) {
|
||||
unsigned char *start;
|
||||
size_t parsed_len, u5_out_len, u8_out_len;
|
||||
enum nostr_bech32_type type;
|
||||
static const int MAX_PREFIX = 8;
|
||||
struct cursor cur, bech32;
|
||||
|
||||
make_cursor(buf, buf + buflen, &cur);
|
||||
make_cursor((unsigned char*)bech32_str, (unsigned char*)bech32_str + bech32_len, &bech32);
|
||||
|
||||
start = bech32.p;
|
||||
if (!parse_nostr_bech32_str(&bech32, &type))
|
||||
return 0;
|
||||
|
||||
const char u5[*parsed_len];
|
||||
parsed_len = bech32.p - start;
|
||||
|
||||
// some random sanity checking
|
||||
if (parsed_len < 10 || parsed_len > 10000)
|
||||
return 0;
|
||||
|
||||
unsigned char u5[parsed_len];
|
||||
char prefix[MAX_PREFIX];
|
||||
|
||||
if (bech32_decode_len(prefix, u5, &u5_out_len, (const char*)start,
|
||||
*parsed_len, MAX_PREFIX) == BECH32_ENCODING_NONE)
|
||||
parsed_len, MAX_PREFIX) == BECH32_ENCODING_NONE)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!parse_nostr_bech32_type(prefix, type))
|
||||
if (!bech32_convert_bits(cur.p, &u8_out_len, 8, u5, u5_out_len, 5, 0))
|
||||
return 0;
|
||||
*/
|
||||
|
||||
return 1;
|
||||
return parse_nostr_bech32_buffer(&cur, type, obj);
|
||||
}
|
||||
|
||||
|
||||
@@ -73,16 +73,20 @@ struct nostr_bech32 {
|
||||
struct bech32_nprofile nprofile;
|
||||
struct bech32_naddr naddr;
|
||||
struct bech32_nrelay nrelay;
|
||||
} data;
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
int parse_nostr_bech32_str(struct cursor *bech32);
|
||||
int parse_nostr_bech32_str(struct cursor *bech32, enum nostr_bech32_type *type);
|
||||
int parse_nostr_bech32_type(const char *prefix, enum nostr_bech32_type *type);
|
||||
|
||||
int parse_nostr_bech32_buffer(struct cursor *cur, enum nostr_bech32_type type,
|
||||
struct nostr_bech32 *obj);
|
||||
|
||||
int parse_nostr_bech32(unsigned char *buf, int buflen,
|
||||
const char *bech32_str, size_t bech32_len,
|
||||
struct nostr_bech32 *obj);
|
||||
|
||||
/*
|
||||
int parse_nostr_bech32(const char *bech32, size_t input_len,
|
||||
unsigned char *outbuf, size_t outlen,
|
||||
|
||||
Reference in New Issue
Block a user