nostrdb: port kernelkind's to the new bech32 parser

Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
William Casarin
2024-02-01 14:36:59 -08:00
committed by Daniel D’Aquino
parent b128330b2a
commit b8bef86ea1
2 changed files with 24 additions and 2 deletions

View File

@@ -102,6 +102,11 @@ static int add_relay(struct ndb_relays *relays, struct nostr_tlv *tlv)
return 1; return 1;
} }
static uint32_t decode_tlv_u32(const uint8_t *bytes) {
beint32_t *be32_bytes = (beint32_t*)bytes;
return be32_to_cpu(*be32_bytes);
}
static int parse_nostr_bech32_nevent(struct cursor *cur, struct bech32_nevent *nevent) { static int parse_nostr_bech32_nevent(struct cursor *cur, struct bech32_nevent *nevent) {
struct nostr_tlv tlv; struct nostr_tlv tlv;
int i; int i;
@@ -109,6 +114,7 @@ static int parse_nostr_bech32_nevent(struct cursor *cur, struct bech32_nevent *n
nevent->event_id = NULL; nevent->event_id = NULL;
nevent->pubkey = NULL; nevent->pubkey = NULL;
nevent->relays.num_relays = 0; nevent->relays.num_relays = 0;
nevent->has_kind = 0;
for (i = 0; i < MAX_TLVS; i++) { for (i = 0; i < MAX_TLVS; i++) {
if (!parse_nostr_tlv(cur, &tlv)) if (!parse_nostr_tlv(cur, &tlv))
@@ -120,6 +126,12 @@ static int parse_nostr_bech32_nevent(struct cursor *cur, struct bech32_nevent *n
return 0; return 0;
nevent->event_id = tlv.value; nevent->event_id = tlv.value;
break; break;
case TLV_KIND:
if (tlv.len != 4)
return 0;
nevent->kind = decode_tlv-U32(tlv.value);
nevent->has_kind = 1;
break;
case TLV_AUTHOR: case TLV_AUTHOR:
if (tlv.len != 32) if (tlv.len != 32)
return 0; return 0;
@@ -141,6 +153,7 @@ static int parse_nostr_bech32_naddr(struct cursor *cur, struct bech32_naddr *nad
naddr->identifier.str = NULL; naddr->identifier.str = NULL;
naddr->identifier.len = 0; naddr->identifier.len = 0;
naddr->pubkey = NULL; naddr->pubkey = NULL;
naddr->has_kind = 0;
naddr->relays.num_relays = 0; naddr->relays.num_relays = 0;
for (i = 0; i < MAX_TLVS; i++) { for (i = 0; i < MAX_TLVS; i++) {
@@ -156,6 +169,11 @@ static int parse_nostr_bech32_naddr(struct cursor *cur, struct bech32_naddr *nad
if (tlv.len != 32) return 0; if (tlv.len != 32) return 0;
naddr->pubkey = tlv.value; naddr->pubkey = tlv.value;
break; break;
case TLV_KIND:
if (tlv.len != 4) return 0;
naddr->kind = decode_tlv_u32(tlv.value);
naddr->has_kind = 1;
break;
case TLV_RELAY: case TLV_RELAY:
add_relay(&naddr->relays, &tlv); add_relay(&naddr->relays, &tlv);
break; break;

View File

@@ -337,6 +337,8 @@ struct bech32_nevent {
struct ndb_relays relays; struct ndb_relays relays;
const unsigned char *event_id; const unsigned char *event_id;
const unsigned char *pubkey; // optional const unsigned char *pubkey; // optional
uint32_t kind;
int has_kind;
}; };
struct bech32_nprofile { struct bech32_nprofile {
@@ -348,13 +350,15 @@ struct bech32_naddr {
struct ndb_relays relays; struct ndb_relays relays;
struct ndb_str_block identifier; struct ndb_str_block identifier;
const unsigned char *pubkey; const unsigned char *pubkey;
uint32_t kind;
int has_kind;
}; };
struct bech32_nrelay { struct bech32_nrelay {
struct ndb_str_block relay; struct ndb_str_block relay;
}; };
struct nostr_bech32 { typedef struct nostr_bech32 {
enum nostr_bech32_type type; enum nostr_bech32_type type;
union { union {
@@ -366,7 +370,7 @@ struct nostr_bech32 {
struct bech32_naddr naddr; struct bech32_naddr naddr;
struct bech32_nrelay nrelay; struct bech32_nrelay nrelay;
}; };
}; } nostr_bech32_t;
struct ndb_mention_bech32_block { struct ndb_mention_bech32_block {