def test_read_dictionary_from_file(self): some_dict = {"a": "b", "c": "d"} contents = json.dumps(some_dict) with open(self.wallet_path, "w") as f: contents = f.write(contents) storage = WalletStorage(self.wallet_path, manual_upgrades=True) self.assertEqual("b", storage.get("a")) self.assertEqual("d", storage.get("c"))
def launch_wizard(): wizard = Factory.InstallWizard(self.electrum_config, self.plugins) wizard.path = path wizard.bind(on_wizard_complete=self.on_wizard_complete) storage = WalletStorage(path, manual_upgrades=True) if not storage.file_exists(): wizard.run('new') elif storage.is_encrypted(): raise Exception("Kivy GUI does not support encrypted wallet files.") elif storage.requires_upgrade(): wizard.upgrade_storage(storage) else: raise Exception("unexpected storage file situation")
def on_filename(filename): # FIXME? "filename" might contain ".." (etc) and hence sketchy path traversals are possible nonlocal temp_storage temp_storage = None msg = None path = os.path.join(wallet_folder, filename) wallet_from_memory = get_wallet_from_daemon(path) try: if wallet_from_memory: temp_storage = wallet_from_memory.storage # type: Optional[WalletStorage] else: temp_storage = WalletStorage(path, manual_upgrades=True) except (StorageReadWriteError, WalletFileException) as e: msg = _('Cannot read file') + f'\n{repr(e)}' except Exception as e: self.logger.exception('') msg = _('Cannot read file') + f'\n{repr(e)}' self.next_button.setEnabled(temp_storage is not None) user_needs_to_enter_password = False if temp_storage: if not temp_storage.file_exists(): msg =_("This file does not exist.") + '\n' \ + _("Press 'Next' to create this wallet, or choose another file.") elif not wallet_from_memory: if temp_storage.is_encrypted_with_user_pw(): msg = _("This file is encrypted with a password.") + '\n' \ + _('Enter your password or choose another file.') user_needs_to_enter_password = True elif temp_storage.is_encrypted_with_hw_device(): msg = _("This file is encrypted using a hardware device.") + '\n' \ + _("Press 'Next' to choose device to decrypt.") else: msg = _("Press 'Next' to open this wallet.") else: msg = _("This file is already open in memory.") + "\n" \ + _("Press 'Next' to create/focus window.") if msg is None: msg = _('Cannot read file') self.msg_label.setText(msg) if user_needs_to_enter_password: self.pw_label.show() self.pw_e.show() self.pw_e.setFocus() else: self.pw_label.hide() self.pw_e.hide()
def test_encrypt_message(self): key = WalletStorage.get_eckey_from_password('secret_password77') msgs = [bytes([0] * 555), b'cannot think of anything funny'] for plaintext in msgs: ciphertext1 = key.encrypt_message(plaintext) ciphertext2 = key.encrypt_message(plaintext) self.assertEqual(plaintext, key.decrypt_message(ciphertext1)) self.assertEqual(plaintext, key.decrypt_message(ciphertext2)) self.assertNotEqual(ciphertext1, ciphertext2)
def __init__(self, config, daemon, plugins): self.config = config self.network = daemon.network storage = WalletStorage(config.get_wallet_path()) if not storage.file_exists(): print("Wallet not found. try 'electrum create'") exit() if storage.is_encrypted(): password = getpass.getpass('Password:'******'') self.encoding = locale.getpreferredencoding() self.stdscr = curses.initscr() curses.noecho() curses.cbreak() curses.start_color() curses.use_default_colors() curses.init_pair(1, curses.COLOR_WHITE, curses.COLOR_BLUE) curses.init_pair(2, curses.COLOR_WHITE, curses.COLOR_CYAN) curses.init_pair(3, curses.COLOR_BLACK, curses.COLOR_WHITE) self.stdscr.keypad(1) self.stdscr.border(0) self.maxy, self.maxx = self.stdscr.getmaxyx() self.set_cursor(0) self.w = curses.newwin(10, 50, 5, 5) console_stderr_handler.setLevel(logging.CRITICAL) self.tab = 0 self.pos = 0 self.popup_pos = 0 self.str_recipient = "" self.str_description = "" self.str_amount = "" self.str_fee = "" self.history = None if self.network: self.network.register_callback( self.update, ['wallet_updated', 'network_updated']) self.tab_names = [ _("History"), _("Send"), _("Receive"), _("Addresses"), _("Contacts"), _("Banner") ] self.num_tabs = len(self.tab_names)
def test_decrypt_message(self): key = WalletStorage.get_eckey_from_password('pw123') self.assertEqual( b'me<(s_s)>age', key.decrypt_message( b'QklFMQMDFtgT3zWSQsa+Uie8H/WvfUjlu9UN9OJtTt3KlgKeSTi6SQfuhcg1uIz9hp3WIUOFGTLr4RNQBdjPNqzXwhkcPi2Xsbiw6UCNJncVPJ6QBg==' )) self.assertEqual( b'me<(s_s)>age', key.decrypt_message( b'QklFMQKXOXbylOQTSMGfo4MFRwivAxeEEkewWQrpdYTzjPhqj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Vk2viaxcGqwTc5KMexQFLAUwqhv+aIik7U+5qk/gEVSuRoVkihoweFzKolNF+BknH2oB4rZdPixag5Zje3DvgjsSFlOl69W/67t/Gs8htfSAaHlsB8vWRQr9+v/lxTbrAw+O0E+sYGoObQ4qQMyQshNZEHbpPg63eWiHtJJnrVBvOeIbIHzoLDnMDsWVWZSMzAQ1vhX1H5QLgSEbRlKSliVY03kDkh/Nk/KOn+B2q37Ialq4JcRoIYFGJ8AoYEAD0tRuTqFddIclE75HzwaNG7NyKW1plsa72ciOPwsPJsdd5F0qdSQ3OSKtooTn7uf6dXOc4lDkfrVYRlZ0PX' ))
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)
config = SimpleConfig({"testnet": True}) # to use ~/.electrum-gzro/testnet as datadir constants.set_testnet() # to set testnet magic bytes daemon = Daemon(config, listen_jsonrpc=False) network = daemon.network assert network.asyncio_loop.is_running() # get wallet on disk wallet_dir = os.path.dirname(config.get_wallet_path()) wallet_path = os.path.join(wallet_dir, "test_wallet") if not os.path.exists(wallet_path): create_new_wallet(path=wallet_path, config=config) # open wallet storage = WalletStorage(wallet_path) wallet = Wallet(storage, config=config) wallet.start_network(network) # you can use ~CLI commands by accessing command_runner command_runner = Commands(config=config, daemon=daemon, network=network) command_runner.wallet = wallet print("balance", command_runner.getbalance()) print("addr", command_runner.getunusedaddress()) print( "gettx", command_runner.gettransaction( "bd3a700b2822e10a034d110c11a596ee7481732533eb6aca7f9ca02911c70a4f")) # but you might as well interact with the underlying methods directly print("balance", wallet.get_balance())