Skip to content

Commit ac23e46

Browse files
committed
Factored psbt_add_keypath_to_last_output into common.
1 parent 883e45b commit ac23e46

File tree

9 files changed

+52
-91
lines changed

9 files changed

+52
-91
lines changed

channeld/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ CHANNELD_COMMON_OBJS := \
7474
common/per_peer_state.o \
7575
common/permute_tx.o \
7676
common/ping.o \
77+
common/psbt_internal.o \
7778
common/psbt_open.o \
7879
common/private_channel_announcement.o \
7980
common/pseudorand.o \

channeld/watchtower.c

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,42 +5,14 @@
55
#include <common/features.h>
66
#include <common/htlc_tx.h>
77
#include <common/keyset.h>
8+
#include <common/psbt_internal.h>
89
#include <common/status.h>
910
#include <common/type_to_string.h>
1011
#include <hsmd/hsmd_wiregen.h>
1112
#include <wire/wire_sync.h>
1213

1314
static const u8 ONE = 0x1;
1415

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-
4416
const struct bitcoin_tx *
4517
penalty_tx_create(const tal_t *ctx,
4618
const struct channel *channel,
@@ -106,7 +78,7 @@ penalty_tx_create(const tal_t *ctx,
10678
NULL, to_them_sats, NULL, wscript);
10779

10880
bitcoin_tx_add_output(tx, final_scriptpubkey, NULL, to_them_sats);
109-
add_keypath_item_to_last_output(tx, final_index, final_ext_key);
81+
psbt_add_keypath_to_last_output(tx, final_index, final_ext_key);
11082

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

closingd/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ CLOSINGD_COMMON_OBJS := \
4848
common/per_peer_state.o \
4949
common/permute_tx.o \
5050
common/ping.o \
51+
common/psbt_internal.o \
5152
common/psbt_open.o \
5253
common/pseudorand.o \
5354
common/read_peer_msg.o \

common/close_tx.c

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,10 @@
22
#include "bitcoin/tx.h"
33
#include "close_tx.h"
44
#include "permute_tx.h"
5+
#include "psbt_internal.h"
56
#include <assert.h>
67
#include <common/utils.h>
78

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-
379
struct bitcoin_tx *create_close_tx(const tal_t *ctx,
3810
const struct chainparams *chainparams,
3911
u32 local_wallet_index,
@@ -77,7 +49,7 @@ struct bitcoin_tx *create_close_tx(const tal_t *ctx,
7749
script = tal_dup_talarr(tx, u8, our_script);
7850
/* One output is to us. */
7951
bitcoin_tx_add_output(tx, script, NULL, to_us);
80-
add_keypath_item_to_last_output(tx, local_wallet_index, local_wallet_ext_key);
52+
psbt_add_keypath_to_last_output(tx, local_wallet_index, local_wallet_ext_key);
8153
num_outputs++;
8254
}
8355

common/psbt_internal.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,33 @@ psbt_to_witness_stacks(const tal_t *ctx,
100100
tal_resize(&stacks, stack_index);
101101
return stacks;
102102
}
103+
104+
void psbt_add_keypath_to_last_output(struct bitcoin_tx *tx,
105+
u32 index,
106+
const struct ext_key *ext) {
107+
// Skip if there is no wallet keypath for this output.
108+
if (index == UINT32_MAX)
109+
return;
110+
111+
size_t outndx = tx->psbt->num_outputs - 1;
112+
struct wally_map *map_in = &tx->psbt->outputs[outndx].keypaths;
113+
114+
u8 fingerprint[BIP32_KEY_FINGERPRINT_LEN];
115+
if (bip32_key_get_fingerprint(
116+
(struct ext_key *) ext, fingerprint, sizeof(fingerprint)) != WALLY_OK) {
117+
return; /* FIXME: throw an error ? */
118+
}
119+
120+
u32 path[1];
121+
path[0] = index;
122+
123+
tal_wally_start();
124+
if (wally_map_add_keypath_item(map_in,
125+
ext->pub_key, sizeof(ext->pub_key),
126+
fingerprint, sizeof(fingerprint),
127+
path, 1) != WALLY_OK) {
128+
return; /* FIXME: throw an error ? */
129+
}
130+
tal_wally_end(tx->psbt);
131+
}
132+

common/psbt_internal.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22
#define LIGHTNING_COMMON_PSBT_INTERNAL_H
33

44
#include "config.h"
5+
#include <ccan/short_types/short_types.h>
56
#include <ccan/tal/tal.h>
67
#include <common/tx_roles.h>
78

9+
struct bitcoin_tx;
10+
struct ext_key;
811
struct wally_psbt;
912
struct wally_psbt_input;
1013
struct witness_element;
@@ -32,4 +35,11 @@ psbt_to_witness_stacks(const tal_t *ctx,
3235
const struct wally_psbt *psbt,
3336
enum tx_role side_to_stack);
3437

38+
/* psbt_add_keypath_to_last_output - augment the last output with the
39+
* given wallet keypath
40+
*/
41+
void psbt_add_keypath_to_last_output(struct bitcoin_tx *tx,
42+
u32 index,
43+
const struct ext_key *ext);
44+
3545
#endif /* LIGHTNING_COMMON_PSBT_INTERNAL_H */

onchaind/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ ONCHAIND_COMMON_OBJS := \
5454
common/onionreply.o \
5555
common/peer_billboard.o \
5656
common/permute_tx.o \
57+
common/psbt_internal.o \
5758
common/psbt_open.o \
5859
common/pseudorand.o \
5960
common/setup.o \

onchaind/onchaind.c

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <common/memleak.h>
1111
#include <common/overflows.h>
1212
#include <common/peer_billboard.h>
13+
#include <common/psbt_internal.h>
1314
#include <common/status.h>
1415
#include <common/subdaemon.h>
1516
#include <common/type_to_string.h>
@@ -659,35 +660,6 @@ static u8 *penalty_to_us(const tal_t *ctx,
659660
tx, wscript);
660661
}
661662

662-
static void add_keypath_item_to_last_output(struct bitcoin_tx *tx,
663-
u32 index,
664-
const struct ext_key *ext) {
665-
// Skip if there is no wallet keypath for this output.
666-
if (index == UINT32_MAX)
667-
return;
668-
669-
size_t outndx = tx->psbt->num_outputs - 1;
670-
struct wally_map *map_in = &tx->psbt->outputs[outndx].keypaths;
671-
672-
u8 fingerprint[BIP32_KEY_FINGERPRINT_LEN];
673-
if (bip32_key_get_fingerprint(
674-
(struct ext_key *) ext, fingerprint, sizeof(fingerprint)) != WALLY_OK) {
675-
abort();
676-
}
677-
678-
u32 path[1];
679-
path[0] = index;
680-
681-
tal_wally_start();
682-
if (wally_map_add_keypath_item(map_in,
683-
ext->pub_key, sizeof(ext->pub_key),
684-
fingerprint, sizeof(fingerprint),
685-
path, 1) != WALLY_OK) {
686-
abort();
687-
}
688-
tal_wally_end(tx->psbt);
689-
}
690-
691663
/*
692664
* This covers:
693665
* 1. to-us output spend (`<local_delayedsig> 0`)
@@ -723,7 +695,7 @@ static struct bitcoin_tx *tx_to_us(const tal_t *ctx,
723695

724696
bitcoin_tx_add_output(
725697
tx, scriptpubkey_p2wpkh(tx, &our_wallet_pubkey), NULL, out->sat);
726-
add_keypath_item_to_last_output(tx, our_wallet_index, &our_wallet_ext_key);
698+
psbt_add_keypath_to_last_output(tx, our_wallet_index, &our_wallet_ext_key);
727699

728700
/* Worst-case sig is 73 bytes */
729701
weight = bitcoin_tx_weight(tx) + 1 + 3 + 73 + 0 + tal_count(wscript);
@@ -850,7 +822,7 @@ replace_penalty_tx_to_us(const tal_t *ctx,
850822
&our_wallet_pubkey),
851823
NULL,
852824
output_amount);
853-
add_keypath_item_to_last_output(tx, our_wallet_index, &our_wallet_ext_key);
825+
psbt_add_keypath_to_last_output(tx, our_wallet_index, &our_wallet_ext_key);
854826
}
855827
else
856828
bitcoin_tx_add_output(tx,

onchaind/test/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ ONCHAIND_TEST_COMMON_OBJS := \
1313
common/amount.o \
1414
common/autodata.o \
1515
common/features.o \
16+
common/psbt_internal.o \
17+
common/psbt_open.o \
1618
common/pseudorand.o \
1719
common/setup.o \
1820
common/type_to_string.o \

0 commit comments

Comments
 (0)