<p>First, if you want to learn how to construct a raw transaction, you should really go to this question: <a href="https://bitcoin.stackexchange.com/questions/32628/redeeming-a-raw-transaction-step-by-step-example-required">Redeeming a raw transaction step by step example required</a></p>

<p>Second, the tx_in needs an <code>outpoint_hash</code>, which is the hash of the previous transaction you're spending from.</p>

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

<p>Fourth, <code>tx_in["script"]</code> 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).</p>

<p>Fifth, this library does the signing elsewhere as <code>script_bytes</code> needs to be the actual public key and signature.</p>

<p>I think you'd find it easier to understand using a slightly more standard python library like pycoin (<a href="https://github.com/richardkiss/pycoin" rel="nofollow noreferrer">https://github.com/richardkiss/pycoin</a>) or pybitcoin (<a href="https://github.com/blockstack/pybitcoin" rel="nofollow noreferrer">https://github.com/blockstack/pybitcoin</a>)</p>

CC BY-SA 3.0
2017-01-02T19:23:01.183Z
2017-01-02T19:23:01.183Z
2017-04-13T12:47:32.940Z
Jimmy Song
<p>Two days ago I've found a few articles, about using raw bitcoin protocol, and now I'm still trying to send a transaction.</p>

<p>My code is based on this <a href="https://github.com/Shultzi/Mybitcoin/blob/master/tx.py#L20" rel="nofollow noreferrer">class</a>, and
if I understand all of this bitcoin magic right, I need to sign <a href="https://blockchain.info/tx/60ee91bc1563e44866c66937b141e9ef4615a272fa9d764b9468c2a673c55e01" rel="nofollow noreferrer">my raw input transaction</a> with the private key for address <code>1LwPhYQi4BRBuuyWSGVeb6kPrTqpSVmoYz</code> and add my public key right after it - and it will be a valid unlocking script?</p>

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

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

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

<p>Thanks for any answers :)</p>

CC BY-SA 3.0
2017-01-02T14:28:17.797Z
That code does not look right.
Pieter Wuille
<script><signature><python>
Sending transaction with Python + raw protocol
Sergey Potekhin
# Sending transaction with Python + raw protocol

(Asked by: username_0 on 2017-01-02)

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](https://github.com/Shultzi/Mybitcoin/blob/master/tx.py#L20), and
if I understand all of this bitcoin magic right, I need to sign [my raw input transaction](https://blockchain.info/tx/60ee91bc1563e44866c66937b141e9ef4615a272fa9d764b9468c2a673c55e01) 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 :)

**Tags:** script, signature, python

> **Comments:**
> **username_1**: That code does not look right.  

---

## 1 answers

### Answer by username_2 on 2017-01-02. Score: 1 | Accepted answer

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](https://bitcoin.stackexchange.com/questions/32628/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>)


---

