Skip to content

Commit

Permalink
devtools: Minor cleanup of the onion command line tool
Browse files Browse the repository at this point in the history
Simplifying some operations, erroring in some cases and moving to global
defines for constants.

Suggested-by: Rusty Russell <@rustyrussell>
Signed-off-by: Christian Decker <[email protected]>
  • Loading branch information
cdecker authored and rustyrussell committed Jul 30, 2019
1 parent 78c7edb commit 581694f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 19 deletions.
2 changes: 2 additions & 0 deletions bitcoin/privkey.h
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
27 changes: 8 additions & 19 deletions devtools/onion.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,14 @@
#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])
{
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;
Expand All @@ -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]);
Expand All @@ -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') {
Expand Down

0 comments on commit 581694f

Please sign in to comment.