<p>You cannot put your plain script in scriptPubKey hence it's considered non-standard and will not be relayed/mined. You need to hash your custom script, put script hash in P2SH output, and reveal the script later when you spend it (in scriptSig).</p>
<p>Here's a step-by-step demonstration:</p>
<pre><code>OP_SUB &lt; 100 &gt; OP_EQUAL
</code></pre>
<ol>
<li>Convert your script to hexadecimal representation:</li>
</ol>
<p>a. OP_SUB is encoded as 0x94 (in hex)</p>
<blockquote>
<p>0x94</p>
</blockquote>
<p>b. 100(dec) is encoded as 0x6400 (in little endian) plus one-byte length prefix</p>
<blockquote>
<p>0x94<strong>026400</strong></p>
</blockquote>
<p>c. OP_EQUAL is encoded as 0x87 (in hex)</p>
<blockquote>
<p>0x94026400<strong>87</strong></p>
</blockquote>
<ol start="2">
<li>Hash your script in order to get script hash:</li>
</ol>
<blockquote>
<p>RIPEMD160(SHA256(0x9402640087)) =
0xa038dd58be8e9c3bd9f26fe585dcdfe85d70ac4d</p>
</blockquote>
<ol start="3">
<li>Your P2SH scriptPubKey is:</li>
</ol>
<blockquote>
<p>OP_HASH160 a038dd58be8e9c3bd9f26fe585dcdfe85d70ac4d OP_EQUAL</p>
</blockquote>
<p>In order to spend/unlock this output your scriptSig should be:</p>
<blockquote>
<p>&lt; 200 &gt; &lt; 100 &gt; &lt; OP SUB &lt; 100 &gt; OP EQUAL &gt;</p>
</blockquote>
<p>In Hex:</p>
<blockquote>
<p>0x02C800026400059402640087</p>
</blockquote>
<p>Please note that arithmetic operations like OP_ADD and OP_SUB can only accept 32-bit integers, so if any input encountered longer than 4 bytes, the script must abort and fail.</p>
<p>Feel free to learn more about P2SH <a href="https://learnmeabitcoin.com/technical/p2sh" rel="nofollow noreferrer">here</a></p>

CC BY-SA 4.0
2020-11-10T04:52:09.263Z
Thank you for your answer, as in your answer stated that the scriptSig is < 200 > < 100 > < OP_SUB < 100 > OP_EQUAL > then the scriptSig would not be able to spend the transaction due to the insufficient fund? 
Because previous transaction of scriptPubKey contains only OP_SUB < 100 > OP_EQUAL ?
Taufik Iqbal Ramdhani
CC BY-SA 4.0
2020-11-10T05:26:54.743Z
I mean because of the OP_SUB that subtracts 100 - 200 then it resulted as -100. Therefore, the OP_EQUAL operation will conclude invalid because -100 is not equal with 100 ?
Taufik Iqbal Ramdhani
CC BY-SA 4.0
2020-11-10T08:41:07.890Z
scriptSig elements are placed in stack from left to right. Top stack element will be "< OP_SUB < 100 > OP_EQUAL >", bottom stack element will be "< 200 >", and "< 100 >" in the middle. Top element is popped out from the stack, hashed and compared against the hash in scriptPukey. If matches, the actual script execution begins:
Brqoo
CC BY-SA 4.0
2020-11-10T08:48:09.447Z
In the actual script execution, two left stack elements are 200 and 100. "< 200 >" at the bottom, "< 100 >" at the top. OP_SUB pops the top two items 100 (a) and 200 (b), then executes "b - a". Learn more about opcodes here: en.bitcoin.it/wiki/Script
Brqoo
CC BY-SA 4.0
2020-11-10T08:55:00.810Z
You can have better understanding of stack and bitcoin script here: https://youtu.be/6Fa04MnURhw
Brqoo
CC BY-SA 4.0
2020-11-09T10:37:18.953Z
2020-11-09T11:12:53.033Z
2020-11-09T11:12:53.033Z
Brqoo
<p>For example, the bitcoin script of a scriptPubKey that consists of OP SUB &lt; 100 &gt; OP EQUAL, and is used to lock an output of a transaction. OP SUB pops the top two elements in the stacks, subtracts them, then pushes back the result. &lt; X &gt; pushes value X on the stack.</p>
<p>Also, the following, a scriptSig script that consists of &lt; 200 &gt;&lt; 100 &gt;, which is used as an unlocking script to spend that output.</p>
<p>In that case, will the scriptSig succeed to unlock the scriptPubKey thus allow the transaction to spend the money? Because of how Bitcoin combines the scripts and how it executes them, like what are the contents of the stack at each step of the execution?</p>

