Intro to P2PKH

Let’s walk through an example of how Script works under the hood.

So the example we are looking at is at the top of the screen.

This script specifies the most common type of transaction in Bitcoin, which is to redeem a previous transaction output.

To redeem this previous transaction, we need to prove our identities with (1) a public key that, when hashed, yields the address to which the previous transaction was sent, and (2) a signature that proves ownership of the private key corresponding to the public key we provided.

This is the most common type of script in Bitcoin Pay to Public Key Hash(shorthanded).

But how do we implement this functionality? With locking and unlocking scripts.

Unlocking scripts are scripts that you provide in your input when you want to spend from a previous transaction, allowing you to redeem the associated bitcoin.

You provide the signature and your public key, which are needed to prove your identity, and to unlock the transaction output.

Your public key is then hashed and checked against the address that owns the UTXO.

Because you provide a signature, this script is called scriptSig.

Locking scripts are the scripts that are found in previous transaction outputs.

The locking script specifies the requirements for redeeming a UTXO.

This essentially “locks” down the UTXO, so that it can only be spent by whoever can unlock it.

In our Pay to Pub Key Hash example, the locking script requires that the users that want to spend from a previous transaction output MUST prove that they possess a private key that hashes to a specific address.

For this reason, this script is called scriptPubKey.

To make unlocking and locking scripts work in tandem, we simply concatenate them.

We put the unlocking script on top of the locking script, scriptSig on top of scriptPubKey, and then run them together.

And the entire resulting script must execute successfully in order for the transaction to be considered valid.

Demo: P2PKH Example