api: Add wallet lock/unlock commands and getcoinseed.

This commit is contained in:
tecnovert
2022-11-12 01:51:30 +02:00
parent 020a65db8a
commit fc31615a97
21 changed files with 412 additions and 121 deletions

View File

@@ -217,13 +217,23 @@ def post_json_req(url, json_data):
req.add_header('Content-Type', 'application/json; charset=utf-8')
post_bytes = json.dumps(json_data).encode('utf-8')
req.add_header('Content-Length', len(post_bytes))
return urlopen(req, post_bytes).read()
return urlopen(req, post_bytes, timeout=300).read()
def read_json_api(port, path=None):
def read_text_api(port, path=None):
url = f'http://127.0.0.1:{port}/json'
if path is not None:
url += '/' + path
return urlopen(url, timeout=300).read().decode('utf-8')
def read_json_api(port, path=None, json_data=None):
url = f'http://127.0.0.1:{port}/json'
if path is not None:
url += '/' + path
if json_data is not None:
return json.loads(post_json_req(url, json_data))
return json.loads(urlopen(url, timeout=300).read())

View File

@@ -14,6 +14,7 @@ import os
import sys
import json
import time
import random
import shutil
import signal
import logging
@@ -204,8 +205,8 @@ def btcRpc(cmd):
return callrpc_cli(cfg.BITCOIN_BINDIR, os.path.join(cfg.TEST_DATADIRS, str(BTC_NODE)), 'regtest', cmd, cfg.BITCOIN_CLI)
def dashRpc(cmd):
return callrpc_cli(cfg.DASH_BINDIR, os.path.join(cfg.TEST_DATADIRS, str(DASH_NODE)), 'regtest', cmd, cfg.DASH_CLI)
def dashRpc(cmd, wallet=None):
return callrpc_cli(cfg.DASH_BINDIR, os.path.join(cfg.TEST_DATADIRS, str(DASH_NODE)), 'regtest', cmd, cfg.DASH_CLI, wallet=wallet)
def signal_handler(sig, frame):
@@ -533,18 +534,17 @@ class Test(unittest.TestCase):
self.swap_clients[0].initialiseWallet(Coins.DASH, raise_errors=True)
assert self.swap_clients[0].checkWalletSeed(Coins.DASH) is True
pivx_addr = dashRpc('getnewaddress \"hd test\"')
assert pivx_addr == 'ybzWYJbZEhZai8kiKkTtPFKTuDNwhpiwac'
addr = dashRpc('getnewaddress \"hd wallet test\"')
assert addr == 'ybzWYJbZEhZai8kiKkTtPFKTuDNwhpiwac'
def pass_99_delay(self):
global stop_test
logging.info('Delay')
for i in range(60 * 5):
if stop_test:
break
time.sleep(1)
print('delay', i)
stop_test = True
logging.info('Test that getcoinseed returns a mnemonic for Dash')
mnemonic = read_json_api(1800, 'getcoinseed', {'coin': 'DASH'})['mnemonic']
new_wallet_name = random.randbytes(10).hex()
dashRpc(f'createwallet \"{new_wallet_name}\"')
dashRpc(f'upgradetohd \"{mnemonic}\"', wallet=new_wallet_name)
addr_test = dashRpc('getnewaddress', wallet=new_wallet_name)
dashRpc('unloadwallet', wallet=new_wallet_name)
assert (addr_test == addr)
if __name__ == '__main__':

View File

@@ -108,6 +108,8 @@ class Test(BaseTest):
firo_daemons = []
firo_addr = None
test_coin_from = Coins.FIRO
start_ltc_nodes = False
start_xmr_nodes = False
test_atomic = True
test_xmr = False
@@ -119,12 +121,6 @@ class Test(BaseTest):
'c5de2be44834e7e47ad7dc8e35c6b77c79f17c6bb40d5509a00fc3dff384a865',
]
@classmethod
def setUpClass(cls):
cls.start_ltc_nodes = False
cls.start_xmr_nodes = False
super(Test, cls).setUpClass()
@classmethod
def prepareExtraDataDir(cls, i):
if not cls.restore_instance:
@@ -135,7 +131,7 @@ class Test(BaseTest):
callrpc_cli(cfg.FIRO_BINDIR, data_dir, 'regtest', '-wallet=wallet.dat create', 'firo-wallet')
cls.firo_daemons.append(startDaemon(os.path.join(cfg.TEST_DATADIRS, 'firo_' + str(i)), cfg.FIRO_BINDIR, cfg.FIROD, opts=extra_opts))
logging.info('Started %s %d', cfg.FIROD, cls.part_daemons[-1].pid)
logging.info('Started %s %d', cfg.FIROD, cls.firo_daemons[-1].pid)
waitForRPC(make_rpc_func(i, base_rpc_port=FIRO_BASE_RPC_PORT))

View File

@@ -349,7 +349,7 @@ class Test(unittest.TestCase):
num_blocks = 3
logging.info('Waiting for Particl chain height %d', num_blocks)
for i in range(60):
particl_blocks = cls.swap_clients[0].callrpc('getblockchaininfo')['blocks']
particl_blocks = cls.swap_clients[0].callrpc('getblockcount')
print('particl_blocks', particl_blocks)
if particl_blocks >= num_blocks:
break

View File

