def create_new_wallet(parent: QWidget, initial_dirpath: str) -> Optional[str]: create_filepath, __ = QFileDialog.getSaveFileName(parent, _("Enter a new wallet file name"), initial_dirpath) assert not os.path.exists(create_filepath) dirpath, filename = os.path.split(create_filepath) if not dirpath or not os.path.isdir(dirpath) or not os.access(dirpath, os.R_OK | os.W_OK): MessageBox.show_error(_("The selected directory is not accessible.")) return None name_edit = QLabel(filename) fields = [ (QLabel(_("Wallet") +":"), name_edit), ] from .password_dialog import ChangePasswordDialog, PasswordAction from .wallet_wizard import PASSWORD_NEW_TEXT d = ChangePasswordDialog(parent, PASSWORD_NEW_TEXT, _("Create New Wallet"), fields, kind=PasswordAction.NEW) success, _old_password, new_password = d.run() if not success or not new_password.strip(): return None from electrumsv.storage import WalletStorage storage = WalletStorage(create_filepath) storage.put("password-token", pw_encode(os.urandom(32).hex(), new_password)) storage.close() return create_filepath
def validatePage(self) -> bool: # Called when 'Next' or 'Finish' is clicked for last-minute validation. result = self.isComplete() if result: new_password = self._password_layout.new_pw.text().strip() # If we are going to exit then create the empty wallet. wizard: WalletWizard = self.wizard() wallet_filepath = wizard.get_wallet_path() storage = WalletStorage(wallet_filepath) storage.put("password-token", pw_encode(os.urandom(32).hex(), new_password)) storage.close() return result
def test_write_dictionary_to_file(self): storage = WalletStorage(self.wallet_path) some_dict = { u"a": u"b", u"c": u"d", u"seed_version": FINAL_SEED_VERSION } for key, value in some_dict.items(): storage.put(key, value) storage.write() contents = "" with open(self.wallet_path, "r") as f: contents = f.read() self.assertEqual(some_dict, json.loads(contents))
def test_write_dictionary_to_file(self): storage = WalletStorage(self.wallet_path) some_dict = { "a": "b", "c": "d", "seed_version": FINAL_SEED_VERSION, "tx_store_aeskey": storage.get("tx_store_aeskey"), "wallet_author": "ESV"} for key, value in some_dict.items(): storage.put(key, value) storage.write() contents = "" with open(self.wallet_path, "r") as f: contents = f.read() self.assertEqual(some_dict, json.loads(contents))
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 = ImportedAddressWallet.from_text(storage, text) elif keystore.is_private_key_list(text): wallet = ImportedPrivkeyWallet.from_text(storage, text, password) 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_threads(network) print("Recovering wallet...") wallet.synchronize() wallet.wait_until_synchronized() 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) elif cmdname == 'create': password = password_dialog() passphrase = config.get('passphrase', '') seed_type = '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("Your wallet generation seed is:\n\"%s\"" % seed) print("Please keep it in a safe place; if you lose it, " "you will not be able to restore your wallet.") wallet.storage.write() print("Wallet saved in '%s'" % wallet.storage.path) sys.exit(0)