-
Notifications
You must be signed in to change notification settings - Fork 21
/
nostril.c
865 lines (704 loc) · 21.2 KB
/
nostril.c
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
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <assert.h>
#include <errno.h>
#include <inttypes.h>
#include <unistd.h>
#include "secp256k1.h"
#include "secp256k1_ecdh.h"
#include "secp256k1_schnorrsig.h"
#include "cursor.h"
#include "hex.h"
#include "base64.h"
#include "aes.h"
#include "sha256.h"
#include "random.h"
#include "proof.h"
#define VERSION "0.1.3"
#define MAX_TAGS 1024
#define MAX_TAG_ELEMS 64
#define HAS_CREATED_AT (1<<1)
#define HAS_KIND (1<<2)
#define HAS_ENVELOPE (1<<3)
#define HAS_ENCRYPT (1<<4)
#define HAS_DIFFICULTY (1<<5)
#define HAS_MINE_PUBKEY (1<<6)
struct key {
secp256k1_keypair pair;
unsigned char secret[32];
unsigned char pubkey[32];
};
struct args {
unsigned int flags;
int kind;
int difficulty;
unsigned char encrypt_to[32];
const char *sec;
const char *tags;
const char *content;
uint64_t created_at;
};
struct nostr_tag {
const char *strs[MAX_TAG_ELEMS];
int num_elems;
};
struct nostr_event {
unsigned char id[32];
unsigned char pubkey[32];
unsigned char sig[64];
const char *content;
uint64_t created_at;
int kind;
const char *explicit_tags;
struct nostr_tag tags[MAX_TAGS];
int num_tags;
};
void usage()
{
printf("usage: nostril [OPTIONS]\n");
printf("\n");
printf(" OPTIONS\n");
printf("\n");
printf(" --content <string> the content of the note\n");
printf(" --dm <hex pubkey> make an encrypted dm to said pubkey. sets kind and tags.\n");
printf(" --envelope wrap in [\"EVENT\",...] for easy relaying\n");
printf(" --kind <number> set kind\n");
printf(" --created-at <unix timestamp> set a specific created-at time\n");
printf(" --sec <hex seckey> set the secret key for signing, otherwise one will be randomly generated\n");
printf(" --pow <difficulty> number of leading 0 bits of the id to mine\n");
printf(" --mine-pubkey mine a pubkey instead of id\n");
printf(" --tag <key> <value> add a tag\n");
printf(" --tagn <N> <tags * N...> add a tag with N elements. eg: `--tagn 3 a b c`, `--tagn 0`\n");
printf(" -e <event_id> shorthand for --tag e <event_id>\n");
printf(" -p <pubkey> shorthand for --tag p <pubkey>\n");
printf(" -t <hashtag> shorthand for --tag t <hashtag>\n");
exit(1);
}
inline static int cursor_push_escaped_char(struct cursor *cur, char c)
{
switch (c) {
case '"': return cursor_push_str(cur, "\\\"");
case '\\': return cursor_push_str(cur, "\\\\");
case '\b': return cursor_push_str(cur, "\\b");
case '\f': return cursor_push_str(cur, "\\f");
case '\n': return cursor_push_str(cur, "\\n");
case '\r': return cursor_push_str(cur, "\\r");
case '\t': return cursor_push_str(cur, "\\t");
// TODO: \u hex hex hex hex
}
return cursor_push_byte(cur, c);
}
static int cursor_push_jsonstr(struct cursor *cur, const char *str)
{
int i;
int len;
len = strlen(str);
if (!cursor_push_byte(cur, '"'))
return 0;
for (i = 0; i < len; i++) {
if (!cursor_push_escaped_char(cur, str[i]))
return 0;
}
if (!cursor_push_byte(cur, '"'))
return 0;
return 1;
}
static int cursor_push_tag(struct cursor *cur, struct nostr_tag *tag)
{
int i;
if (!cursor_push_byte(cur, '['))
return 0;
for (i = 0; i < tag->num_elems; i++) {
if (!cursor_push_jsonstr(cur, tag->strs[i]))
return 0;
if (i != tag->num_elems-1) {
if (!cursor_push_byte(cur, ','))
return 0;
}
}
return cursor_push_byte(cur, ']');
}
static int cursor_push_tags(struct cursor *cur, struct nostr_event *ev)
{
int i;
if (ev->explicit_tags) {
return cursor_push_str(cur, ev->explicit_tags);
}
if (!cursor_push_byte(cur, '['))
return 0;
for (i = 0; i < ev->num_tags; i++) {
if (!cursor_push_tag(cur, &ev->tags[i]))
return 0;
if (i != ev->num_tags-1) {
if (!cursor_push_str(cur, ","))
return 0;
}
}
return cursor_push_byte(cur, ']');
}
int event_commitment(struct nostr_event *ev, unsigned char *buf, int buflen)
{
char timebuf[16] = {0};
char kindbuf[16] = {0};
char pubkey[65];
struct cursor cur;
int ok;
ok = hex_encode(ev->pubkey, sizeof(ev->pubkey), pubkey, sizeof(pubkey));
assert(ok);
make_cursor(buf, buf + buflen, &cur);
snprintf(timebuf, sizeof(timebuf), "%" PRIu64 "", ev->created_at);
snprintf(kindbuf, sizeof(kindbuf), "%d", ev->kind);
ok =
cursor_push_str(&cur, "[0,\"") &&
cursor_push_str(&cur, pubkey) &&
cursor_push_str(&cur, "\",") &&
cursor_push_str(&cur, timebuf) &&
cursor_push_str(&cur, ",") &&
cursor_push_str(&cur, kindbuf) &&
cursor_push_str(&cur, ",") &&
cursor_push_tags(&cur, ev) &&
cursor_push_str(&cur, ",") &&
cursor_push_jsonstr(&cur, ev->content) &&
cursor_push_str(&cur, "]");
if (!ok)
return 0;
return cur.p - cur.start;
}
static int make_sig(secp256k1_context *ctx, struct key *key,
unsigned char *id, unsigned char sig[64])
{
unsigned char aux[32];
if (!fill_random(aux, sizeof(aux))) {
return 0;
}
return secp256k1_schnorrsig_sign32(ctx, sig, id, &key->pair, aux);
}
static int create_key(secp256k1_context *ctx, struct key *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))
return 0;
if (!secp256k1_keypair_xonly_pub(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);
}
static int decode_key(secp256k1_context *ctx, const char *secstr, struct key *key)
{
if (!hex_decode(secstr, strlen(secstr), key->secret, 32)) {
fprintf(stderr, "could not hex decode secret key\n");
return 0;
}
return create_key(ctx, key);
}
static inline void xor_mix(unsigned char *dest, const unsigned char *a, const unsigned char *b, int size)
{
int i;
for (i = 0; i < size; i++)
dest[i] = a[i] ^ b[i];
}
static int generate_key(secp256k1_context *ctx, struct key *key, int *difficulty)
{
uint64_t attempts = 0;
uint64_t duration;
int bits;
double pers;
struct timespec t1, t2;
/* If the secret key is zero or out of range (bigger than secp256k1's
* order), we try to sample a new key. Note that the probability of this
* happening is negligible. */
if (!fill_random(key->secret, sizeof(key->secret))) {
return 0;
}
if (difficulty == NULL) {
return create_key(ctx, key);
}
clock_gettime(CLOCK_MONOTONIC, &t1);
while (1) {
if (!create_key(ctx, key))
return 0;
attempts++;
if ((bits = count_leading_zero_bits(key->pubkey)) >= *difficulty) {
clock_gettime(CLOCK_MONOTONIC, &t2);
duration = ((t2.tv_sec - t1.tv_sec) * 1e9L + (t2.tv_nsec - t1.tv_nsec)) / 1e6L;
pers = (double)attempts / (double)duration;
fprintf(stderr, "mined pubkey with %d bits after %" PRIu64 " attempts, %" PRId64 " ms, %f attempts per ms\n", bits, attempts, duration, pers);
return 1;
}
// NOTE: Get a new secret key by xor mixing the current secret
// key with the current public key. This doesn't rely on the
// system's crypto number generator so it should be fast. There
// shouldn't be any secret key entropy issues since we got a
// good source of entropy from the first fill_random call at
// the start of the function.
xor_mix(key->secret, key->secret, key->pubkey, 32);
}
}
static int init_secp_context(secp256k1_context **ctx)
{
unsigned char randomize[32];
*ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);
if (!fill_random(randomize, sizeof(randomize))) {
return 0;
}
/* Randomizing the context is recommended to protect against side-channel
* leakage See `secp256k1_context_randomize` in secp256k1.h for more
* information about it. This should never fail. */
return secp256k1_context_randomize(*ctx, randomize);
}
static int generate_event_id(struct nostr_event *ev)
{
static unsigned char buf[102400];
int len;
if (!(len = event_commitment(ev, buf, sizeof(buf)))) {
fprintf(stderr, "event_commitment: buffer out of space\n");
return 0;
}
//fprintf(stderr, "commitment: '%.*s'\n", len, buf);
sha256((struct sha256*)ev->id, buf, len);
return 1;
}
static int sign_event(secp256k1_context *ctx, struct key *key, struct nostr_event *ev)
{
if (!make_sig(ctx, key, ev->id, ev->sig))
return 0;
return 1;
}
static int print_event(struct nostr_event *ev, int envelope)
{
unsigned char buf[102400];
char pubkey[65];
char id[65];
char sig[129];
struct cursor cur;
int ok;
ok = hex_encode(ev->id, sizeof(ev->id), id, sizeof(id)) &&
hex_encode(ev->pubkey, sizeof(ev->pubkey), pubkey, sizeof(pubkey)) &&
hex_encode(ev->sig, sizeof(ev->sig), sig, sizeof(sig));
assert(ok);
make_cursor(buf, buf+sizeof(buf), &cur);
if (!cursor_push_tags(&cur, ev))
return 0;
if (envelope)
printf("[\"EVENT\",");
printf("{\"id\": \"%s\",", id);
printf("\"pubkey\": \"%s\",", pubkey);
printf("\"created_at\": %" PRIu64 ",", ev->created_at);
printf("\"kind\": %d,", ev->kind);
printf("\"tags\": %.*s,", (int)cursor_len(&cur), cur.start);
reset_cursor(&cur);
if (!cursor_push_jsonstr(&cur, ev->content))
return 0;
printf("\"content\": %.*s,", (int)cursor_len(&cur), cur.start);
printf("\"sig\": \"%s\"}", sig);
if (envelope)
printf("]");
printf("\n");
return 1;
}
static void make_event_from_args(struct nostr_event *ev, struct args *args)
{
ev->created_at = args->flags & HAS_CREATED_AT? args->created_at : time(NULL);
ev->content = args->content;
ev->kind = args->flags & HAS_KIND ? args->kind : 1;
}
static int parse_num(const char *arg, uint64_t *t)
{
*t = strtol(arg, NULL, 10);
return errno != EINVAL;
}
static int nostr_add_tag_n(struct nostr_event *ev, const char **ts, int n_ts)
{
int i;
struct nostr_tag *tag;
if (ev->num_tags + 1 > MAX_TAGS)
return 0;
tag = &ev->tags[ev->num_tags++];
tag->num_elems = n_ts;
for (i = 0; i < n_ts; i++) {
tag->strs[i] = ts[i];
}
return 1;
}
static int nostr_add_tag(struct nostr_event *ev, const char *t1, const char *t2)
{
const char *ts[] = {t1, t2};
return nostr_add_tag_n(ev, ts, 2);
}
static int nostr_add_arg_tags(struct nostr_event *ev, int *argc, const char **argv[])
{
struct nostr_tag *tag;
int i;
uint64_t n;
uint16_t small_n;
const char *arg;
if (*argc == 0) {
fprintf(stderr, "expected more arguments to --tagn\n");
return 0;
}
arg = *(*argv)++; (*argc)--;
if (!parse_num(arg, &n)) {
fprintf(stderr, "expected X to be a number in --tagn X ...\n");
return 0;
}
if (n > MAX_TAG_ELEMS) {
fprintf(stderr, "too many --tagn arguments (%"PRIu64" > %d)\n", n, MAX_TAG_ELEMS);
return 0;
}
small_n = (uint16_t)n;
if (ev->num_tags + small_n > MAX_TAGS) {
fprintf(stderr, "too many tags (%d + %d > %d)\n", ev->num_tags, small_n, MAX_TAG_ELEMS);
return 0;
}
tag = &ev->tags[ev->num_tags++];
tag->num_elems = 0;
for (i = 0; i < small_n; i++) {
if (*argc <= 0) {
fprintf(stderr, "expected %d tags: --tagn %d ..., got %d args\n", small_n, small_n, i);
return 0;
}
arg = *(*argv)++; (*argc)--;
//fprintf(stderr, "got arg '%s' argc(%d)\n", arg, *argc);
tag->strs[i] = arg;
tag->num_elems++;
}
return 1;
}
static int parse_args(int argc, const char *argv[], struct args *args, struct nostr_event *ev)
{
const char *arg, *arg2;
uint64_t n;
int has_added_tags = 0;
argv++; argc--;
for (; argc; ) {
arg = *argv++; argc--;
if (!strcmp(arg, "--help")) {
usage();
}
if (!argc) {
fprintf(stderr, "expected argument: '%s'\n", arg);
return 0;
}
if (!strcmp(arg, "--sec")) {
args->sec = *argv++; argc--;
} else if (!strcmp(arg, "--created-at")) {
arg = *argv++; argc--;
if (!parse_num(arg, &args->created_at)) {
fprintf(stderr, "created-at must be a unix timestamp\n");
return 0;
} else {
args->flags |= HAS_CREATED_AT;
}
} else if (!strcmp(arg, "--kind")) {
arg = *argv++; argc--;
if (!parse_num(arg, &n)) {
fprintf(stderr, "kind should be a number, got '%s'\n", arg);
return 0;
}
args->kind = (int)n;
args->flags |= HAS_KIND;
} else if (!strcmp(arg, "--envelope")) {
args->flags |= HAS_ENVELOPE;
} else if (!strcmp(arg, "--tags")) {
if (args->flags & HAS_DIFFICULTY) {
fprintf(stderr, "can't combine --tags and --pow (yet)\n");
return 0;
}
if (has_added_tags) {
fprintf(stderr, "can't combine --tags and --tag (yet)");
return 0;
}
arg = *argv++; argc--;
args->tags = arg;
} else if (!strcmp(arg, "-e")) {
has_added_tags = 1;
arg = *argv++; argc--;
if (!nostr_add_tag(ev, "e", arg)) {
fprintf(stderr, "couldn't add e tag");
return 0;
}
} else if (!strcmp(arg, "-p")) {
has_added_tags = 1;
arg = *argv++; argc--;
if (!nostr_add_tag(ev, "p", arg)) {
fprintf(stderr, "couldn't add p tag");
return 0;
}
} else if (!strcmp(arg, "-t")) {
has_added_tags = 1;
arg = *argv++; argc--;
if (!nostr_add_tag(ev, "t", arg)) {
fprintf(stderr, "couldn't add t tag");
return 0;
}
} else if (!strcmp(arg, "--tagn")) {
if (!nostr_add_arg_tags(ev, &argc, &argv)) {
return 0;
}
has_added_tags = 1;
} else if (!strcmp(arg, "--tag")) {
has_added_tags = 1;
if (args->tags) {
fprintf(stderr, "can't combine --tag and --tags (yet)");
return 0;
}
arg = *argv++; argc--;
if (argc == 0) {
fprintf(stderr, "expected two arguments to --tag\n");
return 0;
}
arg2 = *argv++; argc--;
if (!nostr_add_tag(ev, arg, arg2)) {
fprintf(stderr, "couldn't add tag '%s' '%s'\n", arg, arg2);
return 0;
}
} else if (!strcmp(arg, "--mine-pubkey")) {
args->flags |= HAS_MINE_PUBKEY;
} else if (!strcmp(arg, "--pow")) {
if (args->tags) {
fprintf(stderr, "can't combine --tags and --pow (yet)\n");
return 0;
}
arg = *argv++; argc--;
if (!parse_num(arg, &n)) {
fprintf(stderr, "could not parse difficulty as number: '%s'\n", arg);
return 0;
}
args->difficulty = n;
args->flags |= HAS_DIFFICULTY;
} else if (!strcmp(arg, "--dm")) {
arg = *argv++; argc--;
if (!hex_decode(arg, strlen(arg), args->encrypt_to, 32)) {
fprintf(stderr, "could not decode encrypt-to pubkey");
return 0;
}
args->flags |= HAS_ENCRYPT;
} else if (!strcmp(arg, "--content")) {
arg = *argv++; argc--;
args->content = arg;
} else {
fprintf(stderr, "unexpected argument '%s'\n", arg);
return 0;
}
}
if (!args->content)
args->content = "";
return 1;
}
static int aes_encrypt(unsigned char *key, unsigned char *iv,
unsigned char *buf, size_t buflen)
{
struct AES_ctx ctx;
unsigned char padding;
int i;
struct cursor cur;
padding = 16 - (buflen % 16);
make_cursor(buf, buf + buflen + padding, &cur);
cur.p += buflen;
//fprintf(stderr, "aes_encrypt: len %ld, padding %d\n", buflen, padding);
for (i = 0; i < padding; i++) {
if (!cursor_push_byte(&cur, padding)) {
return 0;
}
}
assert(cur.p == cur.end);
assert((cur.p - cur.start) % 16 == 0);
AES_init_ctx_iv(&ctx, key, iv);
//fprintf(stderr, "encrypting %ld bytes: ", cur.p - cur.start);
//print_hex(cur.start, cur.p - cur.start);
AES_CBC_encrypt_buffer(&ctx, cur.start, cur.p - cur.start);
return cur.p - cur.start;
}
static int copyx(unsigned char *output, const unsigned char *x32, const unsigned char *y32, void *data) {
memcpy(output, x32, 32);
return 1;
}
static int ensure_nonce_tag(struct nostr_event *ev, int target, int *index)
{
char *str_target = malloc(8);
struct nostr_tag *tag;
int i;
for (i = 0; i < ev->num_tags; i++) {
tag = &ev->tags[i];
if (tag->num_elems == 2 && !strcmp(tag->strs[0], "nonce")) {
*index = i;
return 1;
}
}
*index = ev->num_tags;
snprintf(str_target, 7, "%d", target);
const char *ts[] = { "nonce", "0", str_target };
return nostr_add_tag_n(ev, ts, 3);
}
static int mine_event(struct nostr_event *ev, int difficulty)
{
char *strnonce = malloc(33);
struct nostr_tag *tag;
uint64_t nonce;
int index, res;
if (!ensure_nonce_tag(ev, difficulty, &index))
return 0;
tag = &ev->tags[index];
assert(tag->num_elems == 3);
assert(!strcmp(tag->strs[0], "nonce"));
tag->strs[1] = strnonce;
for (nonce = 0;; nonce++) {
snprintf(strnonce, 32, "%" PRIu64, nonce);
if (!generate_event_id(ev))
return 0;
if ((res = count_leading_zero_bits(ev->id)) >= difficulty) {
fprintf(stderr, "mined %d bits\n", res);
return 1;
}
}
return 0;
}
static int make_encrypted_dm(secp256k1_context *ctx, struct key *key,
struct nostr_event *ev, unsigned char nostr_pubkey[32], int kind)
{
size_t inl = strlen(ev->content);
int enclen = inl + 16;
size_t buflen = enclen * 3 + 65 * 10;
unsigned char *buf = malloc(buflen);
unsigned char shared_secret[32];
unsigned char iv[16];
unsigned char compressed_pubkey[33];
int content_len = strlen(ev->content);
unsigned char encbuf[content_len + (content_len % 16) + 1];
struct cursor cur;
secp256k1_pubkey pubkey;
compressed_pubkey[0] = 2;
memcpy(&compressed_pubkey[1], nostr_pubkey, 32);
make_cursor(buf, buf + buflen, &cur);
if (!secp256k1_ec_seckey_verify(ctx, key->secret)) {
fprintf(stderr, "make_encrypted_dm: ec_seckey_verify failed\n");
return 0;
}
if (!secp256k1_ec_pubkey_parse(ctx, &pubkey, compressed_pubkey, sizeof(compressed_pubkey))) {
fprintf(stderr, "make_encrypted_dm: ec_pubkey_parse failed\n");
return 0;
}
if (!secp256k1_ecdh(ctx, shared_secret, &pubkey, key->secret, copyx, NULL)) {
fprintf(stderr, "make_encrypted_dm: secp256k1_ecdh failed\n");
return 0;
}
if (!fill_random(iv, sizeof(iv))) {
fprintf(stderr, "make_encrypted_dm: fill_random failed\n");
return 0;
}
fprintf(stderr, "shared_secret ");
print_hex(shared_secret, 32);
memcpy(encbuf, ev->content, strlen(ev->content));
enclen = aes_encrypt(shared_secret, iv, encbuf, strlen(ev->content));
if (enclen == 0) {
fprintf(stderr, "make_encrypted_dm: aes_encrypt failed\n");
free(buf);
return 0;
}
if ((enclen = base64_encode((char *)buf, buflen, (const char*)encbuf, enclen)) == -1) {
fprintf(stderr, "make_encrypted_dm: base64 encode of encrypted fata failed\n");
return 0;
}
cur.p += enclen;
if (!cursor_push_str(&cur, "?iv=")) {
fprintf(stderr, "make_encrypted_dm: buffer too small\n");
return 0;
}
if ((enclen = base64_encode((char *)cur.p, cur.end - cur.p, (const char*)iv, 16)) == -1) {
fprintf(stderr, "make_encrypted_dm: base64 encode of iv failed\n");
return 0;
}
cur.p += enclen;
if (!cursor_push_byte(&cur, 0)) {
fprintf(stderr, "make_encrypted_dm: out of memory by 1 byte!\n");
return 0;
}
ev->content = (const char*)cur.start;
ev->kind = kind;
if (!hex_encode(nostr_pubkey, 32, (char*)cur.p, cur.end - cur.p))
return 0;
if (!nostr_add_tag(ev, "p", (const char*)cur.p)) {
fprintf(stderr, "too many tags\n");
return 0;
}
cur.p += 65;
return 1;
}
static void try_subcommand(int argc, const char *argv[])
{
static char buf[128] = {0};
const char *sub = argv[1];
if (strlen(sub) >= 1 && sub[0] != '-') {
snprintf(buf, sizeof(buf)-1, "nostril-%s", sub);
execvp(buf, (char * const *)argv+1);
}
}
int main(int argc, const char *argv[])
{
struct args args = {0};
struct nostr_event ev = {0};
struct key key;
secp256k1_context *ctx;
if (argc < 2)
usage();
if (!init_secp_context(&ctx))
return 2;
try_subcommand(argc, argv);
if (!parse_args(argc, argv, &args, &ev)) {
usage();
return 10;
}
if (args.tags) {
ev.explicit_tags = args.tags;
}
make_event_from_args(&ev, &args);
if (args.sec) {
if (!decode_key(ctx, args.sec, &key)) {
return 8;
}
} else {
int *difficulty = NULL;
if ((args.flags & HAS_DIFFICULTY) && (args.flags & HAS_MINE_PUBKEY)) {
difficulty = &args.difficulty;
}
if (!generate_key(ctx, &key, difficulty)) {
fprintf(stderr, "could not generate key\n");
return 4;
}
fprintf(stderr, "secret_key ");
print_hex(key.secret, sizeof(key.secret));
fprintf(stderr, "\n");
}
if (args.flags & HAS_ENCRYPT) {
int kind = args.flags & HAS_KIND? args.kind : 4;
if (!make_encrypted_dm(ctx, &key, &ev, args.encrypt_to, kind)) {
fprintf(stderr, "error making encrypted dm\n");
return 0;
}
}
// set the event's pubkey
memcpy(ev.pubkey, key.pubkey, 32);
if (args.flags & HAS_DIFFICULTY && !(args.flags & HAS_MINE_PUBKEY)) {
if (!mine_event(&ev, args.difficulty)) {
fprintf(stderr, "error when mining id\n");
return 22;
}
} else {
if (!generate_event_id(&ev)) {
fprintf(stderr, "could not generate event id\n");
return 5;
}
}
if (!sign_event(ctx, &key, &ev)) {
fprintf(stderr, "could not sign event\n");
return 6;
}
if (!print_event(&ev, args.flags & HAS_ENVELOPE)) {
fprintf(stderr, "buffer too small\n");
return 88;
}
return 0;
}