Debian Codegraph @

tests/test_wallet.py

raw symbols SCIP
1 import shutil
2 import tempfile
3 import sys
4 import os
5 import json
6 from decimal import Decimal
7 import time
8 from io import StringIO
9 import asyncio
10 from unittest import mock
11 from pathlib import Path
12
13 from electrum.storage import WalletStorage
14 from electrum.wallet_db import FINAL_SEED_VERSION
15 from electrum.wallet import (Abstract_Wallet, Standard_Wallet, create_new_wallet,
16 Imported_Wallet, Wallet)
17 from electrum.exchange_rate import ExchangeBase, FxThread
18 from electrum.util import TxMinedInfo, InvalidPassword
19 from electrum.bitcoin import COIN
20 from electrum.wallet_db import WalletDB, JsonDB
21 from electrum.simple_config import SimpleConfig
22 from electrum import util, storage
23 from electrum.daemon import Daemon
24 from electrum.invoices import PR_UNPAID, PR_PAID, PR_UNCONFIRMED
25 from electrum.transaction import tx_from_any
26 from electrum.address_synchronizer import TX_HEIGHT_UNCONFIRMED
27
28 from . import ElectrumTestCase
29 from . import restore_wallet_from_text__for_unittest
30
31
32 class FakeSynchronizer(object):
33
34 def __init__(self, db):
35 self.db = db
36 self.store = []
37
38 def add(self, address):
39 self.store.append(address)
40
41
42 class WalletTestCase(ElectrumTestCase):
43
44 def setUp(self):
45 super(WalletTestCase, self).setUp()
46 self.config = SimpleConfig({'electrum_path': self.electrum_path})
47
48 self.wallet_path = os.path.join(self.electrum_path, "somewallet")
49
50 self._saved_stdout = sys.stdout
51 self._stdout_buffer = StringIO()
52 sys.stdout = self._stdout_buffer
53
54 def tearDown(self):
55 super(WalletTestCase, self).tearDown()
56 # Restore the "real" stdout
57 sys.stdout = self._saved_stdout
58
59
60 class TestWalletStorage(WalletTestCase):
61
62 def test_read_dictionary_from_file(self):
63
64 some_dict = {"a":"b", "c":"d"}
65 contents = json.dumps(some_dict)
66 with open(self.wallet_path, "w") as f:
67 contents = f.write(contents)
68
69 storage = WalletStorage(self.wallet_path)
70 db = JsonDB(storage.read(), storage=storage)
71 self.assertEqual("b", db.get("a"))
72 self.assertEqual("d", db.get("c"))
73
74 def test_write_dictionary_to_file(self):
75
76 storage = WalletStorage(self.wallet_path)
77 db = JsonDB('', storage=storage)
78
79 some_dict = {
80 u"a": u"b",
81 u"c": u"d",
82 u"seed_version": FINAL_SEED_VERSION}
83
84 for key, value in some_dict.items():
85 db.put(key, value)
86 db.write()
87
88 with open(self.wallet_path, "r") as f:
89 contents = f.read()
90 d = json.loads(contents)
91 for key, value in some_dict.items():
92 self.assertEqual(d[key], value)
93
94 async def test_appends_after_incomplete_data_recovery_survive_reload(self):
95 storage = WalletStorage(self.wallet_path, allow_partial_writes=True)
96 db = JsonDB('', storage=storage)
97 db.put("a", "b")
98 db.write()
99 # simulate a crash that truncated an appended patch
100 with open(self.wallet_path, "a") as f:
101 f.write(',\n{"op": "add", "path": "/x", "value": {"inco')
102 # reopen: recovery drops the incomplete patch
103 storage = WalletStorage(self.wallet_path, allow_partial_writes=True)
104 db = JsonDB(storage.read(), storage=storage)
105 self.assertEqual(db.get("a"), "b")
106 # append a new patch to the recovered wallet
107 db.put("c", "d")
108 db.write()
109 # reopen: the appended patch must not be lost
110 storage = WalletStorage(self.wallet_path, allow_partial_writes=True)
111 db = JsonDB(storage.read(), storage=storage)
112 self.assertEqual(db.get("c"), "d")
113
114 async def test_storage_imported_add_privkeys_persistence_test(self):
115 text = ' '.join([
116 'p2wpkh:L4jkdiXszG26SUYvwwJhzGwg37H2nLhrbip7u6crmgNeJysv5FHL',
117 'p2wpkh:L24GxnN7NNUAfCXA6hFzB1jt59fYAAiFZMcLaJ2ZSawGpM3uqhb1'
118 ])
119 d = restore_wallet_from_text__for_unittest(text, path=self.wallet_path, config=self.config)
120 wallet = d['wallet'] # type: Imported_Wallet
121 self.assertEqual(2, len(wallet.get_receiving_addresses()))
122 await wallet.stop()
123
124 # open the wallet anew again, and add a privkey. This should add the new data as a json_patch
125 del wallet
126 wallet = Daemon._load_wallet(self.wallet_path, password=None, config=self.config)
127
128 wallet.import_private_keys(['p2wpkh:KzuqaaLp9zYjVuj8vQtCwFdiZFreW3NJNBachgVS8S9XMgj5y78b'], password=None)
129 self.assertEqual(3, len(wallet.get_receiving_addresses()))
130 self.assertEqual(3, len(wallet.keystore.keypairs))
131 await wallet.stop()
132
133 # open the wallet anew again, and verify if the privkey was stored
134 del wallet
135 wallet = Daemon._load_wallet(self.wallet_path, password=None, config=self.config)
136 self.assertEqual(3, len(wallet.get_receiving_addresses()))
137 self.assertEqual(3, len(wallet.keystore.keypairs))
138 self.assertTrue('03bf450797034dc95693096e575e3b3db14e5f074679b349b727f90fc7804ce7ab' in wallet.keystore.keypairs)
139 self.assertTrue('030dac677b9484e23db6f9255eddf433f4f12c02f9b35e0100f2f103ffbccf540f' in wallet.keystore.keypairs)
140 self.assertTrue('02f11d5f222a728fd08226cb5a1e85a74d58fc257bd3764bf1234346f91defed72' in wallet.keystore.keypairs)
141
142 async def test_storage_prevouts_by_scripthash_persistence(self):
143 text = 'cycle rocket west magnet parrot shuffle foot correct salt library feed song'
144 d = restore_wallet_from_text__for_unittest(text, path=self.wallet_path, config=self.config)
145 wallet1 = d['wallet'] # type: Standard_Wallet
146 # create payreq
147 addr = wallet1.get_unused_address()
148 self.assertEqual("1NNkttn1YvVGdqBW4PR6zvc3Zx3H5owKRf", addr)
149 pr_key = wallet1.create_request(amount_sat=10000, message="msg", address=addr, exp_delay=86400)
150 pr = wallet1.get_request(pr_key)
151 self.assertIsNotNone(pr)
152 self.assertEqual(PR_UNPAID, wallet1.get_invoice_status(pr))
153 await wallet1.stop()
154
155 # open the wallet anew again, and get paid onchain
156 del wallet1
157 wallet1 = Daemon._load_wallet(self.wallet_path, password=None, config=self.config)
158 tx = tx_from_any("02000000000101a97a9ae7fb1a9220fdd170a974987ac24631dcff89b60fa4907c78c3639994db0000000000fdffffff0210270000000000001976a914ea7804a2c266063572cc009a63dc25dcc0e9d9b588ac20491e0000000000160014b8e4fdc91593b67de2bf214694ef47e38dc2ee8e02473044022005326882904906cfa9c1de75333ace1019596f2ab25d21118220d037dfc0e48b02207d0b3f075cfe5e1e0247ff3cdd7155dc05e7459daf1bfa0ea02e9112b9151ec90121026cc6a74c2b0e38661d341ffae48fe7dde5196ca4afe95d28b496673fa4cf646700000000")
159 wallet1.adb.receive_tx_callback(tx, tx_height=TX_HEIGHT_UNCONFIRMED)
160 self.assertEqual(PR_UNCONFIRMED, wallet1.get_invoice_status(pr))
161 await wallet1.stop()
162
163 # open the wallet anew again, and verify payreq is still paid
164 del wallet1
165 wallet1 = Daemon._load_wallet(self.wallet_path, password=None, config=self.config)
166 self.assertEqual(PR_UNCONFIRMED, wallet1.get_invoice_status(pr))
167
168
169 class FakeExchange(ExchangeBase):
170 def __init__(self, rate):
171 super().__init__(lambda self: None, lambda self: None)
172 self._quotes = {'TEST': rate}
173 self._quotes_timestamp = float("inf") # spot price from the far future never becomes stale :P
174
175 class FakeFxThread:
176 def __init__(self, exchange):
177 self.exchange = exchange
178 self.ccy = 'TEST'
179
180 remove_thousands_separator = staticmethod(FxThread.remove_thousands_separator)
181 timestamp_rate = FxThread.timestamp_rate
182 ccy_amount_str = FxThread.ccy_amount_str
183 history_rate = FxThread.history_rate
184
185 class FakeADB:
186 def get_tx_height(self, txid):
187 # because we use a current timestamp, and history is empty,
188 # FxThread.history_rate will use spot prices
189 return TxMinedInfo(_height=10, conf=10, timestamp=int(time.time()), header_hash='def')
190
191 class FakeWallet:
192 def __init__(self, fiat_value):
193 super().__init__()
194 self.fiat_value = fiat_value
195 self.db = WalletDB('', storage=None, upgrade=False)
196 self.adb = FakeADB()
197 self.db.transactions = self.db.verified_tx = {'abc':'Tx'}
198
199 default_fiat_value = Abstract_Wallet.default_fiat_value
200 price_at_timestamp = Abstract_Wallet.price_at_timestamp
201 class storage:
202 put = lambda self, x: None
203
204 txid = 'abc'
205 ccy = 'TEST'
206
207 class TestFiat(ElectrumTestCase):
208 def setUp(self):
209 super().setUp()
210 self.value_sat = COIN
211 self.fiat_value = {}
212 self.wallet = FakeWallet(fiat_value=self.fiat_value)
213 self.fx = FakeFxThread(FakeExchange(Decimal('1000.001')))
214 default_fiat = Abstract_Wallet.default_fiat_value(self.wallet, txid, self.fx, self.value_sat)
215 self.assertEqual(Decimal('1000.001'), default_fiat)
216 self.assertEqual('1 000.00', self.fx.ccy_amount_str(default_fiat, add_thousands_sep=True))
217
218 def test_save_fiat_and_reset(self):
219 self.assertEqual(False, Abstract_Wallet.set_fiat_value(self.wallet, txid, ccy, '1000.01', self.fx, self.value_sat))
220 saved = self.fiat_value[ccy][txid]
221 self.assertEqual('1 000.01', self.fx.ccy_amount_str(Decimal(saved), add_thousands_sep=True))
222 self.assertEqual(True, Abstract_Wallet.set_fiat_value(self.wallet, txid, ccy, '', self.fx, self.value_sat))
223 self.assertNotIn(txid, self.fiat_value[ccy])
224 # even though we are not setting it to the exact fiat value according to the exchange rate, precision is truncated away
225 self.assertEqual(True, Abstract_Wallet.set_fiat_value(self.wallet, txid, ccy, '1 000.002', self.fx, self.value_sat))
226
227 def test_too_high_precision_value_resets_with_no_saved_value(self):
228 self.assertEqual(True, Abstract_Wallet.set_fiat_value(self.wallet, txid, ccy, '1 000.001', self.fx, self.value_sat))
229
230 def test_empty_resets(self):
231 self.assertEqual(True, Abstract_Wallet.set_fiat_value(self.wallet, txid, ccy, '', self.fx, self.value_sat))
232 self.assertNotIn(ccy, self.fiat_value)
233
234 def test_save_garbage(self):
235 self.assertEqual(False, Abstract_Wallet.set_fiat_value(self.wallet, txid, ccy, 'garbage', self.fx, self.value_sat))
236 self.assertNotIn(ccy, self.fiat_value)
237
238
239 class TestHistoryExport(ElectrumTestCase):
240 TESTNET = True
241
242 def setUp(self):
243 # mock timezone, explicitly define timezone as the CI seems to miss a timezone db and cant resolve just 'CET'
244 self.patch_timezone = mock.patch.dict(os.environ, {'TZ': 'CET-1CEST,M3.5.0,M10.5.0/3'})
245 self.patch_timezone.start()
246 time.tzset()
247 super(TestHistoryExport, self).setUp()
248 shutil.copytree(Path(__file__).parent / "fiat_fx_data", Path(self.electrum_path) / "cache")
249 self.config = SimpleConfig({'electrum_path': self.electrum_path})
250
251 def tearDown(self):
252 super(TestHistoryExport, self).tearDown()
253 self.patch_timezone.stop()
254 time.tzset()
255
256 @mock.patch('electrum.wallet.run_hook')
257 @mock.patch.object(storage.WalletStorage, 'write')
258 @mock.patch.object(storage.WalletStorage, 'append')
259 async def test_export_history_to_file(self, _mock_append, _mock_write, mock_run_hook):
260 # prepare wallet with realistic history
261 c = self.config
262 c.NETWORK_OFFLINE = True
263 c.FX_EXCHANGE, c.FX_CURRENCY, c.FX_USE_EXCHANGE_RATE, c.FX_HISTORY_RATES = "BitFinex", "EUR", True, True
264 daemon = Daemon(config=c, listen_jsonrpc=False)
265 test_wallet_name = "client_4_5_2_9dk_with_ln" # has labels, local tx, ln tx
266 wallet_path = self.get_wallet_file_path(test_wallet_name)
267 test_wallet = daemon.load_wallet(wallet_path, None, upgrade=True)
268 self.assertTrue(daemon.fx.has_history())
269 mock_run_hook.return_value = False
270
271 testcases = (
272 f'history_no_fx_{test_wallet_name}.csv',
273 f'history_with_fx_{test_wallet_name}.csv',
274 f'history_no_fx_{test_wallet_name}.json',
275 f'history_with_fx_{test_wallet_name}.json',
276 )
277 for filename in testcases:
278 test_export_path = (Path(self.electrum_path) / filename).as_posix()
279 is_csv = filename.endswith('.csv')
280 fx = daemon.fx if 'with_fx' in filename else None
281 test_wallet.export_history_to_file(
282 file_path=test_export_path,
283 is_csv=is_csv,
284 fx=fx,
285 )
286 mock_run_hook.assert_called_with('export_history_to_file', test_wallet, fx, test_export_path, is_csv)
287 reference_path = Path(__file__).parent / "test_history_export" / filename
288 with open(reference_path, 'r', encoding='utf-8') as reference:
289 reference_text = reference.readlines()
290 with open(test_export_path, 'r', encoding='utf-8') as test_export:
291 test_export_text = test_export.readlines()
292 # compare line by line for more readable traceback on difference
293 for reference, test in zip(reference_text, test_export_text):
294 self.assertEqual(reference, test)
295
296
297 class TestCreateRestoreWallet(WalletTestCase):
298
299 async def test_create_new_wallet(self):
300 passphrase = 'mypassphrase'
301 password = 'mypassword'
302 encrypt_file = True
303 d = create_new_wallet(path=self.wallet_path,
304 passphrase=passphrase,
305 password=password,
306 encrypt_file=encrypt_file,
307 gap_limit=1,
308 gap_limit_for_change=1,
309 config=self.config)
310 wallet = d['wallet'] # type: Standard_Wallet
311
312 # lightning initialization
313 self.assertTrue(wallet.db.get('lightning_xprv').startswith('zprv'))
314
315 wallet.check_password(password)
316 self.assertEqual(passphrase, wallet.keystore.get_passphrase(password))
317 self.assertEqual(d['seed'], wallet.keystore.get_seed(password))
318 self.assertEqual(encrypt_file, wallet.storage.is_encrypted())
319
320 async def test_restore_wallet_from_text_mnemonic(self):
321 text = 'bitter grass shiver impose acquire brush forget axis eager alone wine silver'
322 passphrase = 'mypassphrase'
323 password = 'mypassword'
324 encrypt_file = True
325 d = restore_wallet_from_text__for_unittest(
326 text,
327 path=self.wallet_path,
328 passphrase=passphrase,
329 password=password,
330 encrypt_file=encrypt_file,
331 gap_limit=1,
332 config=self.config)
333 wallet = d['wallet'] # type: Standard_Wallet
334 self.assertEqual(passphrase, wallet.keystore.get_passphrase(password))
335 self.assertEqual(text, wallet.keystore.get_seed(password))
336 self.assertEqual(encrypt_file, wallet.storage.is_encrypted())
337 self.assertEqual('bc1q2ccr34wzep58d4239tl3x3734ttle92a8srmuw', wallet.get_receiving_addresses()[0])
338
339 async def test_restore_wallet_from_text_no_storage(self):
340 text = 'bitter grass shiver impose acquire brush forget axis eager alone wine silver'
341 d = restore_wallet_from_text__for_unittest(
342 text,
343 path=None,
344 gap_limit=1,
345 config=self.config,
346 )
347 wallet = d['wallet'] # type: Standard_Wallet
348 self.assertEqual(None, wallet.storage)
349 self.assertEqual(text, wallet.keystore.get_seed(None))
350 self.assertEqual('bc1q3g5tmkmlvxryhh843v4dz026avatc0zzr6h3af', wallet.get_receiving_addresses()[0])
351
352 async def test_restore_wallet_from_text_xpub(self):
353 text = 'zpub6nydoME6CFdJtMpzHW5BNoPz6i6XbeT9qfz72wsRqGdgGEYeivso6xjfw8cGcCyHwF7BNW4LDuHF35XrZsovBLWMF4qXSjmhTXYiHbWqGLt'
354 d = restore_wallet_from_text__for_unittest(text, path=self.wallet_path, gap_limit=1, config=self.config)
355 wallet = d['wallet'] # type: Standard_Wallet
356 self.assertEqual(text, wallet.keystore.get_master_public_key())
357 self.assertEqual('bc1q2ccr34wzep58d4239tl3x3734ttle92a8srmuw', wallet.get_receiving_addresses()[0])
358
359 async def test_restore_wallet_from_text_xkey_that_is_also_a_valid_electrum_seed_by_chance(self):
360 text = 'yprvAJBpuoF4FKpK92ofzQ7ge6VJMtorow3maAGPvPGj38ggr2xd1xCrC9ojUVEf9jhW5L9SPu6fU2U3o64cLrRQ83zaQGNa6YP3ajZS6hHNPXj'
361 d = restore_wallet_from_text__for_unittest(text, path=self.wallet_path, gap_limit=1, config=self.config)
362 wallet = d['wallet'] # type: Standard_Wallet
363 self.assertEqual(text, wallet.keystore.get_master_private_key(password=None))
364 self.assertEqual('3Pa4hfP3LFWqa2nfphYaF7PZfdJYNusAnp', wallet.get_receiving_addresses()[0])
365
366 async def test_restore_wallet_from_text_xprv(self):
367 text = 'zprvAZzHPqhCMt51fskXBUYB1fTFYgG3CBjJUT4WEZTpGw6hPSDWBPZYZARC5sE9xAcX8NeWvvucFws8vZxEa65RosKAhy7r5MsmKTxr3hmNmea'
368 d = restore_wallet_from_text__for_unittest(text, path=self.wallet_path, gap_limit=1, config=self.config)
369 wallet = d['wallet'] # type: Standard_Wallet
370 self.assertEqual(text, wallet.keystore.get_master_private_key(password=None))
371 self.assertEqual('bc1q2ccr34wzep58d4239tl3x3734ttle92a8srmuw', wallet.get_receiving_addresses()[0])
372
373 async def test_restore_wallet_from_text_addresses(self):
374 text = 'bc1q2ccr34wzep58d4239tl3x3734ttle92a8srmuw bc1qnp78h78vp92pwdwq5xvh8eprlga5q8gu66960c'
375 d = restore_wallet_from_text__for_unittest(text, path=self.wallet_path, config=self.config)
376 wallet = d['wallet'] # type: Imported_Wallet
377 self.assertEqual('bc1q2ccr34wzep58d4239tl3x3734ttle92a8srmuw', wallet.get_receiving_addresses()[0])
378 self.assertEqual(2, len(wallet.get_receiving_addresses()))
379 # also test addr deletion
380 wallet.delete_address('bc1qnp78h78vp92pwdwq5xvh8eprlga5q8gu66960c')
381 self.assertEqual(1, len(wallet.get_receiving_addresses()))
382
383 async def test_restore_wallet_from_text_privkeys(self):
384 text = 'p2wpkh:L4jkdiXszG26SUYvwwJhzGwg37H2nLhrbip7u6crmgNeJysv5FHL p2wpkh:L24GxnN7NNUAfCXA6hFzB1jt59fYAAiFZMcLaJ2ZSawGpM3uqhb1'
385 d = restore_wallet_from_text__for_unittest(text, path=self.wallet_path, config=self.config)
386 wallet = d['wallet'] # type: Imported_Wallet
387 addr0 = wallet.get_receiving_addresses()[0]
388 self.assertEqual('bc1q2ccr34wzep58d4239tl3x3734ttle92a8srmuw', addr0)
389 self.assertEqual('p2wpkh:L4jkdiXszG26SUYvwwJhzGwg37H2nLhrbip7u6crmgNeJysv5FHL',
390 wallet.export_private_key(addr0, password=None))
391 self.assertEqual(2, len(wallet.get_receiving_addresses()))
392 # also test addr deletion
393 wallet.delete_address('bc1qnp78h78vp92pwdwq5xvh8eprlga5q8gu66960c')
394 self.assertEqual(1, len(wallet.get_receiving_addresses()))
395
396
397 class TestWalletPassword(WalletTestCase):
398
399 async def test_update_password_of_imported_wallet(self):
400 wallet_str = '{"addr_history":{"1364Js2VG66BwRdkaoxAaFtdPb1eQgn8Dr":[],"15CyDgLffJsJgQrhcyooFH4gnVDG82pUrA":[],"1Exet2BhHsFxKTwhnfdsBMkPYLGvobxuW6":[]},"addresses":{"change":[],"receiving":["1364Js2VG66BwRdkaoxAaFtdPb1eQgn8Dr","1Exet2BhHsFxKTwhnfdsBMkPYLGvobxuW6","15CyDgLffJsJgQrhcyooFH4gnVDG82pUrA"]},"keystore":{"keypairs":{"0344b1588589958b0bcab03435061539e9bcf54677c104904044e4f8901f4ebdf5":"L2sED74axVXC4H8szBJ4rQJrkfem7UMc6usLCPUoEWxDCFGUaGUM","0389508c13999d08ffae0f434a085f4185922d64765c0bff2f66e36ad7f745cc5f":"L3Gi6EQLvYw8gEEUckmqawkevfj9s8hxoQDFveQJGZHTfyWnbk1U","04575f52b82f159fa649d2a4c353eb7435f30206f0a6cb9674fbd659f45082c37d559ffd19bea9c0d3b7dcc07a7b79f4cffb76026d5d4dff35341efe99056e22d2":"5JyVyXU1LiRXATvRTQvR9Kp8Rx1X84j2x49iGkjSsXipydtByUq"},"type":"imported"},"pruned_txo":{},"seed_version":13,"stored_height":-1,"transactions":{},"tx_fees":{},"txi":{},"txo":{},"use_encryption":false,"verified_tx3":{},"wallet_type":"standard","winpos-qt":[100,100,840,405]}'
401 storage = WalletStorage(self.wallet_path)
402 db = WalletDB(wallet_str, storage=storage, upgrade=True)
403 wallet = Wallet(db, config=self.config)
404
405 wallet.check_password(None)
406
407 wallet.update_password(None, "1234")
408
409 with self.assertRaises(InvalidPassword):
410 wallet.check_password(None)
411 with self.assertRaises(InvalidPassword):
412 wallet.check_password("wrong password")
413 wallet.check_password("1234")
414
415 async def test_update_password_of_standard_wallet(self):
416 wallet_str = '''{"addr_history":{"12ECgkzK6gHouKAZ7QiooYBuk1CgJLJxes":[],"12iR43FPb5M7sw4Mcrr5y1nHKepg9EtZP1":[],"13HT1pfWctsSXVFzF76uYuVdQvcAQ2MAgB":[],"13kG9WH9JqS7hyCcVL1ssLdNv4aXocQY9c":[],"14Tf3qiiHJXStSU4KmienAhHfHq7FHpBpz":[],"14gmBxYV97mzYwWdJSJ3MTLbTHVegaKrcA":[],"15FGuHvRssu1r8fCw98vrbpfc3M4xs5FAV":[],"17oJzweA2gn6SDjsKgA9vUD5ocT1sSnr2Z":[],"18hNcSjZzRcRP6J2bfFRxp9UfpMoC4hGTv":[],"18n9PFxBjmKCGhd4PCDEEqYsi2CsnEfn2B":[],"19a98ZfEezDNbCwidVigV5PAJwrR2kw4Jz":[],"19z3j2ELqbg2pR87byCCt3BCyKR7rc3q8G":[],"1A3XSmvLQvePmvm7yctsGkBMX9ZKKXLrVq":[],"1CmhFe2BN1h9jheFpJf4v39XNPj8F9U6d":[],"1DuphhHUayKzbkdvjVjf5dtjn2ACkz4zEs":[],"1E4ygSNJpWL2uPXZHBptmU2LqwZTqb1Ado":[],"1GTDSjkVc9vaaBBBGNVqTANHJBcoT5VW9z":[],"1GWqgpThAuSq3tDg6uCoLQxPXQNnU8jZ52":[],"1GhmpwqSF5cqNgdr9oJMZx8dKxPRo4pYPP":[],"1J5TTUQKhwehEACw6Jjte1E22FVrbeDmpv":[],"1JWySzjzJhsETUUcqVZHuvQLA7pfFfmesb":[],"1KQHxcy3QUHAWMHKUtJjqD9cMKXcY2RTwZ":[],"1KoxZfc2KsgovjGDxwqanbFEA76uxgYH4G":[],"1KqVEPXdpbYvEbwsZcEKkrA4A2jsgj9hYN":[],"1N16yDSYe76c5A3CoVoWAKxHeAUc8Jhf9J":[],"1Pm8JBhzUJDqeQQKrmnop1Frr4phe1jbTt":[]},"addresses":{"change":["1GhmpwqSF5cqNgdr9oJMZx8dKxPRo4pYPP","1GTDSjkVc9vaaBBBGNVqTANHJBcoT5VW9z","15FGuHvRssu1r8fCw98vrbpfc3M4xs5FAV","1A3XSmvLQvePmvm7yctsGkBMX9ZKKXLrVq","19z3j2ELqbg2pR87byCCt3BCyKR7rc3q8G","1JWySzjzJhsETUUcqVZHuvQLA7pfFfmesb"],"receiving":["14gmBxYV97mzYwWdJSJ3MTLbTHVegaKrcA","13HT1pfWctsSXVFzF76uYuVdQvcAQ2MAgB","19a98ZfEezDNbCwidVigV5PAJwrR2kw4Jz","1J5TTUQKhwehEACw6Jjte1E22FVrbeDmpv","1Pm8JBhzUJDqeQQKrmnop1Frr4phe1jbTt","13kG9WH9JqS7hyCcVL1ssLdNv4aXocQY9c","1KQHxcy3QUHAWMHKUtJjqD9cMKXcY2RTwZ","12ECgkzK6gHouKAZ7QiooYBuk1CgJLJxes","12iR43FPb5M7sw4Mcrr5y1nHKepg9EtZP1","14Tf3qiiHJXStSU4KmienAhHfHq7FHpBpz","1KqVEPXdpbYvEbwsZcEKkrA4A2jsgj9hYN","17oJzweA2gn6SDjsKgA9vUD5ocT1sSnr2Z","1E4ygSNJpWL2uPXZHBptmU2LqwZTqb1Ado","18hNcSjZzRcRP6J2bfFRxp9UfpMoC4hGTv","1KoxZfc2KsgovjGDxwqanbFEA76uxgYH4G","18n9PFxBjmKCGhd4PCDEEqYsi2CsnEfn2B","1CmhFe2BN1h9jheFpJf4v39XNPj8F9U6d","1DuphhHUayKzbkdvjVjf5dtjn2ACkz4zEs","1GWqgpThAuSq3tDg6uCoLQxPXQNnU8jZ52","1N16yDSYe76c5A3CoVoWAKxHeAUc8Jhf9J"]},"keystore":{"seed":"cereal wise two govern top pet frog nut rule sketch bundle logic","type":"bip32","xprv":"xprv9s21ZrQH143K29XjRjUs6MnDB9wXjXbJP2kG1fnRk8zjdDYWqVkQYUqaDtgZp5zPSrH5PZQJs8sU25HrUgT1WdgsPU8GbifKurtMYg37d4v","xpub":"xpub661MyMwAqRbcEdcCXm1sTViwjBn28zK9kFfrp4C3JUXiW1sfP34f6HA45B9yr7EH5XGzWuTfMTdqpt9XPrVQVUdgiYb5NW9m8ij1FSZgGBF"},"pruned_txo":{},"seed_type":"standard","seed_version":13,"stored_height":-1,"transactions":{},"tx_fees":{},"txi":{},"txo":{},"use_encryption":false,"verified_tx3":{},"wallet_type":"standard","winpos-qt":[619,310,840,405]}'''
417 storage = WalletStorage(self.wallet_path)
418 db = WalletDB(wallet_str, storage=storage, upgrade=True)
419 wallet = Wallet(db, config=self.config)
420
421 wallet.check_password(None)
422
423 wallet.update_password(None, "1234")
424 with self.assertRaises(InvalidPassword):
425 wallet.check_password(None)
426 with self.assertRaises(InvalidPassword):
427 wallet.check_password("wrong password")
428 wallet.check_password("1234")
429
430 async def test_update_password_of_standard_wallet_oldseed(self):
431 d = restore_wallet_from_text__for_unittest(
432 "powerful random nobody notice nothing important anyway look away hidden message over", path=self.wallet_path, config=self.config)
433 wallet = d['wallet'] # type: Standard_Wallet
434
435 wallet.check_password(None)
436
437 wallet.update_password(None, "1234")
438 with self.assertRaises(InvalidPassword):
439 wallet.check_password(None)
440 with self.assertRaises(InvalidPassword):
441 wallet.check_password("wrong password")
442 wallet.check_password("1234")
443
444 async def test_update_password_with_app_restarts(self):
445 wallet_str = '{"addr_history":{"1364Js2VG66BwRdkaoxAaFtdPb1eQgn8Dr":[],"15CyDgLffJsJgQrhcyooFH4gnVDG82pUrA":[],"1Exet2BhHsFxKTwhnfdsBMkPYLGvobxuW6":[]},"addresses":{"change":[],"receiving":["1364Js2VG66BwRdkaoxAaFtdPb1eQgn8Dr","1Exet2BhHsFxKTwhnfdsBMkPYLGvobxuW6","15CyDgLffJsJgQrhcyooFH4gnVDG82pUrA"]},"keystore":{"keypairs":{"0344b1588589958b0bcab03435061539e9bcf54677c104904044e4f8901f4ebdf5":"L2sED74axVXC4H8szBJ4rQJrkfem7UMc6usLCPUoEWxDCFGUaGUM","0389508c13999d08ffae0f434a085f4185922d64765c0bff2f66e36ad7f745cc5f":"L3Gi6EQLvYw8gEEUckmqawkevfj9s8hxoQDFveQJGZHTfyWnbk1U","04575f52b82f159fa649d2a4c353eb7435f30206f0a6cb9674fbd659f45082c37d559ffd19bea9c0d3b7dcc07a7b79f4cffb76026d5d4dff35341efe99056e22d2":"5JyVyXU1LiRXATvRTQvR9Kp8Rx1X84j2x49iGkjSsXipydtByUq"},"type":"imported"},"pruned_txo":{},"seed_version":13,"stored_height":-1,"transactions":{},"tx_fees":{},"txi":{},"txo":{},"use_encryption":false,"verified_tx3":{},"wallet_type":"standard","winpos-qt":[100,100,840,405]}'
446 storage = WalletStorage(self.wallet_path)
447 db = WalletDB(wallet_str, storage=storage, upgrade=True)
448 wallet = Wallet(db, config=self.config)
449 await wallet.stop()
450
451 storage = WalletStorage(self.wallet_path)
452 # if storage.is_encrypted():
453 # storage.decrypt(password)
454 db = WalletDB(storage.read(), storage=storage, upgrade=True)
455 wallet = Wallet(db, config=self.config)
456
457 wallet.check_password(None)
458
459 wallet.update_password(None, "1234")
460 with self.assertRaises(InvalidPassword):
461 wallet.check_password(None)
462 with self.assertRaises(InvalidPassword):
463 wallet.check_password("wrong password")
464 wallet.check_password("1234")