@@ -35,6 +35,7 @@ use crate::ln::chan_utils::{
3535 HTLCOutputInCommitment , HolderCommitmentTransaction ,
3636} ;
3737use crate :: ln:: msgs:: DecodeError ;
38+ use crate :: ln:: types:: ChannelId ;
3839use crate :: sign:: { ecdsa:: EcdsaChannelSigner , EntropySource , HTLCDescriptor , SignerProvider } ;
3940use crate :: util:: logger:: Logger ;
4041use crate :: util:: ser:: {
@@ -223,6 +224,7 @@ pub(crate) enum FeerateStrategy {
223224/// do RBF bumping if possible.
224225#[ derive( Clone ) ]
225226pub struct OnchainTxHandler < ChannelSigner : EcdsaChannelSigner > {
227+ channel_id : ChannelId ,
226228 channel_value_satoshis : u64 , // Deprecated as of 0.2.
227229 channel_keys_id : [ u8 ; 32 ] , // Deprecated as of 0.2.
228230 destination_script : ScriptBuf , // Deprecated as of 0.2.
@@ -285,7 +287,8 @@ impl<ChannelSigner: EcdsaChannelSigner> PartialEq for OnchainTxHandler<ChannelSi
285287 #[ rustfmt:: skip]
286288 fn eq ( & self , other : & Self ) -> bool {
287289 // `signer`, `secp_ctx`, and `pending_claim_events` are excluded on purpose.
288- self . channel_value_satoshis == other. channel_value_satoshis &&
290+ self . channel_id == other. channel_id &&
291+ self . channel_value_satoshis == other. channel_value_satoshis &&
289292 self . channel_keys_id == other. channel_keys_id &&
290293 self . destination_script == other. destination_script &&
291294 self . holder_commitment == other. holder_commitment &&
@@ -345,7 +348,9 @@ impl<ChannelSigner: EcdsaChannelSigner> OnchainTxHandler<ChannelSigner> {
345348 entry. write ( writer) ?;
346349 }
347350
348- write_tlv_fields ! ( writer, { } ) ;
351+ write_tlv_fields ! ( writer, {
352+ ( 1 , self . channel_id, required) ,
353+ } ) ;
349354 Ok ( ( ) )
350355 }
351356}
@@ -369,7 +374,7 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
369374 let prev_holder_commitment = Readable :: read ( reader) ?;
370375 let _prev_holder_htlc_sigs: Option < Vec < Option < ( usize , Signature ) > > > = Readable :: read ( reader) ?;
371376
372- let channel_parameters = ReadableArgs :: < Option < u64 > > :: read ( reader, Some ( channel_value_satoshis) ) ?;
377+ let channel_parameters: ChannelTransactionParameters = ReadableArgs :: < Option < u64 > > :: read ( reader, Some ( channel_value_satoshis) ) ?;
373378
374379 // Read the serialized signer bytes, but don't deserialize them, as we'll obtain our signer
375380 // by re-deriving the private key material.
@@ -421,12 +426,24 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
421426 }
422427 }
423428
424- read_tlv_fields ! ( reader, { } ) ;
429+ let mut channel_id = None ;
430+ read_tlv_fields ! ( reader, {
431+ ( 1 , channel_id, option) ,
432+ } ) ;
433+
434+ // For backwards compatibility with monitors serialized before channel_id was added,
435+ // we derive a channel_id from the funding outpoint if not present.
436+ let channel_id = channel_id. or_else ( || {
437+ channel_parameters. funding_outpoint
438+ . map ( |op| ChannelId :: v1_from_funding_outpoint ( op) )
439+ } ) // funding_outpoint must be known during intialization.
440+ . ok_or ( DecodeError :: InvalidValue ) ?;
425441
426442 let mut secp_ctx = Secp256k1 :: new ( ) ;
427443 secp_ctx. seeded_randomize ( & entropy_source. get_secure_random_bytes ( ) ) ;
428444
429445 Ok ( OnchainTxHandler {
446+ channel_id,
430447 channel_value_satoshis,
431448 channel_keys_id,
432449 destination_script,
@@ -446,11 +463,13 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
446463
447464impl < ChannelSigner : EcdsaChannelSigner > OnchainTxHandler < ChannelSigner > {
448465 pub ( crate ) fn new (
449- channel_value_satoshis : u64 , channel_keys_id : [ u8 ; 32 ] , destination_script : ScriptBuf ,
450- signer : ChannelSigner , channel_parameters : ChannelTransactionParameters ,
466+ channel_id : ChannelId , channel_value_satoshis : u64 , channel_keys_id : [ u8 ; 32 ] ,
467+ destination_script : ScriptBuf , signer : ChannelSigner ,
468+ channel_parameters : ChannelTransactionParameters ,
451469 holder_commitment : HolderCommitmentTransaction , secp_ctx : Secp256k1 < secp256k1:: All > ,
452470 ) -> Self {
453471 OnchainTxHandler {
472+ channel_id,
454473 channel_value_satoshis,
455474 channel_keys_id,
456475 destination_script,
@@ -518,7 +537,7 @@ impl<ChannelSigner: EcdsaChannelSigner> OnchainTxHandler<ChannelSigner> {
518537 if tx. is_fully_signed ( ) {
519538 let log_start = if feerate_was_bumped { "Broadcasting RBF-bumped" } else { "Rebroadcasting" } ;
520539 log_info ! ( logger, "{} onchain {}" , log_start, log_tx!( tx. 0 ) ) ;
521- broadcaster. broadcast_transactions ( & [ ( & tx. 0 , BroadcastType :: Claim ) ] ) ;
540+ broadcaster. broadcast_transactions ( & [ ( & tx. 0 , BroadcastType :: Claim { channel_id : self . channel_id } ) ] ) ;
522541 } else {
523542 log_info ! ( logger, "Waiting for signature of unsigned onchain transaction {}" , tx. 0 . compute_txid( ) ) ;
524543 }
@@ -865,7 +884,7 @@ impl<ChannelSigner: EcdsaChannelSigner> OnchainTxHandler<ChannelSigner> {
865884 OnchainClaim :: Tx ( tx) => {
866885 if tx. is_fully_signed ( ) {
867886 log_info ! ( logger, "Broadcasting onchain {}" , log_tx!( tx. 0 ) ) ;
868- broadcaster. broadcast_transactions ( & [ ( & tx. 0 , BroadcastType :: Claim ) ] ) ;
887+ broadcaster. broadcast_transactions ( & [ ( & tx. 0 , BroadcastType :: Claim { channel_id : self . channel_id } ) ] ) ;
869888 } else {
870889 log_info ! ( logger, "Waiting for signature of unsigned onchain transaction {}" , tx. 0 . compute_txid( ) ) ;
871890 }
@@ -1086,7 +1105,7 @@ impl<ChannelSigner: EcdsaChannelSigner> OnchainTxHandler<ChannelSigner> {
10861105 OnchainClaim :: Tx ( bump_tx) => {
10871106 if bump_tx. is_fully_signed ( ) {
10881107 log_info ! ( logger, "Broadcasting RBF-bumped onchain {}" , log_tx!( bump_tx. 0 ) ) ;
1089- broadcaster. broadcast_transactions ( & [ ( & bump_tx. 0 , BroadcastType :: Claim ) ] ) ;
1108+ broadcaster. broadcast_transactions ( & [ ( & bump_tx. 0 , BroadcastType :: Claim { channel_id : self . channel_id } ) ] ) ;
10901109 } else {
10911110 log_info ! ( logger, "Waiting for signature of RBF-bumped unsigned onchain transaction {}" ,
10921111 bump_tx. 0 . compute_txid( ) ) ;
@@ -1189,7 +1208,7 @@ impl<ChannelSigner: EcdsaChannelSigner> OnchainTxHandler<ChannelSigner> {
11891208 OnchainClaim :: Tx ( bump_tx) => {
11901209 if bump_tx. is_fully_signed ( ) {
11911210 log_info ! ( logger, "Broadcasting onchain {}" , log_tx!( bump_tx. 0 ) ) ;
1192- broadcaster. broadcast_transactions ( & [ ( & bump_tx. 0 , BroadcastType :: Claim ) ] ) ;
1211+ broadcaster. broadcast_transactions ( & [ ( & bump_tx. 0 , BroadcastType :: Claim { channel_id : self . channel_id } ) ] ) ;
11931212 } else {
11941213 log_info ! ( logger, "Waiting for signature of unsigned onchain transaction {}" , bump_tx. 0 . compute_txid( ) ) ;
11951214 }
@@ -1283,6 +1302,7 @@ mod tests {
12831302 } ;
12841303 use crate :: ln:: channel_keys:: { DelayedPaymentBasepoint , HtlcBasepoint , RevocationBasepoint } ;
12851304 use crate :: ln:: functional_test_utils:: create_dummy_block;
1305+ use crate :: ln:: types:: ChannelId ;
12861306 use crate :: sign:: { ChannelDerivationParameters , ChannelSigner , HTLCDescriptor , InMemorySigner } ;
12871307 use crate :: types:: payment:: { PaymentHash , PaymentPreimage } ;
12881308 use crate :: util:: test_utils:: { TestBroadcaster , TestFeeEstimator , TestLogger } ;
@@ -1367,6 +1387,7 @@ mod tests {
13671387 let holder_commit = HolderCommitmentTransaction :: dummy ( 1000000 , funding_outpoint, nondust_htlcs) ;
13681388 let destination_script = ScriptBuf :: new ( ) ;
13691389 let mut tx_handler = OnchainTxHandler :: new (
1390+ ChannelId :: from_bytes ( [ 0 ; 32 ] ) ,
13701391 1000000 ,
13711392 [ 0 ; 32 ] ,
13721393 destination_script. clone ( ) ,
0 commit comments