Two days ago I've found a few articles, about using raw bitcoin protocol, and now I'm still trying to send a transaction.

My code is based on this class, and if I understand all of this bitcoin magic right, I need to sign my raw input transaction with the private key for address 1LwPhYQi4BRBuuyWSGVeb6kPrTqpSVmoYz and add my public key right after it - and it will be a valid unlocking script?

Here's a part of my code, which I have completely copied from the previous link:

tx_in_count = struct.pack("<B", 1)
tx_in = {}                                                                                                                                                                                                     tx_in["outpoint_hash"] = flip_byte_order(previous_output_hash).decode("hex")
tx_in["outpoint_index"] = struct.pack("<L", 1)
tx_in["script"] = ("76a914%s88ac" % private_key).decode("hex")
tx_in["script_bytes"] = struct.pack("<B", (len(tx_in["script"])))
tx_in["sequence"] = "ffffffff".decode("hex")

I'm not sure about tx_in["script"] line, because there is nothing about signing transaction or public keys, so probably it's the reason for my failures.

Thanks for any answers :)

  • That code does not look right. Commented Jan 2, 2017 at 14:28

1 Answer 1


First, if you want to learn how to construct a raw transaction, you should really go to this question: Redeeming a raw transaction step by step example required

Second, the tx_in needs an outpoint_hash, which is the hash of the previous transaction you're spending from.

Third, you should never, ever encode your private key to any part of the transaction as you did in tx_in["script"]. That should stay on a secure device and you need that in order to generate the signature.

Fourth, tx_in["script"] should be the same script from the oupoint (hash, index). The specifics of the script language which is encoded in hex (76a914...88ac) are OP codes you should study (see link above).

Fifth, this library does the signing elsewhere as script_bytes needs to be the actual public key and signature.

I think you'd find it easier to understand using a slightly more standard python library like pycoin (https://github.com/richardkiss/pycoin) or pybitcoin (https://github.com/blockstack/pybitcoin)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.