-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathtest.c
More file actions
3299 lines (2654 loc) · 111 KB
/
test.c
File metadata and controls
3299 lines (2654 loc) · 111 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "nostrdb.h"
#include "hex.h"
#include "io.h"
#include "bolt11/bolt11.h"
#include "invoice.h"
#include "block.h"
#include "protected_queue.h"
#include "memchr.h"
#include "print_util.h"
#include "bindings/c/profile_reader.h"
#include "bindings/c/profile_verifier.h"
#include "bindings/c/meta_reader.h"
#include "bindings/c/meta_verifier.h"
#include "secp256k1.h"
#include <stdio.h>
#include <assert.h>
#include <unistd.h>
#include <sys/stat.h>
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
int ndb_print_kind_keys(struct ndb_txn *txn);
#define TEST_DIR "./testdata/db"
static const char *test_dir = TEST_DIR;
static void delete_test_db() {
// Delete ./testdata/db/data.mdb
unlink(TEST_DIR "/data.mdb");
unlink(TEST_DIR "/data.lock");
}
int ndb_rebuild_reaction_metadata(struct ndb_txn *txn, const unsigned char *note_id, struct ndb_note_meta_builder *builder, uint32_t *count);
int ndb_count_replies(struct ndb_txn *txn, const unsigned char *note_id, uint16_t *direct_replies, uint32_t *thread_replies);
static void db_load_events(struct ndb *ndb, const char *filename)
{
size_t filesize;
int written;
char *json;
struct stat st;
stat(filename, &st);
filesize = st.st_size;
json = malloc(filesize + 1); // +1 for '\0' if you need it null-terminated
read_file(filename, (unsigned char*)json, filesize, &written);
assert(ndb_process_client_events(ndb, json, written));
free(json);
}
static NdbProfile_table_t lookup_profile(struct ndb_txn *txn, uint64_t pk)
{
void *root;
size_t len;
assert((root = ndb_get_profile_by_key(txn, pk, &len)));
assert(root);
NdbProfileRecord_table_t profile_record = NdbProfileRecord_as_root(root);
NdbProfile_table_t profile = NdbProfileRecord_profile_get(profile_record);
return profile;
}
static void print_search(struct ndb_txn *txn, struct ndb_search *search)
{
NdbProfile_table_t profile = lookup_profile(txn, search->profile_key);
const char *name = NdbProfile_name_get(profile);
const char *display_name = NdbProfile_display_name_get(profile);
printf("searched_name name:'%s' display_name:'%s' pk:%" PRIu64 " ts:%" PRIu64 " id:", name, display_name, search->profile_key, search->key->timestamp);
print_hex(search->key->id, 32);
printf("\n");
}
static void test_count_metadata()
{
struct ndb *ndb;
struct ndb_config config;
struct ndb_txn txn;
struct ndb_note_meta *meta;
//struct ndb_note_meta_entry *counts;
struct ndb_note_meta_entry *entry;
uint16_t count, direct_replies[2];
uint32_t total_reactions, reactions, thread_replies[2];
int i;
reactions = 0;
delete_test_db();
ndb_default_config(&config);
assert(ndb_init(&ndb, test_dir, &config));
const unsigned char id[] = {
0xd4, 0x4a, 0xd9, 0x6c, 0xb8, 0x92, 0x40, 0x92, 0xa7, 0x6b, 0xc2, 0xaf,
0xdd, 0xeb, 0x12, 0xeb, 0x85, 0x23, 0x3c, 0x0d, 0x03, 0xa7, 0xd9, 0xad,
0xc4, 0x2c, 0x2a, 0x85, 0xa7, 0x9a, 0x43, 0x05
};
db_load_events(ndb, "testdata/test_counts.json");
/* consume all events to ensure we're done processing */
ndb_destroy(ndb);
ndb_init(&ndb, test_dir, &config);
ndb_begin_query(ndb, &txn);
meta = ndb_get_note_meta(&txn, id);
assert(meta);
count = ndb_note_meta_entries_count(meta);
entry = ndb_note_meta_entries(meta);
for (i = 0; i < count; i++) {
entry = ndb_note_meta_entry_at(meta, i);
if (*ndb_note_meta_entry_type(entry) == NDB_NOTE_META_REACTION) {
reactions += *ndb_note_meta_reaction_count(entry);
}
}
entry = ndb_note_meta_find_entry(meta, NDB_NOTE_META_COUNTS, NULL);
assert(entry);
assert(*ndb_note_meta_counts_quotes(entry) == 2);
thread_replies[0] = *ndb_note_meta_counts_thread_replies(entry);
printf("\t# thread replies %d\n", thread_replies[0]);
assert(thread_replies[0] == 93);
direct_replies[0] = *ndb_note_meta_counts_direct_replies(entry);
printf("\t# direct replies %d\n", direct_replies[0]);
assert(direct_replies[0] == 83);
total_reactions = *ndb_note_meta_counts_total_reactions(entry);
printf("\t# total reactions %d\n", reactions);
assert(total_reactions > 0);
printf("\t# reactions %d\n", reactions);
assert(reactions > 0);
assert(total_reactions == reactions);
ndb_end_query(&txn);
ndb_begin_query(ndb, &txn);
/* this is used in the migration code,
* let's make sure it matches the online logic */
ndb_rebuild_reaction_metadata(&txn, id, NULL, &reactions);
printf("\t# after-counted reactions %d\n", reactions);
assert(reactions == total_reactions);
ndb_count_replies(&txn, id, &direct_replies[1], &thread_replies[1]);
printf("\t# after-counted replies direct:%d thread:%d\n", direct_replies[1], thread_replies[1]);
assert(direct_replies[0] == direct_replies[1]);
assert(thread_replies[0] == thread_replies[1]);
ndb_end_query(&txn);
ndb_destroy(ndb);
delete_test_db();
printf("ok test_count_metadata\n");
}
static void test_nip44_test_vector()
{
/*
{
"sec1": "0000000000000000000000000000000000000000000000000000000000000001",
"sec2": "0000000000000000000000000000000000000000000000000000000000000002",
"conversation_key": "c41c775356fd92eadc63ff5a0dc1da211b268cbea22316767095b2871ea1412d",
"nonce": "0000000000000000000000000000000000000000000000000000000000000001",
"plaintext": "a",
"payload": "AgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABee0G5VSK0/9YypIObAtDKfYEAjD35uVkHyB0F4DwrcNaCXlCWZKaArsGrY6M9wnuTMxWfp1RTN9Xga8no+kF5Vsb"
}
*/
/*
static const unsigned char recv_sec[32] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x01
};
static const unsigned char sender_pub[32] = {
0xc6, 0x04, 0x7f, 0x94, 0x41, 0xed, 0x7d, 0x6d, 0x30, 0x45,
0x40, 0x6e, 0x95, 0xc0, 0x7c, 0xd8, 0x5c, 0x77, 0x8e, 0x4b,
0x8c, 0xef, 0x3c, 0xa7, 0xab, 0xac, 0x09, 0xb9, 0x5c, 0x70,
0x9e, 0xe5
};
*/
static const unsigned char recv_sec[32] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x02
};
static const unsigned char sender_pub[32] = {
0x79, 0xbe, 0x66, 0x7e, 0xf9, 0xdc, 0xbb, 0xac, 0x55, 0xa0,
0x62, 0x95, 0xce, 0x87, 0x0b, 0x07, 0x02, 0x9b, 0xfc, 0xdb,
0x2d, 0xce, 0x28, 0xd9, 0x59, 0xf2, 0x81, 0x5b, 0x16, 0xf8,
0x17, 0x98
};
static const char payload[] = "AgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABee0G5VSK0/9YypIObAtDKfYEAjD35uVkHyB0F4DwrcNaCXlCWZKaArsGrY6M9wnuTMxWfp1RTN9Xga8no+kF5Vsb";
static const char *plaintext = "a";
unsigned char buffer[1024];
unsigned char *decrypted;
uint16_t decrypted_len;
enum ndb_decrypt_result res;
secp256k1_context *context;
context = secp256k1_context_create(SECP256K1_CONTEXT_NONE);
res = nip44_decrypt(context,
sender_pub, recv_sec,
payload, sizeof(payload)-1,
buffer, sizeof(buffer),
&decrypted, &decrypted_len);
if (res != NIP44_OK) {
fprintf(stderr, "nip44 error: %s\n", nip44_err_msg(res));
}
//printf("# decrypted(%d) '%.*s'\n", decrypted_len, decrypted_len, (char*)decrypted);
assert(res == NIP44_OK);
assert(memcmp(decrypted, plaintext, 1) == 0);
secp256k1_context_destroy(context);
printf("ok test_nip44_test_vector\n");
}
static void test_nip44_decrypt()
{
static const unsigned char recv_sec[32] = {
0x81, 0xa5, 0x33, 0x9b, 0xf9, 0xfa, 0xf8, 0x91, 0x5c, 0x48,
0xf3, 0xdd, 0xca, 0xd4, 0xd3, 0xa5, 0x54, 0x06, 0xc7, 0x7b,
0x27, 0x81, 0xc8, 0xc9, 0x94, 0x5d, 0xfd, 0xad, 0xe6, 0xae,
0x11, 0x89
};
/* 81a5339bf9faf8915c48f3ddcad4d3a55406c77b2781c8c9945dfdade6ae1189 */
/*
static const unsigned char recv_pub[32] = {
0xf1, 0xec, 0xfe, 0xb2, 0xed, 0xa7, 0xe7, 0x7c, 0xd7, 0x2e,
0x63, 0x3c, 0x54, 0x12, 0xae, 0x99, 0x07, 0xb5, 0x0a, 0xd1,
0xaf, 0xd8, 0x16, 0xab, 0x5a, 0x61, 0xac, 0x6f, 0x2c, 0x3c,
0x6c, 0xb3
};
*/
static const unsigned char sender_pub[32] = {
0x32, 0xe1, 0x82, 0x76, 0x35, 0x45, 0x0e, 0xbb, 0x3c, 0x5a,
0x7d, 0x12, 0xc1, 0xf8, 0xe7, 0xb2, 0xb5, 0x14, 0x43, 0x9a,
0xc1, 0x0a, 0x67, 0xee, 0xf3, 0xd9, 0xfd, 0x9c, 0x5c, 0x68,
0xe2, 0x45
};
static const char encrypted[] = "Ake9vGvum3cJJrF62qHwVDb3HC3UEP2t0G1GLByJlXi7OP4awMDomJej0aVkiZUHYPJvqgsbL30BZwBCfkd8F1596jJcOFC5KUXsCb45mHS+EqA5rs13/37HlImp76PO/1ns";
//static const char encrypted[] = "AknLKK+v4mZyMpShz5+/iyADJk4SUEQwGV7BUWQRmCHDD6KBO7VStk16StpF4TFTGrEV1BmkZVSlNYVu4cl9C0egBXL0X9AzJBM0I3wotSUQEZwGXwnts0HdhFAQepq4kmrc";
static const char *expected = "hello, test.c";
unsigned char buffer[1024];
unsigned char *decrypted;
uint16_t decrypted_len;
enum ndb_decrypt_result res;
secp256k1_context *context;
context = secp256k1_context_create(SECP256K1_CONTEXT_NONE);
res = nip44_decrypt(context,
sender_pub, recv_sec,
encrypted, sizeof(encrypted)-1,
buffer, sizeof(buffer),
&decrypted, &decrypted_len);
if (res != NIP44_OK) {
fprintf(stderr, "nip44 error: %s\n", nip44_err_msg(res));
}
assert(res == NIP44_OK);
assert(strcmp((char *)decrypted, expected) == 0);
printf("ok test_nip44_decrypt\n");
secp256k1_context_destroy(context);
}
static void test_nip44_round_trip()
{
static const unsigned char send_sec[32] = {
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
};
static const unsigned char recv_sec[32] = {
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2
};
static const unsigned char send_pub[32] = {
0x79, 0xbe, 0x66, 0x7e, 0xf9, 0xdc, 0xbb, 0xac, 0x55, 0xa0,
0x62, 0x95, 0xce, 0x87, 0x0b, 0x07, 0x02, 0x9b, 0xfc, 0xdb,
0x2d, 0xce, 0x28, 0xd9, 0x59, 0xf2, 0x81, 0x5b, 0x16, 0xf8,
0x17, 0x98
};
static const unsigned char recv_pub[32] = {
0xc6, 0x04, 0x7f, 0x94, 0x41, 0xed, 0x7d, 0x6d, 0x30, 0x45,
0x40, 0x6e, 0x95, 0xc0, 0x7c, 0xd8, 0x5c, 0x77, 0x8e, 0x4b,
0x8c, 0xef, 0x3c, 0xa7, 0xab, 0xac, 0x09, 0xb9, 0x5c, 0x70,
0x9e, 0xe5
};
static const char plaintext[] = "hello, world";
unsigned char buf[1024];
unsigned char buf2[1024];
char *out, *plaintext_out;
ssize_t out_len;
int ok;
uint16_t plaintext_len;
secp256k1_context *context;
context = secp256k1_context_create(SECP256K1_CONTEXT_NONE);
ok = nip44_encrypt(context, send_sec, recv_pub,
(const unsigned char *)plaintext, sizeof(plaintext)-1,
buf, sizeof(buf), &out, &out_len);
if (ok != NIP44_OK)
printf("nip44 encrypt err: %s\n", nip44_err_msg(ok));
assert(ok == NIP44_OK);
ok = nip44_decrypt(context, send_pub, recv_sec,
out, out_len,
buf2, sizeof(buf2),
(unsigned char**)&plaintext_out, &plaintext_len);
if (ok != NIP44_OK)
printf("nip44 decrypt err: %s\n", nip44_err_msg(ok));
assert(ok == NIP44_OK);
assert(plaintext_len == sizeof(plaintext)-1);
assert(!strcmp(plaintext, plaintext_out));
secp256k1_context_destroy(context);
}
static void kind_filter(struct ndb_filter *f, uint64_t kind)
{
ndb_filter_init(f);
ndb_filter_start_field(f, NDB_FILTER_KINDS);
ndb_filter_add_int_element(f, kind);
ndb_filter_end_field(f);
ndb_filter_end(f);
}
static void test_giftwrap_reprocess()
{
struct ndb *ndb;
struct ndb_filter filter;
struct ndb_config config;
struct ndb_txn txn;
struct ndb_note *rumor, *giftwrap;
int ok;
uint64_t subid;
ndb_default_config(&config);
const char *giftwrap_json;
uint64_t note_ids[2];
char buf[4096];
static unsigned char recv_sec[32] = {
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2
};
static const unsigned char recv_pub[32] = {
0xc6, 0x04, 0x7f, 0x94, 0x41, 0xed, 0x7d, 0x6d, 0x30, 0x45,
0x40, 0x6e, 0x95, 0xc0, 0x7c, 0xd8, 0x5c, 0x77, 0x8e, 0x4b,
0x8c, 0xef, 0x3c, 0xa7, 0xab, 0xac, 0x09, 0xb9, 0x5c, 0x70,
0x9e, 0xe5
};
static const unsigned char giftwrap_id[32] = {
0x94, 0x1e, 0xaf, 0x4a, 0x9b, 0xd0, 0x09, 0x0a, 0xb5, 0xd4,
0x14, 0xff, 0x5a, 0x68, 0x25, 0x4d, 0xa5, 0x78, 0x6f, 0x0b,
0xf8, 0xb8, 0xe1, 0x56, 0xbc, 0x37, 0xa7, 0x7e, 0xa2, 0x68,
0x0a, 0x92
};
delete_test_db();
assert(ndb_init(&ndb, test_dir, &config));
//ndb_add_key(ndb, one);
//ndb_add_key(ndb, recv_sec);
kind_filter(&filter, 1059);
subid = ndb_subscribe(ndb, &filter, 1);
giftwrap_json = "{\"id\":\"941eaf4a9bd0090ab5d414ff5a68254da5786f0bf8b8e156bc37a77ea2680a92\",\"pubkey\":\"5fac96633ffd0f68a037778dc40d7747ddf0ceaadb7986c23c0597a56b9ab6f6\",\"created_at\":1764513655,\"kind\":1059,\"tags\":[[\"p\",\"c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5\"]],\"content\":\"AmoRXHPZ6cYI2YR1PL1J2BpxY8onrJHUvt3cOhAe1IOIk6wnighedqyUqQsJ/Bnq0qAAkldMipUuHmr1TRNeu/3Rf3AZ1+4N6/eSJb2GcVaaWkqJ6DIIW+YHvQZRuS/1ef3lo41+2zOFqJMvLiz/Pw1kLdDdi5pKWy0QHJIKFPuIE9XNhFMTMo+rYSIlpfiAXtzpM6qETFBJxabmaZOMlgnxQG6Mm2gB3Y6rmj9gLgxsPCaOnvmfi5xhA+jRvz8FYQ+WIU1ij7SFsssz27fISRRIMEOoq7CHPcsdY2Ly/PSp1t+aLpSz1e8Chnhf3lq/Y0mumLDS/BzNaFm38wQqCoWbxrn+7iLTMfINXoDm7gY6Qb4eZc/Am/wjRJsHm4F37bPiCAupzYWOPdJWO3a4TPmGaHk6MmyrHRu4V20iZK+svdJ257RuwMrYk39xCWNG985t1acPc5Of+ZKDnUcL5jVw0ZUOTrV10uq/UVoOHwDkz0mcsJ0Adhc0fKgiE7CWZgKbaO0PIaAL0I+2hs350dcEK3Go8vzMfiN1Uc7NUUMpjQzIRJC1J6CVf6HkvvQ+JD52RhXsVZN8TTkrpxEDqvCm8eSpamqIZuMbrTh5jJ/J+S83dI8rHvyAcmGrRes5wMOiVaFPglreO/H//AgvcR+N/zYOrV4qCWboU+oTBz7A/ZzMGGt6mhL4dmDKDW3p/HT0l5dEfAn71fdx/VH+jXquVr/9rCw2F4kLzXiNB6E3OBxp7bjkgnmAWCXeNL8NRcOT2l6LaF1l+xi3+NJGxP6tdry+OYz/C12jAZ223HWtQv62fLS3wAuwJH3QjrYromeWU+MNyTdMEFRyuxMq9mpYno7tYF6FpVAR7w9oIZME7F36+1I2b7MERXsgioEBGJFWotC9v5nEKWLJJajZFPlsFQf96Y6Cwovd4moVNmDJ8s2riRDx9D9NxDJxNkz0l+JSLTloTI9p7uMPC6LJtP6qgNAdNtasrUnPo8z5h7kYJR0U8ClsZbOZcf13cSdZck9jZp0kqiSBREhVHGLQmqirXm7UEnoTZYn8U74l3wsetgGiQ3AOAQ94REbzt1obHtGj4JG9Fd3KydWNMcOv1hLODw==\",\"sig\":\"ee99080081a408aa2ce0e2372a44aa0eb1e8e60aa7b4330909d7c7a701d84076a7815412c83a7aa1b783ed0942b0f5e2e2f63efeee8aa0c69503971c65370c3d\"}";
ndb_process_event(ndb, giftwrap_json, strlen(giftwrap_json));
ok = ndb_wait_for_notes(ndb, subid, note_ids,
sizeof(note_ids)/sizeof(note_ids[0]));
assert(ok == 1);
assert(ndb_unsubscribe(ndb, subid));
ndb_begin_query(ndb, &txn);
giftwrap = ndb_get_note_by_key(&txn, note_ids[0], NULL);
assert(giftwrap);
ndb_end_query(&txn);
ndb_filter_destroy(&filter);
kind_filter(&filter, 1);
subid = ndb_subscribe(ndb, &filter, 1);
ndb_add_key(ndb, recv_sec);
ndb_begin_query(ndb, &txn);
ndb_process_giftwraps(ndb, &txn);
ndb_end_query(&txn);
ok = ndb_wait_for_notes(ndb, subid, note_ids,
sizeof(note_ids)/sizeof(note_ids[0]));
assert(ndb_unsubscribe(ndb, subid));
ndb_begin_query(ndb, &txn);
rumor = ndb_get_note_by_key(&txn, note_ids[0], NULL);
assert(rumor);
assert(ndb_note_is_rumor(rumor) == 1);
assert(ndb_note_kind(rumor) == 1);
assert(!strcmp(ndb_note_content(rumor), "hi"));
assert(!memcmp(ndb_note_rumor_giftwrap_id(rumor), giftwrap_id, 32));
assert(!memcmp(ndb_note_rumor_receiver_pubkey(rumor), recv_pub, 32));
ndb_end_query(&txn);
/* wait for updated giftwrap */
ndb_filter_destroy(&filter);
kind_filter(&filter, 1059);
subid = ndb_subscribe(ndb, &filter, 1);
ok = ndb_wait_for_notes(ndb, subid, note_ids,
sizeof(note_ids)/sizeof(note_ids[0]));
ndb_begin_query(ndb, &txn);
giftwrap = ndb_get_note_by_key(&txn, note_ids[0], NULL);
assert(giftwrap);
assert(*ndb_note_flags(giftwrap) & NDB_NOTE_FLAG_UNWRAPPED);
ndb_note_json(rumor, buf, sizeof(buf));
//printf("# rumor json: '%s'\n", buf);
ndb_end_query(&txn);
ndb_filter_destroy(&filter);
ndb_destroy(ndb);
printf("ok test_giftwrap_reprocess\n");
}
static void test_giftwrap_unwrap()
{
struct ndb *ndb;
struct ndb_filter filter;
struct ndb_config config;
struct ndb_txn txn;
struct ndb_note *rumor, *giftwrap;
int ok;
uint64_t subid;
ndb_default_config(&config);
const char *giftwrap_json;
uint64_t note_ids[2];
char buf[4096];
static unsigned char recv_sec[32] = {
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2
};
static const unsigned char recv_pub[32] = {
0xc6, 0x04, 0x7f, 0x94, 0x41, 0xed, 0x7d, 0x6d, 0x30, 0x45,
0x40, 0x6e, 0x95, 0xc0, 0x7c, 0xd8, 0x5c, 0x77, 0x8e, 0x4b,
0x8c, 0xef, 0x3c, 0xa7, 0xab, 0xac, 0x09, 0xb9, 0x5c, 0x70,
0x9e, 0xe5
};
static const unsigned char rumor_id[32] = {
0x3e, 0x12, 0xe5, 0xc1, 0xc9, 0xa8, 0x9f, 0x3f, 0x08, 0x04,
0x62, 0x3f, 0xd7, 0x61, 0x01, 0xcf, 0xff, 0xba, 0xdf, 0x0d,
0x02, 0x59, 0x38, 0xdb, 0x64, 0x10, 0x55, 0xe6, 0x00, 0xdc,
0x8f, 0x73
};
static const unsigned char giftwrap_id[32] = {
0x94, 0x1e, 0xaf, 0x4a, 0x9b, 0xd0, 0x09, 0x0a, 0xb5, 0xd4,
0x14, 0xff, 0x5a, 0x68, 0x25, 0x4d, 0xa5, 0x78, 0x6f, 0x0b,
0xf8, 0xb8, 0xe1, 0x56, 0xbc, 0x37, 0xa7, 0x7e, 0xa2, 0x68,
0x0a, 0x92
};
delete_test_db();
assert(ndb_init(&ndb, test_dir, &config));
//ndb_add_key(ndb, one);
ndb_add_key(ndb, recv_sec);
ndb_filter_init(&filter);
ndb_filter_start_field(&filter, NDB_FILTER_IDS);
ndb_filter_add_id_element(&filter, rumor_id);
ndb_filter_end_field(&filter);
ndb_filter_end(&filter);
subid = ndb_subscribe(ndb, &filter, 1);
giftwrap_json = "{\"id\":\"941eaf4a9bd0090ab5d414ff5a68254da5786f0bf8b8e156bc37a77ea2680a92\",\"pubkey\":\"5fac96633ffd0f68a037778dc40d7747ddf0ceaadb7986c23c0597a56b9ab6f6\",\"created_at\":1764513655,\"kind\":1059,\"tags\":[[\"p\",\"c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5\"]],\"content\":\"AmoRXHPZ6cYI2YR1PL1J2BpxY8onrJHUvt3cOhAe1IOIk6wnighedqyUqQsJ/Bnq0qAAkldMipUuHmr1TRNeu/3Rf3AZ1+4N6/eSJb2GcVaaWkqJ6DIIW+YHvQZRuS/1ef3lo41+2zOFqJMvLiz/Pw1kLdDdi5pKWy0QHJIKFPuIE9XNhFMTMo+rYSIlpfiAXtzpM6qETFBJxabmaZOMlgnxQG6Mm2gB3Y6rmj9gLgxsPCaOnvmfi5xhA+jRvz8FYQ+WIU1ij7SFsssz27fISRRIMEOoq7CHPcsdY2Ly/PSp1t+aLpSz1e8Chnhf3lq/Y0mumLDS/BzNaFm38wQqCoWbxrn+7iLTMfINXoDm7gY6Qb4eZc/Am/wjRJsHm4F37bPiCAupzYWOPdJWO3a4TPmGaHk6MmyrHRu4V20iZK+svdJ257RuwMrYk39xCWNG985t1acPc5Of+ZKDnUcL5jVw0ZUOTrV10uq/UVoOHwDkz0mcsJ0Adhc0fKgiE7CWZgKbaO0PIaAL0I+2hs350dcEK3Go8vzMfiN1Uc7NUUMpjQzIRJC1J6CVf6HkvvQ+JD52RhXsVZN8TTkrpxEDqvCm8eSpamqIZuMbrTh5jJ/J+S83dI8rHvyAcmGrRes5wMOiVaFPglreO/H//AgvcR+N/zYOrV4qCWboU+oTBz7A/ZzMGGt6mhL4dmDKDW3p/HT0l5dEfAn71fdx/VH+jXquVr/9rCw2F4kLzXiNB6E3OBxp7bjkgnmAWCXeNL8NRcOT2l6LaF1l+xi3+NJGxP6tdry+OYz/C12jAZ223HWtQv62fLS3wAuwJH3QjrYromeWU+MNyTdMEFRyuxMq9mpYno7tYF6FpVAR7w9oIZME7F36+1I2b7MERXsgioEBGJFWotC9v5nEKWLJJajZFPlsFQf96Y6Cwovd4moVNmDJ8s2riRDx9D9NxDJxNkz0l+JSLTloTI9p7uMPC6LJtP6qgNAdNtasrUnPo8z5h7kYJR0U8ClsZbOZcf13cSdZck9jZp0kqiSBREhVHGLQmqirXm7UEnoTZYn8U74l3wsetgGiQ3AOAQ94REbzt1obHtGj4JG9Fd3KydWNMcOv1hLODw==\",\"sig\":\"ee99080081a408aa2ce0e2372a44aa0eb1e8e60aa7b4330909d7c7a701d84076a7815412c83a7aa1b783ed0942b0f5e2e2f63efeee8aa0c69503971c65370c3d\"}";
ndb_process_event(ndb, giftwrap_json, strlen(giftwrap_json));
ok = ndb_wait_for_notes(ndb, subid, note_ids,
sizeof(note_ids)/sizeof(note_ids[0]));
assert(ok == 1);
ndb_begin_query(ndb, &txn);
rumor = ndb_get_note_by_key(&txn, note_ids[0], NULL);
assert(ndb_note_is_rumor(rumor) == 1);
assert(ndb_note_kind(rumor) == 1);
assert(!strcmp(ndb_note_content(rumor), "hi"));
assert(!memcmp(ndb_note_rumor_giftwrap_id(rumor), giftwrap_id, 32));
assert(!memcmp(ndb_note_rumor_receiver_pubkey(rumor), recv_pub, 32));
ndb_end_query(&txn);
ndb_begin_query(ndb, &txn);
giftwrap = ndb_get_note_by_id(&txn, giftwrap_id, NULL, NULL);
assert(giftwrap);
assert(*ndb_note_flags(giftwrap) & NDB_NOTE_FLAG_UNWRAPPED);
ndb_note_json(rumor, buf, sizeof(buf));
//printf("# rumor json: '%s'\n", buf);
ndb_end_query(&txn);
ndb_filter_destroy(&filter);
ndb_destroy(ndb);
printf("ok test_giftwrap_unwrap\n");
}
static void test_pns_unwrap()
{
struct ndb *ndb;
struct ndb_filter filter;
struct ndb_config config;
struct ndb_txn txn;
struct ndb_note *inner, *pns_note;
int ok;
uint64_t subid;
ndb_default_config(&config);
const char *pns_json;
uint64_t note_ids[2];
static unsigned char device_sec[32] = {
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2
};
/* device pubkey for secret key 0x02 */
static const unsigned char device_pub[32] = {
0xc6, 0x04, 0x7f, 0x94, 0x41, 0xed, 0x7d, 0x6d, 0x30, 0x45,
0x40, 0x6e, 0x95, 0xc0, 0x7c, 0xd8, 0x5c, 0x77, 0x8e, 0x4b,
0x8c, 0xef, 0x3c, 0xa7, 0xab, 0xac, 0x09, 0xb9, 0x5c, 0x70,
0x9e, 0xe5
};
static const unsigned char pns_id[32] = {
0xbf, 0xcd, 0x0d, 0x41, 0x5e, 0xa1, 0xb4, 0x77, 0x2d, 0x07,
0x5d, 0xe9, 0xe8, 0xf9, 0x8a, 0xc5, 0xc2, 0x22, 0x4a, 0x9d,
0x25, 0x12, 0xa6, 0x8c, 0x98, 0x4c, 0x39, 0xe5, 0x94, 0xb4,
0x6b, 0x15
};
delete_test_db();
assert(ndb_init(&ndb, test_dir, &config));
/* add key before ingestion so it auto-unwraps */
ndb_add_key(ndb, device_sec);
/* subscribe for kind-1 notes (the inner event) */
kind_filter(&filter, 1);
subid = ndb_subscribe(ndb, &filter, 1);
pns_json = "{\"id\":\"bfcd0d415ea1b4772d075de9e8f98ac5c2224a9d2512a68c984c39e594b46b15\",\"pubkey\":\"fa22d53e9d38ca7af1e66dcf88f5fb2444368df6bd16580b5827c8cfbc622d4e\",\"created_at\":1700000000,\"kind\":1080,\"tags\":[],\"content\":\"Ahtvtc5B/m/6n//vdOxtxR/+UbWx5qDP/teNxr563idfhMEObQ9v3Z1UI0HSyHWHCq2a9zwehpBYrJPXEnyvrzHeJQTQuz3AOKfJA/FT6MSMsGAyi197YDP3YaJfkDcdY0Aqnx5kXpir5IC95LCXjyPwDWms3ndJM3XksPLY0+mG8cYxdPkLxgnpqzs9N1pjf2ecPyvd8vx+3DVEY2APPalYE9L+rCYE5UyZzzDR2YD3MPF7wrb++wGeSL+46rvy2J/ZmUEnbXkC288MxTT77nSroiSB46PpcvbxBBqD82Q+I+G3Q3KWg16hn81MV2faXZ3rajZrZrXM+gs8kVunTGkH86KpAgt22RqdDcQiiogJoSL4k5cKzMGo268R+efqjZLt\",\"sig\":\"54f7ac921abdda5f14c045c2e3ba6f5cac1a41d8d46ffe370f1473f2b2dac938dc2801c818dc6916c6f1912a56c4ad327ed77f74a4acd85cf9ef209626a55616\"}";
ndb_process_event(ndb, pns_json, strlen(pns_json));
ok = ndb_wait_for_notes(ndb, subid, note_ids,
sizeof(note_ids)/sizeof(note_ids[0]));
assert(ok == 1);
assert(ndb_unsubscribe(ndb, subid));
ndb_begin_query(ndb, &txn);
inner = ndb_get_note_by_key(&txn, note_ids[0], NULL);
assert(inner);
assert(ndb_note_is_rumor(inner) == 1);
assert(ndb_note_kind(inner) == 1);
assert(!strcmp(ndb_note_content(inner), "hello from pns"));
assert(!memcmp(ndb_note_rumor_giftwrap_id(inner), pns_id, 32));
assert(!memcmp(ndb_note_rumor_receiver_pubkey(inner), device_pub, 32));
ndb_end_query(&txn);
/* verify the wrapper is marked as unwrapped */
ndb_begin_query(ndb, &txn);
pns_note = ndb_get_note_by_id(&txn, pns_id, NULL, NULL);
assert(pns_note);
assert(*ndb_note_flags(pns_note) & NDB_NOTE_FLAG_UNWRAPPED);
ndb_end_query(&txn);
ndb_filter_destroy(&filter);
ndb_destroy(ndb);
printf("ok test_pns_unwrap\n");
}
static void test_pns_reprocess()
{
struct ndb *ndb;
struct ndb_filter filter;
struct ndb_config config;
struct ndb_txn txn;
struct ndb_note *inner;
int ok;
uint64_t subid;
ndb_default_config(&config);
const char *pns_json;
uint64_t note_ids[2];
static unsigned char device_sec[32] = {
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2
};
static const unsigned char device_pub[32] = {
0xc6, 0x04, 0x7f, 0x94, 0x41, 0xed, 0x7d, 0x6d, 0x30, 0x45,
0x40, 0x6e, 0x95, 0xc0, 0x7c, 0xd8, 0x5c, 0x77, 0x8e, 0x4b,
0x8c, 0xef, 0x3c, 0xa7, 0xab, 0xac, 0x09, 0xb9, 0x5c, 0x70,
0x9e, 0xe5
};
delete_test_db();
assert(ndb_init(&ndb, test_dir, &config));
/* ingest PNS event BEFORE adding keys */
kind_filter(&filter, 1080);
subid = ndb_subscribe(ndb, &filter, 1);
pns_json = "{\"id\":\"bfcd0d415ea1b4772d075de9e8f98ac5c2224a9d2512a68c984c39e594b46b15\",\"pubkey\":\"fa22d53e9d38ca7af1e66dcf88f5fb2444368df6bd16580b5827c8cfbc622d4e\",\"created_at\":1700000000,\"kind\":1080,\"tags\":[],\"content\":\"Ahtvtc5B/m/6n//vdOxtxR/+UbWx5qDP/teNxr563idfhMEObQ9v3Z1UI0HSyHWHCq2a9zwehpBYrJPXEnyvrzHeJQTQuz3AOKfJA/FT6MSMsGAyi197YDP3YaJfkDcdY0Aqnx5kXpir5IC95LCXjyPwDWms3ndJM3XksPLY0+mG8cYxdPkLxgnpqzs9N1pjf2ecPyvd8vx+3DVEY2APPalYE9L+rCYE5UyZzzDR2YD3MPF7wrb++wGeSL+46rvy2J/ZmUEnbXkC288MxTT77nSroiSB46PpcvbxBBqD82Q+I+G3Q3KWg16hn81MV2faXZ3rajZrZrXM+gs8kVunTGkH86KpAgt22RqdDcQiiogJoSL4k5cKzMGo268R+efqjZLt\",\"sig\":\"54f7ac921abdda5f14c045c2e3ba6f5cac1a41d8d46ffe370f1473f2b2dac938dc2801c818dc6916c6f1912a56c4ad327ed77f74a4acd85cf9ef209626a55616\"}";
ndb_process_event(ndb, pns_json, strlen(pns_json));
ok = ndb_wait_for_notes(ndb, subid, note_ids,
sizeof(note_ids)/sizeof(note_ids[0]));
assert(ok == 1);
assert(ndb_unsubscribe(ndb, subid));
/* now add key and reprocess */
ndb_filter_destroy(&filter);
kind_filter(&filter, 1);
subid = ndb_subscribe(ndb, &filter, 1);
ndb_add_key(ndb, device_sec);
ndb_begin_query(ndb, &txn);
ndb_process_pns(ndb, &txn);
ndb_end_query(&txn);
ok = ndb_wait_for_notes(ndb, subid, note_ids,
sizeof(note_ids)/sizeof(note_ids[0]));
assert(ndb_unsubscribe(ndb, subid));
ndb_begin_query(ndb, &txn);
inner = ndb_get_note_by_key(&txn, note_ids[0], NULL);
assert(inner);
assert(ndb_note_is_rumor(inner) == 1);
assert(ndb_note_kind(inner) == 1);
assert(!strcmp(ndb_note_content(inner), "hello from pns"));
assert(!memcmp(ndb_note_rumor_receiver_pubkey(inner), device_pub, 32));
ndb_end_query(&txn);
ndb_filter_destroy(&filter);
ndb_destroy(ndb);
printf("ok test_pns_reprocess\n");
}
static void test_zap_verification()
{
struct ndb *ndb;
struct ndb_filter filter;
struct ndb_config config;
struct ndb_txn txn;
int ok;
uint64_t subid;
uint64_t note_ids[4];
ndb_default_config(&config);
// a kind-1 note that will be the zap target
// note id: aaaa...aaaa (we just need a valid event)
const char *target_json =
"{\"id\":\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\","
"\"pubkey\":\"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\","
"\"created_at\":1700000000,\"kind\":1,\"tags\":[],\"content\":\"hello world\","
"\"sig\":\"cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc\"}";
static const unsigned char target_id[32] = {
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa
};
// a kind-9735 zap receipt with:
// - bolt11 tag: lnbc1230n... (1230 nano-BTC = 123000 sats = 123000000 msats)
// - e tag pointing to the target note
const char *zap_json =
"{\"id\":\"dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd\","
"\"pubkey\":\"eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee\","
"\"created_at\":1700000001,\"kind\":9735,"
"\"tags\":["
"[\"bolt11\",\"lnbc1230n1p5fetpfpp5mqn7v09jz8pkxl67h4hgd8z2xuqfzfhlw0d4yu5dz4z35ermszaqdq57z0cadhsn78tduylnztscqzzsxqyz5vqrzjqvueefmrckfdwyyu39m0lf24sqzcr9vcrmxrvgfn6empxz7phrjxvrttncqq0lcqqyqqqqlgqqqqqqgq2qsp5mhdv3kgh8y57hd0nezqk0yqhdtkjecnykfxer2k4geg7x34xvqyq9qxpqysgqylpwwyjlvfhc4jzw5hl77a5ajdf7ay6hku7vpznc9efe8nw0h2jp58p7hl2km3hsf3k40z6tey4ye26zf3wwt77ws02rdzzl3cem97squshha0\"],"
"[\"e\",\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"],"
"[\"p\",\"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"]"
"],\"content\":\"\","
"\"sig\":\"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\"}";
static const unsigned char zap_id[32] = {
0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd
};
ndb_config_set_flags(&config, NDB_FLAG_SKIP_NOTE_VERIFY);
delete_test_db();
assert(ndb_init(&ndb, test_dir, &config));
// subscribe for kind-1 and kind-9735
kind_filter(&filter, 1);
subid = ndb_subscribe(ndb, &filter, 1);
// ingest the target note first
ndb_process_event(ndb, target_json, strlen(target_json));
ok = ndb_wait_for_notes(ndb, subid, note_ids, 4);
assert(ok >= 1);
ndb_unsubscribe(ndb, subid);
ndb_filter_destroy(&filter);
// now ingest the zap receipt
kind_filter(&filter, 9735);
subid = ndb_subscribe(ndb, &filter, 1);
ndb_process_event(ndb, zap_json, strlen(zap_json));
ok = ndb_wait_for_notes(ndb, subid, note_ids, 4);
assert(ok >= 1);
ndb_unsubscribe(ndb, subid);
ndb_filter_destroy(&filter);
// check unverified zap stats written at ingestion time
ndb_begin_query(ndb, &txn);
{
struct ndb_note_meta *meta;
struct ndb_note_meta_entry *entry;
meta = ndb_get_note_meta(&txn, target_id);
assert(meta);
entry = ndb_note_meta_find_entry(meta, NDB_NOTE_META_ZAP_UNVERIFIED, NULL);
assert(entry);
assert(*ndb_note_meta_zap_unverified_count(entry) == 1);
assert(*ndb_note_meta_zap_unverified_msats(entry) > 0);
// no verified zaps yet
entry = ndb_note_meta_find_entry(meta, NDB_NOTE_META_ZAP, NULL);
assert(entry == NULL);
}
ndb_end_query(&txn);
// verify the zap
ndb_begin_query(ndb, &txn);
ok = ndb_verify_zap(ndb, &txn, zap_id);
assert(ok);
ndb_end_query(&txn);
// wait for writer to process the metadata updates
usleep(100000);
// check: verified count=1, unverified count=0
ndb_begin_query(ndb, &txn);
{
struct ndb_note_meta *meta;
struct ndb_note_meta_entry *entry;
meta = ndb_get_note_meta(&txn, target_id);
assert(meta);
entry = ndb_note_meta_find_entry(meta, NDB_NOTE_META_ZAP, NULL);
assert(entry);
assert(*ndb_note_meta_zap_count(entry) == 1);
assert(*ndb_note_meta_zap_msats(entry) > 0);
entry = ndb_note_meta_find_entry(meta, NDB_NOTE_META_ZAP_UNVERIFIED, NULL);
assert(entry);
assert(*ndb_note_meta_zap_unverified_count(entry) == 0);
assert(*ndb_note_meta_zap_unverified_msats(entry) == 0);
}
ndb_end_query(&txn);
// test idempotency: verify again should not double-count
ndb_begin_query(ndb, &txn);
ok = ndb_verify_zap(ndb, &txn, zap_id);
assert(ok);
ndb_end_query(&txn);
usleep(100000);
ndb_begin_query(ndb, &txn);
{
struct ndb_note_meta *meta;
struct ndb_note_meta_entry *entry;
meta = ndb_get_note_meta(&txn, target_id);
assert(meta);
entry = ndb_note_meta_find_entry(meta, NDB_NOTE_META_ZAP, NULL);
assert(entry);
assert(*ndb_note_meta_zap_count(entry) == 1); // still 1, not 2
}
ndb_end_query(&txn);
ndb_destroy(ndb);
printf("ok test_zap_verification\n");
}
static void test_multiple_zaps()
{
struct ndb *ndb;
struct ndb_filter filter;
struct ndb_config config;
struct ndb_txn txn;
int ok;
uint64_t subid;
uint64_t note_ids[4];
ndb_default_config(&config);
const char *target_json =
"{\"id\":\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\","
"\"pubkey\":\"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\","
"\"created_at\":1700000000,\"kind\":1,\"tags\":[],\"content\":\"hello\","
"\"sig\":\"cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc\"}";
static const unsigned char target_id[32] = {
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa
};
// two zap receipts with different ids, same bolt11 (123000 msats each)
const char *zap1_json =
"{\"id\":\"1111111111111111111111111111111111111111111111111111111111111111\","
"\"pubkey\":\"eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee\","
"\"created_at\":1700000001,\"kind\":9735,"
"\"tags\":["
"[\"bolt11\",\"lnbc1230n1p5fetpfpp5mqn7v09jz8pkxl67h4hgd8z2xuqfzfhlw0d4yu5dz4z35ermszaqdq57z0cadhsn78tduylnztscqzzsxqyz5vqrzjqvueefmrckfdwyyu39m0lf24sqzcr9vcrmxrvgfn6empxz7phrjxvrttncqq0lcqqyqqqqlgqqqqqqgq2qsp5mhdv3kgh8y57hd0nezqk0yqhdtkjecnykfxer2k4geg7x34xvqyq9qxpqysgqylpwwyjlvfhc4jzw5hl77a5ajdf7ay6hku7vpznc9efe8nw0h2jp58p7hl2km3hsf3k40z6tey4ye26zf3wwt77ws02rdzzl3cem97squshha0\"],"
"[\"e\",\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"]"
"],\"content\":\"\","
"\"sig\":\"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\"}";
const char *zap2_json =
"{\"id\":\"2222222222222222222222222222222222222222222222222222222222222222\","
"\"pubkey\":\"eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee\","
"\"created_at\":1700000002,\"kind\":9735,"
"\"tags\":["
"[\"bolt11\",\"lnbc1230n1p5fetpfpp5mqn7v09jz8pkxl67h4hgd8z2xuqfzfhlw0d4yu5dz4z35ermszaqdq57z0cadhsn78tduylnztscqzzsxqyz5vqrzjqvueefmrckfdwyyu39m0lf24sqzcr9vcrmxrvgfn6empxz7phrjxvrttncqq0lcqqyqqqqlgqqqqqqgq2qsp5mhdv3kgh8y57hd0nezqk0yqhdtkjecnykfxer2k4geg7x34xvqyq9qxpqysgqylpwwyjlvfhc4jzw5hl77a5ajdf7ay6hku7vpznc9efe8nw0h2jp58p7hl2km3hsf3k40z6tey4ye26zf3wwt77ws02rdzzl3cem97squshha0\"],"
"[\"e\",\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"]"
"],\"content\":\"\","
"\"sig\":\"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\"}";
static const unsigned char zap1_id[32] = {
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11
};
static const unsigned char zap2_id[32] = {
0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22
};
ndb_config_set_flags(&config, NDB_FLAG_SKIP_NOTE_VERIFY);
delete_test_db();
assert(ndb_init(&ndb, test_dir, &config));
// ingest the target note
kind_filter(&filter, 1);
subid = ndb_subscribe(ndb, &filter, 1);
ndb_process_event(ndb, target_json, strlen(target_json));
ok = ndb_wait_for_notes(ndb, subid, note_ids, 4);
assert(ok >= 1);
ndb_unsubscribe(ndb, subid);
ndb_filter_destroy(&filter);
// ingest both zap receipts
kind_filter(&filter, 9735);
subid = ndb_subscribe(ndb, &filter, 1);
ndb_process_event(ndb, zap1_json, strlen(zap1_json));
ndb_process_event(ndb, zap2_json, strlen(zap2_json));
ok = ndb_wait_for_notes(ndb, subid, note_ids, 4);
assert(ok >= 1);
if (ok < 2)
ok += ndb_wait_for_notes(ndb, subid, note_ids, 4);
assert(ok >= 2);
ndb_unsubscribe(ndb, subid);
ndb_filter_destroy(&filter);
// check: unverified count=2, msats=246000 (123000 * 2)
ndb_begin_query(ndb, &txn);
{
struct ndb_note_meta *meta;
struct ndb_note_meta_entry *entry;
meta = ndb_get_note_meta(&txn, target_id);
assert(meta);
entry = ndb_note_meta_find_entry(meta, NDB_NOTE_META_ZAP_UNVERIFIED, NULL);
assert(entry);
assert(*ndb_note_meta_zap_unverified_count(entry) == 2);
assert(*ndb_note_meta_zap_unverified_msats(entry) == 246000);
entry = ndb_note_meta_find_entry(meta, NDB_NOTE_META_ZAP, NULL);
assert(entry == NULL);
}
ndb_end_query(&txn);
// verify only the first zap
ndb_begin_query(ndb, &txn);
ok = ndb_verify_zap(ndb, &txn, zap1_id);
assert(ok);
ndb_end_query(&txn);
usleep(100000);
// check: verified count=1/123000, unverified count=1/123000
ndb_begin_query(ndb, &txn);
{
struct ndb_note_meta *meta;
struct ndb_note_meta_entry *entry;
meta = ndb_get_note_meta(&txn, target_id);
assert(meta);
entry = ndb_note_meta_find_entry(meta, NDB_NOTE_META_ZAP, NULL);
assert(entry);
assert(*ndb_note_meta_zap_count(entry) == 1);
assert(*ndb_note_meta_zap_msats(entry) == 123000);
entry = ndb_note_meta_find_entry(meta, NDB_NOTE_META_ZAP_UNVERIFIED, NULL);
assert(entry);
assert(*ndb_note_meta_zap_unverified_count(entry) == 1);
assert(*ndb_note_meta_zap_unverified_msats(entry) == 123000);
}
ndb_end_query(&txn);
// verify the second zap
ndb_begin_query(ndb, &txn);
ok = ndb_verify_zap(ndb, &txn, zap2_id);
assert(ok);
ndb_end_query(&txn);
usleep(100000);
// check: verified count=2/246000, unverified count=0/0
ndb_begin_query(ndb, &txn);
{
struct ndb_note_meta *meta;
struct ndb_note_meta_entry *entry;
meta = ndb_get_note_meta(&txn, target_id);
assert(meta);
entry = ndb_note_meta_find_entry(meta, NDB_NOTE_META_ZAP, NULL);
assert(entry);
assert(*ndb_note_meta_zap_count(entry) == 2);
assert(*ndb_note_meta_zap_msats(entry) == 246000);
entry = ndb_note_meta_find_entry(meta, NDB_NOTE_META_ZAP_UNVERIFIED, NULL);
assert(entry);
assert(*ndb_note_meta_zap_unverified_count(entry) == 0);
assert(*ndb_note_meta_zap_unverified_msats(entry) == 0);
}
ndb_end_query(&txn);
ndb_destroy(ndb);
printf("ok test_multiple_zaps\n");
}
static void test_metadata()
{
unsigned char buffer[1024];
union ndb_reaction_str str;
struct ndb_note_meta_builder builder;
struct ndb_note_meta *meta;