Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import struct
- import base58
- import hashlib
- import ecdsa
- import codecs
- import binascii
- #youtube vidceo https://www.youtube.com/watch?v=156XBrW22Bw
- #github example https://github.com/zeltsi/Mybitcoin/blob/master/tx%20as%20seen%20on%20youtube.py
- Bob_addr = "1NWzVg38ggPoVGAG2VWt6ktdWMaV6S1pJK"
- #bob_hashed_pubkey = base58.b58decode_check(Bob_addr)[1:].encode("hex")
- Bob_private_key = "CF933A6C602069F1CBC85990DF087714D7E86DF0D0E48398B7D8953E1F03534A"
- prv_txid = "84d813beb51c3a12cb5d0bb18c6c15062453d476de24cb2f943ca6e20115d85c"
- #Charlie_adr = "17X4s8JdSdLxFyraNUDBzgmnSNeZpjm42g"
- #charlie_hashed_pubkey = base58.b58decode_check(Charlie_adr)[1:].encode("hex")
- def doubleSha256(hex):
- bin = binascii.unhexlify(hex)
- hash = hashlib.sha256(bin).digest()
- hash2 = hashlib.sha256(hash).digest()
- print("Input Hex: "+str(hex,"ascii"))
- print("SHA256:\n "+str(binascii.hexlify(hash),"ascii"))
- print("Double-SHA256:\n "+str(binascii.hexlify(hash2),"ascii"))
- class raw_tx:
- version = struct.pack("<L", 1)
- tx_in_count = struct.pack("<B", 1)
- tx_in = {} #TEMP
- tx_out_count = struct.pack("<B", 1)
- tx_out1 = {} #TEMP
- lock_time = struct.pack("<L", 0)
- #Input of the TX
- rtx = raw_tx()
- #txid hash 32 bytes long and flip bytes to big endian
- #rtx.tx_in["txouthash"] = flip_byte_order(prv_txid).decode("hex")
- rtx.tx_in["txouthash"] = codecs.encode(codecs.decode(prv_txid, 'hex')[::-1], 'hex').decode()
- print(rtx.tx_in["txouthash"])
- #single output to address (vout) index 4 bytes, index 0 for example
- rtx.tx_in["tx_in_index"] = struct.pack("<L", 0)
- #get scriptpubkey of address OP76 OPa9 14 hashedpublickey OP88 OPac
- #hashed public key is 20bytes long represented by 14 in hex
- #from scripthash.py and put into HEX don't flip endian for this one
- #p2pkh script validation, this might not be right just yet, is this correct or needs to be in some other format?
- rtx.tx_in["script"] = "dca35c23679120087ccf656014ecea1f1f89eb34ba0489288480edc024acbf1f"
- print(rtx.tx_in["script"])
- rtx.tx_in["scrip_bytes"] = struct.pack("<B", len(rtx.tx_in["script"]))
- print(rtx.tx_in["scrip_bytes"])
- #4 bytes
- hex_string = "0xffffffff"[2:]
- bytes_object = bytes.fromhex(hex_string)
- print(bytes_object)
- rtx.tx_in["sequence"] = bytes.fromhex('ffffffff').decode('utf-16-be')
- print(rtx.tx_in["sequence"])
- ##########################################
- #Output of the TX
- # amount of coins to send, example 0.00100000, in denariis, BE CAREFUL ON THIS AMOUNT!!!
- #8 byte integer
- rtx.tx_out1["value"] = struct.pack("<Q", 100000)
- rtx.tx_out1["pk_script"] = "dca35c23679120087ccf656014ecea1f1f89eb34ba0489288480edc024acbf1f"
- rtx.tx_out1["pk_script_bytes"] = struct.pack("<B", len(rtx.tx_out1["pk_script"]))
- print(rtx.tx_out1["pk_script_bytes"])
- raw_tx_string = (
- rtx.version
- + rtx.tx_in_count
- + rtx.tx_in["txouthash"]
- + rtx.tx_in["tx_out_index"]
- + rtx.tx_in["scrip_bytes"]
- + rtx.tx_in["script"]
- + rtx.tx_in["sequence"]
- + rtx.tx_out_count
- + rtx.tx_out1["value"]
- + rtx.tx_out1["pk_script_bytes"]
- + rtx.tx_out1["pk_script"]
- + rtx.lock_time
- + struct.pack("<L", 1)
- )
- #hash the transaction 2x sha256 (not sure if this works or what the output should be in)
- hashed_tx_to_sign = doubleSha256(raw_tx_string)
- #create signing key (sk is from youtube example that won't work)
- #sk = ecdsa.SigningKey.from_string(Bob_private_key.decode("hex"), curve = ecdsa.SECP256k1)
- #example i have no clue anymore (https://stackoverflow.com/questions/56013591/python-ecdsa-same-public-keygenerated-with-ecdsa-verifyingkey-does-not-verify)
- sk = ecdsa.SigningKey.generate(curve=ecdsa.NIST256p, hashfunc = hashlib.sha256)
- pk = sk.get_verifying_key()
- publicKeyVerifyObject = ecdsa.VerifyingKey.from_string(bytes.fromhex(binascii.hexlify(pk.to_string()).decode('utf-8')), curve=ecdsa.NIST256p, hashfunc = hashlib.sha256)
- #continue from youtube example, copied and pasted
- public_key = ('\04' + pk.to_string()).encode("hex")
- #Sign Transaction
- signature = sk.sign_digest(hashed_tx_to_sign, sigencode = ecdsa.util.sigencode_der_canonize)
- sigscript = (
- signature
- + "\01"
- + struct.pack("<B", len(public_key.decode("hex")))
- + public_key.decode("hex")
- )
- real_tx = (
- rtx.version
- + rtx.tx_in_count
- + rtx.tx_in["txouthash"]
- + rtx.tx_in["tx_out_index"]
- + struct.pack("<B", len(sigscript) + 1)
- + struct.pack("<B", len(signature) + 1)
- + sigscript
- + rtx.tx_in["sequence"]
- + rtx.tx_out_count
- + rtx.tx_out1["value"]
- + rtx.tx_out1["pk_script_bytes"]
- + rtx.tx_out1["pk_script"]
- + rtx.tx_out2["value"]
- + rtx.tx_out2["pk_script_bytes"]
- + rtx.tx_out2["pk_script"]
- + rtx.lock_time
- )
- print real_tx.encode("hex")
- ########################
- #test junk
- test = bytes.fromhex('4a4b4c').decode('utf-8')
- print(test)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement