commit d8e7b4707e7eeac077ee273df0d486f840fe5d0a
parent a51618cfd31a7ffc97b5382434fb083cc42d04b0
Author: William Casarin <jb55@jb55.com>
Date: Thu, 13 Feb 2025 15:13:07 -0800
nostrdb: nip19: add kind to naddr & nevent
Add support for type KIND for bech32-encoded entities naddr and nevent
as specified in NIP-19.
Co-authored-by: kernelkind <kernelkind@gmail.com>
Signed-off-by: William Casarin <jb55@jb55.com>
Diffstat:
2 files changed, 22 insertions(+), 15 deletions(-)
diff --git a/nostrdb/src/nostr_bech32.c b/nostrdb/src/nostr_bech32.c
@@ -9,6 +9,7 @@
#include <stdlib.h>
#include "cursor.h"
#include "str_block.h"
+#include "ccan/endian/endian.h"
#include "nostrdb.h"
#include "bolt11/bech32.h"
@@ -102,9 +103,13 @@ static int add_relay(struct ndb_relays *relays, struct nostr_tlv *tlv)
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 decode_tlv_u32(const struct nostr_tlv *tlv, uint32_t *ret) {
+ if (tlv->len != 4) {
+ return 0;
+ }
+ beint32_t *be32_bytes = (beint32_t*)tlv->value;
+ *ret = be32_to_cpu(*be32_bytes);
+ return 1;
}
static int parse_nostr_bech32_nevent(struct cursor *cur, struct bech32_nevent *nevent) {
@@ -114,7 +119,6 @@ static int parse_nostr_bech32_nevent(struct cursor *cur, struct bech32_nevent *n
nevent->event_id = NULL;
nevent->pubkey = NULL;
nevent->relays.num_relays = 0;
- nevent->has_kind = 0;
for (i = 0; i < MAX_TLVS; i++) {
if (!parse_nostr_tlv(cur, &tlv))
@@ -126,12 +130,6 @@ static int parse_nostr_bech32_nevent(struct cursor *cur, struct bech32_nevent *n
return 0;
nevent->event_id = tlv.value;
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:
if (tlv.len != 32)
return 0;
@@ -140,6 +138,11 @@ static int parse_nostr_bech32_nevent(struct cursor *cur, struct bech32_nevent *n
case TLV_RELAY:
add_relay(&nevent->relays, &tlv);
break;
+ case TLV_KIND:
+ if (decode_tlv_u32(&tlv, &nevent->kind)) {
+ nevent->has_kind = true;
+ }
+ break;
}
}
@@ -169,14 +172,14 @@ static int parse_nostr_bech32_naddr(struct cursor *cur, struct bech32_naddr *nad
if (tlv.len != 32) return 0;
naddr->pubkey = tlv.value;
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:
add_relay(&naddr->relays, &tlv);
break;
+ case TLV_KIND:
+ if (decode_tlv_u32(&tlv, &naddr->kind)) {
+ has_kind = true;
+ }
+ break;
}
}
diff --git a/nostrdb/src/nostrdb.h b/nostrdb/src/nostrdb.h
@@ -368,11 +368,15 @@ struct bech32_nevent {
struct ndb_relays relays;
const unsigned char *event_id;
const unsigned char *pubkey; // optional
+ uint32_t kind;
+ bool has_kind;
};
struct bech32_nprofile {
struct ndb_relays relays;
const unsigned char *pubkey;
+ uint32_t kind;
+ bool has_kind;
};
struct bech32_naddr {