Demo: P2PKH Example

Now let’s piece everything that we’ve learned so far about Bitcoin script together, and walk through a sample execution of Pay to Public Key Hash.

First notice how the execution order is defined.

On the left hand side, we have the scriptSig on top, and the scriptPubKey on the bottom.

The scriptSig on top is the input script to a new transaction we are creating, and the scriptPubKey on the bottom is the output script of an old transaction we are trying to spend from.

Bitcoin Script is stack-based, which is first in last out, so execution will proceed as follows: We first put our signature on the stack.

Then we put our public key on the stack.

OP_DUP will duplicate the top item on the stack

OP_HASH160 hashes the previous item on the stack, first with SHA-256, and then with RIPEMD-160

Next, <pubKeyHash?> — with the question mark in the angle brackets — is the actual public key hash specified by the previous transaction output.

We put this on the stack as well OP_EQUALVERIFY then checks to see if the top two items in the stack are equal.

If they are equal, we continue with execution.

If not, then the transaction is invalid and we stop execution.

In this case, let’s say the transaction is valid, so we keep going with execution.

Lastly, OP_CHECKSIG checks the validity of the signature with the given public key.

If it’s valid, then it returns true.

And then finally, since the final return value is true, we know that the transaction was valid, and so the transaction will go through.

Proof of Burn