1+
2+ mod keystore;
3+ mod crypto;
4+ mod command;
5+ mod wallet;
6+ mod pkcs8;
7+
8+ use sp_core:: crypto:: { Ss58AddressFormat , set_default_ss58_version} ;
9+ use std:: path:: Path ;
10+ use std:: fs;
11+ use keystore:: Keystore ;
12+ use crypto:: * ;
13+ use wallet:: * ;
14+
15+ fn main ( ) {
16+ let mut app = command:: get_app ( ) ;
17+ let matches = app. clone ( ) . get_matches ( ) ;
18+ set_default_ss58_version ( Ss58AddressFormat :: PolkadotAccount ) ;
19+ let store = WalletStore :: init ( None ) ;
20+
21+ match matches. subcommand ( ) {
22+ ( "getnewaddress" , Some ( matches) ) => {
23+ let label = matches. value_of ( "label" ) . unwrap ( ) ;
24+ let mut address = if matches. is_present ( "ed25519" ) {
25+ Address :: generate :: < Ed25519 > ( )
26+ } else if matches. is_present ( "secp256k1" ) {
27+ Address :: generate :: < Ecdsa > ( )
28+ } else {
29+ Address :: generate :: < Sr25519 > ( )
30+ } ;
31+
32+ address. label = label. to_string ( ) ;
33+ store. save ( address. clone ( ) ) ;
34+ println ! ( "{}" , address. addr) ;
35+ }
36+ ( "listaddresses" , Some ( _) ) => {
37+ let addresses = store. read_all ( ) ;
38+ for address in addresses {
39+ address. print ( ) ;
40+ }
41+ }
42+ ( "restore" , Some ( matches) ) => {
43+ let file = matches. value_of ( "file" ) . unwrap ( ) ;
44+
45+ let keystore = match Keystore :: parse_from_file ( file. to_string ( ) ) {
46+ Ok ( keystore) => keystore,
47+ Err ( _) => {
48+ println ! ( "Failed to parse keystore file" ) ;
49+ return
50+ }
51+ } ;
52+
53+ let password = rpassword:: read_password_from_tty ( Some ( "Password: " ) ) . ok ( ) ;
54+ if let Ok ( address) = Address :: from_keystore ( keystore, password) {
55+ store. save ( address. clone ( ) ) ;
56+ println ! ( "Address `{}` is restored" , address. addr) ;
57+ } else {
58+ println ! ( "Failed to recover address" ) ;
59+ return
60+ }
61+ }
62+ ( "backup" , Some ( matches) ) => {
63+ let label = matches. value_of ( "label" ) . unwrap ( ) ;
64+ let file = matches. value_of ( "path" ) . unwrap ( ) ;
65+
66+ let address = match store. read ( label) {
67+ Some ( address) => address,
68+ None => {
69+ println ! ( "`{}` related address does not exist." , label) ;
70+ return
71+ }
72+ } ;
73+
74+ let path = Path :: new ( file) ;
75+ let full_path = if path. ends_with ( "/" ) || path. is_dir ( ) { // dir
76+ let file_name = format ! ( "{}.json" , address. addr. as_str( ) ) ;
77+ let mut path = path. to_path_buf ( ) ;
78+ path. push ( file_name) ;
79+ path
80+ } else { // file
81+ path. to_path_buf ( )
82+ } ;
83+
84+ if full_path. exists ( ) {
85+ eprintln ! ( "File `{}` aleady exists" , full_path. to_str( ) . unwrap( ) ) ;
86+ return
87+ }
88+
89+ let password = rpassword:: read_password_from_tty ( Some ( "Type password to encrypt seed: " ) ) . ok ( ) ;
90+
91+ let keystore = address. into_keystore ( password) ;
92+
93+ if let Err ( e) = fs:: write ( full_path. clone ( ) , keystore. to_json ( ) ) {
94+ println ! ( "Failed to write to file: {:?}" , e) ;
95+ } else {
96+ println ! ( "Address `{}` is backed up to file `{}`" , address. addr, full_path. to_str( ) . unwrap( ) ) ;
97+ }
98+ }
99+ _ => {
100+ app. print_help ( ) . unwrap ( ) ;
101+ println ! ( ) ;
102+ }
103+ }
104+ }
0 commit comments