@@ -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;
@@ -888,6 +889,14 @@ pub(crate) struct ChannelMonitorImpl<Signer: WriteableEcdsaChannelSigner> {
888
889
889
890
/// The node_id of our counterparty
890
891
counterparty_node_id : Option < PublicKey > ,
892
+
893
+ /// Initial counterparty commmitment data needed to recreate the commitment tx
894
+ /// in the persistence pipeline for third-party watchtowers. This will only be present on
895
+ /// monitors created after 0.0.117.
896
+ ///
897
+ /// Ordering of tuple data: (their_per_commitment_point, feerate_per_kw, to_broadcaster_sats,
898
+ /// to_countersignatory_sats)
899
+ initial_counterparty_commitment_info : Option < ( PublicKey , u32 , u64 , u64 ) > ,
891
900
}
892
901
893
902
/// Transaction outputs to watch for on-chain spends.
@@ -1078,6 +1087,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Writeable for ChannelMonitorImpl<Signe
1078
1087
( 11 , self . confirmed_commitment_tx_counterparty_output, option) ,
1079
1088
( 13 , self . spendable_txids_confirmed, required_vec) ,
1080
1089
( 15 , self . counterparty_fulfilled_htlcs, required) ,
1090
+ ( 17 , self . initial_counterparty_commitment_info, option) ,
1081
1091
} ) ;
1082
1092
1083
1093
Ok ( ( ) )
@@ -1228,6 +1238,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitor<Signer> {
1228
1238
1229
1239
best_block,
1230
1240
counterparty_node_id : Some ( counterparty_node_id) ,
1241
+ initial_counterparty_commitment_info : None ,
1231
1242
} )
1232
1243
}
1233
1244
@@ -1236,11 +1247,31 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitor<Signer> {
1236
1247
self . inner . lock ( ) . unwrap ( ) . provide_secret ( idx, secret)
1237
1248
}
1238
1249
1250
+ /// A variant of `Self::provide_latest_counterparty_commitment_tx` used to provide
1251
+ /// additional information to the monitor to store in order to recreate the initial
1252
+ /// counterparty commitment transaction during persistence (mainly for use in third-party
1253
+ /// watchtowers).
1254
+ ///
1255
+ /// This is used to provide the counterparty commitment information directly to the monitor
1256
+ /// before the initial persistence of a new channel.
1257
+ pub ( crate ) fn provide_initial_counterparty_commitment_tx < L : Deref > (
1258
+ & self , txid : Txid , htlc_outputs : Vec < ( HTLCOutputInCommitment , Option < Box < HTLCSource > > ) > ,
1259
+ commitment_number : u64 , their_cur_per_commitment_point : PublicKey , feerate_per_kw : u32 ,
1260
+ to_broadcaster_value_sat : u64 , to_countersignatory_value_sat : u64 , logger : & L ,
1261
+ )
1262
+ where L :: Target : Logger
1263
+ {
1264
+ self . inner . lock ( ) . unwrap ( ) . provide_initial_counterparty_commitment_tx ( txid,
1265
+ htlc_outputs, commitment_number, their_cur_per_commitment_point, feerate_per_kw,
1266
+ to_broadcaster_value_sat, to_countersignatory_value_sat, logger) ;
1267
+ }
1268
+
1239
1269
/// Informs this monitor of the latest counterparty (ie non-broadcastable) commitment transaction.
1240
1270
/// The monitor watches for it to be broadcasted and then uses the HTLC information (and
1241
1271
/// possibly future revocation/preimage information) to claim outputs where possible.
1242
1272
/// We cache also the mapping hash:commitment number to lighten pruning of old preimages by watchtowers.
1243
- pub ( crate ) fn provide_latest_counterparty_commitment_tx < L : Deref > (
1273
+ #[ cfg( test) ]
1274
+ fn provide_latest_counterparty_commitment_tx < L : Deref > (
1244
1275
& self ,
1245
1276
txid : Txid ,
1246
1277
htlc_outputs : Vec < ( HTLCOutputInCommitment , Option < Box < HTLCSource > > ) > ,
@@ -1376,6 +1407,22 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitor<Signer> {
1376
1407
ret
1377
1408
}
1378
1409
1410
+ /// Gets the counterparty's initial commitment transaction. The returned commitment
1411
+ /// transaction is unsigned. This is intended to be called during the initial persistence of
1412
+ /// the monitor (inside an implementation of [`Persist::persist_new_channel`]), to allow for
1413
+ /// watchtowers in the persistence pipeline to have enough data to form justice transactions.
1414
+ ///
1415
+ /// This is similar to [`Self::counterparty_commitment_txs_from_update`], except
1416
+ /// that for the initial commitment transaction, we don't have a corresponding update.
1417
+ ///
1418
+ /// This will only return `Some` for channel monitors that have been created after upgrading
1419
+ /// to LDK 0.0.117+.
1420
+ ///
1421
+ /// [`Persist::persist_new_channel`]: crate::chain::chainmonitor::Persist::persist_new_channel
1422
+ pub fn initial_counterparty_commitment_tx ( & self ) -> Option < CommitmentTransaction > {
1423
+ self . inner . lock ( ) . unwrap ( ) . initial_counterparty_commitment_tx ( )
1424
+ }
1425
+
1379
1426
/// Gets all of the counterparty commitment transactions provided by the given update. This
1380
1427
/// may be empty if the update doesn't include any new counterparty commitments. Returned
1381
1428
/// commitment transactions are unsigned.
@@ -2255,6 +2302,25 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
2255
2302
Ok ( ( ) )
2256
2303
}
2257
2304
2305
+ pub ( crate ) fn provide_initial_counterparty_commitment_tx < L : Deref > (
2306
+ & mut self , txid : Txid , htlc_outputs : Vec < ( HTLCOutputInCommitment , Option < Box < HTLCSource > > ) > ,
2307
+ commitment_number : u64 , their_per_commitment_point : PublicKey , feerate_per_kw : u32 ,
2308
+ to_broadcaster_value : u64 , to_countersignatory_value : u64 , logger : & L
2309
+ )
2310
+ where L :: Target : Logger
2311
+ {
2312
+ self . initial_counterparty_commitment_info = Some ( ( their_per_commitment_point. clone ( ) ,
2313
+ feerate_per_kw, to_broadcaster_value, to_countersignatory_value) ) ;
2314
+
2315
+ #[ cfg( debug_assertions) ] {
2316
+ let rebuilt_commitment_tx = self . initial_counterparty_commitment_tx ( ) . unwrap ( ) ;
2317
+ debug_assert_eq ! ( rebuilt_commitment_tx. trust( ) . txid( ) , txid) ;
2318
+ }
2319
+
2320
+ self . provide_latest_counterparty_commitment_tx ( txid, htlc_outputs, commitment_number,
2321
+ their_per_commitment_point, logger) ;
2322
+ }
2323
+
2258
2324
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 {
2259
2325
// TODO: Encrypt the htlc_outputs data with the single-hash of the commitment transaction
2260
2326
// so that a remote monitor doesn't learn anything unless there is a malicious close.
@@ -2684,6 +2750,17 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
2684
2750
ret
2685
2751
}
2686
2752
2753
+ pub ( crate ) fn initial_counterparty_commitment_tx ( & mut self ) -> Option < CommitmentTransaction > {
2754
+ let ( their_per_commitment_point, feerate_per_kw, to_broadcaster_value,
2755
+ to_countersignatory_value) = self . initial_counterparty_commitment_info ?;
2756
+ let htlc_outputs = vec ! [ ] ;
2757
+
2758
+ let commitment_tx = self . build_counterparty_commitment_tx ( INITIAL_COMMITMENT_NUMBER ,
2759
+ & their_per_commitment_point, to_broadcaster_value, to_countersignatory_value,
2760
+ feerate_per_kw, htlc_outputs) ;
2761
+ Some ( commitment_tx)
2762
+ }
2763
+
2687
2764
fn build_counterparty_commitment_tx (
2688
2765
& self , commitment_number : u64 , their_per_commitment_point : & PublicKey ,
2689
2766
to_broadcaster_value : u64 , to_countersignatory_value : u64 , feerate_per_kw : u32 ,
@@ -4195,6 +4272,7 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
4195
4272
let mut confirmed_commitment_tx_counterparty_output = None ;
4196
4273
let mut spendable_txids_confirmed = Some ( Vec :: new ( ) ) ;
4197
4274
let mut counterparty_fulfilled_htlcs = Some ( HashMap :: new ( ) ) ;
4275
+ let mut initial_counterparty_commitment_info = None ;
4198
4276
read_tlv_fields ! ( reader, {
4199
4277
( 1 , funding_spend_confirmed, option) ,
4200
4278
( 3 , htlcs_resolved_on_chain, optional_vec) ,
@@ -4204,6 +4282,7 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
4204
4282
( 11 , confirmed_commitment_tx_counterparty_output, option) ,
4205
4283
( 13 , spendable_txids_confirmed, optional_vec) ,
4206
4284
( 15 , counterparty_fulfilled_htlcs, option) ,
4285
+ ( 17 , initial_counterparty_commitment_info, option) ,
4207
4286
} ) ;
4208
4287
4209
4288
Ok ( ( best_block. block_hash ( ) , ChannelMonitor :: from_impl ( ChannelMonitorImpl {
@@ -4259,6 +4338,7 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
4259
4338
4260
4339
best_block,
4261
4340
counterparty_node_id,
4341
+ initial_counterparty_commitment_info,
4262
4342
} ) ) )
4263
4343
}
4264
4344
}
0 commit comments