ut_msg.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #!/usr/bin/env python3
  2. """
  3. test.unit_tests_d.ut_msg: message signing unit tests for the MMGen suite
  4. """
  5. import os
  6. from mmgen.util import msg,bmsg,pumsg,suf
  7. from mmgen.protocol import CoinProtocol
  8. from mmgen.msg import NewMsg,UnsignedMsg,SignedMsg,SignedOnlineMsg,ExportedMsgSigs
  9. from mmgen.addr import MMGenID
  10. from ..include.common import cfg,silence,end_silence,restart_test_daemons,stop_test_daemons
  11. def get_obj(coin,network,msghash_type):
  12. if coin == 'bch':
  13. addrlists = 'DEADBEEF:C:1-20 98831F3A:C:8,2 A091ABAA:L:111 A091ABAA:C:1'
  14. elif coin == 'eth':
  15. addrlists = 'DEADBEEF:E:1-20 98831F3A:E:8,2 A091ABAA:E:111'
  16. else:
  17. # A091ABAA = 98831F3A:5S
  18. addrlists = 'DEADBEEF:C:1-20 98831F3A:B:8,2 A091ABAA:S:10-11 A091ABAA:111 A091ABAA:C:1'
  19. return NewMsg(
  20. cfg = cfg,
  21. coin = coin,
  22. network = network,
  23. message = '08/Jun/2021 Bitcoin Law Enacted by El Salvador Legislative Assembly',
  24. addrlists = addrlists,
  25. msghash_type = msghash_type )
  26. def print_total(n):
  27. msg(f'{n} signature{suf(n)} verified')
  28. async def run_test(network_id,chksum,msghash_type='raw'):
  29. coin,network = CoinProtocol.Base.parse_network_id(network_id)
  30. if not cfg.verbose:
  31. silence()
  32. bmsg(f'\nTesting {coin.upper()} {network.upper()}:\n')
  33. m = get_obj(coin,network,msghash_type)
  34. if m.proto.sign_mode == 'daemon':
  35. restart_test_daemons(network_id)
  36. pumsg('\nTesting data creation:\n')
  37. tmpdir = os.path.join('test','trash2')
  38. os.makedirs(tmpdir,exist_ok=True)
  39. assert m.chksum.upper() == chksum, f'{m.chksum.upper()} != {chksum}'
  40. m.write_to_file(
  41. outdir = tmpdir,
  42. ask_overwrite = False )
  43. pumsg('\nTesting signing:\n')
  44. m = UnsignedMsg( cfg, infile = os.path.join(tmpdir,get_obj(coin,network,msghash_type).filename) )
  45. await m.sign(wallet_files=['test/ref/98831F3A.mmwords'])
  46. m = SignedMsg( cfg, data=m.__dict__ )
  47. m.write_to_file(
  48. outdir = tmpdir,
  49. ask_overwrite = False )
  50. pumsg('\nTesting display:\n')
  51. m = SignedOnlineMsg( cfg, infile = os.path.join(tmpdir,get_obj(coin,network,msghash_type).signed_filename) )
  52. msg(m.format())
  53. single_addr = 'A091ABAA:E:111' if m.proto.base_proto == 'Ethereum' else 'A091ABAA:111'
  54. single_addr_coin = m.sigs[MMGenID(m.proto,single_addr)]['addr']
  55. pumsg('\nTesting single address display:\n')
  56. msg(m.format(single_addr))
  57. pumsg('\nTesting verification:\n')
  58. print_total( await m.verify() )
  59. pumsg('\nTesting single address verification:\n')
  60. print_total( await m.verify(single_addr) )
  61. pumsg('\nTesting JSON dump for export:\n')
  62. msg( m.get_json_for_export() )
  63. pumsg('\nTesting single address JSON dump for export:\n')
  64. msg( m.get_json_for_export(single_addr) )
  65. from mmgen.fileutil import write_data_to_file
  66. exported_sigs = os.path.join(tmpdir,'signatures.json')
  67. write_data_to_file(
  68. cfg = cfg,
  69. outfile = exported_sigs,
  70. data = m.get_json_for_export(),
  71. desc = 'signature data',
  72. ask_overwrite = False )
  73. m = ExportedMsgSigs( cfg, infile=exported_sigs )
  74. pumsg('\nTesting verification (exported data):\n')
  75. print_total( await m.verify() )
  76. pumsg('\nTesting single address verification (exported data):\n')
  77. print_total( await m.verify(single_addr_coin) )
  78. pumsg('\nTesting display (exported data):\n')
  79. msg(m.format())
  80. pumsg('\nTesting single address display (exported data):\n')
  81. msg(m.format(single_addr_coin))
  82. if m.proto.sign_mode == 'daemon':
  83. stop_test_daemons(network_id)
  84. msg('\n')
  85. if not cfg.verbose:
  86. end_silence()
  87. return True
  88. class unit_tests:
  89. altcoin_deps = ('ltc','bch','eth','eth_raw')
  90. def btc(self,name,ut):
  91. return run_test('btc','AA0DB5')
  92. def btc_tn(self,name,ut):
  93. return run_test('btc_tn','A88E1D')
  94. def btc_rt(self,name,ut):
  95. return run_test('btc_rt','578018')
  96. def ltc(self,name,ut):
  97. return run_test('ltc','BA7549')
  98. def bch(self,name,ut):
  99. return run_test('bch','1B8065')
  100. def eth(self,name,ut):
  101. return run_test('eth','35BAD9',msghash_type='eth_sign')
  102. def eth_raw(self,name,ut):
  103. return run_test('eth','9D900C')