From 2042e1cdb746b58b6641211ff96963ad92174889 Mon Sep 17 00:00:00 2001 From: Anthony Towns Date: Tue, 6 Oct 2015 23:49:52 +1000 Subject: [PATCH] onion_key: generate multiple keys at once --- Makefile | 2 +- test/onion_key.c | 41 +++++++++++++++++++++++++++++++++++------ 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index c666091ad..5d229915e 100644 --- a/Makefile +++ b/Makefile @@ -101,7 +101,7 @@ test-cli-tests: $(TEST_CLI_PROGRAMS) set -e; cd test-cli; for args in "" --steal --unilateral --htlc-onchain; do scripts/setup.sh && scripts/test.sh $$args && scripts/shutdown.sh; done test-onion: test/test_onion test/onion_key - set -e; TMPF=/tmp/onion.$$$$; test/test_onion --generate $$(for k in `seq 20`; do test/onion_key $$k; done | cut -d: -f2) > $$TMPF; for k in `seq 20`; do test/test_onion --decode $$(test/onion_key $$k | cut -d: -f1) < $$TMPF > $$TMPF.unwrap; mv $$TMPF.unwrap $$TMPF; done; rm -f $$TMPF + set -e; TMPF=/tmp/onion.$$$$; test/test_onion --generate $$(test/onion_key --pub `seq 20`) > $$TMPF; for k in `seq 20`; do test/test_onion --decode $$(test/onion_key --priv $$k) < $$TMPF > $$TMPF.unwrap; mv $$TMPF.unwrap $$TMPF; done; rm -f $$TMPF check: test-cli-tests test-onion diff --git a/test/onion_key.c b/test/onion_key.c index c6ea69c57..9ab4740a7 100644 --- a/test/onion_key.c +++ b/test/onion_key.c @@ -113,7 +113,7 @@ static void gen_keys(secp256k1_context *ctx, memcpy(pubkey, tmp+1, sizeof(*pubkey)); } -int main(int argc, char *argv[]) +void print_keypair(int pub, int priv) { secp256k1_context *ctx; struct seckey seckey; @@ -121,16 +121,45 @@ int main(int argc, char *argv[]) char sechex[hex_str_size(sizeof(seckey))]; char pubhex[hex_str_size(sizeof(pubkey))]; - if (argv[1]) - srandom(atoi(argv[1])); - else - srandom(time(NULL) + getpid()); + assert(pub || priv); ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN); gen_keys(ctx, &seckey, &pubkey); hex_encode(&seckey, sizeof(seckey), sechex, sizeof(sechex)); hex_encode(&pubkey, sizeof(pubkey), pubhex, sizeof(pubhex)); - printf("%s:%s\n", sechex, pubhex); + + if (pub && priv) { + printf("%s:%s\n", sechex, pubhex); + } else { + printf("%s\n", (priv ? sechex : pubhex)); + } +} + +int main(int argc, char *argv[]) +{ + int pub = 1, priv = 1; + + if (argc >= 1) { + if (strcmp(argv[1], "--pub") == 0) { + pub = 1; priv = 0; + argc--; argv++; + } else if (strcmp(argv[1], "--priv") == 0) { + pub = 0; priv = 1; + argc--; argv++; + } + } + + if (argc <= 1) { + srandom(time(NULL) + getpid()); + print_keypair(pub, priv); + } else { + int i; + for (i = 1; i < argc; i++) { + srandom(atoi(argv[i])); + print_keypair(pub, priv); + } + } + return 0; }