input.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/usr/bin/env python3
  2. #
  3. # MMGen Wallet, a terminal-based cryptocurrency wallet
  4. # Copyright (C)2013-2026 The MMGen Project <mmgen@tuta.io>
  5. #
  6. # Project source code repository: https://github.com/mmgen/mmgen-wallet
  7. # Licensed according to the terms of GPL Version 3. See LICENSE for details.
  8. """
  9. test.cmdtest_d.include.input: Shared input routines for the cmdtest.py test suite
  10. """
  11. import time
  12. from ...include.common import getrand
  13. from .common import randbool
  14. def mnemonic_entry(t, mne, mn):
  15. p_ok, _ = mne.word_prompt
  16. for wnum, word in enumerate(mn, 1):
  17. t.expect(p_ok.format(wnum), word + ' ')
  18. def stealth_mnemonic_entry(t, mne, mn, entry_mode, pad_entry=False):
  19. def pad_mnemonic(mn, ss_len):
  20. def get_pad_chars(n):
  21. ret = ''
  22. for _ in range(n):
  23. m = int.from_bytes(getrand(1), 'big') % 32
  24. ret += r'123579!@#$%^&*()_+-=[]{}"?/,.<>|'[m]
  25. return ret
  26. ret = []
  27. for w in mn:
  28. if entry_mode == 'short':
  29. w = w[:ss_len]
  30. if len(w) < ss_len:
  31. npc = 3
  32. w = w[0] + get_pad_chars(npc) + w[1:]
  33. if pad_entry:
  34. w += '%' * (1 + mne.em.pad_max - npc)
  35. else:
  36. w += '\n'
  37. else:
  38. w = get_pad_chars(1) + w[0] + get_pad_chars(1) + w[1:]
  39. elif len(w) > (3, 5)[ss_len==12]:
  40. w = w + '\n'
  41. else:
  42. w = (
  43. get_pad_chars(2 if randbool() and entry_mode != 'short' else 0)
  44. + w[0] + get_pad_chars(2) + w[1:]
  45. + get_pad_chars(9))
  46. w = w[:ss_len+1]
  47. ret.append(w)
  48. return ret
  49. match entry_mode:
  50. case 'fixed':
  51. mn = ['bkr'] + mn[:5] + ['nfb'] + mn[5:]
  52. ssl = mne.uniq_ss_len
  53. def gen_mn():
  54. for w in mn:
  55. if len(w) >= ssl:
  56. yield w[:ssl]
  57. else:
  58. yield w[0] + 'z\b' + '#' * (ssl-len(w)) + w[1:]
  59. mn = list(gen_mn())
  60. case 'full' | 'short':
  61. mn = ['fzr'] + mn[:5] + ['grd', 'grdbxm'] + mn[5:]
  62. mn = pad_mnemonic(mn, mne.em.ss_len)
  63. mn[10] = '@#$%*##' + mn[10]
  64. wnum = 1
  65. p_ok, p_err = mne.word_prompt
  66. for w in mn:
  67. ret = t.expect((p_ok.format(wnum), p_err.format(wnum-1)))
  68. if ret == 0:
  69. wnum += 1
  70. for char in w:
  71. t.send(char)
  72. time.sleep(0.005)
  73. def user_dieroll_entry(t, data):
  74. for s in data:
  75. t.expect(r'Enter die roll #.+: ', s, regex=True)
  76. time.sleep(0.005)