Skip to content

Commit e71e9ee

Browse files
committed
Added our_wallet_index to onchaind_init for sign_delayed_sweep
1 parent 0e1ea08 commit e71e9ee

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
@@ -14,6 +14,7 @@
1414
#include <lightningd/subd.h>
1515
#include <onchaind/onchaind_wiregen.h>
1616
#include <wallet/txfilter.h>
17+
#include <wally_bip32.h>
1718

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

@@ -706,6 +717,8 @@ enum watch_result onchaind_funding_spent(struct channel *channel,
706717
&our_last_txid,
707718
channel->shutdown_scriptpubkey[LOCAL],
708719
channel->shutdown_scriptpubkey[REMOTE],
720+
channel->final_key_idx,
721+
&final_wallet_ext_key,
709722
&final_key,
710723
channel->opener,
711724
&channel->local_basepoints,

onchaind/onchaind.c

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

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

5556
/* Where we send money to (our wallet) */
57+
static u32 our_wallet_index;
58+
static struct ext_key our_wallet_ext_key;
5659
static struct pubkey our_wallet_pubkey;
5760

5861
/* Their revocation secret (only if they cheated). */
@@ -657,6 +660,35 @@ static u8 *penalty_to_us(const tal_t *ctx,
657660
tx, wscript);
658661
}
659662

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

693725
bitcoin_tx_add_output(
694726
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);
695728

696729
/* Worst-case sig is 73 bytes */
697730
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,
812845
BITCOIN_TX_RBF_SEQUENCE,
813846
NULL, input_amount, NULL, input_wscript);
814847
/* 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)) {
816849
bitcoin_tx_add_output(tx,
817850
scriptpubkey_p2wpkh(tx,
818851
&our_wallet_pubkey),
819852
NULL,
820853
output_amount);
854+
add_keypath_item_to_last_output(tx, our_wallet_index, &our_wallet_ext_key);
855+
}
821856
else
822857
bitcoin_tx_add_output(tx,
823858
scriptpubkey_opreturn_padded(tx),
@@ -4094,6 +4129,8 @@ int main(int argc, char *argv[])
40944129
&our_broadcast_txid,
40954130
&scriptpubkey[LOCAL],
40964131
&scriptpubkey[REMOTE],
4132+
&our_wallet_index,
4133+
&our_wallet_ext_key,
40974134
&our_wallet_pubkey,
40984135
&opener,
40994136
&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
@@ -49,7 +49,7 @@ bool fromwire_onchaind_dev_memleak(const void *p UNNEEDED)
4949
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)
5050
{ fprintf(stderr, "fromwire_onchaind_htlcs called!\n"); abort(); }
5151
/* Generated stub for fromwire_onchaind_init */
52-
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)
52+
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)
5353
{ fprintf(stderr, "fromwire_onchaind_init called!\n"); abort(); }
5454
/* Generated stub for fromwire_onchaind_known_preimage */
5555
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
@@ -54,7 +54,7 @@ bool fromwire_onchaind_dev_memleak(const void *p UNNEEDED)
5454
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)
5555
{ fprintf(stderr, "fromwire_onchaind_htlcs called!\n"); abort(); }
5656
/* Generated stub for fromwire_onchaind_init */
57-
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)
57+
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)
5858
{ fprintf(stderr, "fromwire_onchaind_init called!\n"); abort(); }
5959
/* Generated stub for fromwire_onchaind_known_preimage */
6060
bool fromwire_onchaind_known_preimage(const void *p UNNEEDED, struct preimage *preimage UNNEEDED, bool *is_replay UNNEEDED)

0 commit comments

Comments
 (0)