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