Skip to content

Commit 52ecb99

Browse files
committed
Added our_wallet_index to onchaind_init for sign_delayed_sweep
1 parent d8db20c commit 52ecb99

File tree

7 files changed

+65
-9
lines changed

7 files changed

+65
-9
lines changed

lightningd/onchain_control.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,16 @@ enum watch_result onchaind_funding_spent(struct channel *channel,
623623
channel->final_key_idx);
624624
return KEEP_WATCHING;
625625
}
626+
struct ext_key final_wallet_ext_key;
627+
if (bip32_key_from_parent(
628+
ld->wallet->bip32_base,
629+
channel->final_key_idx,
630+
BIP32_FLAG_KEY_PUBLIC,
631+
&final_wallet_ext_key) != WALLY_OK) {
632+
log_broken(channel->log, "Could not derive onchain ext key %"PRIu64,
633+
channel->final_key_idx);
634+
return KEEP_WATCHING;
635+
}
626636
/* This could be a mutual close, but it doesn't matter. */
627637
bitcoin_txid(channel->last_tx, &our_last_txid);
628638

@@ -687,6 +697,8 @@ enum watch_result onchaind_funding_spent(struct channel *channel,
687697
&our_last_txid,
688698
channel->shutdown_scriptpubkey[LOCAL],
689699
channel->shutdown_scriptpubkey[REMOTE],
700+
channel->final_key_idx,
701+
&final_wallet_ext_key,
690702
&final_key,
691703
channel->opener,
692704
&channel->local_basepoints,

onchaind/onchaind.c

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ static struct amount_sat dust_limit;
6666
static u32 to_self_delay[NUM_SIDES];
6767

6868
/* Where we send money to (our wallet) */
69+
static u32 our_wallet_index;
70+
static struct ext_key our_wallet_ext_key;
6971
static struct pubkey our_wallet_pubkey;
7072

7173
/* Their revocation secret (only if they cheated). */
@@ -660,6 +662,35 @@ static u8 *penalty_to_us(const tal_t *ctx,
660662
tx, wscript);
661663
}
662664

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

696727
bitcoin_tx_add_output(
697728
tx, scriptpubkey_p2wpkh(tx, &our_wallet_pubkey), NULL, out->sat);
729+
add_keypath_item_to_last_output(tx, our_wallet_index, &our_wallet_ext_key);
698730

699731
/* Worst-case sig is 73 bytes */
700732
weight = bitcoin_tx_weight(tx) + 1 + 3 + 73 + 0 + tal_count(wscript);
@@ -816,12 +848,14 @@ replace_penalty_tx_to_us(const tal_t *ctx,
816848
BITCOIN_TX_RBF_SEQUENCE,
817849
NULL, input_amount, NULL, input_wscript);
818850
/* Reconstruct the output with a smaller amount. */
819-
if (amount_sat_greater(output_amount, dust_limit))
851+
if (amount_sat_greater(output_amount, dust_limit)) {
820852
bitcoin_tx_add_output(tx,
821853
scriptpubkey_p2wpkh(tx,
822854
&our_wallet_pubkey),
823855
NULL,
824856
output_amount);
857+
add_keypath_item_to_last_output(tx, our_wallet_index, &our_wallet_ext_key);
858+
}
825859
else
826860
bitcoin_tx_add_output(tx,
827861
scriptpubkey_opreturn_padded(tx),
@@ -4006,6 +4040,8 @@ int main(int argc, char *argv[])
40064040
&our_broadcast_txid,
40074041
&scriptpubkey[LOCAL],
40084042
&scriptpubkey[REMOTE],
4043+
&our_wallet_index,
4044+
&our_wallet_ext_key,
40094045
&our_wallet_pubkey,
40104046
&opener,
40114047
&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/onchaind_wiregen.c

Lines changed: 7 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

onchaind/onchaind_wiregen.h

Lines changed: 4 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

onchaind/test/run-grind_feerate-bug.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ bool fromwire_onchaind_dev_memleak(const void *p UNNEEDED)
5050
bool fromwire_onchaind_htlc(const void *p UNNEEDED, struct htlc_stub *htlc UNNEEDED, bool *tell_if_missing UNNEEDED, bool *tell_immediately UNNEEDED)
5151
{ fprintf(stderr, "fromwire_onchaind_htlc called!\n"); abort(); }
5252
/* Generated stub for fromwire_onchaind_init */
53-
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, u64 *num_htlcs 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)
53+
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, u64 *num_htlcs 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)
5454
{ fprintf(stderr, "fromwire_onchaind_init called!\n"); abort(); }
5555
/* Generated stub for fromwire_onchaind_known_preimage */
5656
bool fromwire_onchaind_known_preimage(const void *p UNNEEDED, struct preimage *preimage UNNEEDED, bool *is_replay UNNEEDED)

0 commit comments

Comments
 (0)