ut_gen.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #!/usr/bin/env python3
  2. """
  3. test.unit_tests_d.ut_gen: key/address generation unit tests for the MMGen suite
  4. """
  5. from mmgen.color import blue
  6. from mmgen.protocol import init_proto
  7. from mmgen.key import PrivKey
  8. from mmgen.addr import MMGenAddrType
  9. from mmgen.addrgen import KeyGenerator,AddrGenerator
  10. from mmgen.keygen import get_backends
  11. from ..include.common import cfg,qmsg
  12. # TODO: add viewkey checks
  13. vectors = { # from tooltest2
  14. 'btc': ( (
  15. '5HwzecKMWD82ppJK3qMKpC7ohXXAwcyAN5VgdJ9PLFaAzpBG4sX',
  16. '1C5VPtgq9xQ6AcTgMAR3J6GDrs72HC4pS1',
  17. 'legacy'
  18. ), (
  19. 'KwojSzt1VvW343mQfWQi3J537siAt5ktL2qbuCg1ZyKR8BLQ6UJm',
  20. '1Kz9fVSUMshzPejpzW9D95kScgA3rY6QxF',
  21. 'compressed'
  22. ), (
  23. 'KwojSzt1VvW343mQfWQi3J537siAt5ktL2qbuCg1ZyKR8BLQ6UJm',
  24. '3AhjTiWHhVJAi1s5CfKMcLzYps12x3gZhg',
  25. 'segwit'
  26. ), (
  27. 'KwojSzt1VvW343mQfWQi3J537siAt5ktL2qbuCg1ZyKR8BLQ6UJm',
  28. 'bc1q6pqnfwwakuuejpm9w52ds342f9d5u36v0qnz7c',
  29. 'bech32' ),
  30. ),
  31. 'eth': ( (
  32. '0000000000000000000000000000000000000000000000000000000000000001',
  33. '7e5f4552091a69125d5dfcb7b8c2659029395bdf',
  34. 'ethereum',
  35. ), ),
  36. 'xmr': ( (
  37. '0000000000000000000000000000000000000000000000000000000000000001',
  38. '42nsXK8WbVGTNayQ6Kjw5UdgqbQY5KCCufdxdCgF7NgTfjC69Mna7DJSYyie77hZTQ8H92G2HwgFhgEUYnDzrnLnQdF28r3',
  39. 'monero',
  40. ), ),
  41. 'zec': ( (
  42. 'SKxny894fJe2rmZjeuoE6GVfNkWoXfPp8337VrLLNWG56FjqVUYR',
  43. 'zceQDpyNwek7dKqF5ZuFGj7YrNVxh7X1aPkrVxDLVxWSiZAFDEuy5C7XNV8VhyZ3ghTPQ61xjCGiyLT3wqpiN1Yi6mdmaCq',
  44. 'zcash_z',
  45. ), ),
  46. }
  47. def do_test(proto,wif,addr_chk,addr_type,internal_keccak):
  48. if internal_keccak:
  49. cfg.use_internal_keccak_module = True
  50. add_msg = ' (internal keccak module)'
  51. else:
  52. add_msg = ''
  53. at = MMGenAddrType(proto,addr_type)
  54. privkey = PrivKey(proto,wif=wif)
  55. for n,backend in enumerate(get_backends(at.pubkey_type)):
  56. kg = KeyGenerator( cfg, proto, at.pubkey_type, n+1 )
  57. qmsg(blue(f' Testing backend {backend!r} for addr type {addr_type!r}{add_msg}'))
  58. data = kg.gen_data(privkey)
  59. for k,v in data._asdict().items():
  60. if v and k in ('pubkey','viewkey_bytes'):
  61. qmsg(f' {k+":":19} {v.hex()}')
  62. ag = AddrGenerator( cfg, proto, addr_type )
  63. addr = ag.to_addr(data)
  64. qmsg(f' addr: {addr}\n')
  65. assert addr == addr_chk, f'{addr} != {addr_chk}'
  66. cfg.use_internal_keccak_module = False
  67. def do_tests(coin,internal_keccak=False):
  68. proto = init_proto( cfg, coin )
  69. for wif,addr,addr_type in vectors[coin]:
  70. do_test(proto,wif,addr,addr_type,internal_keccak)
  71. return True
  72. class unit_tests:
  73. altcoin_deps = ('eth','xmr','zec')
  74. def btc(self,name,ut):
  75. return do_tests('btc')
  76. def eth(self,name,ut):
  77. do_tests('eth')
  78. return do_tests('eth',internal_keccak=True)
  79. def xmr(self,name,ut):
  80. if not cfg.fast:
  81. do_tests('xmr')
  82. return do_tests('xmr',internal_keccak=True)
  83. def zec(self,name,ut):
  84. return do_tests('zec')