bitcoin.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #!/usr/bin/env python
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C) 2013 by philemon <mmgen-py@yandex.com>
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. """
  19. bitcoin.py: Test suite for mmgen.bitcoin module
  20. """
  21. from mmgen.bitcoin import *
  22. from mmgen.utils import msg
  23. from test import *
  24. import sys
  25. def b58_randenc():
  26. r = get_random(24)
  27. r_enc = b58encode(r)
  28. print "Data (hex): %s" % hexlify(r)
  29. print "Base 58: %s" % r_enc
  30. r_dec = b58decode(r_enc)
  31. print "Decoded data: %s" % hexlify(r_dec)
  32. if r_dec != r:
  33. print "ERROR! Decoded data doesn't match original"
  34. sys.exit(9)
  35. def keyconv_compare_randloop(loops, quiet=False):
  36. try:
  37. for i in range(1,int(loops)+1):
  38. wif = numtowif_rand(quiet=True)
  39. if not quiet: sys.stderr.write("-- %s --\n" % i)
  40. ret = keyconv_compare(wif,quiet)
  41. if ret == False: sys.exit(9)
  42. if quiet:
  43. sys.stderr.write("\riteration: %i " % i)
  44. if quiet:
  45. sys.stderr.write("\r%s iterations completed\n" % i)
  46. else:
  47. print "%s iterations completed" % i
  48. except:
  49. print "\nUser interrupt"
  50. def keyconv_compare(wif,quiet=False):
  51. do_msg = nomsg if quiet else msg
  52. do_msg("WIF: %s" % wif)
  53. from subprocess import Popen, PIPE
  54. try:
  55. p = Popen(["keyconv", wif], stdout=PIPE)
  56. except:
  57. print "Error with execution of keyconv"
  58. sys.exit(3)
  59. kc_addr = dict([j.split() for j in p.stdout.readlines()])['Address:']
  60. addr = privnum2addr(wiftonum(wif))
  61. do_msg("Address (mmgen): %s" % addr)
  62. do_msg("Address (keyconv): %s" % kc_addr)
  63. if (kc_addr != addr):
  64. print "'keyconv' addr differs from internally-generated addr!"
  65. print "WIF: %s" % wif
  66. print "keyconv: %s" % kc_addr
  67. print "internal: %s" % addr
  68. return False
  69. else:
  70. return True
  71. def _do_hextowif(hex_in,quiet=False):
  72. do_msg = nomsg if quiet else msg
  73. do_msg("Input: %s" % hex_in)
  74. wif = numtowif(int(hex_in,16))
  75. do_msg("WIF encoded: %s" % wif)
  76. wif_dec = wiftohex(wif)
  77. do_msg("WIF decoded: %s" % wif_dec)
  78. if hex_in != wif_dec:
  79. print "ERROR! Decoded data doesn't match original data"
  80. sys.exit(9)
  81. return wif
  82. def hextowiftopubkey(hex_in,quiet=False):
  83. if len(hex_in) != 64:
  84. print "Input must be a hex number 64 bits in length (%s input)" \
  85. % len(hex_in)
  86. sys.exit(2)
  87. wif = _do_hextowif(hex_in,quiet=quiet)
  88. keyconv_compare(wif)
  89. def numtowif_rand(quiet=False):
  90. r_hex = hexlify(get_random(32))
  91. return _do_hextowif(r_hex,quiet)
  92. def strtob58(s,quiet=False):
  93. print "Input: %s" % s
  94. s_enc = b58encode(s)
  95. print "Encoded data: %s" % s_enc
  96. s_dec = b58decode(s_enc)
  97. print "Decoded data: %s" % s_dec
  98. test_equality(s,s_dec,[""],quiet)
  99. def hextob58(s_in,f_enc=b58encode, f_dec=b58decode, quiet=False):
  100. do_msg = nomsg if quiet else msg
  101. do_msg("Input: %s" % s_in)
  102. s_bin = unhexlify(s_in)
  103. s_enc = f_enc(s_bin)
  104. do_msg("Encoded data: %s" % s_enc)
  105. s_dec = hexlify(f_dec(s_enc))
  106. do_msg("Recoded data: %s" % s_dec)
  107. test_equality(s_in,s_dec,["0"],quiet)
  108. def b58tohex(s_in,f_dec=b58decode, f_enc=b58encode,quiet=False):
  109. print "Input: %s" % s_in
  110. s_dec = f_dec(s_in)
  111. print "Decoded data: %s" % hexlify(s_dec)
  112. s_enc = f_enc(s_dec)
  113. print "Recoded data: %s" % s_enc
  114. test_equality(s_in,s_enc,["1"],quiet)
  115. def hextob58_pad(s_in, quiet=False):
  116. hextob58(s_in,f_enc=b58encode_pad, f_dec=b58decode_pad, quiet=quiet)
  117. def b58tohex_pad(s_in, quiet=False):
  118. b58tohex(s_in,f_dec=b58decode_pad, f_enc=b58encode_pad, quiet=quiet)
  119. def hextob58_pad_randloop(loops, quiet=False):
  120. try:
  121. for i in range(1,int(loops)+1):
  122. r = hexlify(get_random(32))
  123. hextob58(r,f_enc=b58encode_pad, f_dec=b58decode_pad, quiet=quiet)
  124. if not quiet: print
  125. if not i % 100 and quiet:
  126. sys.stderr.write("\riteration: %i " % i)
  127. sys.stderr.write("\r%s iterations completed\n" % i)
  128. except:
  129. print "User interrupt"
  130. def test_wiftohex(s_in,f_dec=wiftohex,f_enc=numtowif):
  131. print "Input: %s" % s_in
  132. s_dec = f_dec(s_in)
  133. print "Decoded data: %s" % s_dec
  134. s_enc = f_enc(int(s_dec,16))
  135. print "Recoded data: %s" % s_enc
  136. def hextosha256(s_in):
  137. print "Entered data: %s" % s_in
  138. s_enc = sha256(unhexlify(s_in)).hexdigest()
  139. print "Encoded data: %s" % s_enc
  140. def pubhextoaddr(s_in):
  141. print "Entered data: %s" % s_in
  142. s_enc = pubhex2addr(s_in)
  143. print "Encoded data: %s" % s_enc
  144. tests = {
  145. "keyconv_compare": ['wif [str]','quiet [bool=False]'],
  146. "keyconv_compare_randloop": ['iterations [int]','quiet [bool=False]'],
  147. "b58_randenc": ['quiet [bool=False]'],
  148. "strtob58": ['string [str]','quiet [bool=False]'],
  149. "hextob58": ['hexnum [str]','quiet [bool=False]'],
  150. "b58tohex": ['b58num [str]','quiet [bool=False]'],
  151. "hextob58_pad": ['hexnum [str]','quiet [bool=False]'],
  152. "b58tohex_pad": ['b58num [str]','quiet [bool=False]'],
  153. "hextob58_pad_randloop": ['iterations [int]','quiet [bool=False]'],
  154. "test_wiftohex": ['wif [str]', 'quiet [bool=False]'],
  155. "numtowif_rand": ['quiet [bool=False]'],
  156. "hextosha256": ['hexnum [str]','quiet [bool=False]'],
  157. "hextowiftopubkey": ['hexnum [str]','quiet [bool=False]'],
  158. "pubhextoaddr": ['hexnum [str]','quiet [bool=False]'],
  159. }
  160. args = process_test_args(sys.argv, tests)
  161. eval(sys.argv[1])(*args)