1
0
mirror of git://jb55.com/damus synced 2024-09-29 16:30:44 +00:00

nip19: add kind to naddr & nevent

Add support for type KIND for bech32-encoded entities naddr and nevent
as specified in NIP-19.

LNURL1DP68GURN8GHJ7EM9W3SKCCNE9E3K7MF0D3H82UNVWQHKWUN9V4HXGCTHDC6RZVGR8SW3G

Signed-off-by: kernelkind <kernelkind@gmail.com>
Reviewed-by: William Casarin <jb55@jb55.com>
Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
kernelkind 2024-01-08 18:26:49 -05:00 committed by William Casarin
parent d49cf5a505
commit 97fc415b8c
2 changed files with 24 additions and 0 deletions

View File

@ -7,8 +7,10 @@
#include "nostr_bech32.h" #include "nostr_bech32.h"
#include <stdlib.h> #include <stdlib.h>
#include "endian.h"
#include "cursor.h" #include "cursor.h"
#include "bech32.h" #include "bech32.h"
#include <stdbool.h>
#define MAX_TLVS 16 #define MAX_TLVS 16
@ -145,6 +147,11 @@ static int tlvs_to_relays(struct nostr_tlvs *tlvs, struct relays *relays) {
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_tlvs tlvs; struct nostr_tlvs tlvs;
struct nostr_tlv *tlv; struct nostr_tlv *tlv;
@ -166,6 +173,13 @@ static int parse_nostr_bech32_nevent(struct cursor *cur, struct bech32_nevent *n
nevent->pubkey = NULL; nevent->pubkey = NULL;
} }
if(find_tlv(&tlvs, TLV_KIND, &tlv)) {
nevent->kind = decode_tlv_u32(tlv->value);
nevent->has_kind = true;
} else {
nevent->has_kind = false;
}
return tlvs_to_relays(&tlvs, &nevent->relays); return tlvs_to_relays(&tlvs, &nevent->relays);
} }
@ -187,6 +201,11 @@ static int parse_nostr_bech32_naddr(struct cursor *cur, struct bech32_naddr *nad
naddr->pubkey = tlv->value; naddr->pubkey = tlv->value;
if(!find_tlv(&tlvs, TLV_KIND, &tlv)) {
return 0;
}
naddr->kind = decode_tlv_u32(tlv->value);
return tlvs_to_relays(&tlvs, &naddr->relays); return tlvs_to_relays(&tlvs, &naddr->relays);
} }

View File

@ -11,6 +11,8 @@
#include <stdio.h> #include <stdio.h>
#include "str_block.h" #include "str_block.h"
#include "cursor.h" #include "cursor.h"
#include <stdbool.h>
typedef unsigned char u8; typedef unsigned char u8;
#define MAX_RELAYS 10 #define MAX_RELAYS 10
@ -45,6 +47,8 @@ struct bech32_nevent {
struct relays relays; struct relays relays;
const u8 *event_id; const u8 *event_id;
const u8 *pubkey; // optional const u8 *pubkey; // optional
uint32_t kind;
bool has_kind;
}; };
struct bech32_nprofile { struct bech32_nprofile {
@ -56,6 +60,7 @@ struct bech32_naddr {
struct relays relays; struct relays relays;
struct str_block identifier; struct str_block identifier;
const u8 *pubkey; const u8 *pubkey;
uint32_t kind;
}; };
struct bech32_nrelay { struct bech32_nrelay {