Exercise: Bitcoin Scripting

Now that we're more familiar with scripting, let's try this exercise to simulate writing real scripts.

Working with Bitcoin scripts can be complex and not easily human-readable. However, there are some fun tools that we can play with. Below are two useful links: one to a Bitcoin script simulator and another to a list of opcodes that we can test out. 

Let's visit the script simulator and start with the very simple script that is present when the page loads…

1 
2 
OP_ADD

 

Note that we have two values, 1, and 2, and then an operator, OP_ADD. This operator takes two values off the stack, adds them together, and returns the sum to the stack. Try it out. 

Now, let's try something a bit more complex. 

6
3
OP_SUB
3
OP_EQUAL

 

We start by adding two values to the stack, 6 and 3. OP_SUB subtracts the second value from the first returning 3 to the stack. We then add 3 to the stack and run OP_EQUAL which takes two values off the stack and checks if they are equal. 

Now let's look at a Bitcoin P2PKH unlocking script. If Bob is spending these funds, the script probably looks like this…

BobSig
BobPubKey
OP_DUP
OP_HASH160
Hash 
OP_EQUALVERIFY
OP_CHECKSIG

 

Playing with Bitcoin scripts can get pretty complex. You'll need to get comfortable with hex, and be very careful to get your formatting correct, etc. So let's simplify things a bit here by removing the complexity of checking the signature. We'll remove BobSig and the OP_CHECKSIG, and we'll give you the values to run. 

02c9950c622494c2e9ff5a003e33b690fe4832477d32c2d256c67eab8bf613b34e
OP_DUP
OP_HASH160
843F0F0B0CFCA867B142D81CFD1B1DEBFF50D524
OP_EQUALVERIFY

 

Run this script through the simulator step and step to see what's happening. 


Source: Saylor Academy
Creative Commons License This work is licensed under a Creative Commons Attribution 4.0 License.

Last modified: Wednesday, October 6, 2021, 12:54 PM