CC BY-SA 4.0
2020-11-09T01:07:41.603Z
What do you mean by "two transactions"? There aren't 2 transactions in your example.
Ava Chow
CC BY-SA 4.0
2020-11-09T07:31:13.360Z
I mean for the inputs of <200> and <100>
Taufik Iqbal Ramdhani
<transactions><script><input-script>
Bitcoin scriptSig with two transactions to unlock the scriptPubKey
Taufik Iqbal Ramdhani
# Bitcoin scriptSig with two transactions to unlock the scriptPubKey

(Asked by: username_0 on 2020-11-09)

For example, the bitcoin script of a scriptPubKey that consists of OP SUB < 100 > OP EQUAL, and is used to lock an output of a transaction. OP SUB pops the top two elements in the stacks, subtracts them, then pushes back the result. < X > pushes value X on the stack.

Also, the following, a scriptSig script that consists of < 200 >< 100 >, which is used as an unlocking script to spend that output.

In that case, will the scriptSig succeed to unlock the scriptPubKey thus allow the transaction to spend the money? Because of how Bitcoin combines the scripts and how it executes them, like what are the contents of the stack at each step of the execution?

**Tags:** transactions, script, input-script

> **Comments:**
> **username_1**: What do you mean by "two transactions"? There aren't 2 transactions in your example.  
> **username_0**: I mean for the inputs of <200> and <100>  

---

## 1 answers

### Answer by username_2 on 2020-11-09. Score: 0 | Accepted answer

You cannot put your plain script in scriptPubKey hence it's considered non-standard and will not be relayed/mined. You need to hash your custom script, put script hash in P2SH output, and reveal the script later when you spend it (in scriptSig).

Here's a step-by-step demonstration:

```
OP_SUB < 100 > OP_EQUAL

```

1. Convert your script to hexadecimal representation:

a. OP\_SUB is encoded as 0x94 (in hex)

> 0x94

b. 100(dec) is encoded as 0x6400 (in little endian) plus one-byte length prefix

> 0x94**026400**

c. OP\_EQUAL is encoded as 0x87 (in hex)

> 0x94026400**87**

2. Hash your script in order to get script hash:

> RIPEMD160(SHA256(0x9402640087)) =
> 0xa038dd58be8e9c3bd9f26fe585dcdfe85d70ac4d

3. Your P2SH scriptPubKey is:

> OP\_HASH160 a038dd58be8e9c3bd9f26fe585dcdfe85d70ac4d OP\_EQUAL

In order to spend/unlock this output your scriptSig should be:

> < 200 > < 100 > < OP SUB < 100 > OP EQUAL >

In Hex:

> 0x02C800026400059402640087

Please note that arithmetic operations like OP\_ADD and OP\_SUB can only accept 32-bit integers, so if any input encountered longer than 4 bytes, the script must abort and fail.

Feel free to learn more about P2SH [here](https://learnmeabitcoin.com/technical/p2sh)

> **Comments:**
> **username_0**: Thank you for your answer, as in your answer stated that the scriptSig is < 200 > < 100 > < OP_SUB < 100 > OP_EQUAL > then the scriptSig would not be able to spend the transaction due to the insufficient fund?  Because previous transaction of scriptPubKey contains only OP_SUB < 100 > OP_EQUAL ?  
> **username_0**: I mean because of the OP_SUB that subtracts 100 - 200 then it resulted as -100. Therefore, the OP_EQUAL operation will conclude invalid because -100 is not equal with 100 ?  
> **username_2**: scriptSig elements are placed in stack from left to right. Top stack element will be "< OP_SUB < 100 > OP_EQUAL >", bottom stack element will be "< 200 >", and "< 100 >" in the middle. Top element is popped out from the stack, hashed and compared against the hash in scriptPukey. If matches, the actual script execution begins:  
> **username_2**: In the actual script execution, two left stack elements are 200 and 100. "< 200 >" at the bottom, "< 100 >" at the top. OP_SUB pops the top two items 100 (a) and 200 (b), then executes "b - a". Learn more about opcodes here: en.bitcoin.it/wiki/Script  
> **username_2**: You can have better understanding of stack and bitcoin script here: https://youtu.be/6Fa04MnURhw  

---

