2022-04-14 10:52:29 -04:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <inttypes.h>
|
2022-11-17 20:20:58 -05:00
|
|
|
#include <unistd.h>
|
2022-04-14 10:52:29 -04:00
|
|
|
|
2022-08-08 18:31:38 -04:00
|
|
|
#include "secp256k1.h"
|
|
|
|
#include "secp256k1_ecdh.h"
|
|
|
|
#include "secp256k1_schnorrsig.h"
|
2022-04-14 10:52:29 -04:00
|
|
|
|
|
|
|
#include "cursor.h"
|
|
|
|
#include "hex.h"
|
2022-04-14 15:35:26 -04:00
|
|
|
#include "base64.h"
|
|
|
|
#include "aes.h"
|
2022-04-14 10:52:29 -04:00
|
|
|
#include "sha256.h"
|
|
|
|
#include "random.h"
|
2022-04-15 13:30:52 -04:00
|
|
|
#include "proof.h"
|
2022-04-14 10:52:29 -04:00
|
|
|
|
2022-11-18 19:26:53 -05:00
|
|
|
#define VERSION "0.1.3"
|
2022-11-17 16:21:07 -05:00
|
|
|
|
2022-04-14 10:52:29 -04:00
|
|
|
#define MAX_TAGS 32
|
|
|
|
#define MAX_TAG_ELEMS 16
|
|
|
|
|
|
|
|
#define HAS_CREATED_AT (1<<1)
|
|
|
|
#define HAS_KIND (1<<2)
|
2022-04-14 11:44:13 -04:00
|
|
|
#define HAS_ENVELOPE (1<<3)
|
2022-04-14 15:35:26 -04:00
|
|
|
#define HAS_ENCRYPT (1<<4)
|
2022-04-15 13:30:52 -04:00
|
|
|
#define HAS_DIFFICULTY (1<<5)
|
2022-08-28 17:34:58 -04:00
|
|
|
#define HAS_MINE_PUBKEY (1<<6)
|
2022-04-14 10:52:29 -04:00
|
|
|
|
|
|
|
struct key {
|
|
|
|
secp256k1_keypair pair;
|
2022-04-14 15:35:26 -04:00
|
|
|
unsigned char secret[32];
|
2022-04-14 10:52:29 -04:00
|
|
|
unsigned char pubkey[32];
|
|
|
|
};
|
|
|
|
|
|
|
|
struct args {
|
|
|
|
unsigned int flags;
|
|
|
|
int kind;
|
2022-04-15 13:30:52 -04:00
|
|
|
int difficulty;
|
2022-04-14 10:52:29 -04:00
|
|
|
|
2022-04-14 15:35:26 -04:00
|
|
|
unsigned char encrypt_to[32];
|
2022-04-14 10:52:29 -04:00
|
|
|
const char *sec;
|
2022-04-17 21:32:00 -04:00
|
|
|
const char *tags;
|
2022-04-14 10:52:29 -04:00
|
|
|
const char *content;
|
2022-04-14 15:35:26 -04:00
|
|
|
|
2022-04-14 10:52:29 -04:00
|
|
|
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;
|
|
|
|
|
2022-04-17 21:32:00 -04:00
|
|
|
const char *explicit_tags;
|
|
|
|
|
2022-04-14 10:52:29 -04:00
|
|
|
struct nostr_tag tags[MAX_TAGS];
|
|
|
|
int num_tags;
|
|
|
|
};
|
|
|
|
|
|
|
|
void usage()
|
|
|
|
{
|
2022-08-09 11:05:57 -04:00
|
|
|
printf("usage: nostril [OPTIONS]\n");
|
2022-04-14 22:44:02 -04:00
|
|
|
printf("\n");
|
|
|
|
printf(" OPTIONS\n");
|
|
|
|
printf("\n");
|
2022-08-09 11:05:57 -04:00
|
|
|
printf(" --content <string> the content of the note\n");
|
2022-04-14 22:44:02 -04:00
|
|
|
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");
|
2022-04-15 13:30:52 -04:00
|
|
|
printf(" --pow <difficulty> number of leading 0 bits of the id to mine\n");
|
2022-10-20 14:34:17 -04:00
|
|
|
printf(" --mine-pubkey mine a pubkey instead of id\n");
|
2022-04-20 13:37:12 -04:00
|
|
|
printf(" --tag <key> <value> add a tag\n");
|
2022-04-20 19:23:00 -04:00
|
|
|
printf(" -e <event_id> shorthand for --tag e <event_id>\n");
|
|
|
|
printf(" -p <pubkey> shorthand for --tag p <pubkey>\n");
|
2022-11-17 00:11:33 -05:00
|
|
|
printf(" -t <hashtag> shorthand for --tag t <hashtag>\n");
|
2022-04-14 10:52:29 -04:00
|
|
|
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;
|
|
|
|
|
2022-04-17 21:32:00 -04:00
|
|
|
if (ev->explicit_tags) {
|
|
|
|
return cursor_push_str(cur, ev->explicit_tags);
|
|
|
|
}
|
|
|
|
|
2022-04-14 10:52:29 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-08-08 18:31:38 -04:00
|
|
|
return secp256k1_schnorrsig_sign32(ctx, sig, id, &key->pair, aux);
|
2022-04-14 10:52:29 -04:00
|
|
|
}
|
|
|
|
|
2022-04-14 15:35:26 -04:00
|
|
|
static int create_key(secp256k1_context *ctx, struct key *key)
|
2022-04-14 10:52:29 -04:00
|
|
|
{
|
|
|
|
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. */
|
2022-04-14 15:35:26 -04:00
|
|
|
if (!secp256k1_keypair_create(ctx, &key->pair, key->secret))
|
2022-04-14 10:52:29 -04:00
|
|
|
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)
|
|
|
|
{
|
2022-04-14 15:35:26 -04:00
|
|
|
if (!hex_decode(secstr, strlen(secstr), key->secret, 32)) {
|
2022-04-14 10:52:29 -04:00
|
|
|
fprintf(stderr, "could not hex decode secret key\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-04-14 15:35:26 -04:00
|
|
|
return create_key(ctx, key);
|
2022-04-14 10:52:29 -04:00
|
|
|
}
|
|
|
|
|
2022-08-30 11:09:30 -04:00
|
|
|
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];
|
|
|
|
}
|
|
|
|
|
2022-08-28 17:34:58 -04:00
|
|
|
static int generate_key(secp256k1_context *ctx, struct key *key, int *difficulty)
|
2022-04-14 10:52:29 -04:00
|
|
|
{
|
2022-08-30 11:09:30 -04:00
|
|
|
uint64_t attempts = 0;
|
|
|
|
uint64_t duration;
|
2022-08-30 11:24:38 -04:00
|
|
|
int bits;
|
2022-08-30 11:09:30 -04:00
|
|
|
double pers;
|
|
|
|
struct timespec t1, t2;
|
|
|
|
|
2022-04-14 10:52:29 -04:00
|
|
|
/* 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. */
|
2022-04-14 15:35:26 -04:00
|
|
|
if (!fill_random(key->secret, sizeof(key->secret))) {
|
2022-04-14 10:52:29 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-08-28 17:34:58 -04:00
|
|
|
if (difficulty == NULL) {
|
|
|
|
return create_key(ctx, key);
|
|
|
|
}
|
|
|
|
|
2022-08-30 11:01:54 -04:00
|
|
|
clock_gettime(CLOCK_MONOTONIC, &t1);
|
2022-08-28 17:34:58 -04:00
|
|
|
while (1) {
|
|
|
|
if (!create_key(ctx, key))
|
|
|
|
return 0;
|
|
|
|
|
2022-08-30 11:01:54 -04:00
|
|
|
attempts++;
|
|
|
|
|
2022-08-30 11:24:38 -04:00
|
|
|
if ((bits = count_leading_zero_bits(key->pubkey)) >= *difficulty) {
|
2022-08-30 11:01:54 -04:00
|
|
|
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;
|
2022-08-30 11:24:38 -04:00
|
|
|
fprintf(stderr, "mined pubkey with %d bits after %" PRIu64 " attempts, %" PRId64 " ms, %f attempts per ms\n", bits, attempts, duration, pers);
|
2022-08-28 17:34:58 -04:00
|
|
|
return 1;
|
2022-08-30 11:01:54 -04:00
|
|
|
}
|
2022-08-30 11:09:30 -04:00
|
|
|
|
|
|
|
// 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);
|
2022-08-28 17:34:58 -04:00
|
|
|
}
|
2022-04-14 10:52:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2022-04-17 21:32:00 -04:00
|
|
|
static unsigned char buf[102400];
|
2022-04-14 10:52:29 -04:00
|
|
|
|
|
|
|
int len;
|
|
|
|
|
|
|
|
if (!(len = event_commitment(ev, buf, sizeof(buf)))) {
|
|
|
|
fprintf(stderr, "event_commitment: buffer out of space\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-04-14 11:00:34 -04:00
|
|
|
//fprintf(stderr, "commitment: '%.*s'\n", len, buf);
|
2022-04-14 15:35:26 -04:00
|
|
|
|
2022-04-14 10:52:29 -04:00
|
|
|
sha256((struct sha256*)ev->id, buf, len);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sign_event(secp256k1_context *ctx, struct key *key, struct nostr_event *ev)
|
|
|
|
{
|
2022-04-14 15:35:26 -04:00
|
|
|
if (!make_sig(ctx, key, ev->id, ev->sig))
|
2022-04-14 10:52:29 -04:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int print_event(struct nostr_event *ev, int envelope)
|
|
|
|
{
|
2022-04-17 21:32:00 -04:00
|
|
|
unsigned char buf[102400];
|
2022-04-14 10:52:29 -04:00
|
|
|
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");
|
2022-04-14 15:35:26 -04:00
|
|
|
|
2022-04-14 10:52:29 -04:00
|
|
|
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;
|
2022-04-21 09:01:52 -04:00
|
|
|
ev->kind = args->flags & HAS_KIND ? args->kind : 1;
|
2022-04-14 10:52:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int parse_num(const char *arg, uint64_t *t)
|
|
|
|
{
|
2022-04-14 15:35:26 -04:00
|
|
|
*t = strtol(arg, NULL, 10);
|
2022-04-14 10:52:29 -04:00
|
|
|
return errno != EINVAL;
|
|
|
|
}
|
|
|
|
|
2022-05-05 19:50:08 -04:00
|
|
|
static int nostr_add_tag_n(struct nostr_event *ev, const char **ts, int n_ts)
|
2022-04-20 13:37:12 -04:00
|
|
|
{
|
2022-05-05 19:50:08 -04:00
|
|
|
int i;
|
2022-04-20 13:37:12 -04:00
|
|
|
struct nostr_tag *tag;
|
|
|
|
|
|
|
|
if (ev->num_tags + 1 > MAX_TAGS)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
tag = &ev->tags[ev->num_tags++];
|
2022-05-05 19:50:08 -04:00
|
|
|
|
|
|
|
tag->num_elems = n_ts;
|
|
|
|
for (i = 0; i < n_ts; i++) {
|
|
|
|
tag->strs[i] = ts[i];
|
|
|
|
}
|
|
|
|
|
2022-04-20 13:37:12 -04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-05-05 19:50:08 -04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-04-20 13:37:12 -04:00
|
|
|
|
|
|
|
static int parse_args(int argc, const char *argv[], struct args *args, struct nostr_event *ev)
|
2022-04-14 10:52:29 -04:00
|
|
|
{
|
2022-04-20 13:37:12 -04:00
|
|
|
const char *arg, *arg2;
|
2022-04-14 10:52:29 -04:00
|
|
|
uint64_t n;
|
2022-04-20 13:37:12 -04:00
|
|
|
int has_added_tags = 0;
|
2022-04-14 10:52:29 -04:00
|
|
|
|
|
|
|
argv++; argc--;
|
|
|
|
for (; argc; ) {
|
|
|
|
arg = *argv++; argc--;
|
2022-11-17 16:21:07 -05:00
|
|
|
|
|
|
|
if (!strcmp(arg, "--help")) {
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
|
2022-04-14 10:52:29 -04:00
|
|
|
if (!argc) {
|
2022-04-20 19:23:00 -04:00
|
|
|
fprintf(stderr, "expected argument: '%s'\n", arg);
|
|
|
|
return 0;
|
2022-04-14 10:52:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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")) {
|
2022-04-21 09:01:52 -04:00
|
|
|
arg = *argv++; argc--;
|
2022-04-14 10:52:29 -04:00
|
|
|
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;
|
2022-04-17 21:32:00 -04:00
|
|
|
} else if (!strcmp(arg, "--tags")) {
|
2022-04-20 13:37:12 -04:00
|
|
|
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;
|
|
|
|
}
|
2022-04-17 21:32:00 -04:00
|
|
|
arg = *argv++; argc--;
|
|
|
|
args->tags = arg;
|
2022-04-20 19:23:00 -04:00
|
|
|
} 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;
|
|
|
|
}
|
2022-11-17 00:11:33 -05:00
|
|
|
} 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;
|
|
|
|
}
|
2022-04-20 13:37:12 -04:00
|
|
|
} 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--;
|
2022-04-20 19:23:00 -04:00
|
|
|
if (argc == 0) {
|
2022-04-20 13:37:12 -04:00
|
|
|
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;
|
|
|
|
}
|
2022-08-28 17:34:58 -04:00
|
|
|
} else if (!strcmp(arg, "--mine-pubkey")) {
|
|
|
|
args->flags |= HAS_MINE_PUBKEY;
|
2022-04-15 13:30:52 -04:00
|
|
|
} else if (!strcmp(arg, "--pow")) {
|
2022-04-20 13:37:12 -04:00
|
|
|
if (args->tags) {
|
|
|
|
fprintf(stderr, "can't combine --tags and --pow (yet)\n");
|
|
|
|
return 0;
|
|
|
|
}
|
2022-04-15 13:30:52 -04:00
|
|
|
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;
|
2022-04-14 15:35:26 -04:00
|
|
|
} 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;
|
2022-04-20 19:23:00 -04:00
|
|
|
} else if (!strcmp(arg, "--content")) {
|
|
|
|
arg = *argv++; argc--;
|
|
|
|
args->content = arg;
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "unexpected argument '%s'\n", arg);
|
2022-04-14 10:52:29 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-09 11:05:57 -04:00
|
|
|
if (!args->content)
|
|
|
|
args->content = "";
|
2022-04-20 19:23:00 -04:00
|
|
|
|
2022-04-14 10:52:29 -04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-04-14 15:35:26 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-05-05 19:50:08 -04:00
|
|
|
static int ensure_nonce_tag(struct nostr_event *ev, int target, int *index)
|
2022-04-15 13:30:52 -04:00
|
|
|
{
|
2022-05-05 19:50:08 -04:00
|
|
|
char *str_target = malloc(8);
|
2022-04-15 13:30:52 -04:00
|
|
|
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;
|
2022-05-05 19:50:08 -04:00
|
|
|
|
|
|
|
snprintf(str_target, 7, "%d", target);
|
|
|
|
const char *ts[] = { "nonce", "0", str_target };
|
|
|
|
|
|
|
|
return nostr_add_tag_n(ev, ts, 3);
|
2022-04-15 13:30:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int mine_event(struct nostr_event *ev, int difficulty)
|
|
|
|
{
|
|
|
|
char *strnonce = malloc(33);
|
|
|
|
struct nostr_tag *tag;
|
|
|
|
uint64_t nonce;
|
|
|
|
int index, res;
|
|
|
|
|
2022-05-05 19:50:08 -04:00
|
|
|
if (!ensure_nonce_tag(ev, difficulty, &index))
|
2022-04-15 13:30:52 -04:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
tag = &ev->tags[index];
|
2022-05-05 19:50:08 -04:00
|
|
|
assert(tag->num_elems == 3);
|
2022-04-15 13:30:52 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-04-14 15:35:26 -04:00
|
|
|
static int make_encrypted_dm(secp256k1_context *ctx, struct key *key,
|
2022-07-28 12:29:33 -04:00
|
|
|
struct nostr_event *ev, unsigned char nostr_pubkey[32], int kind)
|
2022-04-14 15:35:26 -04:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-04-14 22:44:02 -04:00
|
|
|
fprintf(stderr, "shared_secret ");
|
2022-04-14 15:35:26 -04:00
|
|
|
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;
|
2022-07-28 12:29:33 -04:00
|
|
|
ev->kind = kind;
|
2022-04-14 15:35:26 -04:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-11-17 20:20:58 -05:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-04-14 10:52:29 -04:00
|
|
|
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;
|
|
|
|
|
2022-11-17 20:20:58 -05:00
|
|
|
try_subcommand(argc, argv);
|
|
|
|
|
2022-04-20 13:37:12 -04:00
|
|
|
if (!parse_args(argc, argv, &args, &ev)) {
|
2022-04-14 22:44:02 -04:00
|
|
|
usage();
|
2022-04-14 10:52:29 -04:00
|
|
|
return 10;
|
2022-04-14 22:44:02 -04:00
|
|
|
}
|
2022-04-14 10:52:29 -04:00
|
|
|
|
2022-04-17 21:32:00 -04:00
|
|
|
if (args.tags) {
|
|
|
|
ev.explicit_tags = args.tags;
|
|
|
|
}
|
|
|
|
|
2022-04-14 10:52:29 -04:00
|
|
|
make_event_from_args(&ev, &args);
|
|
|
|
|
|
|
|
if (args.sec) {
|
|
|
|
if (!decode_key(ctx, args.sec, &key)) {
|
|
|
|
return 8;
|
|
|
|
}
|
|
|
|
} else {
|
2022-08-28 17:34:58 -04:00
|
|
|
int *difficulty = NULL;
|
|
|
|
if ((args.flags & HAS_DIFFICULTY) && (args.flags & HAS_MINE_PUBKEY)) {
|
|
|
|
difficulty = &args.difficulty;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!generate_key(ctx, &key, difficulty)) {
|
2022-04-14 15:35:26 -04:00
|
|
|
fprintf(stderr, "could not generate key\n");
|
2022-04-14 10:52:29 -04:00
|
|
|
return 4;
|
|
|
|
}
|
2022-04-14 22:44:02 -04:00
|
|
|
fprintf(stderr, "secret_key ");
|
|
|
|
print_hex(key.secret, sizeof(key.secret));
|
2022-08-30 11:22:41 -04:00
|
|
|
fprintf(stderr, "\n");
|
2022-04-14 10:52:29 -04:00
|
|
|
}
|
|
|
|
|
2022-04-14 15:35:26 -04:00
|
|
|
if (args.flags & HAS_ENCRYPT) {
|
2022-07-28 12:29:33 -04:00
|
|
|
int kind = args.flags & HAS_KIND? args.kind : 4;
|
|
|
|
if (!make_encrypted_dm(ctx, &key, &ev, args.encrypt_to, kind)) {
|
2022-04-14 15:35:26 -04:00
|
|
|
fprintf(stderr, "error making encrypted dm\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-14 10:52:29 -04:00
|
|
|
// set the event's pubkey
|
|
|
|
memcpy(ev.pubkey, key.pubkey, 32);
|
|
|
|
|
2022-08-28 17:34:58 -04:00
|
|
|
if (args.flags & HAS_DIFFICULTY && !(args.flags & HAS_MINE_PUBKEY)) {
|
2022-04-15 13:30:52 -04:00
|
|
|
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;
|
|
|
|
}
|
2022-04-14 10:52:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|