@@ -51,6 +51,7 @@ use core::ops::Deref;
51
51
#[ cfg( any( test, fuzzing, debug_assertions) ) ]
52
52
use crate :: sync:: Mutex ;
53
53
use bitcoin:: hashes:: hex:: ToHex ;
54
+ use crate :: sign:: type_resolver:: ChannelSignerType ;
54
55
55
56
#[ cfg( test) ]
56
57
pub struct ChannelValueStat {
@@ -534,7 +535,7 @@ pub(super) struct Channel<Signer: ChannelSigner> {
534
535
535
536
latest_monitor_update_id : u64 ,
536
537
537
- holder_signer : Signer ,
538
+ holder_signer : ChannelSignerType < Signer > ,
538
539
shutdown_scriptpubkey : Option < ShutdownScript > ,
539
540
destination_script : Script ,
540
541
@@ -1042,7 +1043,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
1042
1043
1043
1044
latest_monitor_update_id : 0 ,
1044
1045
1045
- holder_signer,
1046
+ holder_signer : ChannelSignerType :: Ecdsa ( holder_signer ) ,
1046
1047
shutdown_scriptpubkey,
1047
1048
destination_script,
1048
1049
@@ -1397,7 +1398,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
1397
1398
1398
1399
latest_monitor_update_id : 0 ,
1399
1400
1400
- holder_signer,
1401
+ holder_signer : ChannelSignerType :: Ecdsa ( holder_signer ) ,
1401
1402
shutdown_scriptpubkey,
1402
1403
destination_script,
1403
1404
@@ -1836,7 +1837,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
1836
1837
/// The result is a transaction which we can revoke broadcastership of (ie a "local" transaction)
1837
1838
/// TODO Some magic rust shit to compile-time check this?
1838
1839
fn build_holder_transaction_keys ( & self , commitment_number : u64 ) -> TxCreationKeys {
1839
- let per_commitment_point = self . holder_signer . get_per_commitment_point ( commitment_number, & self . secp_ctx ) ;
1840
+ let per_commitment_point = self . holder_signer . as_ref ( ) . get_per_commitment_point ( commitment_number, & self . secp_ctx ) ;
1840
1841
let delayed_payment_base = & self . get_holder_pubkeys ( ) . delayed_payment_basepoint ;
1841
1842
let htlc_basepoint = & self . get_holder_pubkeys ( ) . htlc_basepoint ;
1842
1843
let counterparty_pubkeys = self . get_counterparty_pubkeys ( ) ;
@@ -2323,7 +2324,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
2323
2324
log_trace ! ( logger, "Initial counterparty tx for channel {} is: txid {} tx {}" ,
2324
2325
log_bytes!( self . channel_id( ) ) , counterparty_initial_bitcoin_tx. txid, encode:: serialize_hex( & counterparty_initial_bitcoin_tx. transaction) ) ;
2325
2326
2326
- let counterparty_signature = self . holder_signer . sign_counterparty_commitment ( & counterparty_initial_commitment_tx, Vec :: new ( ) , & self . secp_ctx )
2327
+ let counterparty_signature = self . holder_signer . as_ecdsa ( ) . sign_counterparty_commitment ( & counterparty_initial_commitment_tx, Vec :: new ( ) , & self . secp_ctx )
2327
2328
. map_err ( |_| ChannelError :: Close ( "Failed to get signatures for new commitment_signed" . to_owned ( ) ) ) ?. 0 ;
2328
2329
2329
2330
// We sign "counterparty" commitment transaction, allowing them to broadcast the tx if they wish.
@@ -2363,7 +2364,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
2363
2364
self . channel_transaction_parameters . funding_outpoint = Some ( funding_txo) ;
2364
2365
// This is an externally observable change before we finish all our checks. In particular
2365
2366
// funding_created_signature may fail.
2366
- self . holder_signer . provide_channel_parameters ( & self . channel_transaction_parameters ) ;
2367
+ self . holder_signer . as_mut ( ) . provide_channel_parameters ( & self . channel_transaction_parameters ) ;
2367
2368
2368
2369
let ( counterparty_initial_commitment_txid, initial_commitment_tx, signature) = match self . funding_created_signature ( & msg. signature , logger) {
2369
2370
Ok ( res) => res,
@@ -2386,7 +2387,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
2386
2387
self . counterparty_funding_pubkey ( )
2387
2388
) ;
2388
2389
2389
- self . holder_signer . validate_holder_commitment ( & holder_commitment_tx, Vec :: new ( ) )
2390
+ self . holder_signer . as_ref ( ) . validate_holder_commitment ( & holder_commitment_tx, Vec :: new ( ) )
2390
2391
. map_err ( |_| ChannelError :: Close ( "Failed to validate our commitment" . to_owned ( ) ) ) ?;
2391
2392
2392
2393
// Now that we're past error-generating stuff, update our local state:
@@ -2476,7 +2477,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
2476
2477
self . counterparty_funding_pubkey ( )
2477
2478
) ;
2478
2479
2479
- self . holder_signer . validate_holder_commitment ( & holder_commitment_tx, Vec :: new ( ) )
2480
+ self . holder_signer . as_ref ( ) . validate_holder_commitment ( & holder_commitment_tx, Vec :: new ( ) )
2480
2481
. map_err ( |_| ChannelError :: Close ( "Failed to validate our commitment" . to_owned ( ) ) ) ?;
2481
2482
2482
2483
@@ -3245,7 +3246,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3245
3246
self . counterparty_funding_pubkey ( )
3246
3247
) ;
3247
3248
3248
- self . holder_signer . validate_holder_commitment ( & holder_commitment_tx, commitment_stats. preimages )
3249
+ self . holder_signer . as_ref ( ) . validate_holder_commitment ( & holder_commitment_tx, commitment_stats. preimages )
3249
3250
. map_err ( |_| ChannelError :: Close ( "Failed to validate our commitment" . to_owned ( ) ) ) ?;
3250
3251
3251
3252
// Update state now that we've passed all the can-fail calls...
@@ -3505,7 +3506,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3505
3506
* self . next_remote_commitment_tx_fee_info_cached . lock ( ) . unwrap ( ) = None ;
3506
3507
}
3507
3508
3508
- self . holder_signer . validate_counterparty_revocation (
3509
+ self . holder_signer . as_ecdsa ( ) . validate_counterparty_revocation (
3509
3510
self . cur_counterparty_commitment_transaction_number + 1 ,
3510
3511
& secret
3511
3512
) . map_err ( |_| ChannelError :: Close ( "Failed to validate revocation from peer" . to_owned ( ) ) ) ?;
@@ -3913,7 +3914,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3913
3914
assert ! ( !self . is_outbound( ) || self . minimum_depth == Some ( 0 ) ,
3914
3915
"Funding transaction broadcast by the local client before it should have - LDK didn't do it!" ) ;
3915
3916
self . monitor_pending_channel_ready = false ;
3916
- let next_per_commitment_point = self . holder_signer . get_per_commitment_point ( self . cur_holder_commitment_transaction_number , & self . secp_ctx ) ;
3917
+ let next_per_commitment_point = self . holder_signer . as_ref ( ) . get_per_commitment_point ( self . cur_holder_commitment_transaction_number , & self . secp_ctx ) ;
3917
3918
Some ( msgs:: ChannelReady {
3918
3919
channel_id : self . channel_id ( ) ,
3919
3920
next_per_commitment_point,
@@ -3993,8 +3994,8 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
3993
3994
}
3994
3995
3995
3996
fn get_last_revoke_and_ack ( & self ) -> msgs:: RevokeAndACK {
3996
- let next_per_commitment_point = self . holder_signer . get_per_commitment_point ( self . cur_holder_commitment_transaction_number , & self . secp_ctx ) ;
3997
- let per_commitment_secret = self . holder_signer . release_commitment_secret ( self . cur_holder_commitment_transaction_number + 2 ) ;
3997
+ let next_per_commitment_point = self . holder_signer . as_ref ( ) . get_per_commitment_point ( self . cur_holder_commitment_transaction_number , & self . secp_ctx ) ;
3998
+ let per_commitment_secret = self . holder_signer . as_ref ( ) . release_commitment_secret ( self . cur_holder_commitment_transaction_number + 2 ) ;
3998
3999
msgs:: RevokeAndACK {
3999
4000
channel_id : self . channel_id ,
4000
4001
per_commitment_secret,
@@ -4096,7 +4097,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
4096
4097
}
4097
4098
4098
4099
if msg. next_remote_commitment_number > 0 {
4099
- let expected_point = self . holder_signer . get_per_commitment_point ( INITIAL_COMMITMENT_NUMBER - msg. next_remote_commitment_number + 1 , & self . secp_ctx ) ;
4100
+ let expected_point = self . holder_signer . as_ref ( ) . get_per_commitment_point ( INITIAL_COMMITMENT_NUMBER - msg. next_remote_commitment_number + 1 , & self . secp_ctx ) ;
4100
4101
let given_secret = SecretKey :: from_slice ( & msg. your_last_per_commitment_secret )
4101
4102
. map_err ( |_| ChannelError :: Close ( "Peer sent a garbage channel_reestablish with unparseable secret key" . to_owned ( ) ) ) ?;
4102
4103
if expected_point != PublicKey :: from_secret_key ( & self . secp_ctx , & given_secret) {
@@ -4160,7 +4161,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
4160
4161
}
4161
4162
4162
4163
// We have OurChannelReady set!
4163
- let next_per_commitment_point = self . holder_signer . get_per_commitment_point ( self . cur_holder_commitment_transaction_number , & self . secp_ctx ) ;
4164
+ let next_per_commitment_point = self . holder_signer . as_ref ( ) . get_per_commitment_point ( self . cur_holder_commitment_transaction_number , & self . secp_ctx ) ;
4164
4165
return Ok ( ReestablishResponses {
4165
4166
channel_ready : Some ( msgs:: ChannelReady {
4166
4167
channel_id : self . channel_id ( ) ,
@@ -4196,7 +4197,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
4196
4197
4197
4198
let channel_ready = if msg. next_local_commitment_number == 1 && INITIAL_COMMITMENT_NUMBER - self . cur_holder_commitment_transaction_number == 1 {
4198
4199
// We should never have to worry about MonitorUpdateInProgress resending ChannelReady
4199
- let next_per_commitment_point = self . holder_signer . get_per_commitment_point ( self . cur_holder_commitment_transaction_number , & self . secp_ctx ) ;
4200
+ let next_per_commitment_point = self . holder_signer . as_ref ( ) . get_per_commitment_point ( self . cur_holder_commitment_transaction_number , & self . secp_ctx ) ;
4200
4201
Some ( msgs:: ChannelReady {
4201
4202
channel_id : self . channel_id ( ) ,
4202
4203
next_per_commitment_point,
@@ -4345,7 +4346,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
4345
4346
log_trace ! ( logger, "Proposing initial closing_signed for our counterparty with a fee range of {}-{} sat (with initial proposal {} sats)" ,
4346
4347
our_min_fee, our_max_fee, total_fee_satoshis) ;
4347
4348
4348
- let sig = self . holder_signer
4349
+ let sig = self . holder_signer . as_ecdsa ( )
4349
4350
. sign_closing_transaction ( & closing_tx, & self . secp_ctx )
4350
4351
. map_err ( |( ) | ChannelError :: Close ( "Failed to get signature for closing transaction." . to_owned ( ) ) ) ?;
4351
4352
@@ -4555,7 +4556,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
4555
4556
self . build_closing_transaction( $new_fee, false )
4556
4557
} ;
4557
4558
4558
- let sig = self . holder_signer
4559
+ let sig = self . holder_signer. as_ecdsa ( )
4559
4560
. sign_closing_transaction( & closing_tx, & self . secp_ctx)
4560
4561
. map_err( |_| ChannelError :: Close ( "External signer refused to sign closing transaction" . to_owned( ) ) ) ?;
4561
4562
@@ -4927,7 +4928,8 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
4927
4928
4928
4929
#[ cfg( test) ]
4929
4930
pub fn get_signer ( & self ) -> & Signer {
4930
- & self . holder_signer
4931
+ // the Signer parameterization will only ever be used for the ECDSA signer
4932
+ self . holder_signer . as_ecdsa ( )
4931
4933
}
4932
4934
4933
4935
#[ cfg( test) ]
@@ -5170,7 +5172,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
5170
5172
if self . channel_state & ( ChannelState :: MonitorUpdateInProgress as u32 ) == 0 {
5171
5173
if self . channel_state & ( ChannelState :: PeerDisconnected as u32 ) == 0 {
5172
5174
let next_per_commitment_point =
5173
- self . holder_signer . get_per_commitment_point ( INITIAL_COMMITMENT_NUMBER - 1 , & self . secp_ctx ) ;
5175
+ self . holder_signer . as_ref ( ) . get_per_commitment_point ( INITIAL_COMMITMENT_NUMBER - 1 , & self . secp_ctx ) ;
5174
5176
return Some ( msgs:: ChannelReady {
5175
5177
channel_id : self . channel_id ,
5176
5178
next_per_commitment_point,
@@ -5394,7 +5396,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
5394
5396
panic ! ( "Tried to send an open_channel for a channel that has already advanced" ) ;
5395
5397
}
5396
5398
5397
- let first_per_commitment_point = self . holder_signer . get_per_commitment_point ( self . cur_holder_commitment_transaction_number , & self . secp_ctx ) ;
5399
+ let first_per_commitment_point = self . holder_signer . as_ref ( ) . get_per_commitment_point ( self . cur_holder_commitment_transaction_number , & self . secp_ctx ) ;
5398
5400
let keys = self . get_holder_pubkeys ( ) ;
5399
5401
5400
5402
msgs:: OpenChannel {
@@ -5464,7 +5466,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
5464
5466
///
5465
5467
/// [`msgs::AcceptChannel`]: crate::ln::msgs::AcceptChannel
5466
5468
fn generate_accept_channel_message ( & self ) -> msgs:: AcceptChannel {
5467
- let first_per_commitment_point = self . holder_signer . get_per_commitment_point ( self . cur_holder_commitment_transaction_number , & self . secp_ctx ) ;
5469
+ let first_per_commitment_point = self . holder_signer . as_ref ( ) . get_per_commitment_point ( self . cur_holder_commitment_transaction_number , & self . secp_ctx ) ;
5468
5470
let keys = self . get_holder_pubkeys ( ) ;
5469
5471
5470
5472
msgs:: AcceptChannel {
@@ -5505,7 +5507,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
5505
5507
fn get_outbound_funding_created_signature < L : Deref > ( & mut self , logger : & L ) -> Result < Signature , ChannelError > where L :: Target : Logger {
5506
5508
let counterparty_keys = self . build_remote_transaction_keys ( ) ;
5507
5509
let counterparty_initial_commitment_tx = self . build_commitment_transaction ( self . cur_counterparty_commitment_transaction_number , & counterparty_keys, false , false , logger) . tx ;
5508
- Ok ( self . holder_signer . sign_counterparty_commitment ( & counterparty_initial_commitment_tx, Vec :: new ( ) , & self . secp_ctx )
5510
+ Ok ( self . holder_signer . as_ecdsa ( ) . sign_counterparty_commitment ( & counterparty_initial_commitment_tx, Vec :: new ( ) , & self . secp_ctx )
5509
5511
. map_err ( |_| ChannelError :: Close ( "Failed to get signatures for new commitment_signed" . to_owned ( ) ) ) ?. 0 )
5510
5512
}
5511
5513
@@ -5530,7 +5532,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
5530
5532
}
5531
5533
5532
5534
self . channel_transaction_parameters . funding_outpoint = Some ( funding_txo) ;
5533
- self . holder_signer . provide_channel_parameters ( & self . channel_transaction_parameters ) ;
5535
+ self . holder_signer . as_mut ( ) . provide_channel_parameters ( & self . channel_transaction_parameters ) ;
5534
5536
5535
5537
let signature = match self . get_outbound_funding_created_signature ( logger) {
5536
5538
Ok ( res) => res,
@@ -5639,7 +5641,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
5639
5641
} ,
5640
5642
Ok ( v) => v
5641
5643
} ;
5642
- let our_bitcoin_sig = match self . holder_signer . sign_channel_announcement_with_funding_key ( & announcement, & self . secp_ctx ) {
5644
+ let our_bitcoin_sig = match self . holder_signer . as_ecdsa ( ) . sign_channel_announcement_with_funding_key ( & announcement, & self . secp_ctx ) {
5643
5645
Err ( _) => {
5644
5646
log_error ! ( logger, "Signer rejected channel_announcement signing. Channel will not be announced!" ) ;
5645
5647
return None ;
@@ -5668,7 +5670,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
5668
5670
5669
5671
let our_node_sig = node_signer. sign_gossip_message ( msgs:: UnsignedGossipMessage :: ChannelAnnouncement ( & announcement) )
5670
5672
. map_err ( |_| ChannelError :: Ignore ( "Failed to generate node signature for channel_announcement" . to_owned ( ) ) ) ?;
5671
- let our_bitcoin_sig = self . holder_signer . sign_channel_announcement_with_funding_key ( & announcement, & self . secp_ctx )
5673
+ let our_bitcoin_sig = self . holder_signer . as_ecdsa ( ) . sign_channel_announcement_with_funding_key ( & announcement, & self . secp_ctx )
5672
5674
. map_err ( |_| ChannelError :: Ignore ( "Signer rejected channel_announcement" . to_owned ( ) ) ) ?;
5673
5675
Ok ( msgs:: ChannelAnnouncement {
5674
5676
node_signature_1 : if were_node_one { our_node_sig } else { their_node_sig } ,
@@ -6048,7 +6050,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
6048
6050
htlcs. push ( htlc) ;
6049
6051
}
6050
6052
6051
- let res = self . holder_signer . sign_counterparty_commitment ( & commitment_stats. tx , commitment_stats. preimages , & self . secp_ctx )
6053
+ let res = self . holder_signer . as_ecdsa ( ) . sign_counterparty_commitment ( & commitment_stats. tx , commitment_stats. preimages , & self . secp_ctx )
6052
6054
. map_err ( |_| ChannelError :: Close ( "Failed to get signatures for new commitment_signed" . to_owned ( ) ) ) ?;
6053
6055
signature = res. 0 ;
6054
6056
htlc_signatures = res. 1 ;
@@ -6360,7 +6362,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Writeable for Channel<Signer> {
6360
6362
self . latest_monitor_update_id . write ( writer) ?;
6361
6363
6362
6364
let mut key_data = VecWriter ( Vec :: new ( ) ) ;
6363
- self . holder_signer . write ( & mut key_data) ?;
6365
+ self . holder_signer . as_ecdsa ( ) . write ( & mut key_data) ?;
6364
6366
assert ! ( key_data. 0 . len( ) < core:: usize :: MAX ) ;
6365
6367
assert ! ( key_data. 0 . len( ) < core:: u32 :: MAX as usize ) ;
6366
6368
( key_data. 0 . len ( ) as u32 ) . write ( writer) ?;
@@ -7005,7 +7007,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
7005
7007
7006
7008
latest_monitor_update_id,
7007
7009
7008
- holder_signer,
7010
+ holder_signer : ChannelSignerType :: Ecdsa ( holder_signer ) ,
7009
7011
shutdown_scriptpubkey,
7010
7012
destination_script,
7011
7013
@@ -7688,10 +7690,10 @@ mod tests {
7688
7690
// We can't just use build_holder_transaction_keys here as the per_commitment_secret is not
7689
7691
// derived from a commitment_seed, so instead we copy it here and call
7690
7692
// build_commitment_transaction.
7691
- let delayed_payment_base = & chan. holder_signer . pubkeys ( ) . delayed_payment_basepoint ;
7693
+ let delayed_payment_base = & chan. holder_signer . as_ref ( ) . pubkeys ( ) . delayed_payment_basepoint ;
7692
7694
let per_commitment_secret = SecretKey :: from_slice ( & hex:: decode ( "1f1e1d1c1b1a191817161514131211100f0e0d0c0b0a09080706050403020100" ) . unwrap ( ) [ ..] ) . unwrap ( ) ;
7693
7695
let per_commitment_point = PublicKey :: from_secret_key ( & secp_ctx, & per_commitment_secret) ;
7694
- let htlc_basepoint = & chan. holder_signer . pubkeys ( ) . htlc_basepoint ;
7696
+ let htlc_basepoint = & chan. holder_signer . as_ref ( ) . pubkeys ( ) . htlc_basepoint ;
7695
7697
let keys = TxCreationKeys :: derive_new ( & secp_ctx, & per_commitment_point, delayed_payment_base, htlc_basepoint, & counterparty_pubkeys. revocation_basepoint , & counterparty_pubkeys. htlc_basepoint ) ;
7696
7698
7697
7699
macro_rules! test_commitment {
@@ -7743,7 +7745,7 @@ mod tests {
7743
7745
commitment_tx. clone( ) ,
7744
7746
counterparty_signature,
7745
7747
counterparty_htlc_sigs,
7746
- & chan. holder_signer. pubkeys( ) . funding_pubkey,
7748
+ chan. holder_signer. as_ref ( ) . pubkeys( ) . funding_pubkey,
7747
7749
chan. counterparty_funding_pubkey( )
7748
7750
) ;
7749
7751
let ( holder_sig, htlc_sigs) = signer. sign_holder_commitment_and_htlcs( & holder_commitment_tx, & secp_ctx) . unwrap( ) ;
0 commit comments