|
18 | 18 | #include <onchaind/onchain_types.h> |
19 | 19 | #include <onchaind/onchaind_wiregen.h> |
20 | 20 | #include <unistd.h> |
| 21 | +#include <wally_bip32.h> |
21 | 22 | #include <wire/wire_sync.h> |
22 | 23 | #include "onchain_types_names_gen.h" |
23 | 24 |
|
@@ -53,6 +54,8 @@ static struct amount_sat dust_limit; |
53 | 54 | static u32 to_self_delay[NUM_SIDES]; |
54 | 55 |
|
55 | 56 | /* Where we send money to (our wallet) */ |
| 57 | +static u32 our_wallet_index; |
| 58 | +static struct ext_key our_wallet_ext_key; |
56 | 59 | static struct pubkey our_wallet_pubkey; |
57 | 60 |
|
58 | 61 | /* Their revocation secret (only if they cheated). */ |
@@ -657,6 +660,35 @@ static u8 *penalty_to_us(const tal_t *ctx, |
657 | 660 | tx, wscript); |
658 | 661 | } |
659 | 662 |
|
| 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 | + |
660 | 692 | /* |
661 | 693 | * This covers: |
662 | 694 | * 1. to-us output spend (`<local_delayedsig> 0`) |
@@ -692,6 +724,7 @@ static struct bitcoin_tx *tx_to_us(const tal_t *ctx, |
692 | 724 |
|
693 | 725 | bitcoin_tx_add_output( |
694 | 726 | 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); |
695 | 728 |
|
696 | 729 | /* Worst-case sig is 73 bytes */ |
697 | 730 | weight = bitcoin_tx_weight(tx) + 1 + 3 + 73 + 0 + tal_count(wscript); |
@@ -812,12 +845,14 @@ replace_penalty_tx_to_us(const tal_t *ctx, |
812 | 845 | BITCOIN_TX_RBF_SEQUENCE, |
813 | 846 | NULL, input_amount, NULL, input_wscript); |
814 | 847 | /* Reconstruct the output with a smaller amount. */ |
815 | | - if (amount_sat_greater(output_amount, dust_limit)) |
| 848 | + if (amount_sat_greater(output_amount, dust_limit)) { |
816 | 849 | bitcoin_tx_add_output(tx, |
817 | 850 | scriptpubkey_p2wpkh(tx, |
818 | 851 | &our_wallet_pubkey), |
819 | 852 | NULL, |
820 | 853 | output_amount); |
| 854 | + add_keypath_item_to_last_output(tx, our_wallet_index, &our_wallet_ext_key); |
| 855 | + } |
821 | 856 | else |
822 | 857 | bitcoin_tx_add_output(tx, |
823 | 858 | scriptpubkey_opreturn_padded(tx), |
@@ -4094,6 +4129,8 @@ int main(int argc, char *argv[]) |
4094 | 4129 | &our_broadcast_txid, |
4095 | 4130 | &scriptpubkey[LOCAL], |
4096 | 4131 | &scriptpubkey[REMOTE], |
| 4132 | + &our_wallet_index, |
| 4133 | + &our_wallet_ext_key, |
4097 | 4134 | &our_wallet_pubkey, |
4098 | 4135 | &opener, |
4099 | 4136 | &basepoints[LOCAL], |
|
0 commit comments