@@ -360,7 +360,7 @@ class Test(unittest.TestCase):
num_blocks = 3
logging.info('Waiting for Particl chain height %d', num_blocks)
for i in range(60):
particl_blocks = cls.swap_clients[0].callrpc('getblockchaininfo')['blocks']
particl_blocks = cls.swap_clients[0].callrpc('getblockcount')
print('particl_blocks', particl_blocks)
if particl_blocks >= num_blocks:
break
@@ -563,16 +563,6 @@ class Test(unittest.TestCase):
break
assert found
def pass_99_delay(self):
global stop_test
logging.info('Delay')
for i in range(60 * 5):
if stop_test:
break
time.sleep(1)
print('delay', i)
stop_test = True
if __name__ == '__main__':
unittest.main()

View File

@@ -176,7 +176,7 @@ class Test(unittest.TestCase):
for i in range(60):
if self.delay_event.is_set():
raise ValueError('Test stopped.')
particl_blocks = callpartrpc(0, 'getblockchaininfo')['blocks']
particl_blocks = callpartrpc(0, 'getblockcount')
print('particl_blocks', particl_blocks)
if particl_blocks >= num_blocks:
break

View File

@@ -425,6 +425,39 @@ class TestBTC(BasicSwapTest):
start_ltc_nodes = False
base_rpc_port = BTC_BASE_RPC_PORT
def test_009_wallet_encryption(self):
for coin in ('btc', 'part', 'xmr'):
jsw = read_json_api(1800, f'wallets/{coin}')
assert (jsw['encrypted'] is False)
assert (jsw['locked'] is False)
rv = read_json_api(1800, 'setpassword', {'oldpassword': '', 'newpassword': 'notapassword123'})
# Entire system is locked with Particl wallet
jsw = read_json_api(1800, 'wallets/btc')
assert ('Coin must be unlocked' in jsw['error'])
read_json_api(1800, 'unlock', {'coin': 'part', 'password': 'notapassword123'})
for coin in ('btc', 'xmr'):
jsw = read_json_api(1800, f'wallets/{coin}')
assert (jsw['encrypted'] is True)
assert (jsw['locked'] is True)
read_json_api(1800, 'lock', {'coin': 'part'})
jsw = read_json_api(1800, 'wallets/part')
assert ('Coin must be unlocked' in jsw['error'])
read_json_api(1800, 'setpassword', {'oldpassword': 'notapassword123', 'newpassword': 'notapassword456'})
read_json_api(1800, 'unlock', {'password': 'notapassword456'})
for coin in ('part', 'btc', 'xmr'):
jsw = read_json_api(1800, f'wallets/{coin}')
assert (jsw['encrypted'] is True)
assert (jsw['locked'] is False)
if __name__ == '__main__':
unittest.main()

View File

@@ -14,7 +14,6 @@ $ pytest -v -s tests/basicswap/test_run.py::Test::test_04_ltc_btc
"""
import os
import json
import random
import logging
import unittest
@@ -43,7 +42,6 @@ from tests.basicswap.common import (
wait_for_balance,
wait_for_bid_tx_state,
wait_for_in_progress,
post_json_req,
read_json_api,
TEST_HTTP_PORT,
LTC_BASE_RPC_PORT,
@@ -114,6 +112,21 @@ class Test(BaseTest):
rv = read_json_api(1800, 'rateslist?from=PART&to=BTC')
assert len(rv) == 2
def test_003_api(self):
logging.info('---------- Test API')
help_output = read_json_api(1800, 'help')
assert ('getcoinseed' in help_output['commands'])
rv = read_json_api(1800, 'getcoinseed')
assert (rv['error'] == 'No post data')
rv = read_json_api(1800, 'getcoinseed', {'coin': 'PART'})
assert ('seed is set from the Basicswap mnemonic' in rv['error'])
rv = read_json_api(1800, 'getcoinseed', {'coin': 'BTC'})
assert (rv['seed'] == '8e54a313e6df8918df6d758fafdbf127a115175fdd2238d0e908dd8093c9ac3b')
def test_01_verifyrawtransaction(self):
txn = '0200000001eb6e5c4ebba4efa32f40c7314cad456a64008e91ee30b2dd0235ab9bb67fbdbb01000000ee47304402200956933242dde94f6cf8f195a470f8d02aef21ec5c9b66c5d3871594bdb74c9d02201d7e1b440de8f4da672d689f9e37e98815fb63dbc1706353290887eb6e8f7235012103dc1b24feb32841bc2f4375da91fa97834e5983668c2a39a6b7eadb60e7033f9d205a803b28fe2f86c17db91fa99d7ed2598f79b5677ffe869de2e478c0d1c02cc7514c606382012088a8201fe90717abb84b481c2a59112414ae56ec8acc72273642ca26cc7a5812fdc8f68876a914225fbfa4cb725b75e511810ac4d6f74069bdded26703520140b27576a914207eb66b2fd6ed9924d6217efc7fa7b38dfabe666888acffffffff01e0167118020000001976a9140044e188928710cecba8311f1cf412135b98145c88ac00000000'
prevout = {
@@ -412,7 +425,7 @@ class Test(BaseTest):
'address': ltc_addr,
'subfee': False,
}
json_rv = json.loads(post_json_req('http://127.0.0.1:{}/json/wallets/ltc/withdraw'.format(TEST_HTTP_PORT + 0), post_json))
json_rv = read_json_api('json/wallets/ltc/withdraw', TEST_HTTP_PORT + 0, post_json)
assert (len(json_rv['txid']) == 64)
def test_13_itx_refund(self):