Skip to content

Commit 61cb2e6

Browse files
committed
Added our_wallet_index to onchaind_init for sign_delayed_sweep
1 parent 629fb71 commit 61cb2e6

File tree

5 files changed

+56
-3
lines changed

5 files changed

+56
-3
lines changed

lightningd/onchain_control.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <lightningd/subd.h>
1414
#include <onchaind/onchaind_wiregen.h>
1515
#include <wallet/txfilter.h>
16+
#include <wally_bip32.h>
1617

1718
/* We dump all the known preimages when onchaind starts up. */
1819
static void onchaind_tell_fulfill(struct channel *channel)
@@ -641,6 +642,16 @@ enum watch_result onchaind_funding_spent(struct channel *channel,
641642
channel->final_key_idx);
642643
return KEEP_WATCHING;
643644
}
645+
struct ext_key final_wallet_ext_key;
646+
if (bip32_key_from_parent(
647+
ld->wallet->bip32_base,
648+
channel->final_key_idx,
649+
BIP32_FLAG_KEY_PUBLIC,
650+
&final_wallet_ext_key) != WALLY_OK) {
651+
log_broken(channel->log, "Could not derive onchain ext key %"PRIu64,
652+
channel->final_key_idx);
653+
return KEEP_WATCHING;
654+
}
644655
/* This could be a mutual close, but it doesn't matter. */
645656
bitcoin_txid(channel->last_tx, &our_last_txid);
646657

