def test_write_dictionnary_to_file(self): storage = WalletStorage(self.wallet_path) some_dict = {u"a": u"b", u"c": u"d", u"seed_version": 12} 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 = { 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() 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 = { 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() with open(self.wallet_path, "r") as f: contents = f.read() d = json.loads(contents) for key, value in some_dict.items(): self.assertEqual(d[key], value)
class ElectrumWallet: """ An Electrum wallet wrapper """ def __init__(self, name, config): """ Initializes new electrum wallet instance @param name: Name of the wallet @param config: Configuration dictionary e.g { 'server': 'localhost:7777:s', 'rpc_user': '******', 'rpc_pass_': 'pass', 'electrum_path': '/opt/var/data/electrum', 'seed': '....', 'fee': 10000, 'testnet': 1 } """ self._name = name self._config = config self._config['testnet'] = bool(self._config['testnet']) if self._config['testnet'] is True: constants.set_testnet() self._config['verbos'] = False self._electrum_config = SimpleConfig(self._config) self._wallet_path = os.path.join(self._electrum_config.path, 'wallets', self._name) self._storage = WalletStorage(path=self._wallet_path) if not self._storage.file_exists(): self._electrum_config.set_key('default_wallet_path', self._wallet_path) k = keystore.from_seed(self._config['seed'], self._config['passphrase'], False) k.update_password(None, self._config['password']) self._storage.put('keystore', k.dump()) self._storage.put('wallet_type', 'standard') self._storage.put('use_encryption', bool(self._config['password'])) self._storage.write() self._wallet = Wallet(self._storage) # self._server = daemon.get_server(self._electrum_config) self._network = Network(self._electrum_config) self._network.start() self._wallet.start_threads(self._network) self._wallet.synchronize() self._wallet.wait_until_synchronized() self._wallet.stop_threads() self._wallet.storage.write() else: self._network = None self._wallet = self._wallet = Wallet(self._storage) self._commands = Commands(config=self._electrum_config, wallet=self._wallet, network=self._network) self._init_commands() def _init_commands(self): """ Scans the electrum commands class and binds all its methods to this class """ execlude_cmd = lambda item: (not item[0].startswith('_')) and item[ 0] not in EXECLUDED_COMMANDS for name, func in filter( execlude_cmd, inspect.getmembers(self._commands, inspect.ismethod)): setattr(self, name, func)
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_threads(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)