hsmd: Add wallet index metadata to existing messages

This commit is contained in:
Ken Sedgwick
2021-12-23 12:16:35 -08:00
committed by Rusty Russell
parent 3abe22213c
commit 8f56f96801
19 changed files with 206 additions and 8 deletions

View File

@@ -38,6 +38,7 @@
#include <lightningd/peer_fd.h>
#include <lightningd/subd.h>
#include <openingd/dualopend_wiregen.h>
#include <wally_bip32.h>
struct close_command {
/* Inside struct lightningd close_commands. */
@@ -443,6 +444,29 @@ void peer_start_closingd(struct channel *channel, struct peer_fd *peer_fd)
return;
}
// Determine the wallet index for our output or NULL if not found.
u32 *local_wallet_index = NULL;
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)) {
if (bip32_key_from_parent(
ld->wallet->bip32_base,
index_val,
BIP32_FLAG_KEY_PUBLIC,
&ext_key_val) != WALLY_OK) {
channel_internal_error(channel, "Could not derive ext public key");
return;
}
local_wallet_index = &index_val;
local_wallet_ext_key = &ext_key_val;
}
initmsg = towire_closingd_init(tmpctx,
chainparams,
&channel->cid,
@@ -456,6 +480,8 @@ void peer_start_closingd(struct channel *channel, struct peer_fd *peer_fd)
channel->our_config.dust_limit,
min_feerate, feerate, max_feerate,
feelimit,
local_wallet_index,
local_wallet_ext_key,
channel->shutdown_scriptpubkey[LOCAL],
channel->shutdown_scriptpubkey[REMOTE],
channel->closing_fee_negotiation_step,
@@ -527,6 +553,8 @@ static struct command_result *json_close(struct command *cmd,
struct channel *channel;
unsigned int *timeout;
const u8 *close_to_script = NULL;
u32 *final_index;
u32 index_val;
bool close_script_set, wrong_funding_changed, *force_lease_close;
const char *fee_negotiation_step_str;
struct bitcoin_outpoint *wrong_funding;
@@ -584,6 +612,14 @@ static struct command_result *json_close(struct command *cmd,
channel->lease_expiry,
get_block_height(cmd->ld->topology));
/* Set the wallet index to the default value; it is updated
* below if the close_to_script is found to be in the
* wallet. If the close_to_script is not in the wallet
* final_index will be set to NULL instead.*/
assert(channel->final_key_idx <= UINT32_MAX);
index_val = (u32) channel->final_key_idx;
final_index = &index_val;
/* If we've set a local shutdown script for this peer, and it's not the
* default upfront script, try to close to a different channel.
* Error is an operator error */
@@ -613,6 +649,17 @@ static struct command_result *json_close(struct command *cmd,
channel->shutdown_scriptpubkey[LOCAL]
= tal_steal(channel, cast_const(u8 *, close_to_script));
close_script_set = true;
/* Is the close script in our wallet? */
bool is_p2sh;
if (wallet_can_spend(
cmd->ld->wallet,
channel->shutdown_scriptpubkey[LOCAL],
&index_val,
&is_p2sh)) {
/* index_val has been set to the discovered wallet index */
} else {
final_index = NULL;
}
} else if (!channel->shutdown_scriptpubkey[LOCAL]) {
channel->shutdown_scriptpubkey[LOCAL]
= p2wpkh_for_keyidx(channel, cmd->ld, channel->final_key_idx);
@@ -735,11 +782,28 @@ static struct command_result *json_close(struct command *cmd,
msg = towire_dualopend_send_shutdown(
NULL,
channel->shutdown_scriptpubkey[LOCAL]);
} else
} else {
struct ext_key ext_key_val;
struct ext_key *final_ext_key = NULL;
if (final_index) {
if (bip32_key_from_parent(
channel->peer->ld->wallet->bip32_base,
*final_index,
BIP32_FLAG_KEY_PUBLIC,
&ext_key_val) != WALLY_OK) {
return command_fail(
cmd, LIGHTNINGD,
"Could not derive final_ext_key");
}
final_ext_key = &ext_key_val;
}
msg = towire_channeld_send_shutdown(
NULL,
final_index,
final_ext_key,
channel->shutdown_scriptpubkey[LOCAL],
channel->shutdown_wrong_funding);
}
subd_send_msg(channel->owner, take(msg));
}