11
11
//!
12
12
//! [`Event`]: crate::events::Event
13
13
14
+ use alloc:: collections:: BTreeMap ;
14
15
use core:: convert:: TryInto ;
15
16
use core:: ops:: Deref ;
16
17
@@ -50,45 +51,50 @@ const fn fee_for_weight(feerate_sat_per_1000_weight: u32, weight: u64) -> u64 {
50
51
( ( feerate_sat_per_1000_weight as u64 * weight) + 1000 - 1 ) / 1000
51
52
}
52
53
54
+ /// The parameters required to derive a channel signer via [`SignerProvider`].
55
+ #[ derive( Clone , Debug , PartialEq , Eq ) ]
56
+ pub struct ChannelDerivationParameters {
57
+ /// The value in satoshis of the channel we're attempting to spend the anchor output of.
58
+ pub value_satoshis : u64 ,
59
+ /// The unique identifier to re-derive the signer for the associated channel.
60
+ pub keys_id : [ u8 ; 32 ] ,
61
+ /// The necessary channel parameters that need to be provided to the re-derived signer through
62
+ /// [`ChannelSigner::provide_channel_parameters`].
63
+ ///
64
+ /// [`ChannelSigner::provide_channel_parameters`]: crate::sign::ChannelSigner::provide_channel_parameters
65
+ pub transaction_parameters : ChannelTransactionParameters ,
66
+ }
67
+
53
68
/// A descriptor used to sign for a commitment transaction's anchor output.
54
69
#[ derive( Clone , Debug , PartialEq , Eq ) ]
55
70
pub struct AnchorDescriptor {
56
- /// A unique identifier used along with `channel_value_satoshis` to re-derive the
57
- /// [`InMemorySigner`] required to sign `input`.
58
- ///
59
- /// [`InMemorySigner`]: crate::sign::InMemorySigner
60
- pub channel_keys_id : [ u8 ; 32 ] ,
61
- /// The value in satoshis of the channel we're attempting to spend the anchor output of. This is
62
- /// used along with `channel_keys_id` to re-derive the [`InMemorySigner`] required to sign
63
- /// `input`.
64
- ///
65
- /// [`InMemorySigner`]: crate::sign::InMemorySigner
66
- pub channel_value_satoshis : u64 ,
71
+ /// The parameters required to derive the signer for the anchor input.
72
+ pub channel_derivation_parameters : ChannelDerivationParameters ,
67
73
/// The transaction input's outpoint corresponding to the commitment transaction's anchor
68
74
/// output.
69
75
pub outpoint : OutPoint ,
70
76
}
71
77
78
+ impl AnchorDescriptor {
79
+ /// Derives the channel signer required to sign the anchor input.
80
+ pub fn derive_channel_signer < SP : Deref > ( & self , signer_provider : & SP ) -> <SP :: Target as SignerProvider >:: Signer
81
+ where
82
+ SP :: Target : SignerProvider
83
+ {
84
+ let mut signer = signer_provider. derive_channel_signer (
85
+ self . channel_derivation_parameters . value_satoshis ,
86
+ self . channel_derivation_parameters . keys_id ,
87
+ ) ;
88
+ signer. provide_channel_parameters ( & self . channel_derivation_parameters . transaction_parameters ) ;
89
+ signer
90
+ }
91
+ }
92
+
72
93
/// A descriptor used to sign for a commitment transaction's HTLC output.
73
94
#[ derive( Clone , Debug , PartialEq , Eq ) ]
74
95
pub struct HTLCDescriptor {
75
- /// A unique identifier used along with `channel_value_satoshis` to re-derive the
76
- /// [`InMemorySigner`] required to sign `input`.
77
- ///
78
- /// [`InMemorySigner`]: crate::sign::InMemorySigner
79
- pub channel_keys_id : [ u8 ; 32 ] ,
80
- /// The value in satoshis of the channel we're attempting to spend the anchor output of. This is
81
- /// used along with `channel_keys_id` to re-derive the [`InMemorySigner`] required to sign
82
- /// `input`.
83
- ///
84
- /// [`InMemorySigner`]: crate::sign::InMemorySigner
85
- pub channel_value_satoshis : u64 ,
86
- /// The necessary channel parameters that need to be provided to the re-derived
87
- /// [`InMemorySigner`] through [`ChannelSigner::provide_channel_parameters`].
88
- ///
89
- /// [`InMemorySigner`]: crate::sign::InMemorySigner
90
- /// [`ChannelSigner::provide_channel_parameters`]: crate::sign::ChannelSigner::provide_channel_parameters
91
- pub channel_parameters : ChannelTransactionParameters ,
96
+ /// The parameters required to derive the signer for the HTLC input.
97
+ pub channel_derivation_parameters : ChannelDerivationParameters ,
92
98
/// The txid of the commitment transaction in which the HTLC output lives.
93
99
pub commitment_txid : Txid ,
94
100
/// The number of the commitment transaction in which the HTLC output lives.
@@ -115,7 +121,7 @@ impl HTLCDescriptor {
115
121
/// Returns the delayed output created as a result of spending the HTLC output in the commitment
116
122
/// transaction.
117
123
pub fn tx_output < C : secp256k1:: Signing + secp256k1:: Verification > ( & self , secp : & Secp256k1 < C > ) -> TxOut {
118
- let channel_params = self . channel_parameters . as_holder_broadcastable ( ) ;
124
+ let channel_params = self . channel_derivation_parameters . transaction_parameters . as_holder_broadcastable ( ) ;
119
125
let broadcaster_keys = channel_params. broadcaster_pubkeys ( ) ;
120
126
let counterparty_keys = channel_params. countersignatory_pubkeys ( ) ;
121
127
let broadcaster_delayed_key = chan_utils:: derive_public_key (
@@ -132,7 +138,7 @@ impl HTLCDescriptor {
132
138
133
139
/// Returns the witness script of the HTLC output in the commitment transaction.
134
140
pub fn witness_script < C : secp256k1:: Signing + secp256k1:: Verification > ( & self , secp : & Secp256k1 < C > ) -> Script {
135
- let channel_params = self . channel_parameters . as_holder_broadcastable ( ) ;
141
+ let channel_params = self . channel_derivation_parameters . transaction_parameters . as_holder_broadcastable ( ) ;
136
142
let broadcaster_keys = channel_params. broadcaster_pubkeys ( ) ;
137
143
let counterparty_keys = channel_params. countersignatory_pubkeys ( ) ;
138
144
let broadcaster_htlc_key = chan_utils:: derive_public_key (
@@ -157,6 +163,19 @@ impl HTLCDescriptor {
157
163
signature, & self . counterparty_sig , & self . preimage , witness_script, & ChannelTypeFeatures :: anchors_zero_htlc_fee_and_dependencies ( ) /* opt_anchors */
158
164
)
159
165
}
166
+
167
+ /// Derives the channel signer required to sign the HTLC input.
168
+ pub fn derive_channel_signer < SP : Deref > ( & self , signer_provider : & SP ) -> <SP :: Target as SignerProvider >:: Signer
169
+ where
170
+ SP :: Target : SignerProvider
171
+ {
172
+ let mut signer = signer_provider. derive_channel_signer (
173
+ self . channel_derivation_parameters . value_satoshis ,
174
+ self . channel_derivation_parameters . keys_id ,
175
+ ) ;
176
+ signer. provide_channel_parameters ( & self . channel_derivation_parameters . transaction_parameters ) ;
177
+ signer
178
+ }
160
179
}
161
180
162
181
/// Represents the different types of transactions, originating from LDK, to be bumped.
@@ -175,12 +194,11 @@ pub enum BumpTransactionEvent {
175
194
/// broadcast first, as the child anchor transaction depends on it.
176
195
///
177
196
/// The consumer should be able to sign for any of the additional inputs included within the
178
- /// child anchor transaction. To sign its anchor input, an [`InMemorySigner`] should be
179
- /// re-derived through [`KeysManager::derive_channel_keys`] with the help of
180
- /// [`AnchorDescriptor::channel_keys_id`] and [`AnchorDescriptor::channel_value_satoshis`]. The
181
- /// anchor input signature can be computed with [`EcdsaChannelSigner::sign_holder_anchor_input`],
182
- /// which can then be provided to [`build_anchor_input_witness`] along with the `funding_pubkey`
183
- /// to obtain the full witness required to spend.
197
+ /// child anchor transaction. To sign its anchor input, an [`EcdsaChannelSigner`] should be
198
+ /// re-derived through [`AnchorDescriptor::derive_channel_signer`]. The anchor input signature
199
+ /// can be computed with [`EcdsaChannelSigner::sign_holder_anchor_input`], which can then be
200
+ /// provided to [`build_anchor_input_witness`] along with the `funding_pubkey` to obtain the
201
+ /// full witness required to spend.
184
202
///
185
203
/// It is possible to receive more than one instance of this event if a valid child anchor
186
204
/// transaction is never broadcast or is but not with a sufficient fee to be mined. Care should
@@ -199,8 +217,7 @@ pub enum BumpTransactionEvent {
199
217
/// an empty `pending_htlcs`), confirmation of the commitment transaction can be considered to
200
218
/// be not urgent.
201
219
///
202
- /// [`InMemorySigner`]: crate::sign::InMemorySigner
203
- /// [`KeysManager::derive_channel_keys`]: crate::sign::KeysManager::derive_channel_keys
220
+ /// [`EcdsaChannelSigner`]: crate::sign::EcdsaChannelSigner
204
221
/// [`EcdsaChannelSigner::sign_holder_anchor_input`]: crate::sign::EcdsaChannelSigner::sign_holder_anchor_input
205
222
/// [`build_anchor_input_witness`]: crate::ln::chan_utils::build_anchor_input_witness
206
223
ChannelClose {
@@ -238,11 +255,11 @@ pub enum BumpTransactionEvent {
238
255
/// broadcast by the consumer of the event.
239
256
///
240
257
/// The consumer should be able to sign for any of the non-HTLC inputs added to the resulting
241
- /// HTLC transaction. To sign HTLC inputs, an [`InMemorySigner `] should be re-derived through
242
- /// [`KeysManager::derive_channel_keys`] with the help of `channel_keys_id` and
243
- /// `channel_value_satoshis`. Each HTLC input's signature can be computed with
244
- /// [`EcdsaChannelSigner::sign_holder_htlc_transaction`], which can then be provided to
245
- /// [`HTLCDescriptor::tx_input_witness`] to obtain the fully signed witness required to spend.
258
+ /// HTLC transaction. To sign HTLC inputs, an [`EcdsaChannelSigner `] should be re-derived
259
+ /// through [`HTLCDescriptor::derive_channel_signer`]. Each HTLC input's signature can be
260
+ /// computed with [`EcdsaChannelSigner::sign_holder_htlc_transaction`], which can then be
261
+ /// provided to [`HTLCDescriptor::tx_input_witness`] to obtain the fully signed witness required
262
+ /// to spend.
246
263
///
247
264
/// It is possible to receive more than one instance of this event if a valid HTLC transaction
248
265
/// is never broadcast or is but not with a sufficient fee to be mined. Care should be taken by
@@ -254,8 +271,7 @@ pub enum BumpTransactionEvent {
254
271
/// longer able to commit external confirmed funds to the HTLC transaction or the fee committed
255
272
/// to the HTLC transaction is greater in value than the HTLCs being claimed.
256
273
///
257
- /// [`InMemorySigner`]: crate::sign::InMemorySigner
258
- /// [`KeysManager::derive_channel_keys`]: crate::sign::KeysManager::derive_channel_keys
274
+ /// [`EcdsaChannelSigner`]: crate::sign::EcdsaChannelSigner
259
275
/// [`EcdsaChannelSigner::sign_holder_htlc_transaction`]: crate::sign::EcdsaChannelSigner::sign_holder_htlc_transaction
260
276
/// [`HTLCDescriptor::tx_input_witness`]: HTLCDescriptor::tx_input_witness
261
277
HTLCResolution {
@@ -667,9 +683,7 @@ where
667
683
debug_assert_eq ! ( anchor_tx. output. len( ) , 1 ) ;
668
684
669
685
self . utxo_source . sign_tx ( & mut anchor_tx) ?;
670
- let signer = self . signer_provider . derive_channel_signer (
671
- anchor_descriptor. channel_value_satoshis , anchor_descriptor. channel_keys_id ,
672
- ) ;
686
+ let signer = anchor_descriptor. derive_channel_signer ( & self . signer_provider ) ;
673
687
let anchor_sig = signer. sign_holder_anchor_input ( & anchor_tx, 0 , & self . secp ) ?;
674
688
anchor_tx. input [ 0 ] . witness =
675
689
chan_utils:: build_anchor_input_witness ( & signer. pubkeys ( ) . funding_pubkey , & anchor_sig) ;
@@ -683,25 +697,15 @@ where
683
697
fn build_htlc_tx (
684
698
& self , claim_id : ClaimId , target_feerate_sat_per_1000_weight : u32 ,
685
699
htlc_descriptors : & [ HTLCDescriptor ] , tx_lock_time : PackedLockTime ,
686
- ) -> Result < ( Transaction , HashMap < [ u8 ; 32 ] , < SP :: Target as SignerProvider > :: Signer > ) , ( ) > {
700
+ ) -> Result < Transaction , ( ) > {
687
701
let mut tx = Transaction {
688
702
version : 2 ,
689
703
lock_time : tx_lock_time,
690
704
input : vec ! [ ] ,
691
705
output : vec ! [ ] ,
692
706
} ;
693
- // Unfortunately, we need to derive the signer for each HTLC ahead of time to obtain its
694
- // input.
695
- let mut signers = HashMap :: new ( ) ;
696
707
let mut must_spend = Vec :: with_capacity ( htlc_descriptors. len ( ) ) ;
697
708
for htlc_descriptor in htlc_descriptors {
698
- signers. entry ( htlc_descriptor. channel_keys_id )
699
- . or_insert_with ( ||
700
- self . signer_provider . derive_channel_signer (
701
- htlc_descriptor. channel_value_satoshis , htlc_descriptor. channel_keys_id ,
702
- )
703
- ) ;
704
-
705
709
let htlc_input = htlc_descriptor. unsigned_tx_input ( ) ;
706
710
must_spend. push ( Input {
707
711
outpoint : htlc_input. previous_output . clone ( ) ,
@@ -720,7 +724,7 @@ where
720
724
claim_id, & must_spend, & tx. output , target_feerate_sat_per_1000_weight,
721
725
) ?;
722
726
self . process_coin_selection ( & mut tx, coin_selection) ;
723
- Ok ( ( tx , signers ) )
727
+ Ok ( tx )
724
728
}
725
729
726
730
/// Handles a [`BumpTransactionEvent::HTLCResolution`] event variant by producing a
@@ -729,16 +733,16 @@ where
729
733
& self , claim_id : ClaimId , target_feerate_sat_per_1000_weight : u32 ,
730
734
htlc_descriptors : & [ HTLCDescriptor ] , tx_lock_time : PackedLockTime ,
731
735
) -> Result < ( ) , ( ) > {
732
- let ( mut htlc_tx, signers ) = self . build_htlc_tx (
736
+ let mut htlc_tx = self . build_htlc_tx (
733
737
claim_id, target_feerate_sat_per_1000_weight, htlc_descriptors, tx_lock_time,
734
738
) ?;
735
739
736
740
self . utxo_source . sign_tx ( & mut htlc_tx) ?;
741
+ let mut signers = BTreeMap :: new ( ) ;
737
742
for ( idx, htlc_descriptor) in htlc_descriptors. iter ( ) . enumerate ( ) {
738
- let signer = signers. get ( & htlc_descriptor. channel_keys_id ) . unwrap ( ) ;
739
- let htlc_sig = signer. sign_holder_htlc_transaction (
740
- & htlc_tx, idx, htlc_descriptor, & self . secp
741
- ) ?;
743
+ let signer = signers. entry ( htlc_descriptor. channel_derivation_parameters . keys_id )
744
+ . or_insert_with ( || htlc_descriptor. derive_channel_signer ( & self . signer_provider ) ) ;
745
+ let htlc_sig = signer. sign_holder_htlc_transaction ( & htlc_tx, idx, htlc_descriptor, & self . secp ) ?;
742
746
let witness_script = htlc_descriptor. witness_script ( & self . secp ) ;
743
747
htlc_tx. input [ idx] . witness = htlc_descriptor. tx_input_witness ( & htlc_sig, & witness_script) ;
744
748
}
0 commit comments