@@ -785,13 +785,6 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
785
785
Ok ( chan)
786
786
}
787
787
788
- // Utilities to derive keys:
789
-
790
- fn build_local_commitment_secret ( & self , idx : u64 ) -> SecretKey {
791
- let res = self . local_keys . commitment_secret ( idx) ;
792
- SecretKey :: from_slice ( & res) . unwrap ( )
793
- }
794
-
795
788
// Utilities to build transactions:
796
789
797
790
fn get_commitment_transaction_number_obscure_factor ( & self ) -> u64 {
@@ -1123,7 +1116,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
1123
1116
/// The result is a transaction which we can revoke ownership of (ie a "local" transaction)
1124
1117
/// TODO Some magic rust shit to compile-time check this?
1125
1118
fn build_local_transaction_keys ( & self , commitment_number : u64 ) -> Result < TxCreationKeys , ChannelError > {
1126
- let per_commitment_point = PublicKey :: from_secret_key ( & self . secp_ctx , & self . build_local_commitment_secret ( commitment_number ) ) ;
1119
+ let per_commitment_point = self . local_keys . get_per_commitment_point ( commitment_number , & self . secp_ctx ) ;
1127
1120
let delayed_payment_base = & self . local_keys . pubkeys ( ) . delayed_payment_basepoint ;
1128
1121
let htlc_basepoint = & self . local_keys . pubkeys ( ) . htlc_basepoint ;
1129
1122
let their_pubkeys = self . their_pubkeys . as_ref ( ) . unwrap ( ) ;
@@ -2028,8 +2021,8 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
2028
2021
}
2029
2022
}
2030
2023
2031
- let next_per_commitment_point = PublicKey :: from_secret_key ( & self . secp_ctx , & self . build_local_commitment_secret ( self . cur_local_commitment_transaction_number - 1 ) ) ;
2032
- let per_commitment_secret = self . local_keys . commitment_secret ( self . cur_local_commitment_transaction_number + 1 ) ;
2024
+ let next_per_commitment_point = self . local_keys . get_per_commitment_point ( self . cur_local_commitment_transaction_number - 1 , & self . secp_ctx ) ;
2025
+ let per_commitment_secret = self . local_keys . release_commitment_secret ( self . cur_local_commitment_transaction_number + 1 ) ;
2033
2026
2034
2027
// Update state now that we've passed all the can-fail calls...
2035
2028
let mut need_our_commitment = false ;
@@ -2614,8 +2607,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
2614
2607
let funding_locked = if self . monitor_pending_funding_locked {
2615
2608
assert ! ( !self . channel_outbound, "Funding transaction broadcast without FundingBroadcastSafe!" ) ;
2616
2609
self . monitor_pending_funding_locked = false ;
2617
- let next_per_commitment_secret = self . build_local_commitment_secret ( self . cur_local_commitment_transaction_number ) ;
2618
- let next_per_commitment_point = PublicKey :: from_secret_key ( & self . secp_ctx , & next_per_commitment_secret) ;
2610
+ let next_per_commitment_point = self . local_keys . get_per_commitment_point ( self . cur_local_commitment_transaction_number , & self . secp_ctx ) ;
2619
2611
Some ( msgs:: FundingLocked {
2620
2612
channel_id : self . channel_id ( ) ,
2621
2613
next_per_commitment_point : next_per_commitment_point,
@@ -2667,8 +2659,8 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
2667
2659
}
2668
2660
2669
2661
fn get_last_revoke_and_ack ( & self ) -> msgs:: RevokeAndACK {
2670
- let next_per_commitment_point = PublicKey :: from_secret_key ( & self . secp_ctx , & self . build_local_commitment_secret ( self . cur_local_commitment_transaction_number ) ) ;
2671
- let per_commitment_secret = self . local_keys . commitment_secret ( self . cur_local_commitment_transaction_number + 2 ) ;
2662
+ let next_per_commitment_point = self . local_keys . get_per_commitment_point ( self . cur_local_commitment_transaction_number , & self . secp_ctx ) ;
2663
+ let per_commitment_secret = self . local_keys . release_commitment_secret ( self . cur_local_commitment_transaction_number + 2 ) ;
2672
2664
msgs:: RevokeAndACK {
2673
2665
channel_id : self . channel_id ,
2674
2666
per_commitment_secret,
@@ -2751,7 +2743,10 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
2751
2743
if msg. next_remote_commitment_number > 0 {
2752
2744
match msg. data_loss_protect {
2753
2745
OptionalField :: Present ( ref data_loss) => {
2754
- if self . local_keys . commitment_secret ( INITIAL_COMMITMENT_NUMBER - msg. next_remote_commitment_number + 1 ) != data_loss. your_last_per_commitment_secret {
2746
+ let expected_point = self . local_keys . get_per_commitment_point ( INITIAL_COMMITMENT_NUMBER - msg. next_remote_commitment_number + 1 , & self . secp_ctx ) ;
2747
+ let given_secret = SecretKey :: from_slice ( & data_loss. your_last_per_commitment_secret )
2748
+ . map_err ( |_| ChannelError :: Close ( "Peer sent a garbage channel_reestablish with unparseable secret key" . to_owned ( ) ) ) ?;
2749
+ if expected_point != PublicKey :: from_secret_key ( & self . secp_ctx , & given_secret) {
2755
2750
return Err ( ChannelError :: Close ( "Peer sent a garbage channel_reestablish with secret key not matching the commitment height provided" . to_owned ( ) ) ) ;
2756
2751
}
2757
2752
if msg. next_remote_commitment_number > INITIAL_COMMITMENT_NUMBER - self . cur_local_commitment_transaction_number {
@@ -2787,8 +2782,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
2787
2782
}
2788
2783
2789
2784
// We have OurFundingLocked set!
2790
- let next_per_commitment_secret = self . build_local_commitment_secret ( self . cur_local_commitment_transaction_number ) ;
2791
- let next_per_commitment_point = PublicKey :: from_secret_key ( & self . secp_ctx , & next_per_commitment_secret) ;
2785
+ let next_per_commitment_point = self . local_keys . get_per_commitment_point ( self . cur_local_commitment_transaction_number , & self . secp_ctx ) ;
2792
2786
return Ok ( ( Some ( msgs:: FundingLocked {
2793
2787
channel_id : self . channel_id ( ) ,
2794
2788
next_per_commitment_point : next_per_commitment_point,
@@ -2818,8 +2812,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
2818
2812
2819
2813
let resend_funding_locked = if msg. next_local_commitment_number == 1 && INITIAL_COMMITMENT_NUMBER - self . cur_local_commitment_transaction_number == 1 {
2820
2814
// We should never have to worry about MonitorUpdateFailed resending FundingLocked
2821
- let next_per_commitment_secret = self . build_local_commitment_secret ( self . cur_local_commitment_transaction_number ) ;
2822
- let next_per_commitment_point = PublicKey :: from_secret_key ( & self . secp_ctx , & next_per_commitment_secret) ;
2815
+ let next_per_commitment_point = self . local_keys . get_per_commitment_point ( self . cur_local_commitment_transaction_number , & self . secp_ctx ) ;
2823
2816
Some ( msgs:: FundingLocked {
2824
2817
channel_id : self . channel_id ( ) ,
2825
2818
next_per_commitment_point : next_per_commitment_point,
@@ -3405,8 +3398,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
3405
3398
//a protocol oversight, but I assume I'm just missing something.
3406
3399
if need_commitment_update {
3407
3400
if self . channel_state & ( ChannelState :: MonitorUpdateFailed as u32 ) == 0 {
3408
- let next_per_commitment_secret = self . build_local_commitment_secret ( self . cur_local_commitment_transaction_number ) ;
3409
- let next_per_commitment_point = PublicKey :: from_secret_key ( & self . secp_ctx , & next_per_commitment_secret) ;
3401
+ let next_per_commitment_point = self . local_keys . get_per_commitment_point ( self . cur_local_commitment_transaction_number , & self . secp_ctx ) ;
3410
3402
return Ok ( ( Some ( msgs:: FundingLocked {
3411
3403
channel_id : self . channel_id ,
3412
3404
next_per_commitment_point : next_per_commitment_point,
@@ -3457,7 +3449,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
3457
3449
panic ! ( "Tried to send an open_channel for a channel that has already advanced" ) ;
3458
3450
}
3459
3451
3460
- let local_commitment_secret = self . build_local_commitment_secret ( self . cur_local_commitment_transaction_number ) ;
3452
+ let first_per_commitment_point = self . local_keys . get_per_commitment_point ( self . cur_local_commitment_transaction_number , & self . secp_ctx ) ;
3461
3453
let local_keys = self . local_keys . pubkeys ( ) ;
3462
3454
3463
3455
msgs:: OpenChannel {
@@ -3477,7 +3469,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
3477
3469
payment_point : local_keys. payment_point ,
3478
3470
delayed_payment_basepoint : local_keys. delayed_payment_basepoint ,
3479
3471
htlc_basepoint : local_keys. htlc_basepoint ,
3480
- first_per_commitment_point : PublicKey :: from_secret_key ( & self . secp_ctx , & local_commitment_secret ) ,
3472
+ first_per_commitment_point,
3481
3473
channel_flags : if self . config . announced_channel { 1 } else { 0 } ,
3482
3474
shutdown_scriptpubkey : OptionalField :: Present ( if self . config . commit_upfront_shutdown_pubkey { self . get_closing_scriptpubkey ( ) } else { Builder :: new ( ) . into_script ( ) } )
3483
3475
}
@@ -3494,7 +3486,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
3494
3486
panic ! ( "Tried to send an accept_channel for a channel that has already advanced" ) ;
3495
3487
}
3496
3488
3497
- let local_commitment_secret = self . build_local_commitment_secret ( self . cur_local_commitment_transaction_number ) ;
3489
+ let first_per_commitment_point = self . local_keys . get_per_commitment_point ( self . cur_local_commitment_transaction_number , & self . secp_ctx ) ;
3498
3490
let local_keys = self . local_keys . pubkeys ( ) ;
3499
3491
3500
3492
msgs:: AcceptChannel {
@@ -3511,7 +3503,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
3511
3503
payment_point : local_keys. payment_point ,
3512
3504
delayed_payment_basepoint : local_keys. delayed_payment_basepoint ,
3513
3505
htlc_basepoint : local_keys. htlc_basepoint ,
3514
- first_per_commitment_point : PublicKey :: from_secret_key ( & self . secp_ctx , & local_commitment_secret ) ,
3506
+ first_per_commitment_point,
3515
3507
shutdown_scriptpubkey : OptionalField :: Present ( if self . config . commit_upfront_shutdown_pubkey { self . get_closing_scriptpubkey ( ) } else { Builder :: new ( ) . into_script ( ) } )
3516
3508
}
3517
3509
}
0 commit comments