Skip to content

Commit 0289ee2

Browse files
committed
onion_key: generate multiple keys at once
1 parent ed46dd3 commit 0289ee2

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ test-cli-tests: $(TEST_CLI_PROGRAMS)
101101
set -e; cd test-cli; for args in "" --steal --unilateral --htlc-onchain; do scripts/setup.sh && scripts/test.sh $$args && scripts/shutdown.sh; done
102102

103103
test-onion: test/test_onion test/onion_key
104-
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
104+
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
105105

106106
check: test-cli-tests test-onion
107107

test/onion_key.c

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,24 +113,53 @@ static void gen_keys(secp256k1_context *ctx,
113113
memcpy(pubkey, tmp+1, sizeof(*pubkey));
114114
}
115115

116-
int main(int argc, char *argv[])
116+
void print_keypair(int pub, int priv)
117117
{
118118
secp256k1_context *ctx;
119119
struct seckey seckey;
120120
struct onion_pubkey pubkey;
121121
char sechex[hex_str_size(sizeof(seckey))];
122122
char pubhex[hex_str_size(sizeof(pubkey))];
123123

124-
if (argv[1])
125-
srandom(atoi(argv[1]));
126-
else
127-
srandom(time(NULL) + getpid());
124+
assert(pub || priv);
128125

129126
ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN);
130127
gen_keys(ctx, &seckey, &pubkey);
131128

132129
hex_encode(&seckey, sizeof(seckey), sechex, sizeof(sechex));
133130
hex_encode(&pubkey, sizeof(pubkey), pubhex, sizeof(pubhex));
134-
printf("%s:%s\n", sechex, pubhex);
131+
132+
if (pub && priv) {
133+
printf("%s:%s\n", sechex, pubhex);
134+
} else {
135+
printf("%s\n", (priv ? sechex : pubhex));
136+
}
137+
}
138+
139+
int main(int argc, char *argv[])
140+
{
141+
int pub = 1, priv = 1;
142+
143+
if (argc >= 1) {
144+
if (strcmp(argv[1], "--pub") == 0) {
145+
pub = 1; priv = 0;
146+
argc--; argv++;
147+
} else if (strcmp(argv[1], "--priv") == 0) {
148+
pub = 0; priv = 1;
149+
argc--; argv++;
150+
}
151+
}
152+
153+
if (argc <= 1) {
154+
srandom(time(NULL) + getpid());
155+
print_keypair(pub, priv);
156+
} else {
157+
int i;
158+
for (i = 1; i < argc; i++) {
159+
srandom(atoi(argv[i]));
160+
print_keypair(pub, priv);
161+
}
162+
}
163+
135164
return 0;
136165
}

0 commit comments

Comments
 (0)