commit 954f48b23df64ec395a394f4118ebd800a65c3fe parent cc75a8450a554741ad73c751fc619dabed40b150 Author: William Casarin <jb55@jb55.com> Date: Thu, 25 Jan 2024 14:30:16 -0800 c: move c files into nostrdb in prep for switchover Diffstat:
60 files changed, 24 insertions(+), 414 deletions(-)
diff --git a/damus-c/nostr_bech32.c b/damus-c/nostr_bech32.c @@ -1,325 +0,0 @@ -// -// nostr_bech32.c -// damus -// -// Created by William Casarin on 2023-04-09. -// - -#include "nostr_bech32.h" -#include <stdlib.h> -#include "endian.h" -#include "cursor.h" -#include "bech32.h" -#include <stdbool.h> - -#define MAX_TLVS 16 - -#define TLV_SPECIAL 0 -#define TLV_RELAY 1 -#define TLV_AUTHOR 2 -#define TLV_KIND 3 -#define TLV_KNOWN_TLVS 4 - -struct nostr_tlv { - u8 type; - u8 len; - const u8 *value; -}; - -struct nostr_tlvs { - struct nostr_tlv tlvs[MAX_TLVS]; - int num_tlvs; -}; - -static int parse_nostr_tlv(struct cursor *cur, struct nostr_tlv *tlv) { - // get the tlv tag - if (!pull_byte(cur, &tlv->type)) - return 0; - - // unknown, fail! - if (tlv->type >= TLV_KNOWN_TLVS) - return 0; - - // get the length - if (!pull_byte(cur, &tlv->len)) - return 0; - - // is the reported length greater then our buffer? if so fail - if (cur->p + tlv->len > cur->end) - return 0; - - tlv->value = cur->p; - cur->p += tlv->len; - - return 1; -} - -static int parse_nostr_tlvs(struct cursor *cur, struct nostr_tlvs *tlvs) { - int i; - tlvs->num_tlvs = 0; - - for (i = 0; i < MAX_TLVS; i++) { - if (parse_nostr_tlv(cur, &tlvs->tlvs[i])) { - tlvs->num_tlvs++; - } else { - break; - } - } - - if (tlvs->num_tlvs == 0) - return 0; - - return 1; -} - -static int find_tlv(struct nostr_tlvs *tlvs, u8 type, struct nostr_tlv **tlv) { - *tlv = NULL; - - for (int i = 0; i < tlvs->num_tlvs; i++) { - if (tlvs->tlvs[i].type == type) { - *tlv = &tlvs->tlvs[i]; - return 1; - } - } - - return 0; -} - -static int parse_nostr_bech32_type(const char *prefix, enum nostr_bech32_type *type) { - // Parse type - if (strcmp(prefix, "note") == 0) { - *type = NOSTR_BECH32_NOTE; - return 1; - } else if (strcmp(prefix, "npub") == 0) { - *type = NOSTR_BECH32_NPUB; - return 1; - } else if (strcmp(prefix, "nsec") == 0) { - *type = NOSTR_BECH32_NSEC; - return 1; - } else if (strcmp(prefix, "nprofile") == 0) { - *type = NOSTR_BECH32_NPROFILE; - return 1; - } else if (strcmp(prefix, "nevent") == 0) { - *type = NOSTR_BECH32_NEVENT; - return 1; - } else if (strcmp(prefix, "nrelay") == 0) { - *type = NOSTR_BECH32_NRELAY; - return 1; - } else if (strcmp(prefix, "naddr") == 0) { - *type = NOSTR_BECH32_NADDR; - return 1; - } - - return 0; -} - -static int parse_nostr_bech32_note(struct cursor *cur, struct bech32_note *note) { - return pull_bytes(cur, 32, ¬e->event_id); -} - -static int parse_nostr_bech32_npub(struct cursor *cur, struct bech32_npub *npub) { - return pull_bytes(cur, 32, &npub->pubkey); -} - -static int parse_nostr_bech32_nsec(struct cursor *cur, struct bech32_nsec *nsec) { - return pull_bytes(cur, 32, &nsec->nsec); -} - -static int tlvs_to_relays(struct nostr_tlvs *tlvs, struct relays *relays) { - struct nostr_tlv *tlv; - struct str_block *str; - - relays->num_relays = 0; - - for (int i = 0; i < tlvs->num_tlvs; i++) { - tlv = &tlvs->tlvs[i]; - if (tlv->type != TLV_RELAY) - continue; - - if (relays->num_relays + 1 > MAX_RELAYS) - break; - - str = &relays->relays[relays->num_relays++]; - str->start = (const char*)tlv->value; - str->end = (const char*)(tlv->value + tlv->len); - } - - 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) { - struct nostr_tlvs tlvs; - struct nostr_tlv *tlv; - - if (!parse_nostr_tlvs(cur, &tlvs)) - return 0; - - if (!find_tlv(&tlvs, TLV_SPECIAL, &tlv)) - return 0; - - if (tlv->len != 32) - return 0; - - nevent->event_id = tlv->value; - - if (find_tlv(&tlvs, TLV_AUTHOR, &tlv)) { - nevent->pubkey = tlv->value; - } else { - 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); -} - -static int parse_nostr_bech32_naddr(struct cursor *cur, struct bech32_naddr *naddr) { - struct nostr_tlvs tlvs; - struct nostr_tlv *tlv; - - if (!parse_nostr_tlvs(cur, &tlvs)) - return 0; - - if (!find_tlv(&tlvs, TLV_SPECIAL, &tlv)) - return 0; - - naddr->identifier.start = (const char*)tlv->value; - naddr->identifier.end = (const char*)tlv->value + tlv->len; - - if (!find_tlv(&tlvs, TLV_AUTHOR, &tlv)) - return 0; - - 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); -} - -static int parse_nostr_bech32_nprofile(struct cursor *cur, struct bech32_nprofile *nprofile) { - struct nostr_tlvs tlvs; - struct nostr_tlv *tlv; - - if (!parse_nostr_tlvs(cur, &tlvs)) - return 0; - - if (!find_tlv(&tlvs, TLV_SPECIAL, &tlv)) - return 0; - - if (tlv->len != 32) - return 0; - - nprofile->pubkey = tlv->value; - - return tlvs_to_relays(&tlvs, &nprofile->relays); -} - -static int parse_nostr_bech32_nrelay(struct cursor *cur, struct bech32_nrelay *nrelay) { - struct nostr_tlvs tlvs; - struct nostr_tlv *tlv; - - if (!parse_nostr_tlvs(cur, &tlvs)) - return 0; - - if (!find_tlv(&tlvs, TLV_SPECIAL, &tlv)) - return 0; - - nrelay->relay.start = (const char*)tlv->value; - nrelay->relay.end = (const char*)tlv->value + tlv->len; - - return 1; -} - -int parse_nostr_bech32(struct cursor *cur, struct nostr_bech32 *obj) { - u8 *start, *end; - - start = cur->p; - - if (!consume_until_non_alphanumeric(cur, 1)) { - cur->p = start; - return 0; - } - - end = cur->p; - - size_t data_len; - size_t input_len = end - start; - if (input_len < 10 || input_len > 10000) { - return 0; - } - - obj->buffer = malloc(input_len * 2); - if (!obj->buffer) - return 0; - - u8 data[input_len]; - char prefix[input_len]; - - if (bech32_decode_len(prefix, data, &data_len, (const char*)start, input_len) == BECH32_ENCODING_NONE) { - cur->p = start; - return 0; - } - - obj->buflen = 0; - if (!bech32_convert_bits(obj->buffer, &obj->buflen, 8, data, data_len, 5, 0)) { - goto fail; - } - - if (!parse_nostr_bech32_type(prefix, &obj->type)) { - goto fail; - } - - struct cursor bcur; - make_cursor(obj->buffer, obj->buffer + obj->buflen, &bcur); - - switch (obj->type) { - case NOSTR_BECH32_NOTE: - if (!parse_nostr_bech32_note(&bcur, &obj->data.note)) - goto fail; - break; - case NOSTR_BECH32_NPUB: - if (!parse_nostr_bech32_npub(&bcur, &obj->data.npub)) - goto fail; - break; - case NOSTR_BECH32_NSEC: - if (!parse_nostr_bech32_nsec(&bcur, &obj->data.nsec)) - goto fail; - break; - case NOSTR_BECH32_NEVENT: - if (!parse_nostr_bech32_nevent(&bcur, &obj->data.nevent)) - goto fail; - break; - case NOSTR_BECH32_NADDR: - if (!parse_nostr_bech32_naddr(&bcur, &obj->data.naddr)) - goto fail; - break; - case NOSTR_BECH32_NPROFILE: - if (!parse_nostr_bech32_nprofile(&bcur, &obj->data.nprofile)) - goto fail; - break; - case NOSTR_BECH32_NRELAY: - if (!parse_nostr_bech32_nrelay(&bcur, &obj->data.nrelay)) - goto fail; - break; - } - - return 1; - -fail: - free(obj->buffer); - cur->p = start; - return 0; -} diff --git a/damus-c/nostr_bech32.h b/damus-c/nostr_bech32.h @@ -1,89 +0,0 @@ -// -// nostr_bech32.h -// damus -// -// Created by William Casarin on 2023-04-09. -// - -#ifndef nostr_bech32_h -#define nostr_bech32_h - -#include <stdio.h> -#include "str_block.h" -#include "cursor.h" -#include <stdbool.h> - -typedef unsigned char u8; -#define MAX_RELAYS 10 - -struct relays { - struct str_block relays[MAX_RELAYS]; - int num_relays; -}; - -enum nostr_bech32_type { - NOSTR_BECH32_NOTE = 1, - NOSTR_BECH32_NPUB = 2, - NOSTR_BECH32_NPROFILE = 3, - NOSTR_BECH32_NEVENT = 4, - NOSTR_BECH32_NRELAY = 5, - NOSTR_BECH32_NADDR = 6, - NOSTR_BECH32_NSEC = 7, -}; - -struct bech32_note { - const u8 *event_id; -}; - -struct bech32_npub { - const u8 *pubkey; -}; - -struct bech32_nsec { - const u8 *nsec; -}; - -struct bech32_nevent { - struct relays relays; - const u8 *event_id; - const u8 *pubkey; // optional - uint32_t kind; - bool has_kind; -}; - -struct bech32_nprofile { - struct relays relays; - const u8 *pubkey; -}; - -struct bech32_naddr { - struct relays relays; - struct str_block identifier; - const u8 *pubkey; - uint32_t kind; -}; - -struct bech32_nrelay { - struct str_block relay; -}; - -typedef struct nostr_bech32 { - enum nostr_bech32_type type; - u8 *buffer; // holds strings and tlv stuff - size_t buflen; - - union { - struct bech32_note note; - struct bech32_npub npub; - struct bech32_nsec nsec; - struct bech32_nevent nevent; - struct bech32_nprofile nprofile; - struct bech32_naddr naddr; - struct bech32_nrelay nrelay; - } data; -} nostr_bech32_t; - - -int parse_nostr_bech32(struct cursor *cur, struct nostr_bech32 *obj); - -#endif /* nostr_bech32_h */ diff --git a/damus-c/alignof.h b/nostrdb/alignof.h diff --git a/damus-c/amount.c b/nostrdb/amount.c diff --git a/damus-c/amount.h b/nostrdb/amount.h diff --git a/damus-c/array_size.h b/nostrdb/array_size.h diff --git a/damus-c/bech32.c b/nostrdb/bech32.c diff --git a/damus-c/bech32.h b/nostrdb/bech32.h diff --git a/damus-c/bech32_util.c b/nostrdb/bech32_util.c diff --git a/damus-c/bech32_util.h b/nostrdb/bech32_util.h diff --git a/damus-c/block.h b/nostrdb/block.h diff --git a/damus-c/bolt11.c b/nostrdb/bolt11.c diff --git a/damus-c/bolt11.h b/nostrdb/bolt11.h diff --git a/damus-c/build_assert.h b/nostrdb/build_assert.h diff --git a/damus-c/check_type.h b/nostrdb/check_type.h diff --git a/damus-c/config.h b/nostrdb/config.h diff --git a/damus-c/container_of.h b/nostrdb/container_of.h diff --git a/damus-c/cppmagic.h b/nostrdb/cppmagic.h diff --git a/damus-c/damus-Bridging-Header.h b/nostrdb/damus-Bridging-Header.h diff --git a/damus-c/damus.c b/nostrdb/damus.c diff --git a/damus-c/damus.h b/nostrdb/damus.h diff --git a/damus-c/debug.h b/nostrdb/debug.h diff --git a/damus-c/endian.h b/nostrdb/endian.h diff --git a/damus-c/error.c b/nostrdb/error.c diff --git a/damus-c/error.h b/nostrdb/error.h diff --git a/damus-c/hash_u5.c b/nostrdb/hash_u5.c diff --git a/damus-c/hash_u5.h b/nostrdb/hash_u5.h diff --git a/damus-c/hex.c b/nostrdb/hex.c diff --git a/damus-c/hex.h b/nostrdb/hex.h diff --git a/damus-c/likely.h b/nostrdb/likely.h diff --git a/damus-c/list.c b/nostrdb/list.c diff --git a/damus-c/list.h b/nostrdb/list.h diff --git a/damus-c/mem.c b/nostrdb/mem.c diff --git a/damus-c/mem.h b/nostrdb/mem.h diff --git a/damus-c/node_id.c b/nostrdb/node_id.c diff --git a/damus-c/node_id.h b/nostrdb/node_id.h diff --git a/nostrdb/nostr_bech32.c b/nostrdb/nostr_bech32.c @@ -7,8 +7,10 @@ #include "nostr_bech32.h" #include <stdlib.h> +#include "endian.h" #include "cursor.h" #include "bech32.h" +#include <stdbool.h> #define MAX_TLVS 16 @@ -145,6 +147,11 @@ static int tlvs_to_relays(struct nostr_tlvs *tlvs, struct relays *relays) { 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) { struct nostr_tlvs tlvs; struct nostr_tlv *tlv; @@ -166,6 +173,13 @@ static int parse_nostr_bech32_nevent(struct cursor *cur, struct bech32_nevent *n 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); } @@ -187,6 +201,11 @@ static int parse_nostr_bech32_naddr(struct cursor *cur, struct bech32_naddr *nad 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); } diff --git a/nostrdb/nostr_bech32.h b/nostrdb/nostr_bech32.h @@ -11,6 +11,8 @@ #include <stdio.h> #include "str_block.h" #include "cursor.h" +#include <stdbool.h> + typedef unsigned char u8; #define MAX_RELAYS 10 @@ -45,6 +47,8 @@ struct bech32_nevent { struct relays relays; const u8 *event_id; const u8 *pubkey; // optional + uint32_t kind; + bool has_kind; }; struct bech32_nprofile { @@ -56,6 +60,7 @@ struct bech32_naddr { struct relays relays; struct str_block identifier; const u8 *pubkey; + uint32_t kind; }; struct bech32_nrelay { diff --git a/damus-c/overflows.h b/nostrdb/overflows.h diff --git a/damus-c/parser.h b/nostrdb/parser.h diff --git a/damus-c/sha256.c b/nostrdb/sha256.c diff --git a/damus-c/sha256.h b/nostrdb/sha256.h diff --git a/damus-c/short_types.h b/nostrdb/short_types.h diff --git a/damus-c/str.h b/nostrdb/str.h diff --git a/damus-c/str_block.h b/nostrdb/str_block.h diff --git a/damus-c/str_debug.h b/nostrdb/str_debug.h diff --git a/damus-c/structeq.h b/nostrdb/structeq.h diff --git a/damus-c/take.c b/nostrdb/take.c diff --git a/damus-c/take.h b/nostrdb/take.h diff --git a/damus-c/tal.c b/nostrdb/tal.c diff --git a/damus-c/tal.h b/nostrdb/tal.h diff --git a/damus-c/talstr.c b/nostrdb/talstr.c diff --git a/damus-c/talstr.h b/nostrdb/talstr.h diff --git a/damus-c/typedefs.h b/nostrdb/typedefs.h diff --git a/damus-c/typesafe_cb.h b/nostrdb/typesafe_cb.h diff --git a/damus-c/utf8.c b/nostrdb/utf8.c diff --git a/damus-c/utf8.h b/nostrdb/utf8.h diff --git a/damus-c/varint.h b/nostrdb/varint.h diff --git a/damus-c/wasm.c b/nostrdb/wasm.c diff --git a/damus-c/wasm.h b/nostrdb/wasm.h