<p>A common scriptPubKey format, known as Pay-to-Public-Key-Hash (P2PKH), has the following form:</p>
<p><code>OP_DUP OP_HASH160 &lt;push of 20-byte public key hash&gt; OP_EQUALVERIFY OP_CHECKSIG</code></p>
<p>The hash of the public key is formed by ripemd-160(sha-256(compressed public key)).</p>
<p>You can find the hex encoding of the opcodes here: <a href="https://en.bitcoin.it/wiki/Script" rel="nofollow noreferrer">https://en.bitcoin.it/wiki/Script</a></p>
<p>The amount should be encoded as 8 bytes, in little-endian format. If you want to send 1 million satoshis, this would be <code>0x40420F0000000000</code>.</p>
<p>By the way, you are missing &quot;Input Count: 01&quot; and sequence number in your example transaction.</p>
<hr />
<p>The scriptPubKey in the article you linked is:</p>
<p><code>76a914f76bc4190f3d8e2315e5c11c59cfc8be9df747e388ac</code></p>
<p>Decoded, this is:</p>
<p><code>OP_DUP OP_HASH160 [push 20 bytes 76bc4190f3d8e2315e5c11c59cfc8be9df747e3] OP_EQUALVERIFY OP_CHECKSIG</code></p>
<p>This is exactly the same as above - a P2PKH output spending to the public key whose hash is <code>76bc4190f3d8e2315e5c11c59cfc8be9df74</code>.</p>
<p>The article you linked is incorrect, when it says:</p>
<blockquote>
<p>Locking Script — This is the hash of the locking script that specifies the conditions that must be met in order to spend this output.</p>
</blockquote>
<p>It is referring to a P2SH output, but the output in the example is a P2PKH output.</p>

CC BY-SA 4.0
2022-01-06T07:17:34.713Z
If i write these values in a raw tx, will it be valid?
madhurkant
CC BY-SA 4.0
2022-01-06T07:18:28.450Z
Yes, all you need to do is hash the public key and put that in the script I mentioned.
meshcollider
CC BY-SA 4.0
2022-01-06T07:19:35.143Z
but sriptPubKey here: https://medium.datadriveninvestor.com/bitcoin-raw-transaction-breakdown-c0a5a3aa8688 is very different from hashing.
madhurkant
CC BY-SA 4.0
2022-01-06T07:23:33.483Z
There are many possible scriptPubKey formats. I've shown P2PKH. You can find more info on the others here: https://bitcoin.stackexchange.com/a/111575/51948
meshcollider
CC BY-SA 4.0
2022-01-06T07:31:28.750Z
Actually, the scriptPubKey in your example is exactly the same. I've added an explanation to the bottom of my answer.
meshcollider
CC BY-SA 4.0
2022-01-06T06:43:39.960Z
2022-01-06T07:35:31.523Z
2022-01-06T07:35:31.523Z
meshcollider
<p>I am currently learning bitcoin technology. So I came up around the term raw transaction. There are many tools to create a raw transaction but I want to do it all by myself.</p>
<p>My Idea is to write a raw transaction and broadcast it over network without cli. All I want is to deeply understand the Bitcoin Raw transaction.</p>
<p>To make it simple my transaction has one input and one output. I have got all the input as follows:</p>
<pre><code>#All the values entered here are for example. None need to be true.

Version: 010000
Reverse of previous Tx hash: 8602122a7044b8795b5829b6b48fb1960a124f42ab1c003e769bbaad31cb2
Previous output index: 000000
Script size: 6a
Script sig: 3044022.............................
Output Count: 01
Value(in satoshi): 1000000 //Please check if it is written in correct format or not
Script size: //To be known
scriptPubkey: //To be known
locktime: 00000000
</code></pre>
<p>The problem is I don't know how to generate the <code>scriptPubKey</code> and how to write the value in Satoshi.</p>
<p>The info you may need to generate <code>scriptPubKey</code> is:</p>
<pre><code>Private Key: 94C54DFCAE5E4E64A82B21478A9A243039AA57650EC6395FEC4250ACF90C3902
Public Key: 033c6f7a45e7892df81329e453b9faf5bafef648dfd9bec321eecc8e65512b3c367
</code></pre>
<p>Okay don't rush to block explorer. The wallets are empty. </p>

