shuijian-xu / bitcoin

0 stars 0 forks source link

the first p2sh transaction that i create successfully #220

Open shuijian-xu opened 4 years ago

shuijian-xu commented 4 years ago

from helper import decode_base58, SIGHASH_ALL, hash256, little_endian_to_int, hash160 from script import p2pkh_script, Script from tx import TxIn, TxOut, Tx from ecc import PrivateKey from helper import SIGHASH_ALL, h160_to_p2sh_address

secret_0 = little_endian_to_int(hash256(b'ShuiJin Xu secret 3')) private_key_0 = PrivateKey(secret_0) print(private_key_0.point.address(testnet=True)) print()

secret_1 = little_endian_to_int(hash256(b'ShuiJin Xu secret 4')) private_key_1 = PrivateKey(secret_1) pub_key_sec_1 = private_key_1.point.sec() print(private_key_1.point.address(testnet=True)) print() secret_2 = little_endian_to_int(hash256(b'ShuiJin Xu secret 5')) private_key_2 = PrivateKey(secret_2) pub_key_sec_2 = private_key_2.point.sec() print(private_key_2.point.address(testnet=True)) print()

prev_tx = bytes.fromhex('1b4a5abaa5bcf328c7608d0689a94dd26edf5e96c02ee443c1a3dda1e7602f32') prev_index = 0 tx_in = TxIn(prev_tx, prev_index)

tx_outs = []

change_amount = int(0.0009*100000000) # The amount must be in satoshis; given there are 100,000,000 satoshis per BTC, we have to multiply and cast to an integer. change_h160 = decode_base58('mpwCA8oeX34hRTiCQ4FYdz5T1164yhkaWN') change_script = p2pkh_script(change_h160) change_output = TxOut(amount=change_amount, script_pubkey=change_script)

target_amount = int(0.0001*100000000) # The amount must be in satoshis; given there are 100,000,000 satoshis per BTC, we have to multiply and cast to an integer. target_script = Script([0x52, pub_key_sec_1, pub_key_sec_2, 0x52, 0xae])

target_output = TxOut(amount=target_amount, script_pubkey=target_script) tx_obj = Tx(1, [tx_in], [change_output, target_output], 0, True) print(tx_obj.serialize().hex()) print()

redeemscript = tx_obj.tx_outs[1].script_pubkey redeemscript_str = redeemscript.serialize().hex() print(redeemscript_str) print()

redeemscript_hash = hash160(redeemscript.serialize()).hex() redeemscript_hash_len = len(redeemscript_hash) print(redeemscript_hash) print()

tx_obj.tx_outs[1].script_pubkey = Script([0xa9, bytes.fromhex(redeemscript_hash), 0x87])

redeemscript = tx_obj.tx_outs[1].script_pubkey redeemscript_str = redeemscript.serialize().hex() print(redeemscript_str) print()

print(h160_to_p2sh_address(bytes.fromhex(redeemscript_hash), True)) print()

z = tx_obj.sig_hash(0)

point = private_key_0.point sig = private_key_0.sign(z) print(point.verify(z, sig)) print()

sig_der=sig.der() sigs = sig_der + SIGHASH_ALL.to_bytes(1, 'big') sec = point.sec() script_sig = Script([sigs, sec]) tx_obj.tx_ins[0].script_sig = script_sig

print(tx_obj.serialize().hex()) print()

print(tx_obj.id()) print()

shuijian-xu commented 4 years ago

Notes: We get the transaction id as follow in the first time we run this script. As some part of p2sh scrip pubkey is from hash160 so you will get different result everytime you create the transaction.

address: 2NBFottyz3fvsS75wHP1ntqpgeGVxAojhzK

transaction id in first time: b1c59e7971079dee4bb0b1a869747d6172dc16ecacb702a6570946f7008b3675)

raw transaction in first time: 0100000001322f60e7a1dda3c143e42ec0965edf6ed24da989068d60c728f3bca5ba5a4a1b000000006b483045022100bc9ba0555ce0af56c131210c706525d0070dea1fa2fa71418486cfefcf2654de022005618b87af9caacdd2bc6b1a11f838f2a3bf59c87442df7f6631c91b2c79100c012103cbecf2556a1cc7798943865eae7f33f0aea717f8e5cb568974abc1d326d5bc75ffffffff02905f0100000000001976a914674eb0125d5fa1745e7ddca3398507604986bcf888ac102700000000000017a914c58e917f4b2d338dbb85d0bfb8b0e10bec2ff5f98700000000