From afc31cd68f35d5d7344aef876a515b2e55e063b3 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Tue, 7 Mar 2017 11:29:16 +1030 Subject: [PATCH] lightningd: export bitcoin_pubkey as bip32_pubkey. We're going to need it. Signed-off-by: Rusty Russell --- lightningd/build_utxos.c | 17 +++++++++++++++++ lightningd/build_utxos.h | 5 +++++ lightningd/peer_control.c | 22 ++++------------------ 3 files changed, 26 insertions(+), 18 deletions(-) diff --git a/lightningd/build_utxos.c b/lightningd/build_utxos.c index a737e1175..9223852ae 100644 --- a/lightningd/build_utxos.c +++ b/lightningd/build_utxos.c @@ -246,3 +246,20 @@ const struct utxo **build_utxos(const tal_t *ctx, return tal_free(utxos); } + +bool bip32_pubkey(const struct ext_key *bip32_base, + struct pubkey *pubkey, u32 index) +{ + struct ext_key ext; + + assert(index < BIP32_INITIAL_HARDENED_CHILD); + if (bip32_key_from_parent(bip32_base, index, + BIP32_FLAG_KEY_PUBLIC, &ext) != WALLY_OK) + return false; + + if (!secp256k1_ec_pubkey_parse(secp256k1_ctx, &pubkey->pubkey, + ext.pub_key, sizeof(ext.pub_key))) + return false; + return true; +} + diff --git a/lightningd/build_utxos.h b/lightningd/build_utxos.h index 9fa0fde91..b00344b49 100644 --- a/lightningd/build_utxos.h +++ b/lightningd/build_utxos.h @@ -13,4 +13,9 @@ const struct utxo **build_utxos(const tal_t *ctx, /* Once we've spent them, mark them confirmed. */ void confirm_utxos(struct lightningd *ld, const struct utxo **utxos); + +struct ext_key; +bool bip32_pubkey(const struct ext_key *bip32_base, + struct pubkey *pubkey, u32 index); + #endif /* LIGHTNING_LIGHTNINGD_BUILD_UTXOS_H */ diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index b7786f3c3..aeee6ab2f 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -36,22 +36,6 @@ static void destroy_peer(struct peer *peer) peer->condition); } -static void bitcoin_pubkey(struct lightningd *ld, - struct pubkey *pubkey, u32 index) -{ - struct ext_key ext; - - assert(index < BIP32_INITIAL_HARDENED_CHILD); - assert(index < ld->bip32_max_index); - if (bip32_key_from_parent(ld->bip32_base, index, - BIP32_FLAG_KEY_PUBLIC, &ext) != WALLY_OK) - fatal("BIP32 of %u failed", index); - - if (!secp256k1_ec_pubkey_parse(secp256k1_ctx, &pubkey->pubkey, - ext.pub_key, sizeof(ext.pub_key))) - fatal("Parse of BIP32 child %u pubkey failed", index); -} - void peer_set_condition(struct peer *peer, const char *fmt, ...) { va_list ap; @@ -591,8 +575,10 @@ static void opening_gen_funding(struct subdaemon *opening, const u8 *resp, return; } - if (fc->change) - bitcoin_pubkey(fc->peer->ld, &changekey, fc->change_keyindex); + if (fc->change + && !bip32_pubkey(fc->peer->ld->bip32_base, + &changekey, fc->change_keyindex)) + fatal("Error deriving change key %u", fc->change_keyindex); fc->funding_tx = funding_tx(fc, &outnum, fc->utxomap, fc->satoshi, &fc->local_fundingkey,