Skip to content

Commit 883e45b

Browse files
committed
Added destination wallet keypath to justice sweeps.
1 parent 61cb2e6 commit 883e45b

File tree

7 files changed

+80
-8
lines changed

7 files changed

+80
-8
lines changed

channeld/channeld.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include <gossipd/gossip_store_wiregen.h>
4242
#include <gossipd/gossipd_peerd_wiregen.h>
4343
#include <hsmd/hsmd_wiregen.h>
44+
#include <wally_bip32.h>
4445
#include <wire/common_wiregen.h>
4546
#include <wire/peer_wire.h>
4647
#include <wire/wire_sync.h>
@@ -137,6 +138,8 @@ struct peer {
137138
u32 fee_per_satoshi;
138139

139140
/* The scriptpubkey to use for shutting down. */
141+
u32 final_index;
142+
struct ext_key final_ext_key;
140143
u8 *final_scriptpubkey;
141144

142145
/* If master told us to shut down */
@@ -1844,6 +1847,7 @@ static u8 *got_revoke_msg(struct peer *peer, u64 revoke_num,
18441847
if (pbase) {
18451848
ptx = penalty_tx_create(
18461849
NULL, peer->channel, peer->feerate_penalty,
1850+
peer->final_index, &peer->final_ext_key,
18471851
peer->final_scriptpubkey, per_commitment_secret,
18481852
&pbase->txid, pbase->outnum, pbase->amount,
18491853
HSM_FD);
@@ -3647,7 +3651,10 @@ static void handle_shutdown_cmd(struct peer *peer, const u8 *inmsg)
36473651
{
36483652
u8 *local_shutdown_script;
36493653

3650-
if (!fromwire_channeld_send_shutdown(peer, inmsg, &local_shutdown_script,
3654+
if (!fromwire_channeld_send_shutdown(peer, inmsg,
3655+
&peer->final_index,
3656+
&peer->final_ext_key,
3657+
&local_shutdown_script,
36513658
&peer->shutdown_wrong_funding))
36523659
master_badmsg(WIRE_CHANNELD_SEND_SHUTDOWN, inmsg);
36533660

@@ -3951,6 +3958,8 @@ static void init_channel(struct peer *peer)
39513958
&reconnected,
39523959
&peer->send_shutdown,
39533960
&peer->shutdown_sent[REMOTE],
3961+
&peer->final_index,
3962+
&peer->final_ext_key,
39543963
&peer->final_scriptpubkey,
39553964
&peer->channel_flags,
39563965
&fwd_msg,

channeld/channeld_wire.csv

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <bitcoin/psbt.h>
22
#include <bitcoin/tx.h>
3+
#include <common/bip32.h>
34
#include <common/blockheight_states.h>
45
#include <common/cryptomsg.h>
56
#include <common/channel_config.h>
@@ -58,6 +59,8 @@ msgdata,channeld_init,funding_short_id,short_channel_id,
5859
msgdata,channeld_init,reestablish,bool,
5960
msgdata,channeld_init,send_shutdown,bool,
6061
msgdata,channeld_init,remote_shutdown_received,bool,
62+
msgdata,channeld_init,final_index,u32,
63+
msgdata,channeld_init,final_ext_key,ext_key,
6164
msgdata,channeld_init,final_scriptpubkey_len,u16,
6265
msgdata,channeld_init,final_scriptpubkey,u8,final_scriptpubkey_len
6366
msgdata,channeld_init,flags,u8,
@@ -174,6 +177,8 @@ msgtype,channeld_got_revoke_reply,1122
174177

175178
# Tell peer to shut down channel.
176179
msgtype,channeld_send_shutdown,1023
180+
msgdata,channeld_send_shutdown,final_index,u32,
181+
msgdata,channeld_send_shutdown,final_ext_key,ext_key,
177182
msgdata,channeld_send_shutdown,shutdown_len,u16,
178183
msgdata,channeld_send_shutdown,shutdown_scriptpubkey,u8,shutdown_len
179184
msgdata,channeld_send_shutdown,wrong_funding,?bitcoin_outpoint,

channeld/watchtower.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,41 @@
1212

1313
static const u8 ONE = 0x1;
1414

15+
static void add_keypath_item_to_last_output(struct bitcoin_tx *tx,
16+
u32 index,
17+
const struct ext_key *ext) {
18+
// Skip if there is no wallet keypath for this output.
19+
if (index == UINT32_MAX)
20+
return;
21+
22+
size_t outndx = tx->psbt->num_outputs - 1;
23+
struct wally_map *map_in = &tx->psbt->outputs[outndx].keypaths;
24+
25+
u8 fingerprint[BIP32_KEY_FINGERPRINT_LEN];
26+
if (bip32_key_get_fingerprint(
27+
(struct ext_key *) ext, fingerprint, sizeof(fingerprint)) != WALLY_OK) {
28+
abort();
29+
}
30+
31+
u32 path[1];
32+
path[0] = index;
33+
34+
tal_wally_start();
35+
if (wally_map_add_keypath_item(map_in,
36+
ext->pub_key, sizeof(ext->pub_key),
37+
fingerprint, sizeof(fingerprint),
38+
path, 1) != WALLY_OK) {
39+
abort();
40+
}
41+
tal_wally_end(tx->psbt);
42+
}
43+
1544
const struct bitcoin_tx *
1645
penalty_tx_create(const tal_t *ctx,
1746
const struct channel *channel,
1847
u32 penalty_feerate,
48+
u32 final_index,
49+
struct ext_key *final_ext_key,
1950
u8 *final_scriptpubkey,
2051
const struct secret *revocation_preimage,
2152
const struct bitcoin_txid *commitment_txid,
@@ -75,6 +106,7 @@ penalty_tx_create(const tal_t *ctx,
75106
NULL, to_them_sats, NULL, wscript);
76107

77108
bitcoin_tx_add_output(tx, final_scriptpubkey, NULL, to_them_sats);
109+
add_keypath_item_to_last_output(tx, final_index, final_ext_key);
78110

79111
/* Worst-case sig is 73 bytes */
80112
weight = bitcoin_tx_weight(tx) + 1 + 3 + 73 + 0 + tal_count(wscript);

channeld/watchtower.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@
33
#include "config.h"
44
#include <common/initial_channel.h>
55

6+
struct ext_key;
7+
68
const struct bitcoin_tx *
79
penalty_tx_create(const tal_t *ctx,
810
const struct channel *channel,
911
u32 penalty_feerate,
12+
u32 final_index,
13+
struct ext_key *final_ext_key,
1014
u8 *final_scriptpubkey,
1115
const struct secret *revocation_preimage,
1216
const struct bitcoin_txid *commitment_txid,

lightningd/channel_control.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <lightningd/notification.h>
2121
#include <lightningd/peer_control.h>
2222
#include <lightningd/ping.h>
23+
#include <wally_bip32.h>
2324
#include <wire/common_wiregen.h>
2425

2526
static void update_feerates(struct lightningd *ld, struct channel *channel)
@@ -644,6 +645,18 @@ void peer_start_channeld(struct channel *channel,
644645
pbases = wallet_penalty_base_load_for_channel(
645646
tmpctx, channel->peer->ld->wallet, channel->dbid);
646647

648+
struct ext_key final_ext_key;
649+
if (bip32_key_from_parent(
650+
ld->wallet->bip32_base,
651+
channel->final_key_idx,
652+
BIP32_FLAG_KEY_PUBLIC,
653+
&final_ext_key) != WALLY_OK) {
654+
channel_internal_error(channel,
655+
"Could not derive onchain ext key %"PRIu64,
656+
channel->final_key_idx);
657+
return;
658+
}
659+
647660
initmsg = towire_channeld_init(tmpctx,
648661
chainparams,
649662
ld->our_features,
@@ -693,6 +706,8 @@ void peer_start_channeld(struct channel *channel,
693706
|| channel->state == CLOSINGD_SIGEXCHANGE
694707
|| channel_closed(channel),
695708
channel->shutdown_scriptpubkey[REMOTE] != NULL,
709+
channel->final_key_idx,
710+
&final_ext_key,
696711
channel->shutdown_scriptpubkey[LOCAL],
697712
channel->channel_flags,
698713
fwd_msg,

lightningd/closing_control.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -775,11 +775,24 @@ static struct command_result *json_close(struct command *cmd,
775775
msg = towire_dualopend_send_shutdown(
776776
NULL,
777777
channel->shutdown_scriptpubkey[LOCAL]);
778-
} else
778+
} else {
779+
struct ext_key final_ext_key;
780+
if (bip32_key_from_parent(
781+
channel->peer->ld->wallet->bip32_base,
782+
channel->final_key_idx,
783+
BIP32_FLAG_KEY_PUBLIC,
784+
&final_ext_key) != WALLY_OK) {
785+
return command_fail(
786+
cmd, LIGHTNINGD,
787+
"Could not derive onchain ext key");
788+
}
779789
msg = towire_channeld_send_shutdown(
780790
NULL,
791+
channel->final_key_idx,
792+
&final_ext_key,
781793
channel->shutdown_scriptpubkey[LOCAL],
782794
channel->shutdown_wrong_funding);
795+
}
783796
subd_send_msg(channel->owner, take(msg));
784797
}
785798

lightningd/peer_control.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -177,15 +177,9 @@ u8 *p2wpkh_for_keyidx(const tal_t *ctx, struct lightningd *ld, u64 keyidx)
177177

178178
static struct simple_htlc **collect_htlcs(struct channel *channel, u32 local_feerate) {
179179
// Collect the htlcs for call to hsmd.
180-
//
181-
// We use the existing_htlc to_wire routines, it's unfortunate that
182-
// we have to send a dummy onion_routing_packet ...
183-
//
184180
struct htlc_in_map *htlcs_in = &channel->peer->ld->htlcs_in;
185181
struct htlc_out_map *htlcs_out = &channel->peer->ld->htlcs_out;
186182
struct simple_htlc **htlcs = tal_arr(tmpctx, struct simple_htlc *, 0);
187-
u8 dummy_onion_routing_packet[TOTAL_PACKET_SIZE(ROUTING_INFO_SIZE)];
188-
memset(dummy_onion_routing_packet, 0, sizeof(dummy_onion_routing_packet));
189183

190184
const struct htlc_in *hin;
191185
struct htlc_in_map_iter ini;

0 commit comments

Comments
 (0)