cli/rpc: 'restore' and 'create' commands are now available via RPC
This commit is contained in:
94
run_electrum
94
run_electrum
@@ -65,18 +65,16 @@ if not is_android:
|
||||
check_imports()
|
||||
|
||||
|
||||
from electrum import bitcoin, util
|
||||
from electrum import util
|
||||
from electrum import constants
|
||||
from electrum import SimpleConfig, Network
|
||||
from electrum.wallet import Wallet, Imported_Wallet
|
||||
from electrum import bitcoin, util, constants
|
||||
from electrum import SimpleConfig
|
||||
from electrum.wallet import Wallet
|
||||
from electrum.storage import WalletStorage, get_derivation_used_for_hw_device_encryption
|
||||
from electrum.util import print_msg, print_stderr, json_encode, json_decode, UserCancelled
|
||||
from electrum.util import set_verbosity, InvalidPassword
|
||||
from electrum.commands import get_parser, known_commands, Commands, config_variables
|
||||
from electrum import daemon
|
||||
from electrum import keystore
|
||||
from electrum.mnemonic import Mnemonic
|
||||
|
||||
# get password routine
|
||||
def prompt_password(prompt, confirm=True):
|
||||
@@ -91,80 +89,6 @@ def prompt_password(prompt, confirm=True):
|
||||
return password
|
||||
|
||||
|
||||
|
||||
def run_non_RPC(config):
|
||||
cmdname = config.get('cmd')
|
||||
|
||||
storage = WalletStorage(config.get_wallet_path())
|
||||
if storage.file_exists():
|
||||
sys.exit("Error: Remove the existing wallet first!")
|
||||
|
||||
def password_dialog():
|
||||
return prompt_password("Password (hit return if you do not wish to encrypt your wallet):")
|
||||
|
||||
if cmdname == 'restore':
|
||||
text = config.get('text').strip()
|
||||
passphrase = config.get('passphrase', '')
|
||||
password = password_dialog() if keystore.is_private(text) else None
|
||||
if keystore.is_address_list(text):
|
||||
wallet = Imported_Wallet(storage)
|
||||
for x in text.split():
|
||||
wallet.import_address(x)
|
||||
elif keystore.is_private_key_list(text):
|
||||
k = keystore.Imported_KeyStore({})
|
||||
storage.put('keystore', k.dump())
|
||||
storage.put('use_encryption', bool(password))
|
||||
wallet = Imported_Wallet(storage)
|
||||
for x in text.split():
|
||||
wallet.import_private_key(x, password)
|
||||
storage.write()
|
||||
else:
|
||||
if keystore.is_seed(text):
|
||||
k = keystore.from_seed(text, passphrase, False)
|
||||
elif keystore.is_master_key(text):
|
||||
k = keystore.from_master_key(text)
|
||||
else:
|
||||
sys.exit("Error: Seed or key not recognized")
|
||||
if password:
|
||||
k.update_password(None, password)
|
||||
storage.put('keystore', k.dump())
|
||||
storage.put('wallet_type', 'standard')
|
||||
storage.put('use_encryption', bool(password))
|
||||
storage.write()
|
||||
wallet = Wallet(storage)
|
||||
if not config.get('offline'):
|
||||
network = Network(config)
|
||||
network.start()
|
||||
wallet.start_network(network)
|
||||
print_msg("Recovering wallet...")
|
||||
wallet.synchronize()
|
||||
wallet.wait_until_synchronized()
|
||||
wallet.stop_threads()
|
||||
# note: we don't wait for SPV
|
||||
msg = "Recovery successful" if wallet.is_found() else "Found no history for this wallet"
|
||||
else:
|
||||
msg = "This wallet was restored offline. It may contain more addresses than displayed."
|
||||
print_msg(msg)
|
||||
|
||||
elif cmdname == 'create':
|
||||
password = password_dialog()
|
||||
passphrase = config.get('passphrase', '')
|
||||
seed_type = 'segwit' if config.get('segwit') else 'standard'
|
||||
seed = Mnemonic('en').make_seed(seed_type)
|
||||
k = keystore.from_seed(seed, passphrase, False)
|
||||
storage.put('keystore', k.dump())
|
||||
storage.put('wallet_type', 'standard')
|
||||
wallet = Wallet(storage)
|
||||
wallet.update_password(None, password, True)
|
||||
wallet.synchronize()
|
||||
print_msg("Your wallet generation seed is:\n\"%s\"" % seed)
|
||||
print_msg("Please keep it in a safe place; if you lose it, you will not be able to restore your wallet.")
|
||||
|
||||
wallet.storage.write()
|
||||
print_msg("Wallet saved in '%s'" % wallet.storage.path)
|
||||
sys.exit(0)
|
||||
|
||||
|
||||
def init_daemon(config_options):
|
||||
config = SimpleConfig(config_options)
|
||||
storage = WalletStorage(config.get_wallet_path())
|
||||
@@ -233,14 +157,12 @@ def init_cmdline(config_options, server):
|
||||
else:
|
||||
password = None
|
||||
|
||||
config_options['password'] = password
|
||||
config_options['password'] = config_options.get('password') or password
|
||||
|
||||
if cmd.name == 'password':
|
||||
new_password = prompt_password('New password:')
|
||||
config_options['new_password'] = new_password
|
||||
|
||||
return cmd, password
|
||||
|
||||
|
||||
def get_connected_hw_devices(plugins):
|
||||
support = plugins.get_hardware_support()
|
||||
@@ -297,7 +219,7 @@ def run_offline_command(config, config_options, plugins):
|
||||
# check password
|
||||
if cmd.requires_password and wallet.has_password():
|
||||
try:
|
||||
seed = wallet.check_password(password)
|
||||
wallet.check_password(password)
|
||||
except InvalidPassword:
|
||||
print_msg("Error: This password does not decode this wallet.")
|
||||
sys.exit(1)
|
||||
@@ -320,6 +242,7 @@ def run_offline_command(config, config_options, plugins):
|
||||
wallet.storage.write()
|
||||
return result
|
||||
|
||||
|
||||
def init_plugins(config, gui_name):
|
||||
from electrum.plugin import Plugins
|
||||
return Plugins(config, is_local or is_android, gui_name)
|
||||
@@ -406,11 +329,6 @@ if __name__ == '__main__':
|
||||
elif config.get('simnet'):
|
||||
constants.set_simnet()
|
||||
|
||||
# run non-RPC commands separately
|
||||
if cmdname in ['create', 'restore']:
|
||||
run_non_RPC(config)
|
||||
sys.exit(0)
|
||||
|
||||
if cmdname == 'gui':
|
||||
fd, server = daemon.get_fd_or_server(config)
|
||||
if fd is not None:
|
||||
|
||||
Reference in New Issue
Block a user