CC BY-SA 4.0
2022-01-06T07:29:51.433Z
I want to know how the sriptPubKey is generated? the scriptPubKey here: https://medium.datadriveninvestor.com/bitcoin-raw-transaction-breakdown-c0a5a3aa8688 is very long as compared to just a ripemd160 hash of public key.
madhurkant
CC BY-SA 4.0
2022-01-07T17:14:38.823Z
It sounds like you may be working with actual funds on mainnet. I couldn't find the transaction you are referring to in your question, but you should consider doing this sort of exploratory work on testnet instead in case you're currently using actual funds.
Murch
CC BY-SA 4.0
2024-02-08T09:07:57.067Z
related: https://bitcoin.stackexchange.com/questions/13936/does-createrawtransaction-have-the-ability-to-create-a-transaction-with-custom-s
Ciro Santilli OurBigBook.com
<transactions><script><raw-transaction><p2pkh>
Creating a raw transaction from scratch/
madhurkant
# Creating a raw transaction from scratch/

(Asked by: username_0 on 2022-01-06)

I am currently learning bitcoin technology. So I came up around the term raw transaction. There are many tools to create a raw transaction but I want to do it all by myself.

My Idea is to write a raw transaction and broadcast it over network without cli. All I want is to deeply understand the Bitcoin Raw transaction.

To make it simple my transaction has one input and one output. I have got all the input as follows:

```
#All the values entered here are for example. None need to be true.

Version: 010000
Reverse of previous Tx hash: 8602122a7044b8795b5829b6b48fb1960a124f42ab1c003e769bbaad31cb2
Previous output index: 000000
Script size: 6a
Script sig: 3044022.............................
Output Count: 01
Value(in satoshi): 1000000 //Please check if it is written in correct format or not
Script size: //To be known
scriptPubkey: //To be known
locktime: 00000000

```

The problem is I don't know how to generate the `scriptPubKey` and how to write the value in Satoshi.

The info you may need to generate `scriptPubKey` is:

```
Private Key: 94C54DFCAE5E4E64A82B21478A9A243039AA57650EC6395FEC4250ACF90C3902
Public Key: 033c6f7a45e7892df81329e453b9faf5bafef648dfd9bec321eecc8e65512b3c367

```

Okay don't rush to block explorer. The wallets are empty.

**Tags:** transactions, script, raw-transaction, p2pkh

> **Comments:**
> **username_0**: I want to know how the sriptPubKey is generated? the scriptPubKey here: https://medium.datadriveninvestor.com/bitcoin-raw-transaction-breakdown-c0a5a3aa8688 is very long as compared to just a ripemd160 hash of public key.  
> **username_1**: It sounds like you may be working with actual funds on mainnet. I couldn't find the transaction you are referring to in your question, but you should consider doing this sort of exploratory work on testnet instead in case you're currently using actual funds.  
> **username_2**: related: https://bitcoin.stackexchange.com/questions/13936/does-createrawtransaction-have-the-ability-to-create-a-transaction-with-custom-s  

---

## 1 answers

### Answer by username_3 on 2022-01-06. Score: 2 | Accepted answer

A common scriptPubKey format, known as Pay-to-Public-Key-Hash (P2PKH), has the following form:

`OP_DUP OP_HASH160 <push of 20-byte public key hash> OP_EQUALVERIFY OP_CHECKSIG`

The hash of the public key is formed by ripemd-160(sha-256(compressed public key)).

You can find the hex encoding of the opcodes here: <https://en.bitcoin.it/wiki/Script>

The amount should be encoded as 8 bytes, in little-endian format. If you want to send 1 million satoshis, this would be `0x40420F0000000000`.

By the way, you are missing "Input Count: 01" and sequence number in your example transaction.

---

The scriptPubKey in the article you linked is:

`76a914f76bc4190f3d8e2315e5c11c59cfc8be9df747e388ac`

Decoded, this is:

`OP_DUP OP_HASH160 [push 20 bytes 76bc4190f3d8e2315e5c11c59cfc8be9df747e3] OP_EQUALVERIFY OP_CHECKSIG`

This is exactly the same as above - a P2PKH output spending to the public key whose hash is `76bc4190f3d8e2315e5c11c59cfc8be9df74`.

The article you linked is incorrect, when it says:

> Locking Script — This is the hash of the locking script that specifies the conditions that must be met in order to spend this output.

It is referring to a P2SH output, but the output in the example is a P2PKH output.

> **Comments:**
> **username_0**: If i write these values in a raw tx, will it be valid?  
> **username_3**: Yes, all you need to do is hash the public key and put that in the script I mentioned.  
> **username_0**: but sriptPubKey here: https://medium.datadriveninvestor.com/bitcoin-raw-transaction-breakdown-c0a5a3aa8688 is very different from hashing.  
> **username_3**: There are many possible scriptPubKey formats. I've shown P2PKH. You can find more info on the others here: https://bitcoin.stackexchange.com/a/111575/51948  
> **username_3**: Actually, the scriptPubKey in your example is exactly the same. I've added an explanation to the bottom of my answer.  

---

