16
16
17
17
use crate :: chain:: keysinterface:: SpendableOutputDescriptor ;
18
18
#[ cfg( anchors) ]
19
- use crate :: ln:: chan_utils:: HTLCOutputInCommitment ;
19
+ use crate :: ln:: chan_utils:: { self , ChannelTransactionParameters , HTLCOutputInCommitment } ;
20
20
use crate :: ln:: channelmanager:: { InterceptId , PaymentId } ;
21
21
use crate :: ln:: channel:: FUNDING_CONF_DEADLINE_BLOCKS ;
22
22
use crate :: ln:: features:: ChannelTypeFeatures ;
@@ -29,11 +29,15 @@ use crate::routing::router::{RouteHop, RouteParameters};
29
29
30
30
use bitcoin:: { PackedLockTime , Transaction } ;
31
31
#[ cfg( anchors) ]
32
- use bitcoin:: OutPoint ;
32
+ use bitcoin:: { OutPoint , Txid , TxIn , TxOut , Witness } ;
33
33
use bitcoin:: blockdata:: script:: Script ;
34
34
use bitcoin:: hashes:: Hash ;
35
35
use bitcoin:: hashes:: sha256:: Hash as Sha256 ;
36
36
use bitcoin:: secp256k1:: PublicKey ;
37
+ #[ cfg( anchors) ]
38
+ use bitcoin:: secp256k1:: { self , Secp256k1 } ;
39
+ #[ cfg( anchors) ]
40
+ use bitcoin:: secp256k1:: ecdsa:: Signature ;
37
41
use crate :: io;
38
42
use crate :: prelude:: * ;
39
43
use core:: time:: Duration ;
@@ -237,6 +241,99 @@ pub struct AnchorDescriptor {
237
241
pub outpoint : OutPoint ,
238
242
}
239
243
244
+ #[ cfg( anchors) ]
245
+ /// A descriptor used to sign for a commitment transaction's HTLC output.
246
+ #[ derive( Clone , Debug ) ]
247
+ pub struct HTLCDescriptor {
248
+ /// A unique identifier used along with `channel_value_satoshis` to re-derive the
249
+ /// [`InMemorySigner`] required to sign `input`.
250
+ ///
251
+ /// [`InMemorySigner`]: crate::chain::keysinterface::InMemorySigner
252
+ pub channel_keys_id : [ u8 ; 32 ] ,
253
+ /// The value in satoshis of the channel we're attempting to spend the anchor output of. This is
254
+ /// used along with `channel_keys_id` to re-derive the [`InMemorySigner`] required to sign
255
+ /// `input`.
256
+ ///
257
+ /// [`InMemorySigner`]: crate::chain::keysinterface::InMemorySigner
258
+ pub channel_value_satoshis : u64 ,
259
+ /// The necessary channel parameters that need to be provided to the re-derived
260
+ /// [`InMemorySigner`] through [`BaseSign::ready_channel`].
261
+ ///
262
+ /// [`InMemorySigner`]: crate::chain::keysinterface::InMemorySigner
263
+ /// [`BaseSign::ready_channel`]: crate::chain::keysinterface::BaseSign::ready_channel
264
+ pub channel_parameters : ChannelTransactionParameters ,
265
+ /// The txid of the commitment transaction in which the HTLC output lives.
266
+ pub commitment_txid : Txid ,
267
+ /// The number of the commitment transaction in which the HTLC output lives.
268
+ pub per_commitment_number : u64 ,
269
+ /// The details of the HTLC as it appears in the commitment transaction.
270
+ pub htlc : HTLCOutputInCommitment ,
271
+ /// The preimage, if `Some`, to claim the HTLC output with. If `None`, the timeout path must be
272
+ /// taken.
273
+ pub preimage : Option < PaymentPreimage > ,
274
+ /// The counterparty's signature required to spend the HTLC output.
275
+ pub counterparty_sig : Signature
276
+ }
277
+
278
+ #[ cfg( anchors) ]
279
+ impl HTLCDescriptor {
280
+ /// Returns the unsigned transaction input spending the HTLC output in the commitment
281
+ /// transaction.
282
+ pub fn unsigned_tx_input ( & self ) -> TxIn {
283
+ chan_utils:: build_htlc_input ( & self . commitment_txid , & self . htlc , true /* opt_anchors */ )
284
+ }
285
+
286
+ /// Returns the delayed output created as a result of spending the HTLC output in the commitment
287
+ /// transaction.
288
+ pub fn tx_output < C : secp256k1:: Signing + secp256k1:: Verification > (
289
+ & self , per_commitment_point : & PublicKey , secp : & Secp256k1 < C >
290
+ ) -> TxOut {
291
+ let channel_params = self . channel_parameters . as_holder_broadcastable ( ) ;
292
+ let broadcaster_keys = channel_params. broadcaster_pubkeys ( ) ;
293
+ let counterparty_keys = channel_params. countersignatory_pubkeys ( ) ;
294
+ let broadcaster_delayed_key = chan_utils:: derive_public_key (
295
+ secp, per_commitment_point, & broadcaster_keys. delayed_payment_basepoint
296
+ ) ;
297
+ let counterparty_revocation_key = chan_utils:: derive_public_revocation_key (
298
+ secp, per_commitment_point, & counterparty_keys. revocation_basepoint
299
+ ) ;
300
+ chan_utils:: build_htlc_output (
301
+ 0 /* feerate_per_kw */ , channel_params. contest_delay ( ) , & self . htlc , true /* opt_anchors */ ,
302
+ false /* use_non_zero_fee_anchors */ , & broadcaster_delayed_key, & counterparty_revocation_key
303
+ )
304
+ }
305
+
306
+ /// Returns the witness script of the HTLC output in the commitment transaction.
307
+ pub fn witness_script < C : secp256k1:: Signing + secp256k1:: Verification > (
308
+ & self , per_commitment_point : & PublicKey , secp : & Secp256k1 < C >
309
+ ) -> Script {
310
+ let channel_params = self . channel_parameters . as_holder_broadcastable ( ) ;
311
+ let broadcaster_keys = channel_params. broadcaster_pubkeys ( ) ;
312
+ let counterparty_keys = channel_params. countersignatory_pubkeys ( ) ;
313
+ let broadcaster_htlc_key = chan_utils:: derive_public_key (
314
+ secp, per_commitment_point, & broadcaster_keys. htlc_basepoint
315
+ ) ;
316
+ let counterparty_htlc_key = chan_utils:: derive_public_key (
317
+ secp, per_commitment_point, & counterparty_keys. htlc_basepoint
318
+ ) ;
319
+ let counterparty_revocation_key = chan_utils:: derive_public_revocation_key (
320
+ secp, per_commitment_point, & counterparty_keys. revocation_basepoint
321
+ ) ;
322
+ chan_utils:: get_htlc_redeemscript_with_explicit_keys (
323
+ & self . htlc , true /* opt_anchors */ , & broadcaster_htlc_key, & counterparty_htlc_key,
324
+ & counterparty_revocation_key,
325
+ )
326
+ }
327
+
328
+ /// Returns the fully signed witness required to spend the HTLC output in the commitment
329
+ /// transaction.
330
+ pub fn tx_input_witness ( & self , signature : & Signature , witness_script : & Script ) -> Witness {
331
+ chan_utils:: build_htlc_input_witness (
332
+ signature, & self . counterparty_sig , & self . preimage , witness_script, true /* opt_anchors */
333
+ )
334
+ }
335
+ }
336
+
240
337
#[ cfg( anchors) ]
241
338
/// Represents the different types of transactions, originating from LDK, to be bumped.
242
339
#[ derive( Clone , Debug ) ]
@@ -256,7 +353,10 @@ pub enum BumpTransactionEvent {
256
353
/// The consumer should be able to sign for any of the additional inputs included within the
257
354
/// child anchor transaction. To sign its anchor input, an [`InMemorySigner`] should be
258
355
/// re-derived through [`KeysManager::derive_channel_keys`] with the help of
259
- /// [`AnchorDescriptor::channel_keys_id`] and [`AnchorDescriptor::channel_value_satoshis`].
356
+ /// [`AnchorDescriptor::channel_keys_id`] and [`AnchorDescriptor::channel_value_satoshis`]. The
357
+ /// anchor input signature can be computed with [`BaseSign::sign_holder_anchor_input`],
358
+ /// which can then be provided to [`build_anchor_input_witness`] along with the `funding_pubkey`
359
+ /// to obtain the full witness required to spend.
260
360
///
261
361
/// It is possible to receive more than one instance of this event if a valid child anchor
262
362
/// transaction is never broadcast or is but not with a sufficient fee to be mined. Care should
@@ -277,6 +377,8 @@ pub enum BumpTransactionEvent {
277
377
///
278
378
/// [`InMemorySigner`]: crate::chain::keysinterface::InMemorySigner
279
379
/// [`KeysManager::derive_channel_keys`]: crate::chain::keysinterface::KeysManager::derive_channel_keys
380
+ /// [`BaseSign::sign_holder_anchor_input`]: crate::chain::keysinterface::BaseSign::sign_holder_anchor_input
381
+ /// [`build_anchor_input_witness`]: crate::ln::chan_utils::build_anchor_input_witness
280
382
ChannelClose {
281
383
/// The target feerate that the transaction package, which consists of the commitment
282
384
/// transaction and the to-be-crafted child anchor transaction, must meet.
@@ -295,6 +397,41 @@ pub enum BumpTransactionEvent {
295
397
/// commitment transaction confirms.
296
398
pending_htlcs : Vec < HTLCOutputInCommitment > ,
297
399
} ,
400
+ /// Indicates that a channel featuring anchor outputs has unilaterally closed on-chain by a
401
+ /// holder commitment transaction and its HTLC(s) need to be resolved on-chain. With the
402
+ /// zero-HTLC-transaction-fee variant of anchor outputs, the pre-signed HTLC
403
+ /// transactions have a zero fee, thus requiring additional inputs and/or outputs to be attached
404
+ /// for a timely confirmation within the chain. These additional inputs and/or outputs must be
405
+ /// appended to the resulting HTLC transaction to meet the target feerate. Failure to meet the
406
+ /// target feerate decreases the confirmation odds of the transaction, possibly resulting in a
407
+ /// loss of funds. Once the transaction meets the target feerate, it must be signed for and
408
+ /// broadcast by the consumer of the event.
409
+ ///
410
+ /// The consumer should be able to sign for any of the non-HTLC inputs added to the resulting
411
+ /// HTLC transaction. To sign HTLC inputs, an [`InMemorySigner`] should be re-derived through
412
+ /// [`KeysManager::derive_channel_keys`] with the help of `channel_keys_id` and
413
+ /// `channel_value_satoshis`. Each HTLC input's signature can be computed with
414
+ /// [`BaseSign::sign_holder_htlc_transaction`], which can then be provided to
415
+ /// [`HTLCDescriptor::tx_input_witness`] to obtain the fully signed witness required to spend.
416
+ ///
417
+ /// It is possible to receive more than one instance of this event if a valid HTLC transaction
418
+ /// is never broadcast or is but not with a sufficient fee to be mined. Care should be taken by
419
+ /// the consumer of the event to ensure any future iterations of the HTLC transaction adhere to
420
+ /// the [Replace-By-Fee
421
+ /// rules](https://github.com/bitcoin/bitcoin/blob/master/doc/policy/mempool-replacements.md)
422
+ /// for fee bumps to be accepted into the mempool, and eventually the chain. As the frequency of
423
+ /// these events is not user-controlled, users may ignore/drop the event if either they are no
424
+ /// longer able to commit external confirmed funds to the HTLC transaction or the fee committed
425
+ /// to the HTLC transaction is greater in value than the HTLCs being claimed.
426
+ ///
427
+ /// [`InMemorySigner`]: crate::chain::keysinterface::InMemorySigner
428
+ /// [`KeysManager::derive_channel_keys`]: crate::chain::keysinterface::KeysManager::derive_channel_keys
429
+ /// [`BaseSign::sign_holder_htlc_transaction`]: crate::chain::keysinterface::BaseSign::sign_holder_htlc_transaction
430
+ /// [`HTLCDescriptor::tx_input_witness`]: HTLCDescriptor::tx_input_witness
431
+ HTLCResolution {
432
+ target_feerate_sat_per_1000_weight : u32 ,
433
+ htlc_descriptors : Vec < HTLCDescriptor > ,
434
+ } ,
298
435
}
299
436
300
437
/// Will be used in [`Event::HTLCIntercepted`] to identify the next hop in the HTLC's path.
@@ -983,9 +1120,10 @@ impl Writeable for Event {
983
1120
& Event :: BumpTransaction ( ref event) => {
984
1121
27u8 . write ( writer) ?;
985
1122
match event {
986
- // We never write the ChannelClose events as they'll be replayed upon restarting
987
- // anyway if the commitment transaction remains unconfirmed .
1123
+ // We never write the ChannelClose|HTLCResolution events as they'll be replayed
1124
+ // upon restarting anyway if they remain unresolved .
988
1125
BumpTransactionEvent :: ChannelClose { .. } => { }
1126
+ BumpTransactionEvent :: HTLCResolution { .. } => { }
989
1127
}
990
1128
}
991
1129
& Event :: ChannelReady { ref channel_id, ref user_channel_id, ref counterparty_node_id, ref channel_type } => {
0 commit comments