Browse Source

onion_key: generate multiple keys at once

ppa-0.6.1
Anthony Towns 9 years ago
parent
commit
2042e1cdb7
  1. 2
      Makefile
  2. 41
      test/onion_key.c

2
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

41
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;
}

Loading…
Cancel
Save