@@ -33,6 +33,7 @@ use bitcoin::secp256k1::{Secp256k1, ecdsa::Signature};
33
33
use bitcoin:: secp256k1:: { SecretKey , PublicKey } ;
34
34
use bitcoin:: secp256k1;
35
35
36
+ use crate :: ln:: channel:: INITIAL_COMMITMENT_NUMBER ;
36
37
use crate :: ln:: { PaymentHash , PaymentPreimage } ;
37
38
use crate :: ln:: msgs:: DecodeError ;
38
39
use crate :: ln:: chan_utils:: { self , htlc_success_tx_weight, htlc_timeout_tx_weight} ;
@@ -893,6 +894,14 @@ pub(crate) struct ChannelMonitorImpl<Signer: WriteableEcdsaChannelSigner> {
893
894
/// the counterparty's commitment transactions for third-party watchtowers. This will only be
894
895
/// present on monitors created after 0.0.117.
895
896
counterparty_dust_limit_satoshis : Option < u64 > ,
897
+
898
+ /// Initial counterparty commmitment data needed to recreate the commitment tx
899
+ /// in the persistence pipeline for third-party watchtowers. This will only be present on
900
+ /// monitors created after 0.0.117.
901
+ ///
902
+ /// Ordering of tuple data: (their_per_commitment_point, feerate_per_kw, to_broadcaster_sats,
903
+ /// to_countersignatory_sats)
904
+ initial_counterparty_commitment_info : Option < ( PublicKey , u32 , u64 , u64 ) > ,
896
905
}
897
906
898
907
/// Transaction outputs to watch for on-chain spends.
@@ -1084,6 +1093,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Writeable for ChannelMonitorImpl<Signe
1084
1093
( 13 , self . spendable_txids_confirmed, required_vec) ,
1085
1094
( 15 , self . counterparty_fulfilled_htlcs, required) ,
1086
1095
( 17 , self . counterparty_dust_limit_satoshis, option) ,
1096
+ ( 19 , self . initial_counterparty_commitment_info, option) ,
1087
1097
} ) ;
1088
1098
1089
1099
Ok ( ( ) )
@@ -1236,6 +1246,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitor<Signer> {
1236
1246
best_block,
1237
1247
counterparty_node_id : Some ( counterparty_node_id) ,
1238
1248
counterparty_dust_limit_satoshis : Some ( counterparty_dust_limit_satoshis) ,
1249
+ initial_counterparty_commitment_info : None ,
1239
1250
} )
1240
1251
}
1241
1252
@@ -1244,11 +1255,31 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitor<Signer> {
1244
1255
self . inner . lock ( ) . unwrap ( ) . provide_secret ( idx, secret)
1245
1256
}
1246
1257
1258
+ /// A variant of `Self::provide_latest_counterparty_commitment_tx` used to provide
1259
+ /// additional information to the monitor to store in order to recreate the initial
1260
+ /// counterparty commitment transaction during persistence (mainly for use in third-party
1261
+ /// watchtowers).
1262
+ ///
1263
+ /// This is used to provide the counterparty commitment information directly to the monitor
1264
+ /// before the initial persistence of a new channel.
1265
+ pub ( crate ) fn provide_initial_counterparty_commitment_tx < L : Deref > (
1266
+ & self , txid : Txid , htlc_outputs : Vec < ( HTLCOutputInCommitment , Option < Box < HTLCSource > > ) > ,
1267
+ commitment_number : u64 , their_cur_per_commitment_point : PublicKey , feerate_per_kw : u32 ,
1268
+ to_broadcaster_value_sat : u64 , to_countersignatory_value_sat : u64 , logger : & L ,
1269
+ )
1270
+ where L :: Target : Logger
1271
+ {
1272
+ self . inner . lock ( ) . unwrap ( ) . provide_initial_counterparty_commitment_tx ( txid,
1273
+ htlc_outputs, commitment_number, their_cur_per_commitment_point, feerate_per_kw,
1274
+ to_broadcaster_value_sat, to_countersignatory_value_sat, logger) ;
1275
+ }
1276
+
1247
1277
/// Informs this monitor of the latest counterparty (ie non-broadcastable) commitment transaction.
1248
1278
/// The monitor watches for it to be broadcasted and then uses the HTLC information (and
1249
1279
/// possibly future revocation/preimage information) to claim outputs where possible.
1250
1280
/// We cache also the mapping hash:commitment number to lighten pruning of old preimages by watchtowers.
1251
- pub ( crate ) fn provide_latest_counterparty_commitment_tx < L : Deref > (
1281
+ #[ cfg( test) ]
1282
+ fn provide_latest_counterparty_commitment_tx < L : Deref > (
1252
1283
& self ,
1253
1284
txid : Txid ,
1254
1285
htlc_outputs : Vec < ( HTLCOutputInCommitment , Option < Box < HTLCSource > > ) > ,
@@ -1384,6 +1415,22 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitor<Signer> {
1384
1415
ret
1385
1416
}
1386
1417
1418
+ /// Gets the counterparty's initial commitment transaction. The returned commitment
1419
+ /// transaction is unsigned. This is intended to be called during the initial persistence of
1420
+ /// the monitor (inside an implementation of [`Persist::persist_new_channel`]), to allow for
1421
+ /// watchtowers in the persistence pipeline to have enough data to form justice transactions.
1422
+ ///
1423
+ /// This is similar to [`Self::counterparty_commitment_txs_from_update`], except
1424
+ /// that for the initial commitment transaction, we don't have a corresponding update.
1425
+ ///
1426
+ /// This will only return `Some` for channel monitors that have been created after upgrading
1427
+ /// to LDK 0.0.117+.
1428
+ ///
1429
+ /// [`Persist::persist_new_channel`]: crate::chain::chainmonitor::Persist::persist_new_channel
1430
+ pub fn initial_counterparty_commitment_tx ( & self ) -> Option < CommitmentTransaction > {
1431
+ self . inner . lock ( ) . unwrap ( ) . initial_counterparty_commitment_tx ( )
1432
+ }
1433
+
1387
1434
/// Gets all of the counterparty commitment transactions provided by the given update. This
1388
1435
/// may be empty if the update doesn't include any new counterparty commitments. Returned
1389
1436
/// commitment transactions are unsigned.
@@ -2263,6 +2310,25 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
2263
2310
Ok ( ( ) )
2264
2311
}
2265
2312
2313
+ pub ( crate ) fn provide_initial_counterparty_commitment_tx < L : Deref > (
2314
+ & mut self , txid : Txid , htlc_outputs : Vec < ( HTLCOutputInCommitment , Option < Box < HTLCSource > > ) > ,
2315
+ commitment_number : u64 , their_per_commitment_point : PublicKey , feerate_per_kw : u32 ,
2316
+ to_broadcaster_value : u64 , to_countersignatory_value : u64 , logger : & L
2317
+ )
2318
+ where L :: Target : Logger
2319
+ {
2320
+ self . initial_counterparty_commitment_info = Some ( ( their_per_commitment_point. clone ( ) ,
2321
+ feerate_per_kw, to_broadcaster_value, to_countersignatory_value) ) ;
2322
+
2323
+ #[ cfg( debug_assertions) ] {
2324
+ let rebuilt_commitment_tx = self . initial_counterparty_commitment_tx ( ) . unwrap ( ) ;
2325
+ debug_assert_eq ! ( rebuilt_commitment_tx. trust( ) . txid( ) , txid) ;
2326
+ }
2327
+
2328
+ self . provide_latest_counterparty_commitment_tx ( txid, htlc_outputs, commitment_number,
2329
+ their_per_commitment_point, logger) ;
2330
+ }
2331
+
2266
2332
pub ( crate ) fn provide_latest_counterparty_commitment_tx < L : Deref > ( & mut self , txid : Txid , htlc_outputs : Vec < ( HTLCOutputInCommitment , Option < Box < HTLCSource > > ) > , commitment_number : u64 , their_per_commitment_point : PublicKey , logger : & L ) where L :: Target : Logger {
2267
2333
// TODO: Encrypt the htlc_outputs data with the single-hash of the commitment transaction
2268
2334
// so that a remote monitor doesn't learn anything unless there is a malicious close.
@@ -2692,6 +2758,17 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
2692
2758
ret
2693
2759
}
2694
2760
2761
+ pub ( crate ) fn initial_counterparty_commitment_tx ( & mut self ) -> Option < CommitmentTransaction > {
2762
+ let ( their_per_commitment_point, feerate_per_kw, to_broadcaster_value,
2763
+ to_countersignatory_value) = self . initial_counterparty_commitment_info ?;
2764
+ let htlc_outputs = vec ! [ ] ;
2765
+
2766
+ let commitment_tx = self . build_counterparty_commitment_tx ( INITIAL_COMMITMENT_NUMBER ,
2767
+ & their_per_commitment_point, to_broadcaster_value, to_countersignatory_value,
2768
+ feerate_per_kw, htlc_outputs) ;
2769
+ Some ( commitment_tx)
2770
+ }
2771
+
2695
2772
fn build_counterparty_commitment_tx (
2696
2773
& self , commitment_number : u64 , their_per_commitment_point : & PublicKey ,
2697
2774
to_broadcaster_value : u64 , to_countersignatory_value : u64 , feerate_per_kw : u32 ,
@@ -4219,6 +4296,7 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
4219
4296
let mut spendable_txids_confirmed = Some ( Vec :: new ( ) ) ;
4220
4297
let mut counterparty_fulfilled_htlcs = Some ( HashMap :: new ( ) ) ;
4221
4298
let mut counterparty_dust_limit_satoshis = None ;
4299
+ let mut initial_counterparty_commitment_info = None ;
4222
4300
read_tlv_fields ! ( reader, {
4223
4301
( 1 , funding_spend_confirmed, option) ,
4224
4302
( 3 , htlcs_resolved_on_chain, optional_vec) ,
@@ -4229,6 +4307,7 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
4229
4307
( 13 , spendable_txids_confirmed, optional_vec) ,
4230
4308
( 15 , counterparty_fulfilled_htlcs, option) ,
4231
4309
( 17 , counterparty_dust_limit_satoshis, option) ,
4310
+ ( 19 , initial_counterparty_commitment_info, option) ,
4232
4311
} ) ;
4233
4312
4234
4313
Ok ( ( best_block. block_hash ( ) , ChannelMonitor :: from_impl ( ChannelMonitorImpl {
@@ -4285,6 +4364,7 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
4285
4364
best_block,
4286
4365
counterparty_node_id,
4287
4366
counterparty_dust_limit_satoshis,
4367
+ initial_counterparty_commitment_info,
4288
4368
} ) ) )
4289
4369
}
4290
4370
}
0 commit comments