2019-10-13 01:33:16 +02:00
|
|
|
import asyncio
|
2019-10-16 07:18:39 +02:00
|
|
|
import json
|
2019-11-05 15:31:33 +01:00
|
|
|
|
2020-01-03 04:18:49 +01:00
|
|
|
from lbry.wallet import ENCRYPT_ON_DISK
|
2019-12-31 20:52:57 +01:00
|
|
|
from lbry.error import InvalidPasswordError
|
2019-11-05 15:31:33 +01:00
|
|
|
from lbry.testcase import CommandTestCase
|
|
|
|
from lbry.wallet.dewies import dict_values_to_lbc
|
2019-02-11 00:36:21 +01:00
|
|
|
|
|
|
|
|
2019-10-30 22:05:23 +01:00
|
|
|
class WalletCommands(CommandTestCase):
|
|
|
|
|
|
|
|
async def test_wallet_create_and_add_subscribe(self):
|
2020-12-03 23:08:26 +01:00
|
|
|
session = next(iter(self.conductor.spv_node.server.session_mgr.sessions.values()))
|
2019-10-30 22:05:23 +01:00
|
|
|
self.assertEqual(len(session.hashX_subs), 27)
|
|
|
|
wallet = await self.daemon.jsonrpc_wallet_create('foo', create_account=True, single_key=True)
|
|
|
|
self.assertEqual(len(session.hashX_subs), 28)
|
|
|
|
await self.daemon.jsonrpc_wallet_remove(wallet.id)
|
|
|
|
self.assertEqual(len(session.hashX_subs), 27)
|
|
|
|
await self.daemon.jsonrpc_wallet_add(wallet.id)
|
|
|
|
self.assertEqual(len(session.hashX_subs), 28)
|
|
|
|
|
2020-03-24 04:54:55 +01:00
|
|
|
async def test_wallet_syncing_status(self):
|
|
|
|
address = await self.daemon.jsonrpc_address_unused()
|
2020-03-31 22:20:13 +02:00
|
|
|
self.assertFalse(self.daemon.jsonrpc_wallet_status()['is_syncing'])
|
|
|
|
await self.blockchain.send_to_address(address, 1)
|
|
|
|
await self.ledger._update_tasks.started.wait()
|
|
|
|
self.assertTrue(self.daemon.jsonrpc_wallet_status()['is_syncing'])
|
|
|
|
await self.ledger._update_tasks.done.wait()
|
|
|
|
self.assertFalse(self.daemon.jsonrpc_wallet_status()['is_syncing'])
|
|
|
|
|
|
|
|
wallet = self.daemon.component_manager.get_actual_component('wallet')
|
|
|
|
wallet_manager = wallet.wallet_manager
|
|
|
|
# when component manager hasn't started yet
|
|
|
|
wallet.wallet_manager = None
|
|
|
|
self.assertEqual(
|
2020-03-31 23:22:13 +02:00
|
|
|
{'is_encrypted': None, 'is_syncing': None, 'is_locked': None},
|
2020-03-31 22:20:13 +02:00
|
|
|
self.daemon.jsonrpc_wallet_status()
|
|
|
|
)
|
|
|
|
wallet.wallet_manager = wallet_manager
|
|
|
|
self.assertEqual(
|
|
|
|
{'is_encrypted': False, 'is_syncing': False, 'is_locked': False},
|
|
|
|
self.daemon.jsonrpc_wallet_status()
|
|
|
|
)
|
2020-03-24 04:54:55 +01:00
|
|
|
|
2019-12-11 01:29:17 +01:00
|
|
|
async def test_wallet_reconnect(self):
|
2021-01-21 22:20:01 +01:00
|
|
|
status = await self.daemon.jsonrpc_status()
|
|
|
|
self.assertEqual(len(status['wallet']['servers']), 1)
|
|
|
|
self.assertEqual(status['wallet']['servers'][0]['port'], 50002)
|
2019-12-11 00:32:50 +01:00
|
|
|
await self.conductor.spv_node.stop(True)
|
|
|
|
self.conductor.spv_node.port = 54320
|
|
|
|
await self.conductor.spv_node.start(self.conductor.blockchain_node)
|
|
|
|
status = await self.daemon.jsonrpc_status()
|
2021-01-21 22:20:01 +01:00
|
|
|
self.assertEqual(len(status['wallet']['servers']), 0)
|
2019-12-11 00:32:50 +01:00
|
|
|
self.daemon.jsonrpc_settings_set('lbryum_servers', ['localhost:54320'])
|
2019-12-11 01:29:17 +01:00
|
|
|
await self.daemon.jsonrpc_wallet_reconnect()
|
2019-12-11 00:32:50 +01:00
|
|
|
status = await self.daemon.jsonrpc_status()
|
|
|
|
self.assertEqual(len(status['wallet']['servers']), 1)
|
|
|
|
self.assertEqual(status['wallet']['servers'][0]['port'], 54320)
|
|
|
|
|
2021-06-02 15:51:08 +02:00
|
|
|
async def test_sending_to_scripthash_address(self):
|
|
|
|
self.assertEqual(await self.blockchain.get_balance(), '95.99973580')
|
|
|
|
await self.assertBalance(self.account, '10.0')
|
|
|
|
p2sh_address1 = await self.blockchain.get_new_address(self.blockchain.P2SH_SEGWIT_ADDRESS)
|
2021-06-02 16:14:17 +02:00
|
|
|
tx = await self.account_send('2.0', p2sh_address1)
|
|
|
|
self.assertEqual(tx['outputs'][0]['address'], p2sh_address1)
|
2021-06-02 15:51:08 +02:00
|
|
|
self.assertEqual(await self.blockchain.get_balance(), '98.99973580') # +1 lbc for confirm block
|
|
|
|
await self.assertBalance(self.account, '7.999877')
|
|
|
|
await self.wallet_send('3.0', p2sh_address1)
|
|
|
|
self.assertEqual(await self.blockchain.get_balance(), '102.99973580') # +1 lbc for confirm block
|
|
|
|
await self.assertBalance(self.account, '4.999754')
|
|
|
|
|
2019-11-04 22:41:42 +01:00
|
|
|
async def test_balance_caching(self):
|
2019-11-05 15:31:33 +01:00
|
|
|
account2 = await self.daemon.jsonrpc_account_create("Tip-er")
|
|
|
|
address2 = await self.daemon.jsonrpc_address_unused(account2.id)
|
|
|
|
sendtxid = await self.blockchain.send_to_address(address2, 10)
|
|
|
|
await self.confirm_tx(sendtxid)
|
|
|
|
await self.generate(1)
|
|
|
|
|
2019-11-04 22:41:42 +01:00
|
|
|
wallet_balance = self.daemon.jsonrpc_wallet_balance
|
|
|
|
ledger = self.ledger
|
|
|
|
query_count = self.ledger.db.db.query_count
|
|
|
|
|
|
|
|
expected = {
|
2019-11-05 15:31:33 +01:00
|
|
|
'total': '20.0',
|
|
|
|
'available': '20.0',
|
2019-11-04 22:41:42 +01:00
|
|
|
'reserved': '0.0',
|
|
|
|
'reserved_subtotals': {'claims': '0.0', 'supports': '0.0', 'tips': '0.0'}
|
|
|
|
}
|
|
|
|
self.assertIsNone(ledger._balance_cache.get(self.account.id))
|
|
|
|
|
2021-04-14 17:00:50 +02:00
|
|
|
query_count += 2
|
2019-11-04 22:41:42 +01:00
|
|
|
self.assertEqual(await wallet_balance(), expected)
|
|
|
|
self.assertEqual(self.ledger.db.db.query_count, query_count)
|
2019-11-05 15:31:33 +01:00
|
|
|
self.assertEqual(dict_values_to_lbc(ledger._balance_cache.get(self.account.id))['total'], '10.0')
|
|
|
|
self.assertEqual(dict_values_to_lbc(ledger._balance_cache.get(account2.id))['total'], '10.0')
|
2019-11-04 22:41:42 +01:00
|
|
|
|
|
|
|
# calling again uses cache
|
|
|
|
self.assertEqual(await wallet_balance(), expected)
|
|
|
|
self.assertEqual(self.ledger.db.db.query_count, query_count)
|
2019-11-05 15:31:33 +01:00
|
|
|
self.assertEqual(dict_values_to_lbc(ledger._balance_cache.get(self.account.id))['total'], '10.0')
|
|
|
|
self.assertEqual(dict_values_to_lbc(ledger._balance_cache.get(account2.id))['total'], '10.0')
|
2019-11-04 22:41:42 +01:00
|
|
|
|
|
|
|
await self.stream_create()
|
2019-11-05 15:31:33 +01:00
|
|
|
await self.generate(1)
|
2019-11-04 22:41:42 +01:00
|
|
|
|
|
|
|
expected = {
|
2019-11-05 15:31:33 +01:00
|
|
|
'total': '19.979893',
|
|
|
|
'available': '18.979893',
|
2019-11-04 22:41:42 +01:00
|
|
|
'reserved': '1.0',
|
|
|
|
'reserved_subtotals': {'claims': '1.0', 'supports': '0.0', 'tips': '0.0'}
|
|
|
|
}
|
|
|
|
# on_transaction event reset balance cache
|
2019-11-05 15:31:33 +01:00
|
|
|
query_count = self.ledger.db.db.query_count
|
2019-11-04 22:41:42 +01:00
|
|
|
self.assertEqual(await wallet_balance(), expected)
|
2021-04-14 17:00:50 +02:00
|
|
|
query_count += 1 # only one of the accounts changed
|
2019-11-05 15:31:33 +01:00
|
|
|
self.assertEqual(dict_values_to_lbc(ledger._balance_cache.get(self.account.id))['total'], '9.979893')
|
|
|
|
self.assertEqual(dict_values_to_lbc(ledger._balance_cache.get(account2.id))['total'], '10.0')
|
|
|
|
self.assertEqual(self.ledger.db.db.query_count, query_count)
|
2019-11-04 22:41:42 +01:00
|
|
|
|
|
|
|
async def test_granular_balances(self):
|
|
|
|
account2 = await self.daemon.jsonrpc_account_create("Tip-er")
|
2020-03-20 23:24:24 +01:00
|
|
|
wallet2 = await self.daemon.jsonrpc_wallet_create('foo', create_account=True)
|
|
|
|
account3 = wallet2.default_account
|
|
|
|
address3 = await self.daemon.jsonrpc_address_unused(account3.id, wallet2.id)
|
|
|
|
await self.confirm_tx(await self.blockchain.send_to_address(address3, 1))
|
|
|
|
await self.generate(1)
|
2019-11-04 22:41:42 +01:00
|
|
|
|
|
|
|
account_balance = self.daemon.jsonrpc_account_balance
|
|
|
|
wallet_balance = self.daemon.jsonrpc_wallet_balance
|
|
|
|
|
|
|
|
expected = {
|
|
|
|
'total': '10.0',
|
|
|
|
'available': '10.0',
|
|
|
|
'reserved': '0.0',
|
|
|
|
'reserved_subtotals': {'claims': '0.0', 'supports': '0.0', 'tips': '0.0'}
|
|
|
|
}
|
|
|
|
self.assertEqual(await account_balance(), expected)
|
|
|
|
self.assertEqual(await wallet_balance(), expected)
|
|
|
|
|
|
|
|
# claim with update + supporting our own claim
|
|
|
|
stream1 = await self.stream_create('granularity', '3.0')
|
|
|
|
await self.stream_update(self.get_claim_id(stream1), data=b'news', bid='1.0')
|
|
|
|
await self.support_create(self.get_claim_id(stream1), '2.0')
|
|
|
|
expected = {
|
|
|
|
'total': '9.977534',
|
|
|
|
'available': '6.977534',
|
|
|
|
'reserved': '3.0',
|
|
|
|
'reserved_subtotals': {'claims': '1.0', 'supports': '2.0', 'tips': '0.0'}
|
|
|
|
}
|
|
|
|
self.assertEqual(await account_balance(), expected)
|
|
|
|
self.assertEqual(await wallet_balance(), expected)
|
|
|
|
|
|
|
|
address2 = await self.daemon.jsonrpc_address_unused(account2.id)
|
|
|
|
|
|
|
|
# send lbc to someone else
|
|
|
|
tx = await self.daemon.jsonrpc_account_send('1.0', address2)
|
|
|
|
await self.confirm_tx(tx.id)
|
|
|
|
self.assertEqual(await account_balance(), {
|
|
|
|
'total': '8.97741',
|
|
|
|
'available': '5.97741',
|
|
|
|
'reserved': '3.0',
|
|
|
|
'reserved_subtotals': {'claims': '1.0', 'supports': '2.0', 'tips': '0.0'}
|
|
|
|
})
|
|
|
|
self.assertEqual(await wallet_balance(), {
|
|
|
|
'total': '9.97741',
|
|
|
|
'available': '6.97741',
|
|
|
|
'reserved': '3.0',
|
|
|
|
'reserved_subtotals': {'claims': '1.0', 'supports': '2.0', 'tips': '0.0'}
|
|
|
|
})
|
|
|
|
|
|
|
|
# tip received
|
|
|
|
support1 = await self.support_create(
|
2020-03-20 23:24:24 +01:00
|
|
|
self.get_claim_id(stream1), '0.3', tip=True, wallet_id=wallet2.id
|
2019-11-04 22:41:42 +01:00
|
|
|
)
|
|
|
|
self.assertEqual(await account_balance(), {
|
|
|
|
'total': '9.27741',
|
|
|
|
'available': '5.97741',
|
|
|
|
'reserved': '3.3',
|
|
|
|
'reserved_subtotals': {'claims': '1.0', 'supports': '2.0', 'tips': '0.3'}
|
|
|
|
})
|
|
|
|
self.assertEqual(await wallet_balance(), {
|
2020-03-20 23:24:24 +01:00
|
|
|
'total': '10.27741',
|
|
|
|
'available': '6.97741',
|
2019-11-04 22:41:42 +01:00
|
|
|
'reserved': '3.3',
|
|
|
|
'reserved_subtotals': {'claims': '1.0', 'supports': '2.0', 'tips': '0.3'}
|
|
|
|
})
|
|
|
|
|
|
|
|
# tip claimed
|
|
|
|
tx = await self.daemon.jsonrpc_support_abandon(txid=support1['txid'], nout=0)
|
|
|
|
await self.confirm_tx(tx.id)
|
|
|
|
self.assertEqual(await account_balance(), {
|
|
|
|
'total': '9.277303',
|
|
|
|
'available': '6.277303',
|
|
|
|
'reserved': '3.0',
|
|
|
|
'reserved_subtotals': {'claims': '1.0', 'supports': '2.0', 'tips': '0.0'}
|
|
|
|
})
|
|
|
|
self.assertEqual(await wallet_balance(), {
|
2020-03-20 23:24:24 +01:00
|
|
|
'total': '10.277303',
|
|
|
|
'available': '7.277303',
|
2019-11-04 22:41:42 +01:00
|
|
|
'reserved': '3.0',
|
|
|
|
'reserved_subtotals': {'claims': '1.0', 'supports': '2.0', 'tips': '0.0'}
|
|
|
|
})
|
|
|
|
|
|
|
|
stream2 = await self.stream_create(
|
|
|
|
'granularity-is-cool', '0.1', account_id=account2.id, funding_account_ids=[account2.id]
|
|
|
|
)
|
|
|
|
|
|
|
|
# tip another claim
|
|
|
|
await self.support_create(
|
2020-03-20 23:24:24 +01:00
|
|
|
self.get_claim_id(stream2), '0.2', tip=True, wallet_id=wallet2.id
|
2019-11-04 22:41:42 +01:00
|
|
|
)
|
|
|
|
self.assertEqual(await account_balance(), {
|
2020-03-20 23:24:24 +01:00
|
|
|
'total': '9.277303',
|
|
|
|
'available': '6.277303',
|
2019-11-04 22:41:42 +01:00
|
|
|
'reserved': '3.0',
|
|
|
|
'reserved_subtotals': {'claims': '1.0', 'supports': '2.0', 'tips': '0.0'}
|
|
|
|
})
|
|
|
|
self.assertEqual(await wallet_balance(), {
|
2020-03-20 23:24:24 +01:00
|
|
|
'total': '10.439196',
|
|
|
|
'available': '7.139196',
|
2019-11-04 22:41:42 +01:00
|
|
|
'reserved': '3.3',
|
|
|
|
'reserved_subtotals': {'claims': '1.1', 'supports': '2.0', 'tips': '0.2'}
|
|
|
|
})
|
|
|
|
|
2019-10-30 22:05:23 +01:00
|
|
|
|
2019-10-16 07:18:39 +02:00
|
|
|
class WalletEncryptionAndSynchronization(CommandTestCase):
|
2019-02-11 00:36:21 +01:00
|
|
|
|
2019-10-16 07:18:39 +02:00
|
|
|
SEED = (
|
|
|
|
"carbon smart garage balance margin twelve chest "
|
|
|
|
"sword toast envelope bottom stomach absent"
|
|
|
|
)
|
|
|
|
|
|
|
|
async def asyncSetUp(self):
|
|
|
|
await super().asyncSetUp()
|
|
|
|
self.daemon2 = await self.add_daemon(
|
2019-10-13 01:33:16 +02:00
|
|
|
seed="chest sword toast envelope bottom stomach absent "
|
|
|
|
"carbon smart garage balance margin twelve"
|
2019-02-11 00:36:21 +01:00
|
|
|
)
|
2019-10-16 07:18:39 +02:00
|
|
|
address = (await self.daemon2.wallet_manager.default_account.receiving.get_addresses(limit=1, only_usable=True))[0]
|
2019-10-13 01:33:16 +02:00
|
|
|
sendtxid = await self.blockchain.send_to_address(address, 1)
|
2019-10-16 07:18:39 +02:00
|
|
|
await self.confirm_tx(sendtxid, self.daemon2.ledger)
|
|
|
|
|
|
|
|
def assertWalletEncrypted(self, wallet_path, encrypted):
|
2020-01-17 01:51:49 +01:00
|
|
|
with open(wallet_path) as opened:
|
|
|
|
wallet = json.load(opened)
|
|
|
|
self.assertEqual(wallet['accounts'][0]['private_key'][1:4] != 'prv', encrypted)
|
2019-10-16 07:18:39 +02:00
|
|
|
|
|
|
|
async def test_sync(self):
|
|
|
|
daemon, daemon2 = self.daemon, self.daemon2
|
2019-02-11 00:36:21 +01:00
|
|
|
|
2019-10-13 01:33:16 +02:00
|
|
|
# Preferences
|
|
|
|
self.assertFalse(daemon.jsonrpc_preference_get())
|
|
|
|
self.assertFalse(daemon2.jsonrpc_preference_get())
|
2019-02-11 00:36:21 +01:00
|
|
|
|
2019-10-16 23:19:04 +02:00
|
|
|
daemon.jsonrpc_preference_set("fruit", '["peach", "apricot"]')
|
2019-10-13 01:33:16 +02:00
|
|
|
daemon.jsonrpc_preference_set("one", "1")
|
|
|
|
daemon.jsonrpc_preference_set("conflict", "1")
|
2019-10-16 23:19:04 +02:00
|
|
|
daemon2.jsonrpc_preference_set("another", "A")
|
2019-10-13 01:33:16 +02:00
|
|
|
await asyncio.sleep(1)
|
2019-10-16 23:19:04 +02:00
|
|
|
# these preferences will win after merge since they are "newer"
|
2019-10-13 01:33:16 +02:00
|
|
|
daemon2.jsonrpc_preference_set("two", "2")
|
|
|
|
daemon2.jsonrpc_preference_set("conflict", "2")
|
2019-10-16 23:19:04 +02:00
|
|
|
daemon.jsonrpc_preference_set("another", "B")
|
2019-02-11 00:36:21 +01:00
|
|
|
|
2019-10-13 01:33:16 +02:00
|
|
|
self.assertDictEqual(daemon.jsonrpc_preference_get(), {
|
2019-10-16 23:19:04 +02:00
|
|
|
"one": "1", "conflict": "1", "another": "B", "fruit": ["peach", "apricot"]
|
2019-10-13 01:33:16 +02:00
|
|
|
})
|
2019-10-16 07:18:39 +02:00
|
|
|
self.assertDictEqual(daemon2.jsonrpc_preference_get(), {
|
2019-10-16 23:19:04 +02:00
|
|
|
"two": "2", "conflict": "2", "another": "A"
|
2019-10-16 07:18:39 +02:00
|
|
|
})
|
2019-02-11 00:36:21 +01:00
|
|
|
|
2019-10-26 05:34:44 +02:00
|
|
|
self.assertItemCount(await daemon.jsonrpc_account_list(), 1)
|
2019-02-11 00:36:21 +01:00
|
|
|
|
2019-10-13 01:33:16 +02:00
|
|
|
data = await daemon2.jsonrpc_sync_apply('password')
|
|
|
|
await daemon.jsonrpc_sync_apply('password', data=data['data'], blocking=True)
|
2019-03-11 14:52:35 +01:00
|
|
|
|
2019-10-26 05:34:44 +02:00
|
|
|
self.assertItemCount(await daemon.jsonrpc_account_list(), 2)
|
2019-10-13 01:33:16 +02:00
|
|
|
self.assertDictEqual(
|
|
|
|
# "two" key added and "conflict" value changed to "2"
|
|
|
|
daemon.jsonrpc_preference_get(),
|
2019-10-16 23:19:04 +02:00
|
|
|
{"one": "1", "two": "2", "conflict": "2", "another": "B", "fruit": ["peach", "apricot"]}
|
2019-03-11 14:52:35 +01:00
|
|
|
)
|
|
|
|
|
2019-10-13 01:33:16 +02:00
|
|
|
# Channel Certificate
|
|
|
|
channel = await daemon2.jsonrpc_channel_create('@foo', '0.1')
|
2019-10-16 07:18:39 +02:00
|
|
|
await self.confirm_tx(channel.id, self.daemon2.ledger)
|
2019-09-03 15:51:41 +02:00
|
|
|
|
2019-10-13 01:33:16 +02:00
|
|
|
# both daemons will have the channel but only one has the cert so far
|
2019-10-26 05:34:44 +02:00
|
|
|
self.assertItemCount(await daemon.jsonrpc_channel_list(), 1)
|
2019-10-13 01:33:16 +02:00
|
|
|
self.assertEqual(len(daemon.wallet_manager.default_wallet.accounts[1].channel_keys), 0)
|
2019-10-26 05:34:44 +02:00
|
|
|
self.assertItemCount(await daemon2.jsonrpc_channel_list(), 1)
|
2019-10-13 01:33:16 +02:00
|
|
|
self.assertEqual(len(daemon2.wallet_manager.default_account.channel_keys), 1)
|
2019-09-03 15:51:41 +02:00
|
|
|
|
2019-10-13 01:33:16 +02:00
|
|
|
data = await daemon2.jsonrpc_sync_apply('password')
|
|
|
|
await daemon.jsonrpc_sync_apply('password', data=data['data'], blocking=True)
|
2019-09-03 15:51:41 +02:00
|
|
|
|
2019-10-13 01:33:16 +02:00
|
|
|
# both daemons have the cert after sync'ing
|
|
|
|
self.assertEqual(
|
|
|
|
daemon2.wallet_manager.default_account.channel_keys,
|
|
|
|
daemon.wallet_manager.default_wallet.accounts[1].channel_keys
|
|
|
|
)
|
2019-10-16 07:18:39 +02:00
|
|
|
|
|
|
|
async def test_encryption_and_locking(self):
|
|
|
|
daemon = self.daemon
|
|
|
|
wallet = daemon.wallet_manager.default_wallet
|
2019-10-18 21:58:32 +02:00
|
|
|
wallet.save()
|
2019-10-16 07:18:39 +02:00
|
|
|
|
2020-03-26 18:56:51 +01:00
|
|
|
self.assertEqual(daemon.jsonrpc_wallet_status(), {
|
|
|
|
'is_locked': False, 'is_encrypted': False, 'is_syncing': False
|
|
|
|
})
|
2019-10-16 07:18:39 +02:00
|
|
|
self.assertIsNone(daemon.jsonrpc_preference_get(ENCRYPT_ON_DISK))
|
|
|
|
self.assertWalletEncrypted(wallet.storage.path, False)
|
|
|
|
|
|
|
|
# can't lock an unencrypted account
|
|
|
|
with self.assertRaisesRegex(AssertionError, "Cannot lock an unencrypted wallet, encrypt first."):
|
|
|
|
daemon.jsonrpc_wallet_lock()
|
|
|
|
# safe to call unlock and decrypt, they are no-ops at this point
|
|
|
|
daemon.jsonrpc_wallet_unlock('password') # already unlocked
|
|
|
|
daemon.jsonrpc_wallet_decrypt() # already not encrypted
|
|
|
|
|
|
|
|
daemon.jsonrpc_wallet_encrypt('password')
|
2020-03-26 18:56:51 +01:00
|
|
|
self.assertEqual(daemon.jsonrpc_wallet_status(), {'is_locked': False, 'is_encrypted': True,
|
|
|
|
'is_syncing': False})
|
2019-10-16 15:18:28 +02:00
|
|
|
self.assertEqual(daemon.jsonrpc_preference_get(ENCRYPT_ON_DISK), {'encrypt-on-disk': True})
|
2019-10-16 07:18:39 +02:00
|
|
|
self.assertWalletEncrypted(wallet.storage.path, True)
|
|
|
|
|
|
|
|
daemon.jsonrpc_wallet_lock()
|
2020-03-26 18:56:51 +01:00
|
|
|
self.assertEqual(daemon.jsonrpc_wallet_status(), {'is_locked': True, 'is_encrypted': True,
|
|
|
|
'is_syncing': False})
|
2019-10-16 07:18:39 +02:00
|
|
|
|
2019-10-16 15:18:28 +02:00
|
|
|
# can't sign transactions with locked wallet
|
2019-10-16 19:54:09 +02:00
|
|
|
with self.assertRaises(AssertionError):
|
2019-10-16 07:18:39 +02:00
|
|
|
await daemon.jsonrpc_channel_create('@foo', '1.0')
|
|
|
|
daemon.jsonrpc_wallet_unlock('password')
|
2020-03-26 18:56:51 +01:00
|
|
|
self.assertEqual(daemon.jsonrpc_wallet_status(), {'is_locked': False, 'is_encrypted': True,
|
|
|
|
'is_syncing': False})
|
2019-10-16 07:18:39 +02:00
|
|
|
await daemon.jsonrpc_channel_create('@foo', '1.0')
|
|
|
|
|
|
|
|
daemon.jsonrpc_wallet_decrypt()
|
2020-03-26 18:56:51 +01:00
|
|
|
self.assertEqual(daemon.jsonrpc_wallet_status(), {'is_locked': False, 'is_encrypted': False,
|
|
|
|
'is_syncing': False})
|
2019-10-16 15:18:28 +02:00
|
|
|
self.assertEqual(daemon.jsonrpc_preference_get(ENCRYPT_ON_DISK), {'encrypt-on-disk': False})
|
2019-10-16 07:18:39 +02:00
|
|
|
self.assertWalletEncrypted(wallet.storage.path, False)
|
|
|
|
|
2019-10-18 03:52:20 +02:00
|
|
|
async def test_encryption_with_imported_channel(self):
|
|
|
|
daemon, daemon2 = self.daemon, self.daemon2
|
|
|
|
channel = await self.channel_create()
|
|
|
|
exported = await daemon.jsonrpc_channel_export(self.get_claim_id(channel))
|
|
|
|
await daemon2.jsonrpc_channel_import(exported)
|
|
|
|
self.assertTrue(daemon2.jsonrpc_wallet_encrypt('password'))
|
|
|
|
self.assertTrue(daemon2.jsonrpc_wallet_lock())
|
|
|
|
self.assertTrue(daemon2.jsonrpc_wallet_unlock("password"))
|
2020-03-26 18:56:51 +01:00
|
|
|
self.assertEqual(daemon2.jsonrpc_wallet_status(),
|
|
|
|
{'is_locked': False, 'is_encrypted': True, 'is_syncing': False})
|
2019-10-18 03:52:20 +02:00
|
|
|
|
2019-10-16 07:18:39 +02:00
|
|
|
async def test_sync_with_encryption_and_password_change(self):
|
|
|
|
daemon, daemon2 = self.daemon, self.daemon2
|
|
|
|
wallet, wallet2 = daemon.wallet_manager.default_wallet, daemon2.wallet_manager.default_wallet
|
|
|
|
|
2019-10-16 16:06:17 +02:00
|
|
|
self.assertEqual(wallet2.encryption_password, None)
|
|
|
|
self.assertEqual(wallet2.encryption_password, None)
|
|
|
|
|
2019-10-16 07:18:39 +02:00
|
|
|
daemon.jsonrpc_wallet_encrypt('password')
|
2019-10-16 15:18:28 +02:00
|
|
|
self.assertEqual(wallet.encryption_password, 'password')
|
2019-10-16 07:18:39 +02:00
|
|
|
|
|
|
|
data = await daemon2.jsonrpc_sync_apply('password2')
|
2019-10-16 16:06:17 +02:00
|
|
|
# sync_apply doesn't save password if encrypt-on-disk is False
|
|
|
|
self.assertEqual(wallet2.encryption_password, None)
|
|
|
|
# need to use new password2 in sync_apply
|
2019-12-07 15:16:04 +01:00
|
|
|
with self.assertRaises(InvalidPasswordError):
|
2019-10-16 07:18:39 +02:00
|
|
|
await daemon.jsonrpc_sync_apply('password', data=data['data'], blocking=True)
|
|
|
|
await daemon.jsonrpc_sync_apply('password2', data=data['data'], blocking=True)
|
2019-10-16 16:06:17 +02:00
|
|
|
# sync_apply with new password2 also sets it as new local password
|
2019-10-16 15:18:28 +02:00
|
|
|
self.assertEqual(wallet.encryption_password, 'password2')
|
2020-03-26 18:56:51 +01:00
|
|
|
self.assertEqual(daemon.jsonrpc_wallet_status(), {'is_locked': False, 'is_encrypted': True,
|
|
|
|
'is_syncing': True})
|
2019-10-16 07:18:39 +02:00
|
|
|
self.assertEqual(daemon.jsonrpc_preference_get(ENCRYPT_ON_DISK), {'encrypt-on-disk': True})
|
|
|
|
self.assertWalletEncrypted(wallet.storage.path, True)
|
|
|
|
|
2019-10-16 15:18:28 +02:00
|
|
|
# check new password is active
|
2019-10-16 07:18:39 +02:00
|
|
|
daemon.jsonrpc_wallet_lock()
|
|
|
|
self.assertFalse(daemon.jsonrpc_wallet_unlock('password'))
|
|
|
|
self.assertTrue(daemon.jsonrpc_wallet_unlock('password2'))
|
|
|
|
|
2019-10-16 15:18:28 +02:00
|
|
|
# propagate disk encryption to daemon2
|
2019-10-16 16:06:17 +02:00
|
|
|
data = await daemon.jsonrpc_sync_apply('password3')
|
|
|
|
# sync_apply (even with no data) on wallet with encrypt-on-disk updates local password
|
|
|
|
self.assertEqual(wallet.encryption_password, 'password3')
|
2019-10-16 15:18:28 +02:00
|
|
|
self.assertEqual(wallet2.encryption_password, None)
|
2019-10-16 16:06:17 +02:00
|
|
|
await daemon2.jsonrpc_sync_apply('password3', data=data['data'], blocking=True)
|
|
|
|
# the other device got new password and on disk encryption
|
|
|
|
self.assertEqual(wallet2.encryption_password, 'password3')
|
2020-03-26 18:56:51 +01:00
|
|
|
self.assertEqual(daemon2.jsonrpc_wallet_status(), {'is_locked': False, 'is_encrypted': True,
|
|
|
|
'is_syncing': True})
|
2019-10-16 07:18:39 +02:00
|
|
|
self.assertEqual(daemon2.jsonrpc_preference_get(ENCRYPT_ON_DISK), {'encrypt-on-disk': True})
|
2019-10-16 15:18:28 +02:00
|
|
|
self.assertWalletEncrypted(wallet2.storage.path, True)
|
2019-10-16 07:18:39 +02:00
|
|
|
|
|
|
|
daemon2.jsonrpc_wallet_lock()
|
2019-10-16 16:06:17 +02:00
|
|
|
self.assertTrue(daemon2.jsonrpc_wallet_unlock('password3'))
|