1616// You should have received a copy of the GNU General Public License
1717// along with this program. If not, see <https://www.gnu.org/licenses/>.
1818
19- use sc_network:: config:: NodeKeyConfig ;
19+ use sc_network:: { config:: identity :: ed25519 , config :: NodeKeyConfig } ;
2020use sp_core:: H256 ;
2121use std:: { path:: PathBuf , str:: FromStr } ;
2222use structopt:: StructOpt ;
@@ -83,7 +83,7 @@ pub struct NodeKeyParams {
8383 /// as follows:
8484 ///
8585 /// `ed25519`:
86- /// The file must contain an unencoded 32 byte Ed25519 secret key.
86+ /// The file must contain an unencoded 32 byte or hex encoded Ed25519 secret key.
8787 ///
8888 /// If the file does not exist, it is created with a newly generated secret key of
8989 /// the chosen type.
@@ -100,12 +100,11 @@ impl NodeKeyParams {
100100 let secret = if let Some ( node_key) = self . node_key . as_ref ( ) {
101101 parse_ed25519_secret ( node_key) ?
102102 } else {
103- let path = self
104- . node_key_file
105- . clone ( )
106- . unwrap_or_else ( || net_config_dir. join ( NODE_KEY_ED25519_FILE ) ) ;
107-
108- sc_network:: config:: Secret :: File ( path)
103+ sc_network:: config:: Secret :: File (
104+ self . node_key_file
105+ . clone ( )
106+ . unwrap_or_else ( || net_config_dir. join ( NODE_KEY_ED25519_FILE ) )
107+ )
109108 } ;
110109
111110 NodeKeyConfig :: Ed25519 ( secret)
@@ -124,7 +123,7 @@ fn parse_ed25519_secret(hex: &str) -> error::Result<sc_network::config::Ed25519S
124123 H256 :: from_str ( & hex)
125124 . map_err ( invalid_node_key)
126125 . and_then ( |bytes| {
127- sc_network :: config :: identity :: ed25519:: SecretKey :: from_bytes ( bytes)
126+ ed25519:: SecretKey :: from_bytes ( bytes)
128127 . map ( sc_network:: config:: Secret :: Input )
129128 . map_err ( invalid_node_key)
130129 } )
@@ -133,7 +132,8 @@ fn parse_ed25519_secret(hex: &str) -> error::Result<sc_network::config::Ed25519S
133132#[ cfg( test) ]
134133mod tests {
135134 use super :: * ;
136- use sc_network:: config:: identity:: ed25519;
135+ use sc_network:: config:: identity:: { ed25519, Keypair } ;
136+ use std:: fs;
137137
138138 #[ test]
139139 fn test_node_key_config_input ( ) {
@@ -164,28 +164,34 @@ mod tests {
164164
165165 #[ test]
166166 fn test_node_key_config_file ( ) {
167- fn secret_file ( net_config_dir : & PathBuf ) -> error:: Result < ( ) > {
168- NodeKeyType :: variants ( ) . iter ( ) . try_for_each ( |t| {
169- let node_key_type = NodeKeyType :: from_str ( t) . unwrap ( ) ;
170- let tmp = tempfile:: Builder :: new ( ) . prefix ( "alice" ) . tempdir ( ) ?;
171- let file = tmp. path ( ) . join ( format ! ( "{}_mysecret" , t) ) . to_path_buf ( ) ;
172- let params = NodeKeyParams {
173- node_key_type,
174- node_key : None ,
175- node_key_file : Some ( file. clone ( ) ) ,
176- } ;
177- params. node_key ( net_config_dir) . and_then ( |c| match c {
178- NodeKeyConfig :: Ed25519 ( sc_network:: config:: Secret :: File ( ref f) )
179- if node_key_type == NodeKeyType :: Ed25519 && f == & file =>
180- {
181- Ok ( ( ) )
182- }
183- _ => Err ( error:: Error :: Input ( "Unexpected node key config" . into ( ) ) ) ,
184- } )
185- } )
167+ fn check_key ( file : PathBuf , key : & ed25519:: SecretKey ) {
168+ let params = NodeKeyParams {
169+ node_key_type : NodeKeyType :: Ed25519 ,
170+ node_key : None ,
171+ node_key_file : Some ( file) ,
172+ } ;
173+
174+ let node_key = params. node_key ( & PathBuf :: from ( "not-used" ) )
175+ . expect ( "Creates node key config" )
176+ . into_keypair ( )
177+ . expect ( "Creates node key pair" ) ;
178+
179+ match node_key {
180+ Keypair :: Ed25519 ( ref pair)
181+ if pair. secret ( ) . as_ref ( ) == key. as_ref ( ) => { }
182+ _ => panic ! ( "Invalid key" ) ,
183+ }
186184 }
187185
188- assert ! ( secret_file( & PathBuf :: from_str( "x" ) . unwrap( ) ) . is_ok( ) ) ;
186+ let tmp = tempfile:: Builder :: new ( ) . prefix ( "alice" ) . tempdir ( ) . expect ( "Creates tempfile" ) ;
187+ let file = tmp. path ( ) . join ( "mysecret" ) . to_path_buf ( ) ;
188+ let key = ed25519:: SecretKey :: generate ( ) ;
189+
190+ fs:: write ( & file, hex:: encode ( key. as_ref ( ) ) ) . expect ( "Writes secret key" ) ;
191+ check_key ( file. clone ( ) , & key) ;
192+
193+ fs:: write ( & file, & key) . expect ( "Writes secret key" ) ;
194+ check_key ( file. clone ( ) , & key) ;
189195 }
190196
191197 #[ test]
0 commit comments