5
5
use bitcoin:: secp256k1:: { PublicKey , SecretKey } ;
6
6
7
7
use ln:: peers:: conduit:: Conduit ;
8
- use ln:: peers:: handshake:: states:: { HandshakeState , IHandshakeState } ;
8
+ use ln:: peers:: handshake:: acts:: Act ;
9
+ use ln:: peers:: handshake:: states:: HandshakeState ;
10
+ use ln:: peers:: transport:: IPeerHandshake ;
9
11
10
12
mod acts;
11
13
mod states;
12
14
15
+ /// Interface used by PeerHandshake to interact with NOISE state machine.
16
+ /// State may transition to the same state in the event there are not yet enough bytes to move
17
+ /// forward with the handshake.
18
+ trait IHandshakeState {
19
+ /// Returns the next HandshakeState after processing the input bytes
20
+ fn next ( self , input : & [ u8 ] ) -> Result < ( Option < Act > , HandshakeState ) , String > ;
21
+ }
22
+
13
23
/// Object for managing handshakes.
14
24
/// Currently requires explicit ephemeral private key specification.
15
25
pub struct PeerHandshake {
16
26
state : Option < HandshakeState > ,
17
27
ready_to_process : bool ,
18
28
}
19
29
20
- impl PeerHandshake {
30
+ impl IPeerHandshake for PeerHandshake {
21
31
/// Instantiate a handshake given the peer's static public key. The ephemeral private key MUST
22
32
/// generate a new session with strong cryptographic randomness.
23
- pub fn new_outbound ( initiator_static_private_key : & SecretKey , responder_static_public_key : & PublicKey , initiator_ephemeral_private_key : & SecretKey ) -> Self {
33
+ fn new_outbound ( initiator_static_private_key : & SecretKey , responder_static_public_key : & PublicKey , initiator_ephemeral_private_key : & SecretKey ) -> Self {
24
34
let state = HandshakeState :: new_initiator ( initiator_static_private_key, responder_static_public_key, initiator_ephemeral_private_key) ;
25
35
26
36
Self {
@@ -29,16 +39,8 @@ impl PeerHandshake {
29
39
}
30
40
}
31
41
32
- /// Instantiate a handshake in anticipation of a peer's first handshake act
33
- pub fn new_inbound ( responder_static_private_key : & SecretKey , responder_ephemeral_private_key : & SecretKey ) -> Self {
34
- Self {
35
- state : Some ( HandshakeState :: new_responder ( responder_static_private_key, responder_ephemeral_private_key) ) ,
36
- ready_to_process : true ,
37
- }
38
- }
39
-
40
42
/// Initializes the outbound handshake and provides the initial bytes to send to the responder
41
- pub fn set_up_outbound ( & mut self ) -> Vec < u8 > {
43
+ fn set_up_outbound ( & mut self ) -> Vec < u8 > {
42
44
assert ! ( !self . ready_to_process) ;
43
45
self . ready_to_process = true ;
44
46
@@ -49,6 +51,14 @@ impl PeerHandshake {
49
51
response_vec_option. unwrap ( )
50
52
}
51
53
54
+ /// Instantiate a new handshake in anticipation of a peer's first handshake act
55
+ fn new_inbound ( responder_static_private_key : & SecretKey , responder_ephemeral_private_key : & SecretKey ) -> Self {
56
+ Self {
57
+ state : Some ( HandshakeState :: new_responder ( responder_static_private_key, responder_ephemeral_private_key) ) ,
58
+ ready_to_process : true ,
59
+ }
60
+ }
61
+
52
62
/// Process act dynamically
53
63
/// # Arguments
54
64
/// `input`: Byte slice received from peer as part of the handshake protocol
@@ -57,7 +67,7 @@ impl PeerHandshake {
57
67
/// Returns a tuple with the following components:
58
68
/// `.0`: Byte vector containing the next act to send back to the peer per the handshake protocol
59
69
/// `.1`: Some(Conduit, PublicKey) if the handshake was just processed to completion and messages can now be encrypted and decrypted
60
- pub fn process_act ( & mut self , input : & [ u8 ] ) -> Result < ( Option < Vec < u8 > > , Option < ( Conduit , PublicKey ) > ) , String > {
70
+ fn process_act ( & mut self , input : & [ u8 ] ) -> Result < ( Option < Vec < u8 > > , Option < ( Conduit , PublicKey ) > ) , String > {
61
71
assert ! ( self . ready_to_process) ;
62
72
let cur_state = self . state . take ( ) . unwrap ( ) ;
63
73
@@ -120,15 +130,6 @@ mod test {
120
130
}
121
131
}
122
132
123
- macro_rules! assert_matches {
124
- ( $e: expr, $state_match: pat) => {
125
- match $e {
126
- $state_match => ( ) ,
127
- _ => panic!( )
128
- }
129
- }
130
- }
131
-
132
133
macro_rules! do_process_act_or_panic {
133
134
( $handshake: expr, $input: expr) => {
134
135
$handshake. process_act( $input) . unwrap( ) . 0 . unwrap( )
0 commit comments