Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import __future__
- # from pybitcointools import sha256
- import hashlib #, ecdsa
- # from ecdsa import SigningKey, SECP256k1
- from pycoin.ecdsa import *
- import string
- import array
- import binascii
- def rev(str):
- ret = ""
- for i in range(0, len(str), 2):
- ret = str[i:i+2] + ret
- return ret
- def revbyfours(s):
- res = ""
- for i in range(int(len(s)/8)):
- res += rev(s[8*i:8*i+8])
- return res
- def verify_sig(sig, prefix, xpub, signed_val):
- is_even = (prefix % 2 == 0)
- pub_pair = public_pair_for_x(generator_secp256k1, xpub, is_even)
- print("sig: (" + hex(sig[0]) + ", " + hex(sig[1]) + ")")
- print("pub: (" + hex(pub_pair[0]) + ", " + hex(pub_pair[1]) + ")")
- print("hex: " + hex(signed_val))
- print("is valid: " + str(verify(generator_secp256k1, pub_pair, signed_val, sig)))
- def DoubleHash(string):
- bin = binascii.unhexlify(string)
- hash = hashlib.sha256(hashlib.sha256(bin).digest()).digest()
- raw = str(binascii.hexlify(hash))[2:-1]
- return rev(raw)
- def DER(r, s):
- rlen = int(len(r)/2)
- if (rlen == 32 and int(r[0:2], 16) > 0x7F):
- rlen += 1
- slen = int(len(s)/2)
- if (slen == 32 and int(s[0:2], 16) > 0x7F):
- slen += 1
- res = "30"
- res += hex(4 + rlen + slen)[2:].zfill(2)
- res += "02"
- res += hex(rlen)[2:].zfill(2)
- res += r
- res += "02"
- res += hex(slen)[2:].zfill(2)
- res += s
- return res
- addr = 'mu858WTEPiWWpAJRTMxC4ka6DJqiaCZiSB'
- pubkey_x = 0x79b22a5127d176a49d506c86f791031f94a389227ef46a8ddb725a88c944c37e
- pubkey_y = 0x3f753de6ee0b441a0237801f140810e111a1fd8276a2a5d0ee07224a1b551cc1
- # TX structure with 00 as scriptSig, from bitcoincore createrawtransaction
- rawtx = '01000000015a800fd903679acfe9d4e2fedb752b24c4d7a574b4c82a113dfb993b3864b7720100000000ffffffff01c09ee605000000001976a914dd6cce9f255a8cc17bda8ba0373df8e861cb866e88ac00000000'
- # replacing the '00' scriptSig value
- unsigned = ""
- unsigned += rawtx[:82]
- print (unsigned)
- unsigned += '19'+'76a914953de657be4b305f606d9a9fbd35b070a682475788ac' # scriptSig = scriptPubKey input
- print (unsigned)
- unsigned += rawtx[84:]
- print (unsigned)
- unsigned += '01000000' # appending sighash_all
- print (unsigned)
- unsigned = ''.join(unsigned)
- print (unsigned)
- # unsigned = '01000000015a800fd903679acfe9d4e2fedb752b24c4d7a574b4c82a113dfb993b3864b772010000001976a914953de657be4b305f606d9a9fbd35b070a682475788acffffffff01c09ee605000000001976a914dd6cce9f255a8cc17bda8ba0373df8e861cb866e88ac0000000001000000'
- # See https://github.com/warner/python-ecdsa/blob/master/README.md
- # hashToSign = DoubleHash(unsigned)
- hashToSign = rev(revbyfours(DoubleHash(unsigned)))
- htsBytes = binascii.unhexlify(hashToSign)
- htsInt = int(hashToSign, 16)
- print ("hts: " + hashToSign)
- # priv key for WIF: 93FxXUeMJp93YQAtGeW5cE23gFN4sJbBr1RBmerLFVUDuqQqKL5
- priv = 0xdc57c6d067376c36bbed632c9d00f03767867f337d5a86b5b0308a60004f08ee
- sig = sign(generator_secp256k1, priv, htsInt)
- r = hex(sig[0])[2:]
- s = hex(sig[1])[2:]
- #print ("r: " + r)
- #print ("s: " + s)
- derSig = DER(r, s) + "01" # 01 is for sighashall
- derSig = hex(int(len(derSig)/2))[2:].zfill(2) + derSig
- #print ("der: " + derSig)
- pub = "410479b22a5127d176a49d506c86f791031f94a389227ef46a8ddb725a88c944c37e3f753de6ee0b441a0237801f140810e111a1fd8276a2a5d0ee07224a1b551cc1"
- # inserting the signature into the 00 the core software uses in place of ScriptSig
- signed = rawtx[:82] + hex(int(len(derSig + pub)/2))[2:].zfill(2) + derSig + pub + rawtx[84:]
- print ("signed: " + signed)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement