2
Based on the script from github. I tried to construct a valid testnet transaction. I had to do some modifications to the script so here is the current code.
import struct
import base58
import hashlib
import ecdsa
import struct
import base58
import hashlib
import ecdsa
Bob_addr = "mwRhL3S4NATBKDJkgbaa1PXGFUB9Qi1uuj"
bob_hashed_pubkey = base58.b58decode_check(Bob_addr)[1:].encode("hex")
print(bob_hashed_pubkey)
print(base58.b58decode_check(Bob_addr)[1:])
Bob_private_key = "cQFXsyWHnz1CcxgDb1E4q7zLoB6jVzhCCGMyXSJaj74YGUik2N45"
prv_txid = "8310e8a8c65774651a658af96751cabbb89abd7c38661a95e662c923fa057238"
Charlie_adr = "mwRhL3S4NATBKDJkgbaa1PXGFUB9Qi1uuj"
charlie_hashed_pubkey = base58.b58decode_check(Charlie_adr)[1:].encode("hex")
class raw_tx:
version = struct.pack("<L", 1)
tx_in_count = struct.pack("<B", 1)
tx_in = {} #TEMP
tx_out_count = struct.pack("<B", 2)
tx_out1 = {} #TEMP
tx_out2 = {} #TEMP
lock_time = struct.pack("<L", 0)
def flip_byte_order(string):
flipped = "".join(reversed([string[i:i+2] for i in range(0, len(string), 2)]))
return flipped
rtx = raw_tx()
rtx.tx_in["txouthash"] = flip_byte_order(prv_txid).decode("hex")
rtx.tx_in["tx_out_index"] = struct.pack("<L", 1)
rtx.tx_in["script"] = ("76a914%s88ac" % charlie_hashed_pubkey).decode("hex")
rtx.tx_in["scrip_bytes"] = struct.pack("<B", len(rtx.tx_in["script"]))
rtx.tx_in["sequence"] = "ffffffff".decode("hex")
rtx.tx_out1["value"] = struct.pack("<Q", 100000)
rtx.tx_out1["pk_script"] = ("76a914%s88ac" % bob_hashed_pubkey).decode("hex")
rtx.tx_out1["pk_script_bytes"] = struct.pack("<B", len(rtx.tx_out1["pk_script"]))
rtx.tx_out2["value"] = struct.pack("<Q", 50000)
rtx.tx_out2["pk_script"] = ("76a914%s88ac" % bob_hashed_pubkey).decode("hex")
rtx.tx_out2["pk_script_bytes"] = struct.pack("<B", len(rtx.tx_out2["pk_script"]))
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.tx_out2["value"]
+ rtx.tx_out2["pk_script_bytes"]
+ rtx.tx_out2["pk_script"]
+ rtx.lock_time
+ struct.pack("<L", 1)
)
hashed_tx_to_sign = hashlib.sha256(hashlib.sha256(raw_tx_string).digest()).digest()
print(hashed_tx_to_sign)
#sk = ecdsa.SigningKey.from_string(Bob_private_key.decode("hex"), curve = ecdsa.SECP256k1)
#vk = sk.verifying_key
print(bitcoin.privkey_to_pubkey(Bob_private_key))
vk = bitcoin.privkey_to_pubkey(Bob_private_key)
public_key = ('\04' + vk).encode("hex")
#signature = sk.sign_digest(hashed_tx_to_sign, sigencode = ecdsa.util.sigencode_der_canonize)
signature = (bitcoin.ecdsa_sign(hashed_tx_to_sign, Bob_private_key))
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") )
the script is using the address and transaction found here https://live.blockcypher.com/btc-testnet/address/mwRhL3S4NATBKDJkgbaa1PXGFUB9Qi1uuj/ . When I try to broadcast the transaction using https://live.blockcypher.com/btc-testnet/pushtx/ . I get an error message saying it´s something wrong regarding the script in input transaction has a script based on pay-to-pubkey-hash and that´s how I try to decode it. Any tips?
The tx generated by the script
0100000001387205fa23c962e6951a66387cbd9ab8bbca5167f98a651a657457c6a8e81083010000009e5948314d3064512b6d6668436f4849693154514f79795975646b306f57786263586c4245582f5968636954386d567943375a717a41616d454570577530435950456f72656e644f44467a4f6e5146776b69595737354b67633d014304303364393466316464383732383862613163306666656665636161373161633733393733323132303662666137326631313563313761323639316331633866336539ffffffff02a0860100000000001976a914ae8370a2d4634b2b6f29b938a996f961f73ded4888ac50c30000000000001976a914ae8370a2d4634b2b6f29b938a996f961f73ded4888ac00000000
The error message is
Error validating transaction: Error running script for input 0 referencing 8310e8a8c65774651a658af96751cabbb89abd7c38661a95e662c923fa057238 at 1: Script was NOT verified successfully..
Add the exact error message, please. – Max Vernon – 2017-11-20T18:16:32.710
The error message is "Error validating transaction: Error running script for input 0 referencing 8310e8a8c65774651a658af96751cabbb89abd7c38661a95e662c923fa057238 at 1: Script was NOT verified successfully..". I will update the post with the raw tx. – user2350155 – 2017-11-20T19:13:50.103
Your input script is completely invalid and nothing like what your python script suggests you want to do. What seems to have happened here is that you have encoded a string into the transaction, not the bytes that are required. – Andrew Chow – 2017-11-20T20:10:02.077
How did you find that out? is there a way to decode the tx to analyze the part that went wrong? – user2350155 – 2017-11-20T20:56:31.700
You can use the
decoderawtransactioncall it bitcoin core to decode a raw transaction. From looking at the"asm"field of thescriptSigfrom the transaction's input, you can see clearly that it is a jumble of rubbish data, not at all a valid script – MeshCollider – 2018-01-05T03:16:04.940