gentest.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #!/usr/bin/python
  2. # Chdir to repo root.
  3. # Since script is not in repo root, fix sys.path so that modules are
  4. # imported from repo, not system.
  5. import sys,os
  6. pn = os.path.dirname(sys.argv[0])
  7. os.chdir(os.path.join(pn,os.pardir))
  8. sys.path.__setitem__(0,os.path.abspath(os.curdir))
  9. from binascii import hexlify
  10. import mmgen.opt as opt
  11. import mmgen.config as g
  12. from mmgen.util import msg,msg_r,msgrepr,msgrepr_exit,red,green
  13. from mmgen.bitcoin import hextowif,privnum2addr
  14. rounds = 100
  15. opts_data = {
  16. 'desc': "Test addresses generated by {} against output of 'keyconv'".format(g.proj_name),
  17. 'usage':"[options] [rounds]",
  18. 'options': """
  19. -h, --help Print this help message
  20. -s, --system Test scripts and modules installed on system rather than
  21. those in the repo root
  22. """,
  23. 'notes': """
  24. 'keyconv' is the address generation utility from the well-known vanitygen
  25. package. If it's installed on your system, {pnm} will use it by default to
  26. generate Bitcoin addresses. Otherwise, it falls back on its own internal
  27. routines, which use the Python ecdsa library.
  28. rounds is {} by default.
  29. """.format(rounds,pnm=g.proj_name)
  30. }
  31. cmd_args = opt.opts.init(opts_data,add_opts=["exact_output"])
  32. if len(cmd_args) == 1:
  33. try:
  34. rounds = int(cmd_args[0])
  35. assert rounds > 0
  36. except:
  37. msg("'rounds' must be a positive integer")
  38. sys.exit(1)
  39. elif len(cmd_args) > 1:
  40. opt.opts.usage(opts_data)
  41. if opt.system: sys.path.pop(0)
  42. from mmgen.addr import test_for_keyconv
  43. if not test_for_keyconv(silent=True):
  44. msg(
  45. "To run this test, you must install 'keyconv' from the vanitygen package.")
  46. sys.exit(1)
  47. msg(green("Comparing {}'s internally generated addresses against output of 'keyconv'").format(g.proj_name))
  48. from subprocess import check_output
  49. for i in range(1,rounds+1):
  50. msg_r("\rRound %s/%s " % (i,rounds))
  51. sec = hexlify(os.urandom(32))
  52. wif = hextowif(sec)
  53. a = privnum2addr(int(sec,16))
  54. b = check_output(["keyconv", wif]).split()[1]
  55. if a != b:
  56. msg_r(red("\nERROR: Addresses do not match!"))
  57. msg("""
  58. sec key: {}
  59. WIF key: {}
  60. {pnm}: {}
  61. keyconv: {}
  62. """.format(sec,wif,a,b,pnm=g.proj_name).rstrip())
  63. sys.exit(3)
  64. msg(green("\nOK"))