Skip to content

Commit 260149c

Browse files
committed
lightningd: return addrtype when asking wallet_can_spend.
Not just the key index. Also, wallet_can_spend is no longer slow with lots of inputs! Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
1 parent d3c7d2c commit 260149c

File tree

6 files changed

+22
-18
lines changed

6 files changed

+22
-18
lines changed

lightningd/closing_control.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ void peer_start_closingd(struct channel *channel, struct peer_fd *peer_fd)
485485
ld->wallet,
486486
channel->shutdown_scriptpubkey[LOCAL],
487487
tal_bytelen(channel->shutdown_scriptpubkey[LOCAL]),
488-
&index_val)) {
488+
&index_val, NULL)) {
489489
if (bip32_key_from_parent(
490490
ld->bip32_base,
491491
index_val,
@@ -744,7 +744,7 @@ static struct command_result *json_close(struct command *cmd,
744744

745745
/* If they give a local address, adjust final_key_idx. */
746746
if (!wallet_can_spend(cmd->ld->wallet, close_to_script, tal_bytelen(close_to_script),
747-
&final_key_idx)) {
747+
&final_key_idx, NULL)) {
748748
final_key_idx = channel->final_key_idx;
749749
}
750750
} else {

lightningd/dual_open_control.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,7 @@ openchannel2_hook_cb(struct openchannel2_payload *payload STEALS)
696696
if (wallet_can_spend(dualopend->ld->wallet,
697697
payload->our_shutdown_scriptpubkey,
698698
tal_bytelen(payload->our_shutdown_scriptpubkey),
699-
&found_wallet_index)) {
699+
&found_wallet_index, NULL)) {
700700
our_shutdown_script_wallet_index = tal(tmpctx, u32);
701701
*our_shutdown_script_wallet_index = found_wallet_index;
702702
} else
@@ -3092,7 +3092,7 @@ static struct command_result *openchannel_init(struct command *cmd,
30923092
if (wallet_can_spend(cmd->ld->wallet,
30933093
oa->our_upfront_shutdown_script,
30943094
tal_bytelen(oa->our_upfront_shutdown_script),
3095-
&found_wallet_index)) {
3095+
&found_wallet_index, NULL)) {
30963096
our_upfront_shutdown_script_wallet_index = &found_wallet_index;
30973097
} else
30983098
our_upfront_shutdown_script_wallet_index = NULL;
@@ -3861,7 +3861,7 @@ static struct command_result *json_queryrates(struct command *cmd,
38613861
if (wallet_can_spend(cmd->ld->wallet,
38623862
oa->our_upfront_shutdown_script,
38633863
tal_bytelen(oa->our_upfront_shutdown_script),
3864-
&found_wallet_index)) {
3864+
&found_wallet_index, NULL)) {
38653865
our_upfront_shutdown_script_wallet_index = tal(tmpctx, u32);
38663866
*our_upfront_shutdown_script_wallet_index = found_wallet_index;
38673867
} else
@@ -4209,7 +4209,7 @@ bool peer_restart_dualopend(struct peer *peer,
42094209
if (wallet_can_spend(peer->ld->wallet,
42104210
channel->shutdown_scriptpubkey[LOCAL],
42114211
tal_bytelen(channel->shutdown_scriptpubkey[LOCAL]),
4212-
&found_wallet_index)) {
4212+
&found_wallet_index, NULL)) {
42134213
local_shutdown_script_wallet_index = tal(tmpctx, u32);
42144214
*local_shutdown_script_wallet_index = found_wallet_index;
42154215
} else

lightningd/opening_control.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,7 @@ openchannel_hook_final(struct openchannel_hook_payload *payload STEALS)
717717
if (wallet_can_spend(payload->openingd->ld->wallet,
718718
our_upfront_shutdown_script,
719719
tal_bytelen(our_upfront_shutdown_script),
720-
&found_wallet_index)) {
720+
&found_wallet_index, NULL)) {
721721
upfront_shutdown_script_wallet_index = tal(tmpctx, u32);
722722
*upfront_shutdown_script_wallet_index = found_wallet_index;
723723
} else
@@ -1408,7 +1408,7 @@ static struct command_result *json_fundchannel_start(struct command *cmd,
14081408
if (wallet_can_spend(fc->cmd->ld->wallet,
14091409
fc->our_upfront_shutdown_script,
14101410
tal_bytelen(fc->our_upfront_shutdown_script),
1411-
&found_wallet_index)) {
1411+
&found_wallet_index, NULL)) {
14121412
upfront_shutdown_script_wallet_index = tal(tmpctx, u32);
14131413
*upfront_shutdown_script_wallet_index = found_wallet_index;
14141414
} else

wallet/wallet.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -909,7 +909,7 @@ bool wallet_add_onchaind_utxo(struct wallet *w,
909909
}
910910

911911
bool wallet_can_spend(struct wallet *w, const u8 *script, size_t script_len,
912-
u32 *index)
912+
u32 *index, enum addrtype *addrtype)
913913
{
914914
u64 bip32_max_index;
915915
const struct wallet_address *waddr;
@@ -930,6 +930,8 @@ bool wallet_can_spend(struct wallet *w, const u8 *script, size_t script_len,
930930
db_set_intvar(w->db, "bip32_max_index", waddr->index);
931931

932932
*index = waddr->index;
933+
if (addrtype)
934+
*addrtype = waddr->addrtype;
933935
return true;
934936
}
935937

@@ -2914,6 +2916,7 @@ void wallet_confirm_tx(struct wallet *w,
29142916

29152917
static void got_utxo(struct wallet *w,
29162918
u64 keyindex,
2919+
enum addrtype addrtype,
29172920
const struct wally_tx *wtx,
29182921
size_t outnum,
29192922
bool is_coinbase,
@@ -2925,7 +2928,7 @@ static void got_utxo(struct wallet *w,
29252928
struct amount_asset asset = wally_tx_output_get_amount(txout);
29262929

29272930
utxo->keyindex = keyindex;
2928-
utxo->is_p2sh = is_p2sh(txout->script, txout->script_len, NULL);
2931+
utxo->is_p2sh = (addrtype == ADDR_P2SH_SEGWIT);
29292932
utxo->amount = amount_asset_to_sat(&asset);
29302933
utxo->status = OUTPUT_STATE_AVAILABLE;
29312934
wally_txid(wtx, &utxo->outpoint.txid);
@@ -2987,14 +2990,15 @@ int wallet_extract_owned_outputs(struct wallet *w, const struct wally_tx *wtx,
29872990
const struct wally_tx_output *txout = &wtx->outputs[i];
29882991
u32 keyindex;
29892992
struct amount_asset asset = wally_tx_output_get_amount(txout);
2993+
enum addrtype addrtype;
29902994

29912995
if (!amount_asset_is_main(&asset))
29922996
continue;
29932997

2994-
if (!wallet_can_spend(w, txout->script, txout->script_len, &keyindex))
2998+
if (!wallet_can_spend(w, txout->script, txout->script_len, &keyindex, &addrtype))
29952999
continue;
29963000

2997-
got_utxo(w, keyindex, wtx, i, is_coinbase, blockheight, NULL);
3001+
got_utxo(w, keyindex, addrtype, wtx, i, is_coinbase, blockheight, NULL);
29983002
num_utxos++;
29993003
}
30003004
return num_utxos;
@@ -6556,7 +6560,7 @@ static void mutual_close_p2pkh_catch(struct bitcoind *bitcoind,
65566560
missing->addrs[n].scriptpubkey,
65576561
tal_bytelen(missing->addrs[n].scriptpubkey)))
65586562
continue;
6559-
got_utxo(w, missing->addrs[n].keyidx,
6563+
got_utxo(w, missing->addrs[n].keyidx, ADDR_BECH32,
65606564
wtx, outnum, i == 0, &height, &outp);
65616565
log_broken(bitcoind->ld->log, "Rescan found %s!",
65626566
fmt_bitcoin_outpoint(tmpctx, &outp));

wallet/wallet.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -587,17 +587,17 @@ struct utxo **wallet_utxo_boost(const tal_t *ctx,
587587
/**
588588
* wallet_can_spend - Do we have the private key matching this scriptpubkey?
589589
*
590-
* FIXME: This is very slow with lots of inputs!
591-
*
592590
* @w: (in) wallet holding the pubkeys to check against (privkeys are on HSM)
593591
* @script: (in) the script to check
594592
* @script_len: (in) the length of @script
595593
* @index: (out) the bip32 derivation index that matched the script
594+
* @addrtype: (out) if non-NULL, set to address type of script.
596595
*/
597596
bool wallet_can_spend(struct wallet *w,
598597
const u8 *script,
599598
size_t script_len,
600-
u32 *index);
599+
u32 *index,
600+
enum addrtype *addrtype);
601601

602602
/**
603603
* wallet_get_newindex - get a new index from the wallet.

wallet/walletrpc.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,7 @@ static void match_psbt_outputs_to_wallet(struct wally_psbt *psbt,
729729
const size_t script_len = psbt->outputs[outndx].script_len;
730730
u32 index;
731731

732-
if (!wallet_can_spend(w, script, script_len, &index))
732+
if (!wallet_can_spend(w, script, script_len, &index, NULL))
733733
continue;
734734

735735
if (bip32_key_from_parent(
@@ -936,7 +936,7 @@ static void maybe_notify_new_external_send(struct lightningd *ld,
936936
/* If it's going to our wallet, ignore */
937937
script = wally_psbt_output_get_script(tmpctx,
938938
&psbt->outputs[outnum]);
939-
if (wallet_can_spend(ld->wallet, script, tal_bytelen(script), &index))
939+
if (wallet_can_spend(ld->wallet, script, tal_bytelen(script), &index, NULL))
940940
return;
941941

942942
outpoint.txid = *txid;

0 commit comments

Comments
 (0)