Advertisement
Guest User

Bitcoin SE debugging

a guest
Mar 15th, 2015
513
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.56 KB | None | 0 0
  1. import __future__
  2.  
  3. # from pybitcointools import sha256
  4. import hashlib #, ecdsa
  5.  
  6. # from ecdsa import SigningKey, SECP256k1
  7. from pycoin.ecdsa import *
  8.  
  9. import string
  10. import array
  11. import binascii
  12.  
  13. def rev(str):
  14.     ret = ""
  15.     for i in range(0, len(str), 2):
  16.         ret = str[i:i+2] + ret
  17.     return ret
  18.  
  19. def revbyfours(s):
  20.     res = ""
  21.     for i in range(int(len(s)/8)):
  22.         res += rev(s[8*i:8*i+8])
  23.     return res
  24.  
  25. def verify_sig(sig, prefix, xpub, signed_val):
  26.     is_even = (prefix % 2 == 0)
  27.     pub_pair = public_pair_for_x(generator_secp256k1, xpub, is_even)
  28.     print("sig: (" + hex(sig[0])      + ", " + hex(sig[1])      + ")")
  29.     print("pub: (" + hex(pub_pair[0]) + ", " + hex(pub_pair[1]) + ")")
  30.     print("hex: "  + hex(signed_val))
  31.  
  32.     print("is valid: " + str(verify(generator_secp256k1, pub_pair, signed_val, sig)))
  33.  
  34. def DoubleHash(string):
  35.     bin = binascii.unhexlify(string)
  36.     hash = hashlib.sha256(hashlib.sha256(bin).digest()).digest()
  37.     raw = str(binascii.hexlify(hash))[2:-1]
  38.     return rev(raw)
  39.  
  40. def DER(r, s):
  41.     rlen = int(len(r)/2)
  42.     if (rlen == 32 and int(r[0:2], 16) > 0x7F):
  43.         rlen += 1
  44.    
  45.     slen = int(len(s)/2)              
  46.     if (slen == 32 and int(s[0:2], 16) > 0x7F):
  47.         slen += 1
  48.    
  49.     res = "30"
  50.     res += hex(4 + rlen + slen)[2:].zfill(2)
  51.     res += "02"
  52.     res += hex(rlen)[2:].zfill(2)
  53.     res += r
  54.     res += "02"
  55.     res += hex(slen)[2:].zfill(2)
  56.     res += s
  57.     return res
  58.  
  59. addr     = 'mu858WTEPiWWpAJRTMxC4ka6DJqiaCZiSB'
  60. pubkey_x = 0x79b22a5127d176a49d506c86f791031f94a389227ef46a8ddb725a88c944c37e
  61. pubkey_y = 0x3f753de6ee0b441a0237801f140810e111a1fd8276a2a5d0ee07224a1b551cc1
  62.  
  63. # TX structure with 00 as scriptSig, from bitcoincore createrawtransaction
  64. rawtx = '01000000015a800fd903679acfe9d4e2fedb752b24c4d7a574b4c82a113dfb993b3864b7720100000000ffffffff01c09ee605000000001976a914dd6cce9f255a8cc17bda8ba0373df8e861cb866e88ac00000000'
  65.  
  66. # replacing the '00' scriptSig value
  67. unsigned = ""
  68.  
  69. unsigned += rawtx[:82]
  70. print (unsigned)
  71.  
  72. unsigned += '19'+'76a914953de657be4b305f606d9a9fbd35b070a682475788ac' # scriptSig = scriptPubKey input
  73. print (unsigned)
  74.  
  75. unsigned += rawtx[84:]
  76. print (unsigned)
  77.  
  78. unsigned += '01000000'  # appending sighash_all
  79. print (unsigned)
  80.  
  81. unsigned = ''.join(unsigned)
  82. print (unsigned)
  83.  
  84. # unsigned = '01000000015a800fd903679acfe9d4e2fedb752b24c4d7a574b4c82a113dfb993b3864b772010000001976a914953de657be4b305f606d9a9fbd35b070a682475788acffffffff01c09ee605000000001976a914dd6cce9f255a8cc17bda8ba0373df8e861cb866e88ac0000000001000000'
  85.  
  86. # See https://github.com/warner/python-ecdsa/blob/master/README.md
  87.  
  88. # hashToSign = DoubleHash(unsigned)
  89. hashToSign = rev(revbyfours(DoubleHash(unsigned)))
  90. htsBytes = binascii.unhexlify(hashToSign)
  91. htsInt = int(hashToSign, 16)
  92.  
  93. print ("hts: " + hashToSign)
  94. # priv key for WIF: 93FxXUeMJp93YQAtGeW5cE23gFN4sJbBr1RBmerLFVUDuqQqKL5
  95. priv = 0xdc57c6d067376c36bbed632c9d00f03767867f337d5a86b5b0308a60004f08ee
  96.  
  97. sig = sign(generator_secp256k1, priv, htsInt)
  98.  
  99. r = hex(sig[0])[2:]
  100. s = hex(sig[1])[2:]
  101.  
  102. #print ("r: " + r)
  103. #print ("s: " + s)
  104.  
  105. derSig = DER(r, s) + "01" # 01 is for sighashall
  106. derSig = hex(int(len(derSig)/2))[2:].zfill(2) + derSig
  107.  
  108. #print ("der: " + derSig)
  109.  
  110. pub = "410479b22a5127d176a49d506c86f791031f94a389227ef46a8ddb725a88c944c37e3f753de6ee0b441a0237801f140810e111a1fd8276a2a5d0ee07224a1b551cc1"
  111.  
  112. # inserting the signature into the 00 the core software uses in place of ScriptSig
  113. signed = rawtx[:82] + hex(int(len(derSig + pub)/2))[2:].zfill(2) + derSig + pub + rawtx[84:]
  114.  
  115. print ("signed: " + signed)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement