nostrdb

an unfairly fast embedded nostr database backed by lmdb
git clone git://jb55.com/nostrdb
Log | Files | Refs | Submodules | README | LICENSE

commit c39a33d21544fdff0db6f7a8eda46ff86f5d3e66
parent cee688d5d14c1a3c20f08c2e78ea787997ee0216
Author: William Casarin <jb55@jb55.com>
Date:   Tue, 25 Jul 2023 12:29:55 -0700

type-erase any secp stuff so it's not in the ndb header

Diffstat:
Mnostrdb.c | 25+++++++++++++++++--------
Mnostrdb.h | 18+++++-------------
2 files changed, 22 insertions(+), 21 deletions(-)

diff --git a/nostrdb.c b/nostrdb.c @@ -8,6 +8,16 @@ #include <stdlib.h> #include <limits.h> +#include "secp256k1.h" +#include "secp256k1_ecdh.h" +#include "secp256k1_schnorrsig.h" + +struct ndb_keypair { + unsigned char pubkey[32]; + unsigned char secret[32]; + secp256k1_keypair pair; +}; + struct ndb_json_parser { const char *json; int json_len; @@ -278,7 +288,7 @@ int ndb_calculate_id(struct ndb_note *note, unsigned char *buf, int buflen) { return 1; } -int ndb_sign_id(secp256k1_context *ctx, struct ndb_keypair *keypair, +int ndb_sign_id(void *secp_ctx, struct ndb_keypair *keypair, unsigned char id[32], unsigned char sig[64]) { unsigned char aux[32]; @@ -286,27 +296,26 @@ int ndb_sign_id(secp256k1_context *ctx, struct ndb_keypair *keypair, if (!fill_random(aux, sizeof(aux))) return 0; - return secp256k1_schnorrsig_sign32(ctx, sig, id, &keypair->pair, aux); + return secp256k1_schnorrsig_sign32(secp_ctx, sig, id, &keypair->pair, aux); } -int ndb_create_keypair(secp256k1_context *ctx, struct ndb_keypair *key) +int ndb_create_keypair(void *secp_ctx, struct ndb_keypair *key) { secp256k1_xonly_pubkey pubkey; /* Try to create a keypair with a valid context, it should only * fail if the secret key is zero or out of range. */ - if (!secp256k1_keypair_create(ctx, &key->pair, key->secret)) + if (!secp256k1_keypair_create(secp_ctx, &key->pair, key->secret)) return 0; - if (!secp256k1_keypair_xonly_pub(ctx, &pubkey, NULL, &key->pair)) + if (!secp256k1_keypair_xonly_pub(secp_ctx, &pubkey, NULL, &key->pair)) return 0; /* Serialize the public key. Should always return 1 for a valid public key. */ - return secp256k1_xonly_pubkey_serialize(ctx, key->pubkey, &pubkey); + return secp256k1_xonly_pubkey_serialize(secp_ctx, key->pubkey, &pubkey); } -int ndb_decode_key(secp256k1_context *ctx, const char *secstr, - struct ndb_keypair *keypair) +int ndb_decode_key(void *ctx, const char *secstr, struct ndb_keypair *keypair) { if (!hex_decode(secstr, strlen(secstr), keypair->secret, 32)) { fprintf(stderr, "could not hex decode secret key\n"); diff --git a/nostrdb.h b/nostrdb.h @@ -4,10 +4,6 @@ #include <inttypes.h> #include "cursor.h" -#include "secp256k1.h" -#include "secp256k1_ecdh.h" -#include "secp256k1_schnorrsig.h" - struct ndb_str { unsigned char flag; union { @@ -16,12 +12,8 @@ struct ndb_str { }; }; -struct ndb_keypair { - unsigned char pubkey[32]; - unsigned char secret[32]; - secp256k1_keypair pair; -}; - +struct secp256k1_context; +struct ndb_keypair; // these must be byte-aligned, they are directly accessing the serialized data // representation @@ -91,9 +83,9 @@ struct ndb_iterator { // HELPERS int ndb_calculate_id(struct ndb_note *note, unsigned char *buf, int buflen); -int ndb_sign_id(secp256k1_context *ctx, struct ndb_keypair *keypair, unsigned char id[32], unsigned char sig[64]); -int ndb_create_keypair(secp256k1_context *ctx, struct ndb_keypair *key); -int ndb_decode_key(secp256k1_context *ctx, const char *secstr, struct ndb_keypair *keypair); +int ndb_sign_id(void *secp_ctx, struct ndb_keypair *keypair, unsigned char id[32], unsigned char sig[64]); +int ndb_create_keypair(void *secp_ctx, struct ndb_keypair *key); +int ndb_decode_key(void *secp_ctx, const char *secstr, struct ndb_keypair *keypair); // BUILDER int ndb_note_from_json(const char *json, int len, struct ndb_note **, unsigned char *buf, int buflen);