ut_xmrseed.py 5.4 KB

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