Skip to content

Commit 8287a9f

Browse files
committed
Added wallet keypath to PSBT for local output of closing transaction.
1 parent dcc32ab commit 8287a9f

File tree

8 files changed

+116
-6
lines changed

8 files changed

+116
-6
lines changed

closingd/closingd.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ static struct bitcoin_tx *close_tx(const tal_t *ctx,
3636
const struct chainparams *chainparams,
3737
struct per_peer_state *pps,
3838
const struct channel_id *channel_id,
39+
u32 local_wallet_index,
40+
const struct ext_key *local_wallet_ext_key,
3941
u8 *scriptpubkey[NUM_SIDES],
4042
const struct bitcoin_txid *funding_txid,
4143
unsigned int funding_txout,
@@ -69,6 +71,7 @@ static struct bitcoin_tx *close_tx(const tal_t *ctx,
6971
/* FIXME: We need to allow this! */
7072
tx = create_close_tx(ctx,
7173
chainparams,
74+
local_wallet_index, local_wallet_ext_key,
7275
scriptpubkey[LOCAL], scriptpubkey[REMOTE],
7376
funding_wscript,
7477
funding_txid,
@@ -135,6 +138,8 @@ static void send_offer(struct per_peer_state *pps,
135138
const struct channel_id *channel_id,
136139
const struct pubkey funding_pubkey[NUM_SIDES],
137140
const u8 *funding_wscript,
141+
u32 local_wallet_index,
142+
const struct ext_key *local_wallet_ext_key,
138143
u8 *scriptpubkey[NUM_SIDES],
139144
const struct bitcoin_txid *funding_txid,
140145
unsigned int funding_txout,
@@ -156,6 +161,8 @@ static void send_offer(struct per_peer_state *pps,
156161
* #3](03-transactions.md#closing-transaction).
157162
*/
158163
tx = close_tx(tmpctx, chainparams, pps, channel_id,
164+
local_wallet_index,
165+
local_wallet_ext_key,
159166
scriptpubkey,
160167
funding_txid,
161168
funding_txout,
@@ -215,6 +222,8 @@ receive_offer(struct per_peer_state *pps,
215222
const struct channel_id *channel_id,
216223
const struct pubkey funding_pubkey[NUM_SIDES],
217224
const u8 *funding_wscript,
225+
u32 local_wallet_index,
226+
const struct ext_key *local_wallet_ext_key,
218227
u8 *scriptpubkey[NUM_SIDES],
219228
const struct bitcoin_txid *funding_txid,
220229
unsigned int funding_txout,
@@ -269,6 +278,8 @@ receive_offer(struct per_peer_state *pps,
269278
* - MUST fail the connection.
270279
*/
271280
tx = close_tx(tmpctx, chainparams, pps, channel_id,
281+
local_wallet_index,
282+
local_wallet_ext_key,
272283
scriptpubkey,
273284
funding_txid,
274285
funding_txout,
@@ -300,6 +311,8 @@ receive_offer(struct per_peer_state *pps,
300311
* - MAY eliminate its own output.
301312
*/
302313
trimmed = close_tx(tmpctx, chainparams, pps, channel_id,
314+
local_wallet_index,
315+
local_wallet_ext_key,
303316
scriptpubkey,
304317
funding_txid,
305318
funding_txout,
@@ -506,6 +519,8 @@ int main(int argc, char *argv[])
506519
struct amount_sat min_fee_to_accept, commitment_fee, offer[NUM_SIDES];
507520
struct feerange feerange;
508521
enum side opener;
522+
u32 local_wallet_index;
523+
struct ext_key local_wallet_ext_key;
509524
u8 *scriptpubkey[NUM_SIDES], *funding_wscript;
510525
u64 fee_negotiation_step;
511526
u8 fee_negotiation_step_unit;
@@ -533,6 +548,8 @@ int main(int argc, char *argv[])
533548
&our_dust_limit,
534549
&min_fee_to_accept, &commitment_fee,
535550
&offer[LOCAL],
551+
&local_wallet_index,
552+
&local_wallet_ext_key,
536553
&scriptpubkey[LOCAL],
537554
&scriptpubkey[REMOTE],
538555
&fee_negotiation_step,
@@ -590,6 +607,8 @@ int main(int argc, char *argv[])
590607
if (whose_turn == LOCAL) {
591608
send_offer(pps, chainparams,
592609
&channel_id, funding_pubkey, funding_wscript,
610+
local_wallet_index,
611+
&local_wallet_ext_key,
593612
scriptpubkey, &funding_txid, funding_txout,
594613
funding, out, opener,
595614
our_dust_limit,
@@ -610,6 +629,8 @@ int main(int argc, char *argv[])
610629
= receive_offer(pps, chainparams,
611630
&channel_id, funding_pubkey,
612631
funding_wscript,
632+
local_wallet_index,
633+
&local_wallet_ext_key,
613634
scriptpubkey, &funding_txid,
614635
funding_txout, funding,
615636
out, opener,
@@ -641,6 +662,8 @@ int main(int argc, char *argv[])
641662
fee_negotiation_step_unit);
642663
send_offer(pps, chainparams, &channel_id,
643664
funding_pubkey, funding_wscript,
665+
local_wallet_index,
666+
&local_wallet_ext_key,
644667
scriptpubkey, &funding_txid, funding_txout,
645668
funding, out, opener,
646669
our_dust_limit,
@@ -656,6 +679,8 @@ int main(int argc, char *argv[])
656679
= receive_offer(pps, chainparams, &channel_id,
657680
funding_pubkey,
658681
funding_wscript,
682+
local_wallet_index,
683+
&local_wallet_ext_key,
659684
scriptpubkey, &funding_txid,
660685
funding_txout, funding,
661686
out, opener,

closingd/closingd_wire.csv

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <bitcoin/tx.h>
2+
#include <common/bip32.h>
23
#include <common/channel_id.h>
34
#include <common/cryptomsg.h>
45
#include <common/htlc_wire.h>
@@ -20,6 +21,8 @@ msgdata,closingd_init,our_dust_limit,amount_sat,
2021
msgdata,closingd_init,min_fee_satoshi,amount_sat,
2122
msgdata,closingd_init,fee_limit_satoshi,amount_sat,
2223
msgdata,closingd_init,initial_fee_satoshi,amount_sat,
24+
msgdata,closingd_init,local_wallet_index,u32,
25+
msgdata,closingd_init,local_wallet_ext_key,ext_key,
2326
msgdata,closingd_init,local_scriptpubkey_len,u16,
2427
msgdata,closingd_init,local_scriptpubkey,u8,local_scriptpubkey_len
2528
msgdata,closingd_init,remote_scriptpubkey_len,u16,

closingd/closingd_wiregen.c

Lines changed: 7 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

closingd/closingd_wiregen.h

Lines changed: 4 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

common/close_tx.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,39 @@
55
#include <assert.h>
66
#include <common/utils.h>
77

8+
static void add_keypath_item_to_last_output(struct bitcoin_tx *tx,
9+
u32 index,
10+
const struct ext_key *ext) {
11+
// Skip if there is no wallet keypath for this output.
12+
if (index == UINT32_MAX)
13+
return;
14+
15+
size_t outndx = tx->psbt->num_outputs - 1;
16+
struct wally_map *map_in = &tx->psbt->outputs[outndx].keypaths;
17+
18+
u8 fingerprint[BIP32_KEY_FINGERPRINT_LEN];
19+
if (bip32_key_get_fingerprint(
20+
(struct ext_key *) ext, fingerprint, sizeof(fingerprint)) != WALLY_OK) {
21+
abort();
22+
}
23+
24+
u32 path[1];
25+
path[0] = index;
26+
27+
tal_wally_start();
28+
if (wally_map_add_keypath_item(map_in,
29+
ext->pub_key, sizeof(ext->pub_key),
30+
fingerprint, sizeof(fingerprint),
31+
path, 1) != WALLY_OK) {
32+
abort();
33+
}
34+
tal_wally_end(tx->psbt);
35+
}
36+
837
struct bitcoin_tx *create_close_tx(const tal_t *ctx,
938
const struct chainparams *chainparams,
39+
u32 local_wallet_index,
40+
const struct ext_key *local_wallet_ext_key,
1041
const u8 *our_script,
1142
const u8 *their_script,
1243
const u8 *funding_wscript,
@@ -47,6 +78,7 @@ struct bitcoin_tx *create_close_tx(const tal_t *ctx,
4778
script = tal_dup_talarr(tx, u8, our_script);
4879
/* One output is to us. */
4980
bitcoin_tx_add_output(tx, script, NULL, to_us);
81+
add_keypath_item_to_last_output(tx, local_wallet_index, local_wallet_ext_key);
5082
num_outputs++;
5183
}
5284

common/close_tx.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ struct pubkey;
1313
* input scriptsig. */
1414
struct bitcoin_tx *create_close_tx(const tal_t *ctx,
1515
const struct chainparams *chainparams,
16+
u32 local_wallet_index,
17+
const struct ext_key *local_wallet_ext_key,
1618
const u8 *our_script,
1719
const u8 *their_script,
1820
const u8 *funding_wscript,

contrib/remote_hsmd/proxy.cc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,11 +212,26 @@ void marshal_single_input_tx(struct bitcoin_tx const *tx,
212212

213213
for (size_t ii = 0; ii < tx->wtx->num_outputs; ii++) {
214214
OutputDescriptor *odesc = o_tp->add_output_descs();
215+
216+
// Add witness script
215217
if (tx->psbt->outputs[ii].witness_script_len)
216218
odesc->set_witscript(
217219
(const char *)
218220
tx->psbt->outputs[ii].witness_script,
219221
tx->psbt->outputs[ii].witness_script_len);
222+
223+
// Add keypath
224+
struct wally_map *mp = &tx->psbt->outputs[ii].keypaths;
225+
if (mp->num_items == 1) {
226+
const struct wally_map_item *ip = &mp->items[0];
227+
size_t npath =
228+
(ip->value_len - BIP32_KEY_FINGERPRINT_LEN) / sizeof(uint32_t);
229+
uint32_t *path = (uint32_t *) (ip->value + BIP32_KEY_FINGERPRINT_LEN);
230+
for (size_t jj = 0; jj < npath; ++jj) {
231+
odesc->mutable_key_loc()->add_key_path(le32_to_cpu(path[jj]));
232+
}
233+
}
234+
220235
}
221236
}
222237

lightningd/closing_control.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,32 @@ void peer_start_closingd(struct channel *channel,
285285
return;
286286
}
287287

288+
// Determine the wallet index for our output, UINT32_MAX if not found.
289+
u32 local_wallet_index;
290+
struct ext_key local_wallet_ext_key;
291+
bool is_p2sh;
292+
if (wallet_can_spend(
293+
ld->wallet,
294+
channel->shutdown_scriptpubkey[LOCAL],
295+
&local_wallet_index,
296+
&is_p2sh)) {
297+
if (bip32_key_from_parent(
298+
ld->wallet->bip32_base,
299+
local_wallet_index,
300+
BIP32_FLAG_KEY_PUBLIC,
301+
&local_wallet_ext_key) != WALLY_OK) {
302+
abort();
303+
}
304+
} else {
305+
local_wallet_index = UINT32_MAX;
306+
char *dummy_bip32 =
307+
"tpubDAY5hwtonH4NE8zY46ZMFf6B6F3fqMis7cwfNihXXpAg6XzBZNo"
308+
"HAdAzAZx2peoU8nTWFqvUncXwJ9qgE5VxcnUKxdut8F6mptVmKjfiwDQ";
309+
if (bip32_key_from_base58(dummy_bip32, &local_wallet_ext_key) != WALLY_OK) {
310+
abort();
311+
}
312+
}
313+
288314
initmsg = towire_closingd_init(tmpctx,
289315
chainparams,
290316
pps,
@@ -299,6 +325,8 @@ void peer_start_closingd(struct channel *channel,
299325
amount_msat_to_sat_round_down(their_msat),
300326
channel->our_config.dust_limit,
301327
minfee, feelimit, startfee,
328+
local_wallet_index,
329+
&local_wallet_ext_key,
302330
channel->shutdown_scriptpubkey[LOCAL],
303331
channel->shutdown_scriptpubkey[REMOTE],
304332
channel->closing_fee_negotiation_step,

0 commit comments

Comments
 (0)