1+ from erdpy .wallet .keyfile import save_to_key_file
2+ from erdpy .wallet .core import generate_mnemonic
13import logging
4+ import getpass
5+ from pathlib import Path
26from typing import Any , List
37
4- from erdpy import cli_shared , wallet
8+ from erdpy import cli_shared , wallet , utils
59from erdpy .accounts import Account , Address
610from erdpy .wallet import pem
711
@@ -12,10 +16,24 @@ def setup_parser(args: List[str], subparsers: Any) -> Any:
1216 parser = cli_shared .add_group_subparser (
1317 subparsers ,
1418 "wallet" ,
15- "Derive private key from mnemonic, bech32 address helpers etc."
19+ "Create wallet, derive secret key from mnemonic, bech32 address helpers etc."
1620 )
1721 subparsers = parser .add_subparsers ()
1822
23+ sub = cli_shared .add_command_subparser (
24+ subparsers ,
25+ "wallet" ,
26+ "new" ,
27+ "Create a new wallet and print its mnemonic; optionally save as password-protected JSON (recommended) or PEM (not recommended)"
28+ )
29+ sub .add_argument ("--json" ,
30+ help = "whether to create a json key file" , action = "store_true" , default = False )
31+ sub .add_argument ("--pem" ,
32+ help = "whether to create a pem key file" , action = "store_true" , default = False )
33+ sub .add_argument ("--output-path" ,
34+ help = "the output path and base file name for the generated wallet files (default: %(default)s)" , type = str , default = "./wallet" )
35+ sub .set_defaults (func = new_wallet )
36+
1937 sub = cli_shared .add_command_subparser (
2038 subparsers ,
2139 "wallet" ,
@@ -69,19 +87,42 @@ def setup_parser(args: List[str], subparsers: Any) -> Any:
6987 return subparsers
7088
7189
90+ def new_wallet (args : Any ):
91+ mnemonic = generate_mnemonic ()
92+ print (f"Mnemonic: { mnemonic } " )
93+ secret_key , pubkey = wallet .derive_keys (mnemonic )
94+ if args .pem :
95+ pem_file = prepare_file (args .output_path , ".pem" )
96+ address = Address (pubkey )
97+ pem .write (pem_file , secret_key , pubkey , name = address .bech32 ())
98+ logger .info (f"Pem wallet generated: { pem_file } " )
99+ if args .json :
100+ json_file = prepare_file (args .output_path , ".json" )
101+ password = getpass .getpass ("Enter a new password:" )
102+ save_to_key_file (json_file , secret_key , pubkey , password )
103+ logger .info (f"Json wallet generated: { json_file } " )
104+
105+
106+ def prepare_file (output_path : str , suffix : str ) -> Path :
107+ base_path = Path (output_path )
108+ utils .ensure_folder (base_path .parent )
109+ file_path = base_path .with_suffix (suffix )
110+ return utils .uniquify (file_path )
111+
112+
72113def generate_pem (args : Any ):
73114 pem_file = args .pem
74115 mnemonic = args .mnemonic
75116 index = args .index
76117
77- seed , pubkey = wallet .generate_pair ()
118+ secret_key , pubkey = wallet .generate_pair ()
78119 if mnemonic :
79120 mnemonic = input ("Enter mnemonic:\n " )
80121 mnemonic = mnemonic .strip ()
81- seed , pubkey = wallet .derive_keys (mnemonic , index )
122+ secret_key , pubkey = wallet .derive_keys (mnemonic , index )
82123
83124 address = Address (pubkey )
84- pem .write (pem_file , seed , pubkey , name = address .bech32 ())
125+ pem .write (pem_file , secret_key , pubkey , name = address .bech32 ())
85126 logger .info (f"Created PEM file [{ pem_file } ] for [{ address .bech32 ()} ]" )
86127
87128
0 commit comments