@@ -780,13 +780,6 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
780
780
Ok ( chan)
781
781
}
782
782
783
- // Utilities to derive keys:
784
-
785
- fn build_local_commitment_secret ( & self , idx : u64 ) -> SecretKey {
786
- let res = self . local_keys . commitment_secret ( idx) ;
787
- SecretKey :: from_slice ( & res) . unwrap ( )
788
- }
789
-
790
783
// Utilities to build transactions:
791
784
792
785
fn get_commitment_transaction_number_obscure_factor ( & self ) -> u64 {
@@ -1118,7 +1111,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
1118
1111
/// The result is a transaction which we can revoke ownership of (ie a "local" transaction)
1119
1112
/// TODO Some magic rust shit to compile-time check this?
1120
1113
fn build_local_transaction_keys ( & self , commitment_number : u64 ) -> Result < TxCreationKeys , ChannelError > {
1121
- let per_commitment_point = PublicKey :: from_secret_key ( & self . secp_ctx , & self . build_local_commitment_secret ( commitment_number ) ) ;
1114
+ let per_commitment_point = self . local_keys . get_per_commitment_point ( commitment_number , & self . secp_ctx ) ;
1122
1115
let delayed_payment_base = & self . local_keys . pubkeys ( ) . delayed_payment_basepoint ;
1123
1116
let htlc_basepoint = & self . local_keys . pubkeys ( ) . htlc_basepoint ;
1124
1117
let their_pubkeys = self . their_pubkeys . as_ref ( ) . unwrap ( ) ;
@@ -2020,8 +2013,8 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
2020
2013
}
2021
2014
}
2022
2015
2023
- let next_per_commitment_point = PublicKey :: from_secret_key ( & self . secp_ctx , & self . build_local_commitment_secret ( self . cur_local_commitment_transaction_number - 1 ) ) ;
2024
- let per_commitment_secret = self . local_keys . commitment_secret ( self . cur_local_commitment_transaction_number + 1 ) ;
2016
+ let next_per_commitment_point = self . local_keys . get_per_commitment_point ( self . cur_local_commitment_transaction_number - 1 , & self . secp_ctx ) ;
2017
+ let per_commitment_secret = self . local_keys . release_commitment_secret ( self . cur_local_commitment_transaction_number + 1 ) ;
2025
2018
2026
2019
// Update state now that we've passed all the can-fail calls...
2027
2020
let mut need_our_commitment = false ;
@@ -2606,8 +2599,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
2606
2599
let funding_locked = if self . monitor_pending_funding_locked {
2607
2600
assert ! ( !self . channel_outbound, "Funding transaction broadcast without FundingBroadcastSafe!" ) ;
2608
2601
self . monitor_pending_funding_locked = false ;
2609
- let next_per_commitment_secret = self . build_local_commitment_secret ( self . cur_local_commitment_transaction_number ) ;
2610
- let next_per_commitment_point = PublicKey :: from_secret_key ( & self . secp_ctx , & next_per_commitment_secret) ;
2602
+ let next_per_commitment_point = self . local_keys . get_per_commitment_point ( self . cur_local_commitment_transaction_number , & self . secp_ctx ) ;
2611
2603
Some ( msgs:: FundingLocked {
2612
2604
channel_id : self . channel_id ( ) ,
2613
2605
next_per_commitment_point : next_per_commitment_point,
@@ -2659,8 +2651,8 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
2659
2651
}
2660
2652
2661
2653
fn get_last_revoke_and_ack ( & self ) -> msgs:: RevokeAndACK {
2662
- let next_per_commitment_point = PublicKey :: from_secret_key ( & self . secp_ctx , & self . build_local_commitment_secret ( self . cur_local_commitment_transaction_number ) ) ;
2663
- let per_commitment_secret = self . local_keys . commitment_secret ( self . cur_local_commitment_transaction_number + 2 ) ;
2654
+ let next_per_commitment_point = self . local_keys . get_per_commitment_point ( self . cur_local_commitment_transaction_number , & self . secp_ctx ) ;
2655
+ let per_commitment_secret = self . local_keys . release_commitment_secret ( self . cur_local_commitment_transaction_number + 2 ) ;
2664
2656
msgs:: RevokeAndACK {
2665
2657
channel_id : self . channel_id ,
2666
2658
per_commitment_secret,
@@ -2743,7 +2735,10 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
2743
2735
if msg. next_remote_commitment_number > 0 {
2744
2736
match msg. data_loss_protect {
2745
2737
OptionalField :: Present ( ref data_loss) => {
2746
- if self . local_keys . commitment_secret ( INITIAL_COMMITMENT_NUMBER - msg. next_remote_commitment_number + 1 ) != data_loss. your_last_per_commitment_secret {
2738
+ let expected_point = self . local_keys . get_per_commitment_point ( INITIAL_COMMITMENT_NUMBER - msg. next_remote_commitment_number + 1 , & self . secp_ctx ) ;
2739
+ let given_secret = SecretKey :: from_slice ( & data_loss. your_last_per_commitment_secret )
2740
+ . map_err ( |_| ChannelError :: Close ( "Peer sent a garbage channel_reestablish with unparseable secret key" ) ) ?;
2741
+ if expected_point != PublicKey :: from_secret_key ( & self . secp_ctx , & given_secret) {
2747
2742
return Err ( ChannelError :: Close ( "Peer sent a garbage channel_reestablish with secret key not matching the commitment height provided" ) ) ;
2748
2743
}
2749
2744
if msg. next_remote_commitment_number > INITIAL_COMMITMENT_NUMBER - self . cur_local_commitment_transaction_number {
@@ -2779,8 +2774,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
2779
2774
}
2780
2775
2781
2776
// We have OurFundingLocked set!
2782
- let next_per_commitment_secret = self . build_local_commitment_secret ( self . cur_local_commitment_transaction_number ) ;
2783
- let next_per_commitment_point = PublicKey :: from_secret_key ( & self . secp_ctx , & next_per_commitment_secret) ;
2777
+ let next_per_commitment_point = self . local_keys . get_per_commitment_point ( self . cur_local_commitment_transaction_number , & self . secp_ctx ) ;
2784
2778
return Ok ( ( Some ( msgs:: FundingLocked {
2785
2779
channel_id : self . channel_id ( ) ,
2786
2780
next_per_commitment_point : next_per_commitment_point,
@@ -2810,8 +2804,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
2810
2804
2811
2805
let resend_funding_locked = if msg. next_local_commitment_number == 1 && INITIAL_COMMITMENT_NUMBER - self . cur_local_commitment_transaction_number == 1 {
2812
2806
// We should never have to worry about MonitorUpdateFailed resending FundingLocked
2813
- let next_per_commitment_secret = self . build_local_commitment_secret ( self . cur_local_commitment_transaction_number ) ;
2814
- let next_per_commitment_point = PublicKey :: from_secret_key ( & self . secp_ctx , & next_per_commitment_secret) ;
2807
+ let next_per_commitment_point = self . local_keys . get_per_commitment_point ( self . cur_local_commitment_transaction_number , & self . secp_ctx ) ;
2815
2808
Some ( msgs:: FundingLocked {
2816
2809
channel_id : self . channel_id ( ) ,
2817
2810
next_per_commitment_point : next_per_commitment_point,
@@ -3397,8 +3390,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
3397
3390
//a protocol oversight, but I assume I'm just missing something.
3398
3391
if need_commitment_update {
3399
3392
if self . channel_state & ( ChannelState :: MonitorUpdateFailed as u32 ) == 0 {
3400
- let next_per_commitment_secret = self . build_local_commitment_secret ( self . cur_local_commitment_transaction_number ) ;
3401
- let next_per_commitment_point = PublicKey :: from_secret_key ( & self . secp_ctx , & next_per_commitment_secret) ;
3393
+ let next_per_commitment_point = self . local_keys . get_per_commitment_point ( self . cur_local_commitment_transaction_number , & self . secp_ctx ) ;
3402
3394
return Ok ( ( Some ( msgs:: FundingLocked {
3403
3395
channel_id : self . channel_id ,
3404
3396
next_per_commitment_point : next_per_commitment_point,
@@ -3449,7 +3441,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
3449
3441
panic ! ( "Tried to send an open_channel for a channel that has already advanced" ) ;
3450
3442
}
3451
3443
3452
- let local_commitment_secret = self . build_local_commitment_secret ( self . cur_local_commitment_transaction_number ) ;
3444
+ let first_per_commitment_point = self . local_keys . get_per_commitment_point ( self . cur_local_commitment_transaction_number , & self . secp_ctx ) ;
3453
3445
let local_keys = self . local_keys . pubkeys ( ) ;
3454
3446
3455
3447
msgs:: OpenChannel {
@@ -3469,7 +3461,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
3469
3461
payment_point : local_keys. payment_point ,
3470
3462
delayed_payment_basepoint : local_keys. delayed_payment_basepoint ,
3471
3463
htlc_basepoint : local_keys. htlc_basepoint ,
3472
- first_per_commitment_point : PublicKey :: from_secret_key ( & self . secp_ctx , & local_commitment_secret ) ,
3464
+ first_per_commitment_point,
3473
3465
channel_flags : if self . config . announced_channel { 1 } else { 0 } ,
3474
3466
shutdown_scriptpubkey : OptionalField :: Present ( if self . config . commit_upfront_shutdown_pubkey { self . get_closing_scriptpubkey ( ) } else { Builder :: new ( ) . into_script ( ) } )
3475
3467
}
@@ -3486,7 +3478,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
3486
3478
panic ! ( "Tried to send an accept_channel for a channel that has already advanced" ) ;
3487
3479
}
3488
3480
3489
- let local_commitment_secret = self . build_local_commitment_secret ( self . cur_local_commitment_transaction_number ) ;
3481
+ let first_per_commitment_point = self . local_keys . get_per_commitment_point ( self . cur_local_commitment_transaction_number , & self . secp_ctx ) ;
3490
3482
let local_keys = self . local_keys . pubkeys ( ) ;
3491
3483
3492
3484
msgs:: AcceptChannel {
@@ -3503,7 +3495,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
3503
3495
payment_point : local_keys. payment_point ,
3504
3496
delayed_payment_basepoint : local_keys. delayed_payment_basepoint ,
3505
3497
htlc_basepoint : local_keys. htlc_basepoint ,
3506
- first_per_commitment_point : PublicKey :: from_secret_key ( & self . secp_ctx , & local_commitment_secret ) ,
3498
+ first_per_commitment_point,
3507
3499
shutdown_scriptpubkey : OptionalField :: Present ( if self . config . commit_upfront_shutdown_pubkey { self . get_closing_scriptpubkey ( ) } else { Builder :: new ( ) . into_script ( ) } )
3508
3500
}
3509
3501
}
0 commit comments