3
3
#include "jsmn.h"
4
4
#include "hex.h"
5
5
#include "cursor.h"
6
+ #include "random.h"
6
7
#include "sha256.h"
7
8
#include <stdlib.h>
8
9
#include <limits.h>
@@ -25,7 +26,6 @@ int ndb_builder_init(struct ndb_builder *builder, unsigned char *buf,
25
26
int bufsize )
26
27
{
27
28
struct ndb_note * note ;
28
- struct cursor mem ;
29
29
int half , size , str_indices_size ;
30
30
31
31
// come on bruh
@@ -39,14 +39,14 @@ int ndb_builder_init(struct ndb_builder *builder, unsigned char *buf,
39
39
//debug("size %d half %d str_indices %d\n", size, half, str_indices_size);
40
40
41
41
// make a safe cursor of our available memory
42
- make_cursor (buf , buf + bufsize , & mem );
42
+ make_cursor (buf , buf + bufsize , & builder -> mem );
43
43
44
44
note = builder -> note = (struct ndb_note * )buf ;
45
45
46
46
// take slices of the memory into subcursors
47
- if (!(cursor_slice (& mem , & builder -> note_cur , half ) &&
48
- cursor_slice (& mem , & builder -> strings , half ) &&
49
- cursor_slice (& mem , & builder -> str_indices , str_indices_size ))) {
47
+ if (!(cursor_slice (& builder -> mem , & builder -> note_cur , half ) &&
48
+ cursor_slice (& builder -> mem , & builder -> strings , half ) &&
49
+ cursor_slice (& builder -> mem , & builder -> str_indices , str_indices_size ))) {
50
50
return 0 ;
51
51
}
52
52
@@ -265,7 +265,7 @@ static int ndb_event_commitment(struct ndb_note *ev, unsigned char *buf, int buf
265
265
return cur .p - cur .start ;
266
266
}
267
267
268
- int ndb_calculate_note_id (struct ndb_note * note , unsigned char * buf , int buflen ) {
268
+ int ndb_calculate_id (struct ndb_note * note , unsigned char * buf , int buflen ) {
269
269
int len ;
270
270
271
271
if (!(len = ndb_event_commitment (note , buf , buflen )))
@@ -278,12 +278,50 @@ int ndb_calculate_note_id(struct ndb_note *note, unsigned char *buf, int buflen)
278
278
return 1 ;
279
279
}
280
280
281
+ int ndb_sign_id (secp256k1_context * ctx , struct ndb_keypair * keypair ,
282
+ unsigned char id [32 ], unsigned char sig [64 ])
283
+ {
284
+ unsigned char aux [32 ];
285
+
286
+ if (!fill_random (aux , sizeof (aux )))
287
+ return 0 ;
288
+
289
+ return secp256k1_schnorrsig_sign32 (ctx , sig , id , & keypair -> pair , aux );
290
+ }
291
+
292
+ int ndb_create_keypair (secp256k1_context * ctx , struct ndb_keypair * key )
293
+ {
294
+ secp256k1_xonly_pubkey pubkey ;
295
+
296
+ /* Try to create a keypair with a valid context, it should only
297
+ * fail if the secret key is zero or out of range. */
298
+ if (!secp256k1_keypair_create (ctx , & key -> pair , key -> secret ))
299
+ return 0 ;
300
+
301
+ if (!secp256k1_keypair_xonly_pub (ctx , & pubkey , NULL , & key -> pair ))
302
+ return 0 ;
303
+
304
+ /* Serialize the public key. Should always return 1 for a valid public key. */
305
+ return secp256k1_xonly_pubkey_serialize (ctx , key -> pubkey , & pubkey );
306
+ }
281
307
282
- int ndb_builder_finalize (struct ndb_builder * builder , struct ndb_note * * note )
308
+ int ndb_decode_key (secp256k1_context * ctx , const char * secstr ,
309
+ struct ndb_keypair * keypair )
310
+ {
311
+ if (!hex_decode (secstr , strlen (secstr ), keypair -> secret , 32 )) {
312
+ fprintf (stderr , "could not hex decode secret key\n" );
313
+ return 0 ;
314
+ }
315
+
316
+ return ndb_create_keypair (ctx , keypair );
317
+ }
318
+
319
+ int ndb_builder_finalize (struct ndb_builder * builder , struct ndb_note * * note ,
320
+ struct ndb_keypair * keypair )
283
321
{
284
322
int strings_len = builder -> strings .p - builder -> strings .start ;
285
- unsigned char * end = builder -> note_cur .p + strings_len ;
286
- int total_size = end - builder -> note_cur .start ;
323
+ unsigned char * note_end = builder -> note_cur .p + strings_len ;
324
+ int total_size = note_end - builder -> note_cur .start ;
287
325
288
326
// move the strings buffer next to the end of our ndb_note
289
327
memmove (builder -> note_cur .p , builder -> strings .start , strings_len );
@@ -296,6 +334,22 @@ int ndb_builder_finalize(struct ndb_builder *builder, struct ndb_note **note)
296
334
297
335
* note = builder -> note ;
298
336
337
+ // generate id and sign if we're building this manually
338
+ if (keypair ) {
339
+ // use the remaining memory for building our id buffer
340
+ unsigned char * end = builder -> mem .end ;
341
+ unsigned char * start = (unsigned char * )(* note ) + total_size ;
342
+
343
+ if (!ndb_calculate_id (* note , start , end - start ))
344
+ return 0 ;
345
+
346
+ secp256k1_context * ctx =
347
+ secp256k1_context_create (SECP256K1_CONTEXT_SIGN );
348
+
349
+ if (!ndb_sign_id (ctx , keypair , (* note )-> id , (* note )-> sig ))
350
+ return 0 ;
351
+ }
352
+
299
353
return total_size ;
300
354
}
301
355
@@ -626,7 +680,7 @@ int ndb_note_from_json(const char *json, int len, struct ndb_note **note,
626
680
// sig
627
681
tok = & parser .toks [i + 1 ];
628
682
hex_decode (json + tok -> start , toksize (tok ), hexbuf , sizeof (hexbuf ));
629
- ndb_builder_set_signature (& parser .builder , hexbuf );
683
+ ndb_builder_set_sig (& parser .builder , hexbuf );
630
684
} else if (start [0 ] == 'k' && jsoneq (json , tok , tok_len , "kind" )) {
631
685
// kind
632
686
tok = & parser .toks [i + 1 ];
@@ -668,7 +722,7 @@ int ndb_note_from_json(const char *json, int len, struct ndb_note **note,
668
722
}
669
723
}
670
724
671
- return ndb_builder_finalize (& parser .builder , note );
725
+ return ndb_builder_finalize (& parser .builder , note , NULL );
672
726
}
673
727
674
728
void ndb_builder_set_pubkey (struct ndb_builder * builder , unsigned char * pubkey )
@@ -681,10 +735,9 @@ void ndb_builder_set_id(struct ndb_builder *builder, unsigned char *id)
681
735
memcpy (builder -> note -> id , id , 32 );
682
736
}
683
737
684
- void ndb_builder_set_signature (struct ndb_builder * builder ,
685
- unsigned char * signature )
738
+ void ndb_builder_set_sig (struct ndb_builder * builder , unsigned char * sig )
686
739
{
687
- memcpy (builder -> note -> signature , signature , 64 );
740
+ memcpy (builder -> note -> sig , sig , 64 );
688
741
}
689
742
690
743
void ndb_builder_set_kind (struct ndb_builder * builder , uint32_t kind )
0 commit comments