Skip to content

Commit

Permalink
hsmd: Add wallet index metadata to existing messages
Browse files Browse the repository at this point in the history
  • Loading branch information
ksedgwic authored and rustyrussell committed Mar 9, 2022
1 parent 3abe222 commit 8f56f96
Show file tree
Hide file tree
Showing 19 changed files with 206 additions and 8 deletions.
1 change: 1 addition & 0 deletions channeld/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ CHANNELD_COMMON_OBJS := \
common/per_peer_state.o \
common/permute_tx.o \
common/ping.o \
common/psbt_keypath.o \
common/psbt_open.o \
common/private_channel_announcement.o \
common/pseudorand.o \
Expand Down
24 changes: 23 additions & 1 deletion channeld/channeld.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include <gossipd/gossip_store_wiregen.h>
#include <gossipd/gossipd_peerd_wiregen.h>
#include <hsmd/hsmd_wiregen.h>
#include <wally_bip32.h>
#include <wire/peer_wire.h>
#include <wire/wire_sync.h>

Expand Down Expand Up @@ -122,6 +123,8 @@ struct peer {
u32 fee_per_satoshi;

/* The scriptpubkey to use for shutting down. */
u32 *final_index;
struct ext_key *final_ext_key;
u8 *final_scriptpubkey;

/* If master told us to shut down */
Expand Down Expand Up @@ -1722,6 +1725,7 @@ static u8 *got_revoke_msg(struct peer *peer, u64 revoke_num,
if (pbase) {
ptx = penalty_tx_create(
NULL, peer->channel, peer->feerate_penalty,
peer->final_index, peer->final_ext_key,
peer->final_scriptpubkey, per_commitment_secret,
&pbase->txid, pbase->outnum, pbase->amount,
HSM_FD);
Expand Down Expand Up @@ -3470,12 +3474,23 @@ static void handle_fail(struct peer *peer, const u8 *inmsg)

static void handle_shutdown_cmd(struct peer *peer, const u8 *inmsg)
{
u32 *final_index;
struct ext_key *final_ext_key;
u8 *local_shutdown_script;

if (!fromwire_channeld_send_shutdown(peer, inmsg, &local_shutdown_script,
if (!fromwire_channeld_send_shutdown(peer, inmsg,
&final_index,
&final_ext_key,
&local_shutdown_script,
&peer->shutdown_wrong_funding))
master_badmsg(WIRE_CHANNELD_SEND_SHUTDOWN, inmsg);

tal_free(peer->final_index);
peer->final_index = final_index;

tal_free(peer->final_ext_key);
peer->final_ext_key = final_ext_key;

tal_free(peer->final_scriptpubkey);
peer->final_scriptpubkey = local_shutdown_script;

Expand Down Expand Up @@ -3652,6 +3667,8 @@ static void init_channel(struct peer *peer)
enum side opener;
struct existing_htlc **htlcs;
bool reconnected;
u32 final_index;
struct ext_key final_ext_key;
u8 *fwd_msg;
const u8 *msg;
struct fee_states *fee_states;
Expand Down Expand Up @@ -3715,6 +3732,8 @@ static void init_channel(struct peer *peer)
&reconnected,
&peer->send_shutdown,
&peer->shutdown_sent[REMOTE],
&final_index,
&final_ext_key,
&peer->final_scriptpubkey,
&peer->channel_flags,
&fwd_msg,
Expand All @@ -3734,6 +3753,9 @@ static void init_channel(struct peer *peer)
master_badmsg(WIRE_CHANNELD_INIT, msg);
}

peer->final_index = tal_dup(peer, u32, &final_index);
peer->final_ext_key = tal_dup(peer, struct ext_key, &final_ext_key);

#if DEVELOPER
peer->dev_disable_commit = dev_disable_commit;
peer->dev_fast_gossip = dev_fast_gossip;
Expand Down
6 changes: 6 additions & 0 deletions channeld/channeld_wire.csv
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <bitcoin/psbt.h>
#include <bitcoin/tx.h>
#include <common/bip32.h>
#include <common/blockheight_states.h>
#include <common/cryptomsg.h>
#include <common/channel_config.h>
Expand Down Expand Up @@ -56,6 +57,8 @@ msgdata,channeld_init,funding_short_id,short_channel_id,
msgdata,channeld_init,reestablish,bool,
msgdata,channeld_init,send_shutdown,bool,
msgdata,channeld_init,remote_shutdown_received,bool,
msgdata,channeld_init,final_index,u32,
msgdata,channeld_init,final_ext_key,ext_key,
msgdata,channeld_init,final_scriptpubkey_len,u16,
msgdata,channeld_init,final_scriptpubkey,u8,final_scriptpubkey_len
msgdata,channeld_init,flags,u8,
Expand Down Expand Up @@ -173,8 +176,11 @@ msgdata,channeld_got_revoke,penalty_tx,?bitcoin_tx,
# (eg. if we sent another commitment_signed, that would implicitly ack).
msgtype,channeld_got_revoke_reply,1122

#include <wally_bip32.h>
# Tell peer to shut down channel.
msgtype,channeld_send_shutdown,1023
msgdata,channeld_send_shutdown,final_index,?u32,
msgdata,channeld_send_shutdown,final_ext_key,?ext_key,
msgdata,channeld_send_shutdown,shutdown_len,u16,
msgdata,channeld_send_shutdown,shutdown_scriptpubkey,u8,shutdown_len
msgdata,channeld_send_shutdown,wrong_funding,?bitcoin_outpoint,
Expand Down
6 changes: 6 additions & 0 deletions channeld/watchtower.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <common/features.h>
#include <common/htlc_tx.h>
#include <common/keyset.h>
#include <common/psbt_keypath.h>
#include <common/status.h>
#include <common/type_to_string.h>
#include <hsmd/hsmd_wiregen.h>
Expand All @@ -17,6 +18,8 @@ const struct bitcoin_tx *
penalty_tx_create(const tal_t *ctx,
const struct channel *channel,
u32 penalty_feerate,
u32 *final_index,
struct ext_key *final_ext_key,
u8 *final_scriptpubkey,
const struct secret *revocation_preimage,
const struct bitcoin_txid *commitment_txid,
Expand Down Expand Up @@ -76,6 +79,9 @@ penalty_tx_create(const tal_t *ctx,
NULL, to_them_sats, NULL, wscript);

bitcoin_tx_add_output(tx, final_scriptpubkey, NULL, to_them_sats);
assert((final_index == NULL) == (final_ext_key == NULL));
if (final_index)
psbt_add_keypath_to_last_output(tx, *final_index, final_ext_key);

/* Worst-case sig is 73 bytes */
weight = bitcoin_tx_weight(tx) + 1 + 3 + 73 + 0 + tal_count(wscript);
Expand Down
4 changes: 4 additions & 0 deletions channeld/watchtower.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
#include "config.h"
#include <common/initial_channel.h>

struct ext_key;

const struct bitcoin_tx *
penalty_tx_create(const tal_t *ctx,
const struct channel *channel,
u32 penalty_feerate,
u32 *final_index,
struct ext_key *final_ext_key,
u8 *final_scriptpubkey,
const struct secret *revocation_preimage,
const struct bitcoin_txid *commitment_txid,
Expand Down
1 change: 1 addition & 0 deletions closingd/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ CLOSINGD_COMMON_OBJS := \
common/per_peer_state.o \
common/permute_tx.o \
common/ping.o \
common/psbt_keypath.o \
common/psbt_open.o \
common/pseudorand.o \
common/status_wiregen.o \
Expand Down
42 changes: 40 additions & 2 deletions closingd/closingd.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <inttypes.h>
#include <stdio.h>
#include <unistd.h>
#include <wally_bip32.h>
#include <wire/peer_wire.h>
#include <wire/wire_sync.h>

Expand All @@ -51,6 +52,8 @@ static struct bitcoin_tx *close_tx(const tal_t *ctx,
const struct chainparams *chainparams,
struct per_peer_state *pps,
const struct channel_id *channel_id,
u32 *local_wallet_index,
const struct ext_key *local_wallet_ext_key,
u8 *scriptpubkey[NUM_SIDES],
const struct bitcoin_outpoint *funding,
struct amount_sat funding_sats,
Expand Down Expand Up @@ -83,6 +86,7 @@ static struct bitcoin_tx *close_tx(const tal_t *ctx,
/* FIXME: We need to allow this! */
tx = create_close_tx(ctx,
chainparams,
local_wallet_index, local_wallet_ext_key,
scriptpubkey[LOCAL], scriptpubkey[REMOTE],
funding_wscript,
funding,
Expand Down Expand Up @@ -130,6 +134,8 @@ static void send_offer(struct per_peer_state *pps,
const struct channel_id *channel_id,
const struct pubkey funding_pubkey[NUM_SIDES],
const u8 *funding_wscript,
u32 *local_wallet_index,
const struct ext_key *local_wallet_ext_key,
u8 *scriptpubkey[NUM_SIDES],
const struct bitcoin_outpoint *funding,
struct amount_sat funding_sats,
Expand All @@ -152,6 +158,8 @@ static void send_offer(struct per_peer_state *pps,
* #3](03-transactions.md#closing-transaction).
*/
tx = close_tx(tmpctx, chainparams, pps, channel_id,
local_wallet_index,
local_wallet_ext_key,
scriptpubkey,
funding,
funding_sats,
Expand Down Expand Up @@ -225,6 +233,8 @@ receive_offer(struct per_peer_state *pps,
const struct channel_id *channel_id,
const struct pubkey funding_pubkey[NUM_SIDES],
const u8 *funding_wscript,
u32 *local_wallet_index,
const struct ext_key *local_wallet_ext_key,
u8 *scriptpubkey[NUM_SIDES],
const struct bitcoin_outpoint *funding,
struct amount_sat funding_sats,
Expand Down Expand Up @@ -285,6 +295,8 @@ receive_offer(struct per_peer_state *pps,
* - MUST fail the connection.
*/
tx = close_tx(tmpctx, chainparams, pps, channel_id,
local_wallet_index,
local_wallet_ext_key,
scriptpubkey,
funding,
funding_sats,
Expand Down Expand Up @@ -315,6 +327,8 @@ receive_offer(struct per_peer_state *pps,
* - MAY eliminate its own output.
*/
trimmed = close_tx(tmpctx, chainparams, pps, channel_id,
local_wallet_index,
local_wallet_ext_key,
scriptpubkey,
funding,
funding_sats,
Expand Down Expand Up @@ -559,14 +573,17 @@ static size_t closing_tx_weight_estimate(u8 *scriptpubkey[NUM_SIDES],
const u8 *funding_wscript,
const struct amount_sat *out,
struct amount_sat funding_sats,
struct amount_sat dust_limit)
struct amount_sat dust_limit,
u32 *local_wallet_index,
const struct ext_key *local_wallet_ext_key)
{
/* We create a dummy close */
struct bitcoin_tx *tx;
struct bitcoin_outpoint dummy_funding;

memset(&dummy_funding, 0, sizeof(dummy_funding));
tx = create_close_tx(tmpctx, chainparams,
local_wallet_index, local_wallet_ext_key,
scriptpubkey[LOCAL], scriptpubkey[REMOTE],
funding_wscript,
&dummy_funding,
Expand Down Expand Up @@ -671,6 +688,8 @@ static void do_quickclose(struct amount_sat offer[NUM_SIDES],
const struct channel_id *channel_id,
const struct pubkey funding_pubkey[NUM_SIDES],
const u8 *funding_wscript,
u32 *local_wallet_index,
const struct ext_key *local_wallet_ext_key,
u8 *scriptpubkey[NUM_SIDES],
const struct bitcoin_outpoint *funding,
struct amount_sat funding_sats,
Expand Down Expand Up @@ -750,6 +769,7 @@ static void do_quickclose(struct amount_sat offer[NUM_SIDES],
offer[LOCAL] = offer[REMOTE];
send_offer(pps, chainparams,
channel_id, funding_pubkey, funding_wscript,
local_wallet_index, local_wallet_ext_key,
scriptpubkey, funding,
funding_sats, out, opener,
our_dust_limit,
Expand Down Expand Up @@ -789,6 +809,7 @@ static void do_quickclose(struct amount_sat offer[NUM_SIDES],
}
send_offer(pps, chainparams,
channel_id, funding_pubkey, funding_wscript,
local_wallet_index, local_wallet_ext_key,
scriptpubkey, funding,
funding_sats, out, opener,
our_dust_limit,
Expand All @@ -802,6 +823,7 @@ static void do_quickclose(struct amount_sat offer[NUM_SIDES],
= receive_offer(pps, chainparams,
channel_id, funding_pubkey,
funding_wscript,
local_wallet_index, local_wallet_ext_key,
scriptpubkey, funding,
funding_sats,
out, opener,
Expand Down Expand Up @@ -851,6 +873,8 @@ int main(int argc, char *argv[])
u32 min_feerate, initial_feerate, *max_feerate;
struct feerange feerange;
enum side opener;
u32 *local_wallet_index;
struct ext_key *local_wallet_ext_key;
u8 *scriptpubkey[NUM_SIDES], *funding_wscript;
u64 fee_negotiation_step;
u8 fee_negotiation_step_unit;
Expand Down Expand Up @@ -879,6 +903,8 @@ int main(int argc, char *argv[])
&our_dust_limit,
&min_feerate, &initial_feerate, &max_feerate,
&commitment_fee,
&local_wallet_index,
&local_wallet_ext_key,
&scriptpubkey[LOCAL],
&scriptpubkey[REMOTE],
&fee_negotiation_step,
Expand All @@ -899,7 +925,9 @@ int main(int argc, char *argv[])
calc_fee_bounds(closing_tx_weight_estimate(scriptpubkey,
funding_wscript,
out, funding_sats,
our_dust_limit),
our_dust_limit,
local_wallet_index,
local_wallet_ext_key),
min_feerate, initial_feerate, max_feerate,
commitment_fee, funding_sats, opener,
&min_fee_to_accept, &offer[LOCAL], &max_fee_to_accept);
Expand Down Expand Up @@ -957,6 +985,7 @@ int main(int argc, char *argv[])
if (whose_turn == LOCAL) {
send_offer(pps, chainparams,
&channel_id, funding_pubkey, funding_wscript,
local_wallet_index, local_wallet_ext_key,
scriptpubkey, &funding,
funding_sats, out, opener,
our_dust_limit,
Expand All @@ -978,6 +1007,8 @@ int main(int argc, char *argv[])
= receive_offer(pps, chainparams,
&channel_id, funding_pubkey,
funding_wscript,
local_wallet_index,
local_wallet_ext_key,
scriptpubkey, &funding,
funding_sats,
out, opener,
Expand All @@ -991,6 +1022,7 @@ int main(int argc, char *argv[])
do_quickclose(offer,
pps, &channel_id, funding_pubkey,
funding_wscript,
local_wallet_index, local_wallet_ext_key,
scriptpubkey,
&funding,
funding_sats, out, opener,
Expand Down Expand Up @@ -1024,6 +1056,8 @@ int main(int argc, char *argv[])
fee_negotiation_step_unit);
send_offer(pps, chainparams, &channel_id,
funding_pubkey, funding_wscript,
local_wallet_index,
local_wallet_ext_key,
scriptpubkey, &funding,
funding_sats, out, opener,
our_dust_limit,
Expand All @@ -1040,6 +1074,8 @@ int main(int argc, char *argv[])
= receive_offer(pps, chainparams, &channel_id,
funding_pubkey,
funding_wscript,
local_wallet_index,
local_wallet_ext_key,
scriptpubkey, &funding,
funding_sats,
out, opener,
Expand All @@ -1064,6 +1100,8 @@ int main(int argc, char *argv[])
tal_free(our_feerange);
tal_free(their_feerange);
tal_free(max_feerate);
tal_free(local_wallet_index);
tal_free(local_wallet_ext_key);
closing_dev_memleak(ctx, scriptpubkey, funding_wscript);
#endif

Expand Down
4 changes: 4 additions & 0 deletions closingd/closingd_wire.csv
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#include <bitcoin/tx.h>
#include <common/bip32.h>
#include <common/channel_id.h>
#include <common/cryptomsg.h>
#include <common/htlc_wire.h>
#include <common/status_wire.h>
#include <wally_bip32.h>
# Begin! (passes peer fd, gossipd-client fd)
msgtype,closingd_init,2001
msgdata,closingd_init,chainparams,chainparams,
Expand All @@ -19,6 +21,8 @@ msgdata,closingd_init,min_feerate_perksipa,u32,
msgdata,closingd_init,preferred_feerate_perksipa,u32,
msgdata,closingd_init,max_feerate_perksipa,?u32,
msgdata,closingd_init,fee_limit_satoshi,amount_sat,
msgdata,closingd_init,local_wallet_index,?u32,
msgdata,closingd_init,local_wallet_ext_key,?ext_key,
msgdata,closingd_init,local_scriptpubkey_len,u16,
msgdata,closingd_init,local_scriptpubkey,u8,local_scriptpubkey_len
msgdata,closingd_init,remote_scriptpubkey_len,u16,
Expand Down
Loading

0 comments on commit 8f56f96

Please sign in to comment.