From ca0dc01bee6f536b14831d8714900e3b80f48bb7 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Sat, 16 Feb 2019 18:04:02 +0100 Subject: [PATCH] sphinx: Fix the onion cli tool to take public keys This was a mismatch between the go tool and this test tool so far. Just aligning the tools to allows for easier testing. Signed-off-by: Christian Decker --- devtools/onion.c | 38 +++++++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/devtools/onion.c b/devtools/onion.c index b383d9b57..0d110be02 100644 --- a/devtools/onion.c +++ b/devtools/onion.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -12,6 +13,8 @@ #include #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]) @@ -19,25 +22,45 @@ static void do_generate(int argc, char **argv, const tal_t *ctx = talz(NULL, tal_t); int num_hops = argc - 1; struct pubkey *path = tal_arr(ctx, struct pubkey, num_hops); - u8 privkeys[argc - 1][32]; + u8 rawpubkey[PUBKEY_LEN], rawprivkey[PRIVKEY_LEN]; struct secret session_key; struct hop_data hops_data[num_hops]; struct secret *shared_secrets; struct sphinx_path *sp; - assocdata = tal_arr(ctx, u8, 32); + assocdata = tal_arr(ctx, u8, ASSOC_DATA_SIZE); memset(&session_key, 'A', sizeof(struct secret)); sp = sphinx_path_new_with_key(ctx, assocdata, &session_key); for (int i = 0; i < num_hops; i++) { size_t klen = strcspn(argv[1 + i], "/"); - if (!hex_decode(argv[1 + i], klen, privkeys[i], 32)) - errx(1, "Invalid private key hex '%s'", argv[1 + i]); + if (klen == 2 * PRIVKEY_LEN) { + if (!hex_decode(argv[1 + i], klen, rawprivkey, PRIVKEY_LEN)) + errx(1, "Invalid private key hex '%s'", + argv[1 + i]); - if (secp256k1_ec_pubkey_create(secp256k1_ctx, &path[i].pubkey, - privkeys[i]) != 1) - errx(1, "Could not decode pubkey"); + if (secp256k1_ec_pubkey_create(secp256k1_ctx, + &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)) { + 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]); + } fprintf(stderr, "Node %d pubkey %s\n", i, secp256k1_pubkey_to_hexstr(ctx, &path[i].pubkey)); @@ -171,6 +194,7 @@ int main(int argc, char **argv) SECP256K1_CONTEXT_SIGN); opt_register_noarg("--help|-h", opt_usage_and_exit, + "--generate [/hopdata] [/hopdata]... OR\n" "--generate [/hopdata] [/hopdata]... OR\n" "--decode \n" "Either create an onion message, or decode one step",