Skip to content

Commit

Permalink
wallet: remove output_is_p2sh from wallet_can_spend
Browse files Browse the repository at this point in the history
Only one caller needs it, and they can trivially and cheaply compute it
themselves.

Signed-off-by: Jon Griffiths <[email protected]>
  • Loading branch information
jgriffiths authored and rustyrussell committed Mar 18, 2024
1 parent 3f678ad commit f01e9fe
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 61 deletions.
8 changes: 2 additions & 6 deletions lightningd/closing_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -461,12 +461,10 @@ void peer_start_closingd(struct channel *channel, struct peer_fd *peer_fd)
struct ext_key *local_wallet_ext_key = NULL;
u32 index_val;
struct ext_key ext_key_val;
bool is_p2sh;
if (wallet_can_spend(
ld->wallet,
channel->shutdown_scriptpubkey[LOCAL],
&index_val,
&is_p2sh)) {
&index_val)) {
if (bip32_key_from_parent(
ld->bip32_base,
index_val,
Expand Down Expand Up @@ -690,8 +688,6 @@ static struct command_result *json_close(struct command *cmd,
assert(channel->final_key_idx <= UINT32_MAX);

if (close_to_script) {
bool is_p2sh;

if (!tal_arr_eq(close_to_script, channel->shutdown_scriptpubkey[LOCAL])
&& !cmd->ld->dev_allow_shutdown_destination_change) {
const u8 *defp2tr, *defp2wpkh;
Expand Down Expand Up @@ -727,7 +723,7 @@ static struct command_result *json_close(struct command *cmd,

/* If they give a local address, adjust final_key_idx. */
if (!wallet_can_spend(cmd->ld->wallet, close_to_script,
&final_key_idx, &is_p2sh)) {
&final_key_idx)) {
final_key_idx = channel->final_key_idx;
}
} else {
Expand Down
16 changes: 4 additions & 12 deletions lightningd/dual_open_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -698,11 +698,9 @@ openchannel2_hook_cb(struct openchannel2_payload *payload STEALS)
/* Determine the wallet index for our_shutdown_scriptpubkey,
* NULL if not found. */
u32 found_wallet_index;
bool is_p2sh;
if (wallet_can_spend(dualopend->ld->wallet,
payload->our_shutdown_scriptpubkey,
&found_wallet_index,
&is_p2sh)) {
&found_wallet_index)) {
our_shutdown_script_wallet_index = tal(tmpctx, u32);
*our_shutdown_script_wallet_index = found_wallet_index;
} else
Expand Down Expand Up @@ -3176,11 +3174,9 @@ static struct command_result *json_openchannel_init(struct command *cmd,
/* Determine the wallet index for our_upfront_shutdown_script,
* NULL if not found. */
u32 found_wallet_index;
bool is_p2sh;
if (wallet_can_spend(cmd->ld->wallet,
oa->our_upfront_shutdown_script,
&found_wallet_index,
&is_p2sh)) {
&found_wallet_index)) {
our_upfront_shutdown_script_wallet_index = tal(tmpctx, u32);
*our_upfront_shutdown_script_wallet_index = found_wallet_index;
} else
Expand Down Expand Up @@ -3776,11 +3772,9 @@ static struct command_result *json_queryrates(struct command *cmd,
/* Determine the wallet index for our_upfront_shutdown_script,
* NULL if not found. */
u32 found_wallet_index;
bool is_p2sh;
if (wallet_can_spend(cmd->ld->wallet,
oa->our_upfront_shutdown_script,
&found_wallet_index,
&is_p2sh)) {
&found_wallet_index)) {
our_upfront_shutdown_script_wallet_index = tal(tmpctx, u32);
*our_upfront_shutdown_script_wallet_index = found_wallet_index;
} else
Expand Down Expand Up @@ -4126,11 +4120,9 @@ bool peer_restart_dualopend(struct peer *peer,
/* Determine the wallet index for the LOCAL shutdown_scriptpubkey,
* NULL if not found. */
u32 found_wallet_index;
bool is_p2sh;
if (wallet_can_spend(peer->ld->wallet,
channel->shutdown_scriptpubkey[LOCAL],
&found_wallet_index,
&is_p2sh)) {
&found_wallet_index)) {
local_shutdown_script_wallet_index = tal(tmpctx, u32);
*local_shutdown_script_wallet_index = found_wallet_index;
} else
Expand Down
8 changes: 2 additions & 6 deletions lightningd/opening_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -717,11 +717,9 @@ openchannel_hook_final(struct openchannel_hook_payload *payload STEALS)
/* Determine the wallet index for our_upfront_shutdown_script,
* NULL if not found. */
u32 found_wallet_index;
bool is_p2sh;
if (wallet_can_spend(payload->openingd->ld->wallet,
our_upfront_shutdown_script,
&found_wallet_index,
&is_p2sh)) {
&found_wallet_index)) {
upfront_shutdown_script_wallet_index = tal(tmpctx, u32);
*upfront_shutdown_script_wallet_index = found_wallet_index;
} else
Expand Down Expand Up @@ -1321,11 +1319,9 @@ static struct command_result *json_fundchannel_start(struct command *cmd,
/* Determine the wallet index for our_upfront_shutdown_script,
* NULL if not found. */
u32 found_wallet_index;
bool is_p2sh;
if (wallet_can_spend(fc->cmd->ld->wallet,
fc->our_upfront_shutdown_script,
&found_wallet_index,
&is_p2sh)) {
&found_wallet_index)) {
upfront_shutdown_script_wallet_index = tal(tmpctx, u32);
*upfront_shutdown_script_wallet_index = found_wallet_index;
} else
Expand Down
45 changes: 19 additions & 26 deletions wallet/wallet.c
Original file line number Diff line number Diff line change
Expand Up @@ -795,20 +795,21 @@ bool wallet_add_onchaind_utxo(struct wallet *w,
}

bool wallet_can_spend(struct wallet *w, const u8 *script,
u32 *index, bool *output_is_p2sh)
u32 *index)
{
struct ext_key ext;
u64 bip32_max_index;
size_t script_len = tal_bytelen(script);
u32 i;
bool output_is_p2sh;

/* If not one of these, can't be for us. */
if (is_p2sh(script, script_len, NULL))
*output_is_p2sh = true;
output_is_p2sh = true;
else if (is_p2wpkh(script, script_len, NULL))
*output_is_p2sh = false;
output_is_p2sh = false;
else if (is_p2tr(script, script_len, NULL))
*output_is_p2sh = false;
output_is_p2sh = false;
else
return false;

Expand All @@ -822,7 +823,7 @@ bool wallet_can_spend(struct wallet *w, const u8 *script,
abort();
}
s = scriptpubkey_p2wpkh_derkey(w, ext.pub_key);
if (*output_is_p2sh) {
if (output_is_p2sh) {
u8 *p2sh = scriptpubkey_p2sh(w, s);
tal_free(s);
s = p2sh;
Expand Down Expand Up @@ -2739,45 +2740,37 @@ int wallet_extract_owned_outputs(struct wallet *w, const struct wally_tx *wtx,

if (total)
*total = AMOUNT_SAT(0);
for (size_t output = 0; output < wtx->num_outputs; output++) {
for (size_t i = 0; i < wtx->num_outputs; i++) {
const struct wally_tx_output *txout = &wtx->outputs[i];
struct utxo *utxo;
u32 index;
bool is_p2sh;
const u8 *script;
struct amount_asset asset =
wally_tx_output_get_amount(&wtx->outputs[output]);
struct amount_asset asset = wally_tx_output_get_amount(txout);
struct chain_coin_mvt *mvt;

if (!amount_asset_is_main(&asset))
continue;

script = cln_wally_tx_output_get_script(tmpctx,
&wtx->outputs[output]);
if (!script)
continue;

if (!wallet_can_spend(w, script, &index, &is_p2sh))
if (!wallet_can_spend(w, txout->script, &index))
continue;

utxo = tal(w, struct utxo);
utxo->keyindex = index;
utxo->is_p2sh = is_p2sh;
utxo->is_p2sh = is_p2sh(txout->script, txout->script_len, NULL);
utxo->amount = amount_asset_to_sat(&asset);
utxo->status = OUTPUT_STATE_AVAILABLE;
wally_txid(wtx, &utxo->outpoint.txid);
utxo->outpoint.n = output;
utxo->outpoint.n = i;
utxo->close_info = NULL;
utxo->is_in_coinbase = is_coinbase;

utxo->blockheight = blockheight ? blockheight : NULL;
utxo->spendheight = NULL;
utxo->scriptPubkey = tal_dup_talarr(utxo, u8, script);

utxo->scriptPubkey = tal_dup_arr(utxo, u8, txout->script, txout->script_len, 0);
log_debug(w->log, "Owning output %zu %s (%s) txid %s%s%s",
output,
i,
type_to_string(tmpctx, struct amount_sat,
&utxo->amount),
is_p2sh ? "P2SH" : "SEGWIT",
utxo->is_p2sh ? "P2SH" : "SEGWIT",
type_to_string(tmpctx, struct bitcoin_txid,
&utxo->outpoint.txid),
blockheight ? " CONFIRMED" : "",
Expand All @@ -2792,7 +2785,7 @@ int wallet_extract_owned_outputs(struct wallet *w, const struct wally_tx *wtx,
notify_chain_mvt(w->ld, mvt);
}

if (!wallet_add_utxo(w, utxo, is_p2sh ? p2sh_wpkh : our_change)) {
if (!wallet_add_utxo(w, utxo, utxo->is_p2sh ? p2sh_wpkh : our_change)) {
/* In case we already know the output, make
* sure we actually track its
* blockheight. This can happen when we grab
Expand All @@ -2806,14 +2799,14 @@ int wallet_extract_owned_outputs(struct wallet *w, const struct wally_tx *wtx,
}

/* This is an unconfirmed change output, we should track it */
if (!is_p2sh && !blockheight)
txfilter_add_scriptpubkey(w->ld->owned_txfilter, script);
if (!utxo->is_p2sh && !blockheight)
txfilter_add_scriptpubkey(w->ld->owned_txfilter, txout->script);

outpointfilter_add(w->owned_outpoints, &utxo->outpoint);

if (total && !amount_sat_add(total, *total, utxo->amount))
fatal("Cannot add utxo output %zu/%zu %s + %s",
output, wtx->num_outputs,
i, wtx->num_outputs,
type_to_string(tmpctx, struct amount_sat, total),
type_to_string(tmpctx, struct amount_sat,
&utxo->amount));
Expand Down
6 changes: 3 additions & 3 deletions wallet/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -567,10 +567,10 @@ struct utxo **wallet_utxo_boost(const tal_t *ctx,
* @w: (in) wallet holding the pubkeys to check against (privkeys are on HSM)
* @script: (in) the script to check
* @index: (out) the bip32 derivation index that matched the script
* @output_is_p2sh: (out) whether the script is a p2sh, or p2wpkh
*/
bool wallet_can_spend(struct wallet *w, const u8 *script,
u32 *index, bool *output_is_p2sh);
bool wallet_can_spend(struct wallet *w,
const u8 *script,
u32 *index);

/**
* wallet_get_newindex - get a new index from the wallet.
Expand Down
11 changes: 3 additions & 8 deletions wallet/walletrpc.c
Original file line number Diff line number Diff line change
Expand Up @@ -664,12 +664,8 @@ static void match_psbt_outputs_to_wallet(struct wally_psbt *psbt,
const u8 *script = psbt->outputs[outndx].script;
const size_t script_len = psbt->outputs[outndx].script_len;
u32 index;
bool is_p2sh;

if (!script_len)
continue;

if (!wallet_can_spend(w, script, &index, &is_p2sh))
if (!wallet_can_spend(w, script, &index))
continue;

if (bip32_key_from_parent(
Expand Down Expand Up @@ -872,7 +868,6 @@ static void maybe_notify_new_external_send(struct lightningd *ld,
struct bitcoin_outpoint outpoint;
struct amount_sat amount;
u32 index;
bool is_p2sh;
const u8 *script;

/* If it's not going to an external address, ignore */
Expand All @@ -881,8 +876,8 @@ static void maybe_notify_new_external_send(struct lightningd *ld,

/* If it's going to our wallet, ignore */
script = wally_psbt_output_get_script(tmpctx,
&psbt->outputs[outnum]);
if (wallet_can_spend(ld->wallet, script, &index, &is_p2sh))
&psbt->outputs[outnum]);
if (wallet_can_spend(ld->wallet, script, &index))
return;

outpoint.txid = *txid;
Expand Down

0 comments on commit f01e9fe

Please sign in to comment.