devtools: Minor cleanup of the onion command line tool

Simplifying some operations, erroring in some cases and moving to global
defines for constants.

Suggested-by: Rusty Russell <@rustyrussell>
Signed-off-by: Christian Decker <decker.christian@gmail.com>
This commit is contained in:
Christian Decker 2019-07-26 18:30:08 +02:00 committed by Rusty Russell
parent 78c7edb889
commit 581694fdda
2 changed files with 10 additions and 19 deletions

View File

@ -4,6 +4,8 @@
#include <ccan/short_types/short_types.h>
#include <ccan/structeq/structeq.h>
#define PRIVKEY_LEN 32
/* General 256-bit secret, which must be private. Used in various places. */
struct secret {
u8 data[32];

View File

@ -18,8 +18,6 @@
#include <unistd.h>
#define ASSOC_DATA_SIZE 32
#define PUBKEY_LEN 33
#define PRIVKEY_LEN 32
static void do_generate(int argc, char **argv,
const u8 assocdata[ASSOC_DATA_SIZE])
@ -27,7 +25,7 @@ static void do_generate(int argc, char **argv,
const tal_t *ctx = talz(NULL, tal_t);
int num_hops = argc - 2;
struct pubkey *path = tal_arr(ctx, struct pubkey, num_hops);
u8 rawpubkey[PUBKEY_LEN], rawprivkey[PRIVKEY_LEN];
u8 rawprivkey[PRIVKEY_LEN];
struct secret session_key;
struct secret *shared_secrets;
struct sphinx_path *sp;
@ -40,7 +38,7 @@ static void do_generate(int argc, char **argv,
for (int i = 0; i < num_hops; i++) {
size_t klen = strcspn(argv[1 + i], "/");
if (klen == 2 * PRIVKEY_LEN) {
if (hex_data_size(klen) == PRIVKEY_LEN) {
if (!hex_decode(argv[1 + i], klen, rawprivkey, PRIVKEY_LEN))
errx(1, "Invalid private key hex '%s'",
argv[1 + i]);
@ -49,25 +47,16 @@ static void do_generate(int argc, char **argv,
&path[i].pubkey,
rawprivkey) != 1)
errx(1, "Could not decode pubkey");
} else if (klen == 2 * PUBKEY_LEN) {
if (!hex_decode(argv[1 + i], 2 * PUBKEY_LEN, rawpubkey,
PUBKEY_LEN)) {
} else if (hex_data_size(klen) == PUBKEY_CMPR_LEN) {
if (!pubkey_from_hexstr(argv[i + 1], klen, &path[i]))
errx(1, "Invalid public key hex '%s'",
argv[1 + i]);
}
if (secp256k1_ec_pubkey_parse(secp256k1_ctx,
&path[i].pubkey,
rawpubkey, PUBKEY_LEN) != 1)
errx(1, "Could not decode pubkey");
} else {
fprintf(stderr,
"Provided key is neither a pubkey nor a "
"privkey: %s\n",
argv[1 + i]);
errx(1,
"Provided key is neither a pubkey nor a privkey: "
"%s\n",
argv[1 + i]);
}
fprintf(stderr, "Node %d pubkey %s\n", i,
secp256k1_pubkey_to_hexstr(ctx, &path[i].pubkey));
memset(&hops_data[i], 0, sizeof(hops_data[i]));
if (argv[1 + i][klen] != '\0') {