ut_addrparse.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #!/usr/bin/env python3
  2. """
  3. test/unit_tests_d/ut_addrparse: address parsing tests for the MMGen suite
  4. """
  5. from mmgen.color import yellow,cyan
  6. from mmgen.util import msg,msg_r,pp_fmt
  7. from ..include.common import cfg,vmsg
  8. vectors = {
  9. 'btc_mainnet': [
  10. {'std': '1C5VPtgq9xQ6AcTgMAR3J6GDrs72HC4pS1'},
  11. {'std': '3AhjTiWHhVJAi1s5CfKMcLzYps12x3gZhg'},
  12. {'std': 'bc1q6pqnfwwakuuejpm9w52ds342f9d5u36v0qnz7c'}
  13. ],
  14. 'ltc_mainnet': [
  15. {'std': 'LUbHQNYoy23RByq4dKQotLA4ugk9FhpAMT'},
  16. {'std': 'MCoZrHYPqYKqvpiwyzzqf3EPxF5no6puEf'},
  17. {'std': 'ltc1qvmqas4maw7lg9clqu6kqu9zq9cluvllnst5pxs'}
  18. ],
  19. 'xmr_mainnet': [
  20. { # ut_xmrseed.vectors[0]:
  21. 'std': '42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm',
  22. # https://github.com/monero-project/monero/tests/functional_tests/integrated_address.py
  23. 'int': '4CMe2PUhs4J4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfSbLRB61BQVATzerHGj',
  24. 'id': '0123456789abcdef'
  25. },{
  26. 'std': '46r4nYSevkfBUMhuykdK3gQ98XDqDTYW1hNLaXNvjpsJaSbNtdXh1sKMsdVgqkaihChAzEy29zEDPMR3NHQvGoZCLGwTerK',
  27. 'int': '4GYjoMG9Y2BBUMhuykdK3gQ98XDqDTYW1hNLaXNvjpsJaSbNtdXh1sKMsdVgqkaihChAzEy29zEDPMR3NHQvGoZCVSs1ZojwrDCGS5rUuo',
  28. 'id': '1122334455667788'
  29. }
  30. ],
  31. 'zec_mainnet': [
  32. {'std': 't1KQYLBvjpmcQuATommo6gx2QTQDLPikB8Q'},
  33. {'std': 'zceQDpyNwek7dKqF5ZuFGj7YrNVxh7X1aPkrVxDLVxWSiZAFDEuy5C7XNV8VhyZ3ghTPQ61xjCGiyLT3wqpiN1Yi6mdmaCq'},
  34. ],
  35. 'eth_mainnet': [
  36. {'std': '7e5f4552091a69125d5dfcb7b8c2659029395bdf'},
  37. ],
  38. }
  39. def test_network(proto,addrs):
  40. def check_equal(a,b):
  41. assert a == b, f'{a.hex()} != {b.hex()}'
  42. def check_bytes(addr):
  43. if addr.parsed.ver_bytes is not None:
  44. check_equal(
  45. addr.parsed.ver_bytes,
  46. proto.addr_fmt_to_ver_bytes.get(addr.addr_fmt) )
  47. check_equal(
  48. addr.parsed.data + ((addr.parsed.payment_id or b'') if proto.coin == 'XMR' else b''),
  49. addr.bytes )
  50. def fmt_addr_data(addr):
  51. return pp_fmt({k:(v.hex() if isinstance(v,bytes) else v) for k,v in addr.parsed._asdict().items()})
  52. def print_info(addr):
  53. vmsg('\n{}\n{}\n{}'.format(yellow(addr.addr_fmt), cyan(addr), fmt_addr_data(addr)))
  54. msg_r(f'Testing {proto.coin} address parsing...')
  55. vmsg('')
  56. from mmgen.addr import CoinAddr
  57. for addr in addrs:
  58. a1 = CoinAddr(proto,addr['std'])
  59. print_info(a1)
  60. check_bytes(a1)
  61. assert not hasattr(a1.parsed,'payment_id') or a1.parsed.payment_id is None
  62. if 'int' in addr:
  63. a2 = CoinAddr(proto,addr['int'])
  64. print_info(a2)
  65. check_bytes(a2)
  66. check_equal( a1.parsed.data, a2.parsed.data )
  67. check_equal( a2.parsed.payment_id, bytes.fromhex(addr['id']) )
  68. msg('OK')
  69. vmsg('')
  70. class unit_test:
  71. def run_test(self,name,ut):
  72. from mmgen.protocol import init_proto
  73. for net_id,addrs in vectors.items():
  74. coin,network = net_id.split('_')
  75. if cfg.no_altcoin_deps and coin != 'btc':
  76. continue
  77. test_network(
  78. init_proto( cfg, coin, network=network ),
  79. addrs )
  80. return True