@@ -705,6 +716,8 @@ enum watch_result onchaind_funding_spent(struct channel *channel,
705716
&our_last_txid,
706717
channel->shutdown_scriptpubkey[LOCAL],
707718
channel->shutdown_scriptpubkey[REMOTE],
719+
channel->final_key_idx,
720+
&final_wallet_ext_key,
708721
&final_key,
709722
channel->opener,
710723
&channel->local_basepoints,

onchaind/onchaind.c

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <onchaind/onchain_types.h>
1818
#include <onchaind/onchaind_wiregen.h>
1919
#include <unistd.h>
20+
#include <wally_bip32.h>
2021
#include <wire/wire_sync.h>
2122
#include "onchain_types_names_gen.h"
2223

@@ -52,6 +53,8 @@ static struct amount_sat dust_limit;
5253
static u32 to_self_delay[NUM_SIDES];
5354

5455
/* Where we send money to (our wallet) */
56+
static u32 our_wallet_index;
57+
static struct ext_key our_wallet_ext_key;
5558
static struct pubkey our_wallet_pubkey;
5659

5760
/* Their revocation secret (only if they cheated). */
@@ -656,6 +659,35 @@ static u8 *penalty_to_us(const tal_t *ctx,
656659
tx, wscript);
657660
}
658661

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+
659691
/*
660692
* This covers:
661693
* 1. to-us output spend (`<local_delayedsig> 0`)
@@ -691,6 +723,7 @@ static struct bitcoin_tx *tx_to_us(const tal_t *ctx,
691723

692724
bitcoin_tx_add_output(
693725
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);
694727

695728
/* Worst-case sig is 73 bytes */
696729
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,
811844
BITCOIN_TX_RBF_SEQUENCE,
812845
NULL, input_amount, NULL, input_wscript);
813846
/* 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)) {
815848
bitcoin_tx_add_output(tx,
816849
scriptpubkey_p2wpkh(tx,
817850
&our_wallet_pubkey),
818851
NULL,
819852
output_amount);
853+
add_keypath_item_to_last_output(tx, our_wallet_index, &our_wallet_ext_key);
854+
}
820855
else
821856
bitcoin_tx_add_output(tx,
822857
scriptpubkey_opreturn_padded(tx),
@@ -4093,6 +4128,8 @@ int main(int argc, char *argv[])
40934128
&our_broadcast_txid,
40944129
&scriptpubkey[LOCAL],
40954130
&scriptpubkey[REMOTE],
4131+
&our_wallet_index,
4132+
&our_wallet_ext_key,
40964133
&our_wallet_pubkey,
40974134
&opener,
40984135
&basepoints[LOCAL],

onchaind/onchaind_wire.csv

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <bitcoin/tx_parts.h>
2+
#include <common/bip32.h>
23
#include <common/coin_mvt.h>
34
#include <common/derive_basepoints.h>
45
#include <common/htlc_wire.h>
@@ -29,6 +30,8 @@ msgdata,onchaind_init,local_scriptpubkey_len,u16,
2930
msgdata,onchaind_init,local_scriptpubkey,u8,local_scriptpubkey_len
3031
msgdata,onchaind_init,remote_scriptpubkey_len,u16,
3132
msgdata,onchaind_init,remote_scriptpubkey,u8,remote_scriptpubkey_len
33+
msgdata,onchaind_init,ourwallet_index,u32,
34+
msgdata,onchaind_init,ourwallet_ext_key,ext_key,
3235
msgdata,onchaind_init,ourwallet_pubkey,pubkey,
3336
# We need these two for commit number obscurer
3437
msgdata,onchaind_init,opener,enum side,

onchaind/test/run-grind_feerate-bug.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ bool fromwire_onchaind_dev_memleak(const void *p UNNEEDED)
4848
bool fromwire_onchaind_htlcs(const tal_t *ctx UNNEEDED, const void *p UNNEEDED, struct htlc_stub **htlc UNNEEDED, bool **tell_if_missing UNNEEDED, bool **tell_immediately UNNEEDED)
4949
{ fprintf(stderr, "fromwire_onchaind_htlcs called!\n"); abort(); }
5050
/* Generated stub for fromwire_onchaind_init */
51-
bool fromwire_onchaind_init(const tal_t *ctx UNNEEDED, const void *p UNNEEDED, struct shachain *shachain UNNEEDED, const struct chainparams **chainparams UNNEEDED, struct amount_sat *funding_amount_satoshi UNNEEDED, struct amount_msat *our_msat UNNEEDED, struct pubkey *old_remote_per_commitment_point UNNEEDED, struct pubkey *remote_per_commitment_point UNNEEDED, u32 *local_to_self_delay UNNEEDED, u32 *remote_to_self_delay UNNEEDED, u32 *delayed_to_us_feerate UNNEEDED, u32 *htlc_feerate UNNEEDED, u32 *penalty_feerate UNNEEDED, struct amount_sat *local_dust_limit_satoshi UNNEEDED, struct bitcoin_txid *our_broadcast_txid UNNEEDED, u8 **local_scriptpubkey UNNEEDED, u8 **remote_scriptpubkey UNNEEDED, struct pubkey *ourwallet_pubkey UNNEEDED, enum side *opener UNNEEDED, struct basepoints *local_basepoints UNNEEDED, struct basepoints *remote_basepoints UNNEEDED, struct tx_parts **tx_parts UNNEEDED, u32 *locktime UNNEEDED, u32 *tx_blockheight UNNEEDED, u32 *reasonable_depth UNNEEDED, struct bitcoin_signature **htlc_signature UNNEEDED, u32 *min_possible_feerate UNNEEDED, u32 *max_possible_feerate UNNEEDED, struct pubkey **possible_remote_per_commit_point UNNEEDED, struct pubkey *local_funding_pubkey UNNEEDED, struct pubkey *remote_funding_pubkey UNNEEDED, u64 *local_static_remotekey_start UNNEEDED, u64 *remote_static_remotekey_start UNNEEDED, bool *option_anchor_outputs UNNEEDED, bool *is_replay UNNEEDED, u32 *min_relay_feerate UNNEEDED)
51+
bool fromwire_onchaind_init(const tal_t *ctx UNNEEDED, const void *p UNNEEDED, struct shachain *shachain UNNEEDED, const struct chainparams **chainparams UNNEEDED, struct amount_sat *funding_amount_satoshi UNNEEDED, struct amount_msat *our_msat UNNEEDED, struct pubkey *old_remote_per_commitment_point UNNEEDED, struct pubkey *remote_per_commitment_point UNNEEDED, u32 *local_to_self_delay UNNEEDED, u32 *remote_to_self_delay UNNEEDED, u32 *delayed_to_us_feerate UNNEEDED, u32 *htlc_feerate UNNEEDED, u32 *penalty_feerate UNNEEDED, struct amount_sat *local_dust_limit_satoshi UNNEEDED, struct bitcoin_txid *our_broadcast_txid UNNEEDED, u8 **local_scriptpubkey UNNEEDED, u8 **remote_scriptpubkey UNNEEDED, u32 *ourwallet_index UNNEEDED, struct ext_key *ourwallet_ext_key UNNEEDED, struct pubkey *ourwallet_pubkey UNNEEDED, enum side *opener UNNEEDED, struct basepoints *local_basepoints UNNEEDED, struct basepoints *remote_basepoints UNNEEDED, struct tx_parts **tx_parts UNNEEDED, u32 *locktime UNNEEDED, u32 *tx_blockheight UNNEEDED, u32 *reasonable_depth UNNEEDED, struct bitcoin_signature **htlc_signature UNNEEDED, u32 *min_possible_feerate UNNEEDED, u32 *max_possible_feerate UNNEEDED, struct pubkey **possible_remote_per_commit_point UNNEEDED, struct pubkey *local_funding_pubkey UNNEEDED, struct pubkey *remote_funding_pubkey UNNEEDED, u64 *local_static_remotekey_start UNNEEDED, u64 *remote_static_remotekey_start UNNEEDED, bool *option_anchor_outputs UNNEEDED, bool *is_replay UNNEEDED, u32 *min_relay_feerate UNNEEDED)
5252
{ fprintf(stderr, "fromwire_onchaind_init called!\n"); abort(); }
5353
/* Generated stub for fromwire_onchaind_known_preimage */
5454
bool fromwire_onchaind_known_preimage(const void *p UNNEEDED, struct preimage *preimage UNNEEDED, bool *is_replay UNNEEDED)

