Skip to content

Commit f3fc949

Browse files
committed
Added destination wallet keypath to justice sweeps.
1 parent e71e9ee commit f3fc949

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
@@ -42,6 +42,7 @@
4242
#include <gossipd/gossip_store_wiregen.h>
4343
#include <gossipd/gossipd_peerd_wiregen.h>
4444
#include <hsmd/hsmd_wiregen.h>
45+
#include <wally_bip32.h>
4546
#include <wire/common_wiregen.h>
4647
#include <wire/peer_wire.h>
4748
#include <wire/wire_sync.h>
@@ -138,6 +139,8 @@ struct peer {
138139
u32 fee_per_satoshi;
139140

140141
/* The scriptpubkey to use for shutting down. */
142+
u32 final_index;
143+
struct ext_key final_ext_key;
141144
u8 *final_scriptpubkey;
142145

143146
/* If master told us to shut down */
@@ -1845,6 +1848,7 @@ static u8 *got_revoke_msg(struct peer *peer, u64 revoke_num,
18451848
if (pbase) {
18461849
ptx = penalty_tx_create(
18471850
NULL, peer->channel, peer->feerate_penalty,
1851+
peer->final_index, &peer->final_ext_key,
18481852
peer->final_scriptpubkey, per_commitment_secret,
18491853
&pbase->txid, pbase->outnum, pbase->amount,
18501854
HSM_FD);
@@ -3653,7 +3657,10 @@ static void handle_shutdown_cmd(struct peer *peer, const u8 *inmsg)
36533657
{
36543658
u8 *local_shutdown_script;
36553659

3656-
if (!fromwire_channeld_send_shutdown(peer, inmsg, &local_shutdown_script,
3660+
if (!fromwire_channeld_send_shutdown(peer, inmsg,
3661+
&peer->final_index,
3662+
&peer->final_ext_key,
3663+
&local_shutdown_script,
36573664
&peer->shutdown_wrong_funding))
36583665
master_badmsg(WIRE_CHANNELD_SEND_SHUTDOWN, inmsg);
36593666

@@ -3957,6 +3964,8 @@ static void init_channel(struct peer *peer)
39573964
&reconnected,
39583965
&peer->send_shutdown,
39593966
&peer->shutdown_sent[REMOTE],
3967+
&peer->final_index,
3968+
&peer->final_ext_key,
39603969
&peer->final_scriptpubkey,
39613970
&peer->channel_flags,
39623971
&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
@@ -21,6 +21,7 @@
2121
#include <lightningd/notification.h>
2222
#include <lightningd/peer_control.h>
2323
#include <lightningd/ping.h>
24+
#include <wally_bip32.h>
2425
#include <wire/common_wiregen.h>
2526

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

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

lightningd/closing_control.c

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

lightningd/peer_control.c

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

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

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

0 commit comments

Comments
 (0)