ut_xmrseed.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #!/usr/bin/env python3
  2. """
  3. test/unit_tests_d/ut_xmrseed: Monero mnemonic unit test for the MMGen suite
  4. """
  5. from mmgen.common import *
  6. class unit_test(object):
  7. vectors = ( # private keys are reduced
  8. ( '148d78d2aba7dbca5cd8f6abcfb0b3c009ffbdbea1ff373d50ed94d78286640e', # Monero repo
  9. 'velvet lymph giddy number token physics poetry unquoted nibs useful sabotage limits benches ' +
  10. 'lifestyle eden nitrogen anvil fewest avoid batch vials washing fences goat unquoted',
  11. ),
  12. ( 'e8164dda6d42bd1e261a3406b2038dcbddadbeefdeadbeefdeadbeefdeadbe0f',
  13. 'viewpoint donuts ardent template unveil agile meant unafraid urgent athlete rustled mime azure ' +
  14. 'jaded hawk baby jagged haystack baby jagged haystack ramped oncoming point template'
  15. ),
  16. ( '6900dea9753f5c7ced87b53bdcfb109a8417bca6a2797a708194157b227fb60b',
  17. 'criminal bamboo scamper gnaw limits womanly wrong tuition birth mundane donuts square cohesive ' +
  18. 'dolphin titans narrate fuel saved wrap aloof magically mirror together update wrap'
  19. ),
  20. ( '0000000000000000000000000000000000000000000000000000000000000001',
  21. 'abbey abbey abbey abbey abbey abbey abbey abbey abbey abbey abbey abbey abbey abbey abbey abbey ' +
  22. 'abbey abbey abbey abbey abbey bamboo jaws jerseys abbey'
  23. ),
  24. ( '1c95988d7431ecd670cf7d73f45befc6feffffffffffffffffffffffffffff0f',
  25. 'powder directed sayings enmity bacon vapidly entrance bumper noodles iguana sleepless nasty flying ' +
  26. 'soil software foamy solved soggy foamy solved soggy jury yawning ankle solved'
  27. ),
  28. ( '2c94988d7431ecd670cf7d73f45befc6feffffffffffffffffffffffffffff0f',
  29. 'memoir apart olive enmity bacon vapidly entrance bumper noodles iguana sleepless nasty flying soil ' +
  30. 'software foamy solved soggy foamy solved soggy jury yawning ankle foamy'
  31. ),
  32. ( '4bb0288c9673b69fa68c2174851884abbaaedce6af48a03bbfd25e8cd0364102',
  33. 'rated bicycle pheasants dejected pouch fizzle shipped rash citadel queen avatar sample muzzle mews ' +
  34. 'jagged origin yeti dunes obtains godfather unbending pastry vortex washing citadel'
  35. ),
  36. ( '4bb0288c9673b69fa68c2174851884abbaaedce6af48a03bbfd25e8cd0364100',
  37. 'rated bicycle pheasants dejected pouch fizzle shipped rash citadel queen avatar sample muzzle mews ' +
  38. 'jagged origin yeti dunes obtains godfather unbending kangaroo auctions audio citadel'
  39. ),
  40. ( '1d95988d7431ecd670cf7d73f45befc6feffffffffffffffffffffffffffff0e',
  41. 'pram distance scamper enmity bacon vapidly entrance bumper noodles iguana sleepless nasty flying ' +
  42. 'soil software foamy solved soggy foamy solved soggy hashing mullet onboard solved'
  43. ),
  44. ( 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0f',
  45. 'foamy solved soggy foamy solved soggy foamy solved soggy foamy solved soggy foamy solved soggy ' +
  46. 'foamy solved soggy foamy solved soggy jury yawning ankle soggy'
  47. ),
  48. )
  49. def run_test(self,name,ut):
  50. def test_fromhex(b):
  51. vmsg('')
  52. qmsg('Checking seed to mnemonic conversion:')
  53. for privhex,chk in self.vectors:
  54. vmsg(f' {chk}')
  55. chk = tuple(chk.split())
  56. res = b.fromhex(privhex)
  57. if use_monero_python:
  58. mp_chk = tuple( wl.encode(privhex).split() )
  59. assert res == mp_chk, f'check failed:\nres: {res}\nchk: {chk}'
  60. assert res == chk, f'check failed:\nres: {res}\nchk: {chk}'
  61. def test_tohex(b):
  62. vmsg('')
  63. qmsg('Checking mnemonic to seed conversion:')
  64. for chk,words in self.vectors:
  65. vmsg(f' {chk}')
  66. res = b.tohex( words.split() )
  67. if use_monero_python:
  68. mp_chk = wl.decode( words )
  69. assert res == mp_chk, f'check failed:\nres: {res}\nchk: {mp_chk}'
  70. assert res == chk, f'check failed:\nres: {res}\nchk: {chk}'
  71. msg_r('Testing xmrseed conversion routines...')
  72. qmsg('')
  73. from mmgen.xmrseed import xmrseed
  74. b = xmrseed()
  75. b.check_wordlist()
  76. try:
  77. from monero.wordlists.english import English
  78. wl = English()
  79. except ImportError:
  80. use_monero_python = False
  81. ymsg('Warning: unable to import monero-python, skipping external library checks')
  82. else:
  83. use_monero_python = True
  84. test_fromhex(b)
  85. test_tohex(b)
  86. vmsg('')
  87. qmsg('Checking error handling:')
  88. bad_chksum_mn = ('abbey ' * 21 + 'bamboo jaws jerseys donuts').split()
  89. bad_word_mn = "admire zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo".split()
  90. bad_seed = 'deadbeef'
  91. good_mn = self.vectors[0][1].split()
  92. good_hex = self.vectors[0][0]
  93. bad_len_mn = good_mn[:22]
  94. th = b.tohex
  95. fh = b.fromhex
  96. bad_data = (
  97. ('hex', 'HexadecimalStringError', 'not a hexadecimal', lambda:fh('xx')),
  98. ('seed len', 'SeedLengthError', 'invalid seed byte len', lambda:fh(bad_seed)),
  99. ('mnemonic type', 'AssertionError', 'must be list', lambda:th('string')),
  100. ('pad arg (fromhex)', 'AssertionError', "invalid 'pad' arg", lambda:fh(good_hex,pad=23)),
  101. ('pad arg (tohex)', 'AssertionError', "invalid 'pad' arg", lambda:th(good_mn,pad=23)),
  102. ('word', 'MnemonicError', "not in Monero", lambda:th(bad_word_mn)),
  103. ('checksum', 'MnemonicError', "checksum", lambda:th(bad_chksum_mn)),
  104. ('seed phrase len', 'MnemonicError', "phrase len", lambda:th(bad_len_mn)),
  105. )
  106. ut.process_bad_data(bad_data)
  107. vmsg('')
  108. msg('OK')
  109. return True