@@ -25,7 +25,6 @@ use crate::{
2525 calculate_commitment_tx_fee, calculate_shutdown_tx_fee, calculate_tlc_forward_fee,
2626 shutdown_tx_size,
2727 } ,
28- key:: blake2b_hash_with_salt,
2928 network:: SendOnionPacketCommand ,
3029 network:: { get_chain_hash, sign_network_message, FiberMessageWithTarget } ,
3130 types:: {
@@ -40,7 +39,7 @@ use crate::{
4039 now_timestamp_as_millis_u64, NetworkServiceEvent ,
4140} ;
4241use crate :: { debug_event, fiber:: types:: TxAbort , utils:: tx:: compute_tx_message} ;
43- use ckb_hash:: { blake2b_256, new_blake2b } ;
42+ use ckb_hash:: blake2b_256;
4443use ckb_sdk:: { util:: blake160, Since , SinceType } ;
4544use ckb_types:: {
4645 core:: {
@@ -52,22 +51,21 @@ use ckb_types::{
5251 H256 ,
5352} ;
5453use fiber_types:: {
55- AddTlcCommand , AppliedFlags , AwaitingChannelReadyFlags , AwaitingTxSignaturesFlags ,
56- BasicMppPaymentData , ChannelActorData , ChannelAnnouncement , ChannelBasePublicKeys ,
57- ChannelConstraints , ChannelFlags , ChannelOpenRecord , ChannelState , ChannelTlcInfo ,
58- ChannelUpdate , ChannelUpdateChannelFlags , ChannelUpdateMessageFlags , CloseFlags ,
59- CollaboratingFundingTxFlags , CommitmentNumbers , EcdsaSignature , Hash256 , InMemorySigner ,
60- InboundTlcStatus , NegotiatingFundingFlags , OutboundTlcStatus , PaymentCustomRecords ,
61- PeeledPaymentOnionPacket , PendingNotifySettleTlc , PrevTlcInfo , Privkey , Pubkey ,
62- PublicChannelInfo , RemoveTlcFulfill , RemoveTlcReason , RetryableTlcOperation , RevocationData ,
63- RevokeAndAck , SettlementData , SettlementTlc , ShutdownInfo , ShuttingDownFlags ,
64- SigningCommitmentFlags , TLCId , TlcErr , TlcErrData , TlcErrPacket , TlcErrorCode , TlcInfo ,
65- TlcStatus , NO_SHARED_SECRET ,
54+ blake2b_hash_with_salt , derive_tlc_pubkey , AddTlcCommand , AppliedFlags ,
55+ AwaitingChannelReadyFlags , AwaitingTxSignaturesFlags , BasicMppPaymentData , ChannelActorData ,
56+ ChannelAnnouncement , ChannelBasePublicKeys , ChannelConstraints , ChannelFlags ,
57+ ChannelOpenRecord , ChannelState , ChannelTlcInfo , ChannelUpdate , ChannelUpdateChannelFlags ,
58+ ChannelUpdateMessageFlags , CloseFlags , CollaboratingFundingTxFlags , CommitmentNumbers ,
59+ EcdsaSignature , Hash256 , InMemorySigner , InboundTlcStatus , Musig2Context ,
60+ NegotiatingFundingFlags , OutboundTlcStatus , PaymentCustomRecords , PeeledPaymentOnionPacket ,
61+ PendingNotifySettleTlc , PrevTlcInfo , Privkey , Pubkey , PublicChannelInfo , RemoveTlcFulfill ,
62+ RemoveTlcReason , RetryableTlcOperation , RevocationData , RevokeAndAck , SettlementData ,
63+ SettlementTlc , ShutdownInfo , ShuttingDownFlags , SigningCommitmentFlags , TLCId , TlcErr ,
64+ TlcErrData , TlcErrPacket , TlcErrorCode , TlcInfo , TlcStatus , NO_SHARED_SECRET ,
6665} ;
6766use molecule:: prelude:: { Builder , Entity } ;
6867#[ cfg( test) ]
6968use musig2:: BinaryEncoding ;
70- use musig2:: SecNonceBuilder ;
7169use musig2:: {
7270 aggregate_partial_signatures,
7371 errors:: { RoundFinalizeError , SigningError , VerifyError } ,
@@ -3222,24 +3220,6 @@ fn derive_temp_channel_id_from_tlc_key(tlc_basepoint: &Pubkey) -> Hash256 {
32223220 new_channel_id_from_seed ( & preimage)
32233221}
32243222
3225- pub fn get_commitment_secret ( commitment_seed : & [ u8 ; 32 ] , commitment_number : u64 ) -> [ u8 ; 32 ] {
3226- // Note that here, we hold the same assumption to bolts for commitment number,
3227- // i.e. this number should be in the range [0, 2^48).
3228- let mut res: [ u8 ; 32 ] = * commitment_seed;
3229- for i in 0 ..48 {
3230- let bitpos = 47 - i;
3231- if commitment_number & ( 1 << bitpos) == ( 1 << bitpos) {
3232- res[ bitpos / 8 ] ^= 1 << ( bitpos & 7 ) ;
3233- res = blake2b_256 ( res) ;
3234- }
3235- }
3236- res
3237- }
3238-
3239- pub fn get_commitment_point ( commitment_seed : & [ u8 ; 32 ] , commitment_number : u64 ) -> Pubkey {
3240- Privkey :: from ( & get_commitment_secret ( commitment_seed, commitment_number) ) . pubkey ( )
3241- }
3242-
32433223pub ( crate ) fn get_funding_and_reserved_amount (
32443224 total_amount : u128 ,
32453225 shutdown_script : & Script ,
@@ -7103,119 +7083,3 @@ impl From<&AcceptChannel> for ChannelBasePublicKeys {
71037083 }
71047084 }
71057085}
7106-
7107- pub ( crate ) fn get_tweak_by_commitment_point ( commitment_point : & Pubkey ) -> [ u8 ; 32 ] {
7108- let mut hasher = new_blake2b ( ) ;
7109- hasher. update ( & commitment_point. serialize ( ) ) ;
7110- let mut result = [ 0u8 ; 32 ] ;
7111- hasher. finalize ( & mut result) ;
7112- result
7113- }
7114-
7115- pub ( crate ) fn derive_private_key ( secret : & Privkey , commitment_point : & Pubkey ) -> Privkey {
7116- secret. tweak ( get_tweak_by_commitment_point ( commitment_point) )
7117- }
7118-
7119- fn derive_public_key ( base_key : & Pubkey , commitment_point : & Pubkey ) -> Pubkey {
7120- base_key. tweak ( get_tweak_by_commitment_point ( commitment_point) )
7121- }
7122-
7123- pub ( crate ) fn derive_tlc_pubkey ( base_key : & Pubkey , commitment_point : & Pubkey ) -> Pubkey {
7124- derive_public_key ( base_key, commitment_point)
7125- }
7126-
7127- pub enum Musig2Context {
7128- Commitment ,
7129- Revoke ,
7130- }
7131-
7132- impl std:: fmt:: Display for Musig2Context {
7133- fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
7134- let context_str = match self {
7135- Musig2Context :: Commitment => "COMMITMENT" ,
7136- Musig2Context :: Revoke => "REVOKE" ,
7137- } ;
7138- write ! ( f, "{}" , context_str)
7139- }
7140- }
7141-
7142- /// Extension trait providing methods for `InMemorySigner` that depend on
7143- /// fiber-lib-only functions (key derivation, ckb_hash, Musig2Context).
7144- pub trait InMemorySignerExt {
7145- /// Generate an `InMemorySigner` from a seed.
7146- fn generate_from_seed ( params : & [ u8 ] ) -> InMemorySigner ;
7147- /// Get the base public keys for this signer.
7148- fn get_base_public_keys ( & self ) -> ChannelBasePublicKeys ;
7149- /// Returns the commitment point for the given commitment number.
7150- fn get_commitment_point ( & self , commitment_number : u64 ) -> Pubkey ;
7151- /// Returns the commitment secret for the given commitment number.
7152- fn get_commitment_secret ( & self , commitment_number : u64 ) -> [ u8 ; 32 ] ;
7153- /// Derive the TLC key for the given commitment number.
7154- fn derive_tlc_key ( & self , new_commitment_number : u64 ) -> Privkey ;
7155- /// Derive a musig2 nonce for the given commitment number and context.
7156- fn derive_musig2_nonce ( & self , commitment_number : u64 , context : Musig2Context ) -> SecNonce ;
7157- }
7158-
7159- impl InMemorySignerExt for InMemorySigner {
7160- fn generate_from_seed ( params : & [ u8 ] ) -> InMemorySigner {
7161- let seed = ckb_hash:: blake2b_256 ( params) ;
7162-
7163- let commitment_seed = {
7164- let mut hasher = new_blake2b ( ) ;
7165- hasher. update ( & seed) ;
7166- hasher. update ( & b"commitment seed" [ ..] ) ;
7167- let mut result = [ 0u8 ; 32 ] ;
7168- hasher. finalize ( & mut result) ;
7169- result
7170- } ;
7171-
7172- let key_derive = |seed : & [ u8 ] , info : & [ u8 ] | {
7173- let result = blake2b_hash_with_salt ( seed, info) ;
7174- Privkey :: from_slice ( & result)
7175- } ;
7176-
7177- let funding_key = key_derive ( & seed, b"funding key" ) ;
7178- let tlc_base_key = key_derive ( funding_key. as_ref ( ) , b"HTLC base key" ) ;
7179- let musig2_base_nonce = key_derive ( tlc_base_key. as_ref ( ) , b"musig nocne" ) ;
7180-
7181- InMemorySigner {
7182- funding_key,
7183- tlc_base_key,
7184- musig2_base_nonce,
7185- commitment_seed,
7186- }
7187- }
7188-
7189- fn get_base_public_keys ( & self ) -> ChannelBasePublicKeys {
7190- ChannelBasePublicKeys {
7191- funding_pubkey : self . funding_key . pubkey ( ) ,
7192- tlc_base_key : self . tlc_base_key . pubkey ( ) ,
7193- }
7194- }
7195-
7196- /// Returns the commitment point for the given commitment number.
7197- ///
7198- /// The commitment point is the public key derived from the commitment seed and the commitment number.
7199- /// The commitment point is used to derive the pubkeys used in the TLC (htlc and revocation outputs).
7200- fn get_commitment_point ( & self , commitment_number : u64 ) -> Pubkey {
7201- get_commitment_point ( & self . commitment_seed , commitment_number)
7202- }
7203-
7204- fn get_commitment_secret ( & self , commitment_number : u64 ) -> [ u8 ; 32 ] {
7205- get_commitment_secret ( & self . commitment_seed , commitment_number)
7206- }
7207-
7208- fn derive_tlc_key ( & self , new_commitment_number : u64 ) -> Privkey {
7209- let per_commitment_point = self . get_commitment_point ( new_commitment_number) ;
7210- derive_private_key ( & self . tlc_base_key , & per_commitment_point)
7211- }
7212-
7213- fn derive_musig2_nonce ( & self , commitment_number : u64 , context : Musig2Context ) -> SecNonce {
7214- let commitment_point = self . get_commitment_point ( commitment_number) ;
7215- let seckey = derive_private_key ( & self . musig2_base_nonce , & commitment_point) ;
7216-
7217- SecNonceBuilder :: new ( seckey. as_ref ( ) )
7218- . with_extra_input ( & context. to_string ( ) )
7219- . build ( )
7220- }
7221- }
0 commit comments