electrum

Electrum Bitcoin wallet
git clone https://git.parazyd.org/electrum
Log | Files | Refs | Submodules

commit f85b8f349ddc8905d9137d4fd92193aefaac51b9
parent ae80f143e7b82914c2f830d369df99e700bb1ccc
Author: SomberNight <somber.night@protonmail.com>
Date:   Thu, 28 Feb 2019 20:15:38 +0100

wallet: fix bug in restore_wallet_from_text, and write tests

Diffstat:
Melectrum/tests/test_wallet.py | 73++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---
Melectrum/wallet.py | 2+-
2 files changed, 71 insertions(+), 4 deletions(-)

diff --git a/electrum/tests/test_wallet.py b/electrum/tests/test_wallet.py @@ -4,13 +4,13 @@ import sys import os import json from decimal import Decimal -from unittest import TestCase import time from io import StringIO from electrum.storage import WalletStorage from electrum.json_db import FINAL_SEED_VERSION -from electrum.wallet import Abstract_Wallet +from electrum.wallet import (Abstract_Wallet, Standard_Wallet, create_new_wallet, + restore_wallet_from_text) from electrum.exchange_rate import ExchangeBase, FxThread from electrum.util import TxMinedInfo from electrum.bitcoin import COIN @@ -114,8 +114,9 @@ class FakeWallet: txid = 'abc' ccy = 'TEST' -class TestFiat(TestCase): +class TestFiat(SequentialTestCase): def setUp(self): + super().setUp() self.value_sat = COIN self.fiat_value = {} self.wallet = FakeWallet(fiat_value=self.fiat_value) @@ -143,3 +144,69 @@ class TestFiat(TestCase): def test_save_garbage(self): self.assertEqual(False, Abstract_Wallet.set_fiat_value(self.wallet, txid, ccy, 'garbage', self.fx, self.value_sat)) self.assertNotIn(ccy, self.fiat_value) + + +class TestCreateRestoreWallet(WalletTestCase): + + def test_create_new_wallet(self): + passphrase = 'mypassphrase' + password = 'mypassword' + encrypt_file = True + d = create_new_wallet(path=self.wallet_path, + passphrase=passphrase, + password=password, + encrypt_file=encrypt_file, + segwit=True) + wallet = d['wallet'] # type: Standard_Wallet + wallet.check_password(password) + self.assertEqual(passphrase, wallet.keystore.get_passphrase(password)) + self.assertEqual(d['seed'], wallet.keystore.get_seed(password)) + self.assertEqual(encrypt_file, wallet.storage.is_encrypted()) + + def test_restore_wallet_from_text_mnemonic(self): + text = 'bitter grass shiver impose acquire brush forget axis eager alone wine silver' + passphrase = 'mypassphrase' + password = 'mypassword' + encrypt_file = True + d = restore_wallet_from_text(text, + path=self.wallet_path, + network=None, + passphrase=passphrase, + password=password, + encrypt_file=encrypt_file) + wallet = d['wallet'] # type: Standard_Wallet + self.assertEqual(passphrase, wallet.keystore.get_passphrase(password)) + self.assertEqual(text, wallet.keystore.get_seed(password)) + self.assertEqual(encrypt_file, wallet.storage.is_encrypted()) + self.assertEqual('bc1q2ccr34wzep58d4239tl3x3734ttle92a8srmuw', wallet.get_receiving_addresses()[0]) + + def test_restore_wallet_from_text_xpub(self): + text = 'zpub6nydoME6CFdJtMpzHW5BNoPz6i6XbeT9qfz72wsRqGdgGEYeivso6xjfw8cGcCyHwF7BNW4LDuHF35XrZsovBLWMF4qXSjmhTXYiHbWqGLt' + d = restore_wallet_from_text(text, path=self.wallet_path, network=None) + wallet = d['wallet'] # type: Standard_Wallet + self.assertEqual(text, wallet.keystore.get_master_public_key()) + self.assertEqual('bc1q2ccr34wzep58d4239tl3x3734ttle92a8srmuw', wallet.get_receiving_addresses()[0]) + + def test_restore_wallet_from_text_xprv(self): + text = 'zprvAZzHPqhCMt51fskXBUYB1fTFYgG3CBjJUT4WEZTpGw6hPSDWBPZYZARC5sE9xAcX8NeWvvucFws8vZxEa65RosKAhy7r5MsmKTxr3hmNmea' + d = restore_wallet_from_text(text, path=self.wallet_path, network=None) + wallet = d['wallet'] # type: Standard_Wallet + self.assertEqual(text, wallet.keystore.get_master_private_key(password=None)) + self.assertEqual('bc1q2ccr34wzep58d4239tl3x3734ttle92a8srmuw', wallet.get_receiving_addresses()[0]) + + def test_restore_wallet_from_text_addresses(self): + text = 'bc1q2ccr34wzep58d4239tl3x3734ttle92a8srmuw bc1qnp78h78vp92pwdwq5xvh8eprlga5q8gu66960c' + d = restore_wallet_from_text(text, path=self.wallet_path, network=None) + wallet = d['wallet'] # type: Abstract_Wallet + self.assertEqual('bc1q2ccr34wzep58d4239tl3x3734ttle92a8srmuw', wallet.get_receiving_addresses()[0]) + self.assertEqual(2, len(wallet.get_receiving_addresses())) + + def test_restore_wallet_from_text_privkeys(self): + text = 'p2wpkh:L4jkdiXszG26SUYvwwJhzGwg37H2nLhrbip7u6crmgNeJysv5FHL p2wpkh:L24GxnN7NNUAfCXA6hFzB1jt59fYAAiFZMcLaJ2ZSawGpM3uqhb1' + d = restore_wallet_from_text(text, path=self.wallet_path, network=None) + wallet = d['wallet'] # type: Abstract_Wallet + addr0 = wallet.get_receiving_addresses()[0] + self.assertEqual('bc1q2ccr34wzep58d4239tl3x3734ttle92a8srmuw', addr0) + self.assertEqual('p2wpkh:L4jkdiXszG26SUYvwwJhzGwg37H2nLhrbip7u6crmgNeJysv5FHL', + wallet.export_private_key(addr0, password=None)[0]) + self.assertEqual(2, len(wallet.get_receiving_addresses())) diff --git a/electrum/wallet.py b/electrum/wallet.py @@ -1892,7 +1892,7 @@ def restore_wallet_from_text(text, *, path, network, passphrase=None, password=N k = keystore.Imported_KeyStore({}) storage.put('keystore', k.dump()) wallet = Imported_Wallet(storage) - keys = keystore.get_private_keys(text) + keys = keystore.get_private_keys(text, allow_spaces_inside_key=False) good_inputs, bad_inputs = wallet.import_private_keys(keys, None, write_to_disk=False) # FIXME tell user about bad_inputs if not good_inputs: