11use libp2p_identity:: PeerId ;
2+ use mina_node_account:: AccountPublicKey ;
23use node:: { account:: AccountSecretKey , p2p:: identity:: SecretKey } ;
34use std:: { fs:: File , io:: Write } ;
45
@@ -46,18 +47,44 @@ impl P2PKeyPair {
4647 }
4748}
4849
49- #[ derive( Debug , Clone , clap:: Args ) ]
50+ #[ derive( Debug , Clone , Default , clap:: Args ) ]
5051pub struct MinaKeyPair {
5152 #[ arg( long, short = 's' , env = "MINA_SEC_KEY" ) ]
5253 secret_key : Option < AccountSecretKey > ,
54+
55+ #[ arg( long, help = "Format as a web-node-secrets.json" ) ]
56+ web_node_secrets : bool ,
57+ }
58+
59+ // TODO: This should really be the same type that the webnode
60+ // TODO: uses to parse web-node-secrets.json
61+ #[ derive( serde:: Serialize ) ]
62+ #[ serde( rename_all = "camelCase" ) ]
63+ struct GeneratedMinaKeyPair {
64+ public_key : AccountPublicKey ,
65+ private_key : AccountSecretKey ,
66+ }
67+
68+ impl GeneratedMinaKeyPair {
69+ fn print ( & self ) {
70+ println ! ( "secret key: {}" , self . private_key) ;
71+ println ! ( "public key: {}" , self . public_key) ;
72+ }
5373}
5474
5575impl MinaKeyPair {
5676 pub fn run ( self ) -> anyhow:: Result < ( ) > {
57- let secret_key = self . secret_key . unwrap_or_else ( AccountSecretKey :: rand) ;
58- let public_key = secret_key. public_key ( ) ;
59- println ! ( "secret key: {secret_key}" ) ;
60- println ! ( "public key: {public_key}" ) ;
77+ let private_key = self . secret_key . unwrap_or_else ( AccountSecretKey :: rand) ;
78+ let public_key = private_key. public_key ( ) ;
79+ let keypair = GeneratedMinaKeyPair {
80+ public_key,
81+ private_key,
82+ } ;
83+ if self . web_node_secrets {
84+ println ! ( "{}" , serde_json:: to_string_pretty( & keypair) ?) ;
85+ } else {
86+ keypair. print ( ) ;
87+ }
6188
6289 Ok ( ( ) )
6390 }
@@ -304,7 +331,7 @@ mod tests {
304331
305332 #[ test]
306333 fn test_mina_key_pair_generates_random_key ( ) {
307- let cmd = MinaKeyPair { secret_key : None } ;
334+ let cmd = MinaKeyPair :: default ( ) ;
308335
309336 let result = cmd. run ( ) ;
310337 assert ! ( result. is_ok( ) ) ;
@@ -315,6 +342,7 @@ mod tests {
315342 let secret_key = AccountSecretKey :: rand ( ) ;
316343 let cmd = MinaKeyPair {
317344 secret_key : Some ( secret_key) ,
345+ web_node_secrets : false ,
318346 } ;
319347
320348 let result = cmd. run ( ) ;
0 commit comments