Skip to content

Commit 64220fd

Browse files
committed
Factored psbt_add_keypath_to_last_output into common.
1 parent db499ee commit 64220fd

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
@@ -72,6 +72,7 @@ CHANNELD_COMMON_OBJS := \
7272
common/per_peer_state.o \
7373
common/permute_tx.o \
7474
common/ping.o \
75+
common/psbt_internal.o \
7576
common/psbt_open.o \
7677
common/private_channel_announcement.o \
7778
common/pseudorand.o \

channeld/watchtower.c

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,42 +7,14 @@
77
#include <common/htlc_tx.h>
88
#include <common/key_derive.h>
99
#include <common/keyset.h>
10+
#include <common/psbt_internal.h>
1011
#include <common/status.h>
1112
#include <common/type_to_string.h>
1213
#include <hsmd/hsmd_wiregen.h>
1314
#include <wire/wire_sync.h>
1415

1516
static const u8 ONE = 0x1;
1617

17-
static void add_keypath_item_to_last_output(struct bitcoin_tx *tx,
18-
u32 index,
19-
const struct ext_key *ext) {
20-
// Skip if there is no wallet keypath for this output.
21-
if (index == UINT32_MAX)
22-
return;
23-
24-
size_t outndx = tx->psbt->num_outputs - 1;
25-
struct wally_map *map_in = &tx->psbt->outputs[outndx].keypaths;
26-
27-
u8 fingerprint[BIP32_KEY_FINGERPRINT_LEN];
28-
if (bip32_key_get_fingerprint(
29-
(struct ext_key *) ext, fingerprint, sizeof(fingerprint)) != WALLY_OK) {
30-
abort();
31-
}
32-
33-
u32 path[1];
34-
path[0] = index;
35-
36-
tal_wally_start();
37-
if (wally_map_add_keypath_item(map_in,
38-
ext->pub_key, sizeof(ext->pub_key),
39-
fingerprint, sizeof(fingerprint),
40-
path, 1) != WALLY_OK) {
41-
abort();
42-
}
43-
tal_wally_end(tx->psbt);
44-
}
45-
4618
const struct bitcoin_tx *
4719
penalty_tx_create(const tal_t *ctx,
4820
const struct channel *channel,
@@ -104,7 +76,7 @@ penalty_tx_create(const tal_t *ctx,
10476
NULL, to_them_sats, NULL, wscript);
10577

10678
bitcoin_tx_add_output(tx, final_scriptpubkey, NULL, to_them_sats);
107-
add_keypath_item_to_last_output(tx, final_index, final_ext_key);
79+
psbt_add_keypath_to_last_output(tx, final_index, final_ext_key);
10880

10981
/* Worst-case sig is 73 bytes */
11082
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
@@ -46,6 +46,7 @@ CLOSINGD_COMMON_OBJS := \
4646
common/peer_failed.o \
4747
common/per_peer_state.o \
4848
common/permute_tx.o \
49+
common/psbt_internal.o \
4950
common/psbt_open.o \
5051
common/pseudorand.o \
5152
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,
@@ -78,7 +50,7 @@ struct bitcoin_tx *create_close_tx(const tal_t *ctx,
7850
script = tal_dup_talarr(tx, u8, our_script);
7951
/* One output is to us. */
8052
bitcoin_tx_add_output(tx, script, NULL, to_us);
81-
add_keypath_item_to_last_output(tx, local_wallet_index, local_wallet_ext_key);
53+
psbt_add_keypath_to_last_output(tx, local_wallet_index, local_wallet_ext_key);
8254
num_outputs++;
8355
}
8456

common/psbt_internal.c

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

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
@@ -53,6 +53,7 @@ ONCHAIND_COMMON_OBJS := \
5353
common/onionreply.o \
5454
common/peer_billboard.o \
5555
common/permute_tx.o \
56+
common/psbt_internal.o \
5657
common/psbt_open.o \
5758
common/pseudorand.o \
5859
common/setup.o \

onchaind/onchaind.c

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <common/memleak.h>
1717
#include <common/overflows.h>
1818
#include <common/peer_billboard.h>
19+
#include <common/psbt_internal.h>
1920
#include <common/status.h>
2021
#include <common/subdaemon.h>
2122
#include <common/type_to_string.h>
@@ -662,35 +663,6 @@ static u8 *penalty_to_us(const tal_t *ctx,
662663
tx, wscript);
663664
}
664665

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

727699
bitcoin_tx_add_output(
728700
tx, scriptpubkey_p2wpkh(tx, &our_wallet_pubkey), NULL, out->sat);
729-
add_keypath_item_to_last_output(tx, our_wallet_index, &our_wallet_ext_key);
701+
psbt_add_keypath_to_last_output(tx, our_wallet_index, &our_wallet_ext_key);
730702

731703
/* Worst-case sig is 73 bytes */
732704
weight = bitcoin_tx_weight(tx) + 1 + 3 + 73 + 0 + tal_count(wscript);
@@ -854,7 +826,7 @@ replace_penalty_tx_to_us(const tal_t *ctx,
854826
&our_wallet_pubkey),
855827
NULL,
856828
output_amount);
857-
add_keypath_item_to_last_output(tx, our_wallet_index, &our_wallet_ext_key);
829+
psbt_add_keypath_to_last_output(tx, our_wallet_index, &our_wallet_ext_key);
858830
}
859831
else
860832
bitcoin_tx_add_output(tx,

onchaind/test/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ ALL_TEST_PROGRAMS += $(ONCHAIND_TEST_PROGRAMS)
1212
ONCHAIND_TEST_COMMON_OBJS := \
1313
common/amount.o \
1414
common/features.o \
15+
common/psbt_internal.o \
16+
common/psbt_open.o \
1517
common/pseudorand.o \
1618
common/setup.o \
1719
common/type_to_string.o \

0 commit comments

Comments
 (0)