@@ -9,8 +9,8 @@ use bitcoin_hashes::sha256::Hash as Sha256;
99use secp256k1:: { PublicKey , SecretKey } ;
1010
1111use ln:: peers:: { chacha, hkdf} ;
12- use ln:: peers:: conduit:: Conduit ;
13- use ln:: peers:: handshake:: acts:: { ActOne , ActThree , ActTwo } ;
12+ use ln:: peers:: conduit:: { Conduit , SymmetricKey } ;
13+ use ln:: peers:: handshake:: acts:: { ActOne , ActThree , ActTwo , ACT_ONE_LENGTH , ACT_TWO_LENGTH , ACT_THREE_LENGTH } ;
1414use ln:: peers:: handshake:: hash:: HandshakeHash ;
1515use ln:: peers:: handshake:: states:: { ActOneExpectation , ActThreeExpectation , ActTwoExpectation , HandshakeState } ;
1616
@@ -33,7 +33,7 @@ impl PeerHandshake {
3333 /// Instantiate a new handshake with a node identity secret key and an ephemeral private key
3434 pub fn new ( private_key : & SecretKey , ephemeral_private_key : & SecretKey ) -> Self {
3535 Self {
36- state : Some ( HandshakeState :: Blank ) ,
36+ state : Some ( HandshakeState :: Uninitiated ) ,
3737 private_key : ( * private_key) . clone ( ) ,
3838 ephemeral_private_key : ( * ephemeral_private_key) . clone ( ) ,
3939 read_buffer : Vec :: new ( ) ,
@@ -68,7 +68,15 @@ impl PeerHandshake {
6868 }
6969
7070 /// Process act dynamically
71- /// The role must be set before this method can be called
71+ /// # Arguments
72+ /// `input`: Byte slice received from peer as part of the handshake protocol
73+ /// `remote_public_key`: If outbound, the peer's static identity public key needs is required for the handshake initiation
74+ ///
75+ /// # Return values
76+ /// Returns a tuple with the following components:
77+ /// `.0`: Byte vector containing the next act to send back to the peer per the handshake protocol
78+ /// `.1`: Conduit option if the handshake was just processed to completion and messages can now be encrypted and decrypted
79+ /// `.2`: Public key option if the handshake was inbound and the peer's static identity pubkey was just learned
7280 pub fn process_act ( & mut self , input : & [ u8 ] , remote_public_key : Option < & PublicKey > ) -> Result < ( Vec < u8 > , Option < Conduit > , Option < PublicKey > ) , String > {
7381 let mut response: Vec < u8 > = Vec :: new ( ) ;
7482 let mut connected_peer = None ;
@@ -78,7 +86,7 @@ impl PeerHandshake {
7886 let read_buffer_length = self . read_buffer . len ( ) ;
7987
8088 match & self . state {
81- & Some ( HandshakeState :: Blank ) => {
89+ & Some ( HandshakeState :: Uninitiated ) => {
8290 let remote_public_key = remote_public_key. ok_or ( "remote_public_key must be Some for outbound connections" ) ?;
8391 let act_one = self . initiate ( & remote_public_key) ?;
8492 response = act_one. 0 . to_vec ( ) ;
@@ -89,22 +97,21 @@ impl PeerHandshake {
8997 return Err ( "need at least 50 bytes" . to_string ( ) ) ;
9098 }
9199
92- let mut act_one_buffer = [ 0u8 ; 50 ] ;
93- act_one_buffer. copy_from_slice ( & self . read_buffer [ ..act_length ] ) ;
94- self . read_buffer . drain ( ..act_length ) ;
100+ let mut act_one_buffer = [ 0u8 ; ACT_ONE_LENGTH ] ;
101+ act_one_buffer. copy_from_slice ( & self . read_buffer [ ..ACT_ONE_LENGTH ] ) ;
102+ self . read_buffer . drain ( ..ACT_ONE_LENGTH ) ;
95103
96104 let act_two = self . process_act_one ( ActOne ( act_one_buffer) ) ?;
97105 response = act_two. 0 . to_vec ( ) ;
98106 }
99107 & Some ( HandshakeState :: AwaitingActTwo ( _) ) => {
100- let act_length = 50 ;
101- if read_buffer_length < act_length {
108+ if read_buffer_length < ACT_TWO_LENGTH {
102109 return Err ( "need at least 50 bytes" . to_string ( ) ) ;
103110 }
104111
105- let mut act_two_buffer = [ 0u8 ; 50 ] ;
106- act_two_buffer. copy_from_slice ( & self . read_buffer [ ..act_length ] ) ;
107- self . read_buffer . drain ( ..act_length ) ;
112+ let mut act_two_buffer = [ 0u8 ; ACT_TWO_LENGTH ] ;
113+ act_two_buffer. copy_from_slice ( & self . read_buffer [ ..ACT_TWO_LENGTH ] ) ;
114+ self . read_buffer . drain ( ..ACT_TWO_LENGTH ) ;
108115
109116 let ( act_three, mut conduit) = self . process_act_two ( ActTwo ( act_two_buffer) ) ?;
110117
@@ -117,14 +124,13 @@ impl PeerHandshake {
117124 connected_peer = Some ( conduit) ;
118125 }
119126 & Some ( HandshakeState :: AwaitingActThree ( _) ) => {
120- let act_length = 66 ;
121- if read_buffer_length < act_length {
122- return Err ( "need at least 50 bytes" . to_string ( ) ) ;
127+ if read_buffer_length < ACT_THREE_LENGTH {
128+ return Err ( "need at least 66 bytes" . to_string ( ) ) ;
123129 }
124130
125- let mut act_three_buffer = [ 0u8 ; 66 ] ;
126- act_three_buffer. copy_from_slice ( & self . read_buffer [ ..act_length ] ) ;
127- self . read_buffer . drain ( ..act_length ) ;
131+ let mut act_three_buffer = [ 0u8 ; ACT_THREE_LENGTH ] ;
132+ act_three_buffer. copy_from_slice ( & self . read_buffer [ ..ACT_THREE_LENGTH ] ) ;
133+ self . read_buffer . drain ( ..ACT_THREE_LENGTH ) ;
128134
129135 let ( public_key, mut conduit) = self . process_act_three ( ActThree ( act_three_buffer) ) ?;
130136
@@ -145,7 +151,7 @@ impl PeerHandshake {
145151
146152 /// Initiate the handshake with a peer and return the first act
147153 pub fn initiate ( & mut self , remote_public_key : & PublicKey ) -> Result < ActOne , String > {
148- if let & Some ( HandshakeState :: Blank ) = & self . state { } else {
154+ if let & Some ( HandshakeState :: Uninitiated ) = & self . state { } else {
149155 return Err ( "incorrect state" . to_string ( ) ) ;
150156 }
151157
@@ -174,9 +180,8 @@ impl PeerHandshake {
174180 let state = self . state . take ( ) ;
175181 let act_one_expectation = match state {
176182 Some ( HandshakeState :: AwaitingActOne ( act_state) ) => act_state,
177- Some ( HandshakeState :: Blank ) => {
183+ Some ( HandshakeState :: Uninitiated ) => {
178184 // this can also be initiated from a blank state
179- // public key
180185 let public_key = Self :: private_key_to_public_key ( & self . private_key ) ;
181186 let ( hash, chaining_key) = Self :: initialize_state ( & public_key) ;
182187 ActOneExpectation {
@@ -315,7 +320,7 @@ impl PeerHandshake {
315320 Ok ( ( remote_pubkey, connected_peer) )
316321 }
317322
318- fn calculate_act_message ( local_private_key : & SecretKey , remote_public_key : & PublicKey , chaining_key : [ u8 ; 32 ] , hash : & mut HandshakeHash ) -> ( [ u8 ; 50 ] , [ u8 ; 32 ] , [ u8 ; 32 ] ) {
323+ fn calculate_act_message ( local_private_key : & SecretKey , remote_public_key : & PublicKey , chaining_key : [ u8 ; 32 ] , hash : & mut HandshakeHash ) -> ( [ u8 ; 50 ] , SymmetricKey , SymmetricKey ) {
319324 let local_public_key = Self :: private_key_to_public_key ( local_private_key) ;
320325
321326 hash. update ( & local_public_key. serialize ( ) ) ;
@@ -333,7 +338,7 @@ impl PeerHandshake {
333338 ( act, chaining_key, temporary_key)
334339 }
335340
336- fn process_act_message ( act_bytes : [ u8 ; 50 ] , local_private_key : & SecretKey , chaining_key : [ u8 ; 32 ] , hash : & mut HandshakeHash ) -> Result < ( PublicKey , [ u8 ; 32 ] , [ u8 ; 32 ] ) , String > {
341+ fn process_act_message ( act_bytes : [ u8 ; 50 ] , local_private_key : & SecretKey , chaining_key : [ u8 ; 32 ] , hash : & mut HandshakeHash ) -> Result < ( PublicKey , SymmetricKey , SymmetricKey ) , String > {
337342 let version = act_bytes[ 0 ] ;
338343 if version != 0 {
339344 return Err ( "unexpected version" . to_string ( ) ) ;
@@ -358,7 +363,7 @@ impl PeerHandshake {
358363 // HKDF(chaining key, ECDH) -> chaining key' + next temporary key
359364 let ( chaining_key, temporary_key) = hkdf:: derive ( & chaining_key, & ecdh) ;
360365
361- // Validate chacha tag (temporary key, 0, self. hash, chacha_tag)
366+ // Validate chacha tag (temporary key, 0, hash, chacha_tag)
362367 let _tag_check = chacha:: decrypt ( & temporary_key, 0 , & hash. value , & chacha_tag) ?;
363368
364369 hash. update ( & chacha_tag) ;
@@ -372,7 +377,7 @@ impl PeerHandshake {
372377 pk_object
373378 }
374379
375- fn ecdh ( private_key : & SecretKey , public_key : & PublicKey ) -> [ u8 ; 32 ] {
380+ fn ecdh ( private_key : & SecretKey , public_key : & PublicKey ) -> SymmetricKey {
376381 let curve = secp256k1:: Secp256k1 :: new ( ) ;
377382 let mut pk_object = public_key. clone ( ) ;
378383 pk_object. mul_assign ( & curve, & private_key[ ..] ) . expect ( "invalid multiplication" ) ;
0 commit comments