mmgen-wallet/test/misc/tool_api_test.py

116 lines
3.5 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
#
2024-10-18 10:32:06 +00:00
# MMGen Wallet, a terminal-based cryptocurrency wallet
2026-02-11 13:02:12 +00:00
# Copyright (C)2013-2026 The MMGen Project <mmgen@tuta.io>
"""
2022-11-14 09:54:07 +00:00
test.misc.tool_api_test: test the MMGen suite tool API
"""
2023-10-11 12:58:51 +00:00
import sys
from mmgen.key import PrivKey
from mmgen.addr import CoinAddr
2020-06-09 09:20:17 +00:00
keys = [
'118089d66b4a5853765e94923abdd5de4616c6e5118089d66b4a5853765e9492',
'deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef',
]
2024-10-18 10:32:13 +00:00
def check_equal(a, b):
assert a == b, f'{a} != {b}'
2024-10-18 10:32:13 +00:00
def msg(*args, **kwargs):
print(*args, **kwargs, file=sys.stdout)
2024-10-18 10:32:13 +00:00
def init_coin(t, coinsym, network, addrtype):
t.init_coin(coinsym, network)
t.addrtype = addrtype
2024-10-18 10:32:13 +00:00
check_equal(type(t.addrtype).__name__, 'MMGenAddrType')
2020-06-09 09:20:17 +00:00
msg(f'\n{t.coin} {t.proto.network.capitalize()} {t.addrtype.name.upper()} ({t.addrtype})')
def test_randpair(t):
2024-10-18 10:32:13 +00:00
wif, addr = t.randpair()
wif_chk = PrivKey(proto=t.proto, wif=wif).wif
check_equal(wif, wif_chk)
2020-06-09 09:20:17 +00:00
msg('\n === randwif ===')
2024-10-18 10:32:13 +00:00
msg(' wif:', wif)
msg(' addr:', CoinAddr(proto=t.proto, addr=addr))
2020-06-09 09:20:17 +00:00
2024-10-18 10:32:13 +00:00
def test_wif2addr(t, wif_chk, addr_chk, key_idx):
2020-06-09 09:20:17 +00:00
key_bytes = bytes.fromhex(keys[key_idx])
wif = PrivKey(
proto = t.proto,
s = key_bytes,
compressed = t.addrtype.compressed,
2024-10-18 10:32:13 +00:00
pubkey_type = t.addrtype.pubkey_type).wif
2020-06-09 09:20:17 +00:00
addr = t.wif2addr(wif)
msg('\n === wif2addr ===')
2024-10-18 10:32:13 +00:00
msg(' wif:', PrivKey(proto=t.proto, wif=wif).wif)
msg(' addr:', CoinAddr(proto=t.proto, addr=addr))
2020-06-09 09:20:17 +00:00
addr_ph = t.privhex2addr(key_bytes.hex())
2024-10-18 10:32:13 +00:00
check_equal(addr, addr_ph)
check_equal(wif, wif_chk)
check_equal(addr, addr_chk)
2020-06-09 09:20:17 +00:00
2024-10-18 10:32:13 +00:00
def test_triplet(tool, coin, network, addrtype, key_idx, wif_chk, addr_chk):
init_coin(tool, coin, network, addrtype)
2020-06-09 09:20:17 +00:00
test_randpair(tool)
2024-10-18 10:32:13 +00:00
test_wif2addr(tool, wif_chk, addr_chk, key_idx)
def run_test():
from mmgen.tool.api import tool_api
tool = tool_api(cfg)
tool.coins
tool.print_addrtypes()
tool.usr_randchars # getter
tool.usr_randchars = 0 # setter
2024-10-18 10:32:13 +00:00
check_equal(tool.usr_randchars, 0)
2024-10-18 10:32:13 +00:00
check_equal(f'{tool.coin} {tool.proto.cls_name} {tool.addrtype}', 'BTC mainnet L')
2020-06-09 09:20:17 +00:00
# test vectors from tooltest2.py:
2024-10-18 10:32:13 +00:00
test_triplet(tool, 'btc', 'mainnet', 'L', 0,
2020-06-09 09:20:17 +00:00
'5HwzecKMWD82ppJK3qMKpC7ohXXAwcyAN5VgdJ9PLFaAzpBG4sX',
2024-10-18 10:32:13 +00:00
'1C5VPtgq9xQ6AcTgMAR3J6GDrs72HC4pS1')
2024-10-18 10:32:13 +00:00
test_triplet(tool, 'btc', 'mainnet', 'C', 0,
2020-06-09 09:20:17 +00:00
'KwojSzt1VvW343mQfWQi3J537siAt5ktL2qbuCg1ZyKR8BLQ6UJm',
2024-10-18 10:32:13 +00:00
'1Kz9fVSUMshzPejpzW9D95kScgA3rY6QxF')
2024-10-18 10:32:13 +00:00
test_triplet(tool, 'btc', 'mainnet', 'segwit', 0,
2020-06-09 09:20:17 +00:00
'KwojSzt1VvW343mQfWQi3J537siAt5ktL2qbuCg1ZyKR8BLQ6UJm',
2024-10-18 10:32:13 +00:00
'3AhjTiWHhVJAi1s5CfKMcLzYps12x3gZhg')
2024-10-18 10:32:13 +00:00
test_triplet(tool, 'btc', 'mainnet', 'B', 0,
2020-06-09 09:20:17 +00:00
'KwojSzt1VvW343mQfWQi3J537siAt5ktL2qbuCg1ZyKR8BLQ6UJm',
2024-10-18 10:32:13 +00:00
'bc1q6pqnfwwakuuejpm9w52ds342f9d5u36v0qnz7c')
2022-05-03 21:01:05 +00:00
if not 'no_altcoin' in sys.argv:
2024-10-18 10:32:13 +00:00
test_triplet(tool, 'ltc', 'regtest', 'bech32', 1,
2022-05-03 21:01:05 +00:00
'cV3ZRqf8PhyfiFwtJfkvGu2qmBsazE1wXoA2A16S3nixb3BTvvVx',
2024-10-18 10:32:13 +00:00
'rltc1qvmqas4maw7lg9clqu6kqu9zq9cluvllnz4kj9y')
2022-05-03 21:01:05 +00:00
2024-10-18 10:32:13 +00:00
test_triplet(tool, 'xmr', 'mainnet', 'M', 1,
2022-05-03 21:01:05 +00:00
'e8164dda6d42bd1e261a3406b2038dcbddadbeefdeadbeefdeadbeefdeadbe0f',
2024-10-18 10:32:13 +00:00
'41i7saPWA53EoHenmJVRt34dubPxsXwoWMnw8AdMyx4mTD1svf7qYzcVjxxRfteLNdYrAxWUMmiPegFW9EfoNgXx7vDMExv')
2022-05-03 21:01:05 +00:00
2024-10-18 10:32:13 +00:00
test_triplet(tool, 'etc', 'mainnet', 'E', 1,
2022-05-03 21:01:05 +00:00
'deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef',
2024-10-18 10:32:13 +00:00
'c96aaa54e2d44c299564da76e1cd3184a2386b8d')
2022-05-03 21:01:05 +00:00
2024-10-18 10:32:13 +00:00
test_triplet(tool, 'zec', 'mainnet', 'Z', 1,
2022-05-03 21:01:05 +00:00
'SKxuS56e99jpCeD9mMQ5o63zoGPakNdM9HCvt4Vt2cypvRjCdvGJ',
2024-10-18 10:32:13 +00:00
'zchFELwBxqsAubsLQ8yZgPCDDGukjXJssgCbiTPwFNmFwn9haLnDatzfhLdZzJT4PcU4o2yr92B52UFirUzEdF6ZYM2gBkM')
2023-04-04 16:04:10 +00:00
from mmgen.cfg import Config
2023-04-04 16:04:10 +00:00
cfg = Config(process_opts=True)
run_test()