bip32_pubkey: use more widely, don't open-code.

As a side-effect, we now only add txfilters for addresses we actually
expose, rather than always filtering for both p2sh and native segwit.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2019-06-05 14:58:59 +09:30
parent 92da5ef5f4
commit aa21eea62b
2 changed files with 15 additions and 34 deletions

View File

@ -1439,7 +1439,6 @@ static struct io_plan *handle_sign_withdrawal_tx(struct io_conn *conn,
u32 change_keyindex; u32 change_keyindex;
struct utxo **utxos; struct utxo **utxos;
struct bitcoin_tx *tx; struct bitcoin_tx *tx;
struct ext_key ext;
struct pubkey changekey; struct pubkey changekey;
u8 *scriptpubkey; u8 *scriptpubkey;
@ -1448,12 +1447,10 @@ static struct io_plan *handle_sign_withdrawal_tx(struct io_conn *conn,
&scriptpubkey, &utxos)) &scriptpubkey, &utxos))
return bad_req(conn, c, msg_in); return bad_req(conn, c, msg_in);
if (bip32_key_from_parent(&secretstuff.bip32, change_keyindex, if (!bip32_pubkey(&secretstuff.bip32, &changekey, change_keyindex))
BIP32_FLAG_KEY_PUBLIC, &ext) != WALLY_OK)
return bad_req_fmt(conn, c, msg_in, return bad_req_fmt(conn, c, msg_in,
"Failed to get key %u", change_keyindex); "Failed to get key %u", change_keyindex);
pubkey_from_der(ext.pub_key, sizeof(ext.pub_key), &changekey);
tx = withdraw_tx(tmpctx, cast_const2(const struct utxo **, utxos), tx = withdraw_tx(tmpctx, cast_const2(const struct utxo **, utxos),
scriptpubkey, satoshi_out, scriptpubkey, satoshi_out,
&changekey, change_out, NULL, NULL); &changekey, change_out, NULL, NULL);

View File

@ -98,8 +98,7 @@ static struct command_result *json_withdraw(struct command *cmd,
struct withdrawal *withdraw = tal(cmd, struct withdrawal); struct withdrawal *withdraw = tal(cmd, struct withdrawal);
u32 *feerate_per_kw; u32 *feerate_per_kw;
struct bitcoin_tx *tx; struct bitcoin_tx *tx;
struct ext_key ext; struct pubkey changekey;
struct pubkey pubkey;
enum address_parse_result addr_parse; enum address_parse_result addr_parse;
struct command_result *res; struct command_result *res;
u32 *minconf, maxheight; u32 *minconf, maxheight;
@ -147,17 +146,12 @@ static struct command_result *json_withdraw(struct command *cmd,
if (res) if (res)
return res; return res;
if (bip32_key_from_parent(cmd->ld->wallet->bip32_base, withdraw->wtx.change_key_index, if (!bip32_pubkey(cmd->ld->wallet->bip32_base, &changekey,
BIP32_FLAG_KEY_PUBLIC, &ext) != WALLY_OK) { withdraw->wtx.change_key_index)) {
return command_fail(cmd, LIGHTNINGD, "Keys generation failure"); return command_fail(cmd, LIGHTNINGD, "Keys generation failure");
} }
txfilter_add_scriptpubkey(cmd->ld->owned_txfilter,
if (!secp256k1_ec_pubkey_parse(secp256k1_ctx, &pubkey.pubkey, scriptpubkey_p2wpkh(tmpctx, &changekey));
ext.pub_key, sizeof(ext.pub_key))) {
return command_fail(cmd, LIGHTNINGD, "Key parsing failure");
}
txfilter_add_derkey(cmd->ld->owned_txfilter, ext.pub_key);
u8 *msg = towire_hsm_sign_withdrawal(cmd, u8 *msg = towire_hsm_sign_withdrawal(cmd,
withdraw->wtx.amount, withdraw->wtx.amount,
@ -302,11 +296,11 @@ static struct command_result *json_newaddr(struct command *cmd,
const jsmntok_t *params) const jsmntok_t *params)
{ {
struct json_stream *response; struct json_stream *response;
struct ext_key ext;
struct pubkey pubkey; struct pubkey pubkey;
enum addrtype *addrtype; enum addrtype *addrtype;
s64 keyidx; s64 keyidx;
char *p2sh, *bech32; char *p2sh, *bech32;
u8 *b32script;
if (!param(cmd, buffer, params, if (!param(cmd, buffer, params,
p_opt_def("addresstype", param_newaddr, &addrtype, ADDR_BECH32), p_opt_def("addresstype", param_newaddr, &addrtype, ADDR_BECH32),
@ -318,17 +312,15 @@ static struct command_result *json_newaddr(struct command *cmd,
return command_fail(cmd, LIGHTNINGD, "Keys exhausted "); return command_fail(cmd, LIGHTNINGD, "Keys exhausted ");
} }
if (bip32_key_from_parent(cmd->ld->wallet->bip32_base, keyidx, if (!bip32_pubkey(cmd->ld->wallet->bip32_base, &pubkey, keyidx))
BIP32_FLAG_KEY_PUBLIC, &ext) != WALLY_OK) {
return command_fail(cmd, LIGHTNINGD, "Keys generation failure"); return command_fail(cmd, LIGHTNINGD, "Keys generation failure");
}
if (!secp256k1_ec_pubkey_parse(secp256k1_ctx, &pubkey.pubkey, b32script = scriptpubkey_p2wpkh(tmpctx, &pubkey);
ext.pub_key, sizeof(ext.pub_key))) { if (*addrtype & ADDR_BECH32)
return command_fail(cmd, LIGHTNINGD, "Key parsing failure"); txfilter_add_scriptpubkey(cmd->ld->owned_txfilter, b32script);
} if (*addrtype & ADDR_P2SH_SEGWIT)
txfilter_add_scriptpubkey(cmd->ld->owned_txfilter,
txfilter_add_derkey(cmd->ld->owned_txfilter, ext.pub_key); scriptpubkey_p2sh(tmpctx, b32script));
p2sh = encode_pubkey_to_addr(cmd, cmd->ld, &pubkey, true, NULL); p2sh = encode_pubkey_to_addr(cmd, cmd->ld, &pubkey, true, NULL);
bech32 = encode_pubkey_to_addr(cmd, cmd->ld, &pubkey, false, NULL); bech32 = encode_pubkey_to_addr(cmd, cmd->ld, &pubkey, false, NULL);
@ -365,7 +357,6 @@ static struct command_result *json_listaddrs(struct command *cmd,
const jsmntok_t *params) const jsmntok_t *params)
{ {
struct json_stream *response; struct json_stream *response;
struct ext_key ext;
struct pubkey pubkey; struct pubkey pubkey;
u64 *bip32_max_index; u64 *bip32_max_index;
@ -389,15 +380,8 @@ static struct command_result *json_listaddrs(struct command *cmd,
break; break;
} }
if (bip32_key_from_parent(cmd->ld->wallet->bip32_base, keyidx, if (!bip32_pubkey(cmd->ld->wallet->bip32_base, &pubkey, keyidx))
BIP32_FLAG_KEY_PUBLIC, &ext) != WALLY_OK) {
abort(); abort();
}
if (!secp256k1_ec_pubkey_parse(secp256k1_ctx, &pubkey.pubkey,
ext.pub_key, sizeof(ext.pub_key))) {
abort();
}
// p2sh // p2sh
u8 *redeemscript_p2sh; u8 *redeemscript_p2sh;