onchaind/test/run-grind_feerate.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ bool fromwire_onchaind_dev_memleak(const void *p UNNEEDED)
5353
bool fromwire_onchaind_htlcs(const tal_t *ctx UNNEEDED, const void *p UNNEEDED, struct htlc_stub **htlc UNNEEDED, bool **tell_if_missing UNNEEDED, bool **tell_immediately UNNEEDED)
5454
{ fprintf(stderr, "fromwire_onchaind_htlcs called!\n"); abort(); }
5555
/* Generated stub for fromwire_onchaind_init */
56-
bool fromwire_onchaind_init(const tal_t *ctx UNNEEDED, const void *p UNNEEDED, struct shachain *shachain UNNEEDED, const struct chainparams **chainparams UNNEEDED, struct amount_sat *funding_amount_satoshi UNNEEDED, struct amount_msat *our_msat UNNEEDED, struct pubkey *old_remote_per_commitment_point UNNEEDED, struct pubkey *remote_per_commitment_point UNNEEDED, u32 *local_to_self_delay UNNEEDED, u32 *remote_to_self_delay UNNEEDED, u32 *delayed_to_us_feerate UNNEEDED, u32 *htlc_feerate UNNEEDED, u32 *penalty_feerate UNNEEDED, struct amount_sat *local_dust_limit_satoshi UNNEEDED, struct bitcoin_txid *our_broadcast_txid UNNEEDED, u8 **local_scriptpubkey UNNEEDED, u8 **remote_scriptpubkey UNNEEDED, struct pubkey *ourwallet_pubkey UNNEEDED, enum side *opener UNNEEDED, struct basepoints *local_basepoints UNNEEDED, struct basepoints *remote_basepoints UNNEEDED, struct tx_parts **tx_parts UNNEEDED, u32 *locktime UNNEEDED, u32 *tx_blockheight UNNEEDED, u32 *reasonable_depth UNNEEDED, struct bitcoin_signature **htlc_signature UNNEEDED, u32 *min_possible_feerate UNNEEDED, u32 *max_possible_feerate UNNEEDED, struct pubkey **possible_remote_per_commit_point UNNEEDED, struct pubkey *local_funding_pubkey UNNEEDED, struct pubkey *remote_funding_pubkey UNNEEDED, u64 *local_static_remotekey_start UNNEEDED, u64 *remote_static_remotekey_start UNNEEDED, bool *option_anchor_outputs UNNEEDED, bool *is_replay UNNEEDED, u32 *min_relay_feerate UNNEEDED)
56+
bool fromwire_onchaind_init(const tal_t *ctx UNNEEDED, const void *p UNNEEDED, struct shachain *shachain UNNEEDED, const struct chainparams **chainparams UNNEEDED, struct amount_sat *funding_amount_satoshi UNNEEDED, struct amount_msat *our_msat UNNEEDED, struct pubkey *old_remote_per_commitment_point UNNEEDED, struct pubkey *remote_per_commitment_point UNNEEDED, u32 *local_to_self_delay UNNEEDED, u32 *remote_to_self_delay UNNEEDED, u32 *delayed_to_us_feerate UNNEEDED, u32 *htlc_feerate UNNEEDED, u32 *penalty_feerate UNNEEDED, struct amount_sat *local_dust_limit_satoshi UNNEEDED, struct bitcoin_txid *our_broadcast_txid UNNEEDED, u8 **local_scriptpubkey UNNEEDED, u8 **remote_scriptpubkey UNNEEDED, u32 *ourwallet_index UNNEEDED, struct ext_key *ourwallet_ext_key UNNEEDED, struct pubkey *ourwallet_pubkey UNNEEDED, enum side *opener UNNEEDED, struct basepoints *local_basepoints UNNEEDED, struct basepoints *remote_basepoints UNNEEDED, struct tx_parts **tx_parts UNNEEDED, u32 *locktime UNNEEDED, u32 *tx_blockheight UNNEEDED, u32 *reasonable_depth UNNEEDED, struct bitcoin_signature **htlc_signature UNNEEDED, u32 *min_possible_feerate UNNEEDED, u32 *max_possible_feerate UNNEEDED, struct pubkey **possible_remote_per_commit_point UNNEEDED, struct pubkey *local_funding_pubkey UNNEEDED, struct pubkey *remote_funding_pubkey UNNEEDED, u64 *local_static_remotekey_start UNNEEDED, u64 *remote_static_remotekey_start UNNEEDED, bool *option_anchor_outputs UNNEEDED, bool *is_replay UNNEEDED, u32 *min_relay_feerate UNNEEDED)
5757
{ fprintf(stderr, "fromwire_onchaind_init called!\n"); abort(); }
5858
/* Generated stub for fromwire_onchaind_known_preimage */
5959
bool fromwire_onchaind_known_preimage(const void *p UNNEEDED, struct preimage *preimage UNNEEDED, bool *is_replay UNNEEDED)

0 commit comments

Comments
 (0)