tool_api_test.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2023 The MMGen Project <mmgen@tuta.io>
  5. """
  6. test.misc.tool_api_test: test the MMGen suite tool API
  7. """
  8. import sys
  9. from mmgen.key import PrivKey
  10. from mmgen.addr import CoinAddr
  11. keys = [
  12. '118089d66b4a5853765e94923abdd5de4616c6e5118089d66b4a5853765e9492',
  13. 'deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef',
  14. ]
  15. def check_equal(a,b):
  16. assert a == b, f'{a} != {b}'
  17. def msg(*args,**kwargs):
  18. print(*args,**kwargs,file=sys.stdout)
  19. def init_coin(t,coinsym,network,addrtype):
  20. t.init_coin(coinsym,network)
  21. t.addrtype = addrtype
  22. check_equal(type(t.addrtype).__name__,'MMGenAddrType')
  23. msg(f'\n{t.coin} {t.proto.network.capitalize()} {t.addrtype.name.upper()} ({t.addrtype})')
  24. def test_randpair(t):
  25. wif,addr = t.randpair()
  26. wif_chk = PrivKey(proto=t.proto,wif=wif).wif
  27. check_equal(wif,wif_chk)
  28. msg('\n === randwif ===')
  29. msg(' wif:',wif)
  30. msg(' addr:',CoinAddr(proto=t.proto,addr=addr))
  31. def test_wif2addr(t,wif_chk,addr_chk,key_idx):
  32. key_bytes = bytes.fromhex(keys[key_idx])
  33. wif = PrivKey(
  34. proto = t.proto,
  35. s = key_bytes,
  36. compressed = t.addrtype.compressed,
  37. pubkey_type = t.addrtype.pubkey_type ).wif
  38. addr = t.wif2addr(wif)
  39. msg('\n === wif2addr ===')
  40. msg(' wif:',PrivKey(proto=t.proto,wif=wif).wif)
  41. msg(' addr:',CoinAddr(proto=t.proto,addr=addr))
  42. addr_ph = t.privhex2addr(key_bytes.hex())
  43. check_equal(addr,addr_ph)
  44. check_equal(wif,wif_chk)
  45. check_equal(addr,addr_chk)
  46. def test_triplet(tool,coin,network,addrtype,key_idx,wif_chk,addr_chk):
  47. init_coin(tool,coin,network,addrtype)
  48. test_randpair(tool)
  49. test_wif2addr(tool,wif_chk,addr_chk,key_idx)
  50. def run_test():
  51. from mmgen.tool.api import tool_api
  52. tool = tool_api(cfg)
  53. tool.coins
  54. tool.print_addrtypes()
  55. tool.usr_randchars # getter
  56. tool.usr_randchars = 0 # setter
  57. check_equal(tool.usr_randchars,0)
  58. check_equal(f'{tool.coin} {tool.proto.cls_name} {tool.addrtype}', 'BTC mainnet L' )
  59. # test vectors from tooltest2.py:
  60. test_triplet(tool,'btc','mainnet','L',0,
  61. '5HwzecKMWD82ppJK3qMKpC7ohXXAwcyAN5VgdJ9PLFaAzpBG4sX',
  62. '1C5VPtgq9xQ6AcTgMAR3J6GDrs72HC4pS1' )
  63. test_triplet(tool,'btc','mainnet','C',0,
  64. 'KwojSzt1VvW343mQfWQi3J537siAt5ktL2qbuCg1ZyKR8BLQ6UJm',
  65. '1Kz9fVSUMshzPejpzW9D95kScgA3rY6QxF' )
  66. test_triplet(tool,'btc','mainnet','segwit',0,
  67. 'KwojSzt1VvW343mQfWQi3J537siAt5ktL2qbuCg1ZyKR8BLQ6UJm',
  68. '3AhjTiWHhVJAi1s5CfKMcLzYps12x3gZhg' )
  69. test_triplet(tool,'btc','mainnet','B',0,
  70. 'KwojSzt1VvW343mQfWQi3J537siAt5ktL2qbuCg1ZyKR8BLQ6UJm',
  71. 'bc1q6pqnfwwakuuejpm9w52ds342f9d5u36v0qnz7c' )
  72. if not 'no_altcoin' in sys.argv:
  73. test_triplet(tool,'ltc','regtest','bech32',1,
  74. 'cV3ZRqf8PhyfiFwtJfkvGu2qmBsazE1wXoA2A16S3nixb3BTvvVx',
  75. 'rltc1qvmqas4maw7lg9clqu6kqu9zq9cluvllnz4kj9y' )
  76. test_triplet(tool,'xmr','mainnet','M',1,
  77. 'e8164dda6d42bd1e261a3406b2038dcbddadbeefdeadbeefdeadbeefdeadbe0f',
  78. '41i7saPWA53EoHenmJVRt34dubPxsXwoWMnw8AdMyx4mTD1svf7qYzcVjxxRfteLNdYrAxWUMmiPegFW9EfoNgXx7vDMExv' )
  79. test_triplet(tool,'etc','mainnet','E',1,
  80. 'deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef',
  81. 'c96aaa54e2d44c299564da76e1cd3184a2386b8d' )
  82. test_triplet(tool,'zec','mainnet','Z',1,
  83. 'SKxuS56e99jpCeD9mMQ5o63zoGPakNdM9HCvt4Vt2cypvRjCdvGJ',
  84. 'zchFELwBxqsAubsLQ8yZgPCDDGukjXJssgCbiTPwFNmFwn9haLnDatzfhLdZzJT4PcU4o2yr92B52UFirUzEdF6ZYM2gBkM' )
  85. from mmgen.cfg import Config
  86. cfg = Config(process_opts=True)
  87. run_test()