From 8876c03791db7119606a362e7b887bc642bf4592 Mon Sep 17 00:00:00 2001 From: Ken Sedgwick Date: Thu, 23 Dec 2021 11:57:33 -0800 Subject: [PATCH] hsmd: Add PSBT keypath utility functions --- common/Makefile | 1 + common/psbt_keypath.c | 33 +++++++++++++++++++++++++++++++++ common/psbt_keypath.h | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+) create mode 100644 common/psbt_keypath.c create mode 100644 common/psbt_keypath.h diff --git a/common/Makefile b/common/Makefile index a203a734a..86f1588b4 100644 --- a/common/Makefile +++ b/common/Makefile @@ -69,6 +69,7 @@ COMMON_SRC_NOGEN := \ common/ping.c \ common/private_channel_announcement.c \ common/psbt_internal.c \ + common/psbt_keypath.c \ common/psbt_open.c \ common/pseudorand.c \ common/random_select.c \ diff --git a/common/psbt_keypath.c b/common/psbt_keypath.c new file mode 100644 index 000000000..5037a0a40 --- /dev/null +++ b/common/psbt_keypath.c @@ -0,0 +1,33 @@ +#include "config.h" +#include +#include +#include +#include +#include + +void psbt_set_keypath(u32 index, const struct ext_key *ext, struct wally_map *map_in) { + u8 fingerprint[BIP32_KEY_FINGERPRINT_LEN]; + if (bip32_key_get_fingerprint( + (struct ext_key *) ext, fingerprint, sizeof(fingerprint)) != WALLY_OK) + abort(); + + u32 path[1]; + path[0] = index; + + if (wally_map_add_keypath_item(map_in, + ext->pub_key, sizeof(ext->pub_key), + fingerprint, sizeof(fingerprint), + path, 1) != WALLY_OK) + abort(); +} + +void psbt_add_keypath_to_last_output(struct bitcoin_tx *tx, + u32 key_index, + const struct ext_key *ext) { + size_t outndx = tx->psbt->num_outputs - 1; + struct wally_map *map_in = &tx->psbt->outputs[outndx].keypaths; + + tal_wally_start(); + psbt_set_keypath(key_index, ext, map_in); + tal_wally_end(tx->psbt); +} diff --git a/common/psbt_keypath.h b/common/psbt_keypath.h new file mode 100644 index 000000000..f19d85761 --- /dev/null +++ b/common/psbt_keypath.h @@ -0,0 +1,32 @@ +#ifndef LIGHTNING_COMMON_PSBT_KEYPATH_H +#define LIGHTNING_COMMON_PSBT_KEYPATH_H + +#include "config.h" +#include + +struct bitcoin_tx; +struct ext_key; +struct wally_map; + +/* psbt_set_keypath - Set the keypath of a PSBT output. + * + * @index - child index of the wallet key + * @ext - extended public key of the immediate parent of the wallet key + * @map_in - wally keypaths map + */ +void psbt_set_keypath(u32 index, + const struct ext_key *ext, + struct wally_map *map_in); + +/* psbt_add_keypath_to_last_output - augment the last output with the + * given wallet keypath + * + * @tx - transaction to modify + * @index - child index of the wallet key + * @ext - extended public key of the immediate parent of the wallet key + */ +void psbt_add_keypath_to_last_output(struct bitcoin_tx *tx, + u32 index, + const struct ext_key *ext); + +#endif /* LIGHTNING_COMMON_PSBT_KEYPATH_H */