gentest.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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.globalvars as g
  12. from mmgen.util import msg,msg_r,mmsg,mdie,red,green,vmsg
  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. -v, --verbose Produce more verbose output
  23. """,
  24. 'notes': """
  25. 'keyconv' is the address generation utility from the well-known vanitygen
  26. package. If it's installed on your system, {pnm} will use it by default to
  27. generate Bitcoin addresses. Otherwise, it falls back on its own internal
  28. routines, which use the Python ecdsa library.
  29. rounds is {} by default.
  30. """.format(rounds,pnm=g.proj_name)
  31. }
  32. cmd_args = opt.opts.init(opts_data,add_opts=["exact_output"])
  33. if len(cmd_args) == 1:
  34. try:
  35. rounds = int(cmd_args[0])
  36. assert rounds > 0
  37. except:
  38. msg("'rounds' must be a positive integer")
  39. sys.exit(1)
  40. elif len(cmd_args) > 1:
  41. opt.opts.usage(opts_data)
  42. if opt.system: sys.path.pop(0)
  43. from mmgen.addr import test_for_keyconv
  44. if not test_for_keyconv(silent=True):
  45. msg(
  46. "To run this test, you must install 'keyconv' from the vanitygen package.")
  47. sys.exit(1)
  48. msg(green("Comparing {}'s internally generated addresses against output of 'keyconv'").format(g.proj_name))
  49. from subprocess import check_output
  50. for i in range(1,rounds+1):
  51. msg_r("\rRound %s/%s " % (i,rounds))
  52. sec = hexlify(os.urandom(32))
  53. wif = hextowif(sec)
  54. a = privnum2addr(int(sec,16))
  55. vmsg("\nkey: %s\naddr: %s\n" % (wif,a))
  56. b = check_output(["keyconv", wif]).split()[1]
  57. if a != b:
  58. msg_r(red("\nERROR: Addresses do not match!"))
  59. msg("""
  60. sec key: {}
  61. WIF key: {}
  62. {pnm}: {}
  63. keyconv: {}
  64. """.format(sec,wif,a,b,pnm=g.proj_name).rstrip())
  65. sys.exit(3)
  66. msg(green("%sOK" % ("" if opt.verbose else "\n")))