Skip to content

Commit 57bbc99

Browse files
committed
Factored psbt_add_keypath_to_last_output into common.
1 parent f3fc949 commit 57bbc99

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
@@ -3,37 +3,9 @@
33
#include <bitcoin/script.h>
44
#include <common/close_tx.h>
55
#include <common/permute_tx.h>
6+
#include <common/psbt_internal.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
@@ -101,3 +101,33 @@ psbt_to_witness_stacks(const tal_t *ctx,
101101
tal_resize(&stacks, stack_index);
102102
return stacks;
103103
}
104+
105+
void psbt_add_keypath_to_last_output(struct bitcoin_tx *tx,
106+
u32 index,
107+
const struct ext_key *ext) {
108+
// Skip if there is no wallet keypath for this output.
109+
if (index == UINT32_MAX)
110+
return;
111+
112+
size_t outndx = tx->psbt->num_outputs - 1;
113+
struct wally_map *map_in = &tx->psbt->outputs[outndx].keypaths;
114+
115+
u8 fingerprint[BIP32_KEY_FINGERPRINT_LEN];
116+
if (bip32_key_get_fingerprint(
117+
(struct ext_key *) ext, fingerprint, sizeof(fingerprint)) != WALLY_OK) {
118+
return; /* FIXME: throw an error ? */
119+
}
120+
121+
u32 path[1];
122+
path[0] = index;
123+
124+
tal_wally_start();
125+
if (wally_map_add_keypath_item(map_in,
126+
ext->pub_key, sizeof(ext->pub_key),
127+
fingerprint, sizeof(fingerprint),
128+
path, 1) != WALLY_OK) {
129+
return; /* FIXME: throw an error ? */
130+
}
131+
tal_wally_end(tx->psbt);
132+
}
133+

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
@@ -11,6 +11,7 @@
1111
#include <common/memleak.h>
1212
#include <common/overflows.h>
1313
#include <common/peer_billboard.h>
14+
#include <common/psbt_internal.h>
1415
#include <common/status.h>
1516
#include <common/subdaemon.h>
1617
#include <common/type_to_string.h>
@@ -660,35 +661,6 @@ static u8 *penalty_to_us(const tal_t *ctx,
660661
tx, wscript);
661662
}
662663

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

725697
bitcoin_tx_add_output(
726698
tx, scriptpubkey_p2wpkh(tx, &our_wallet_pubkey), NULL, out->sat);
727-
add_keypath_item_to_last_output(tx, our_wallet_index, &our_wallet_ext_key);
699+
psbt_add_keypath_to_last_output(tx, our_wallet_index, &our_wallet_ext_key);
728700

729701
/* Worst-case sig is 73 bytes */
730702
weight = bitcoin_tx_weight(tx) + 1 + 3 + 73 + 0 + tal_count(wscript);
@@ -851,7 +823,7 @@ replace_penalty_tx_to_us(const tal_t *ctx,
851823
&our_wallet_pubkey),
852824
NULL,
853825
output_amount);
854-
add_keypath_item_to_last_output(tx, our_wallet_index, &our_wallet_ext_key);
826+
psbt_add_keypath_to_last_output(tx, our_wallet_index, &our_wallet_ext_key);
855827
}
856828
else
857829
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)