0

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?

2
  • What do you mean by "two transactions"? There aren't 2 transactions in your example.
    – Andrew Chow
    Nov 9, 2020 at 1:07
  • I mean for the inputs of <200> and <100> Nov 9, 2020 at 7:31

1 Answer 1

0

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

0x94026400

c. OP_EQUAL is encoded as 0x87 (in hex)

0x9402640087

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

RIPEMD160(SHA256(0x9402640087)) = 0xa038dd58be8e9c3bd9f26fe585dcdfe85d70ac4d

  1. 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

5
  • 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 ? Nov 10, 2020 at 4:52
  • 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 ? Nov 10, 2020 at 5:26
  • 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
    Nov 10, 2020 at 8:41
  • 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
    Nov 10, 2020 at 8:48
  • You can have better understanding of stack and bitcoin script here: youtu.be/6Fa04MnURhw
    – Brqoo
    Nov 10, 2020 at 8:55

Your Answer

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

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