When we discuss how Bitcoin transactions work, P2PKH plays an essential role in assisting the functioning of Bitcoin transactions. It helps ensure the transfer and storage of Bitcoins. The P2PKH concept stands for “Pay to Public Key Hash”. The Public Key Hash is one of many formats of the Bitcoin address, the other being “Pay to Script Hash”. At the most basic level, P2PKH means “pay to this Bitcoin address”. It serves as an instruction on the blockchain for users wanting to transfer Bitcoin to one another. Behind every transaction, there are underlying codes working behind the scene. This scripting language is known as the Bitcoin Scripting Language.

Fundamentals to Understand

An extremely important concept to note before going deeper in the discussion of the P2PKH concept is Unspent Transaction Outputs (UTXOs). In Bitcoin transactions, UTXOs are what we often use and consume. The UTXOs expenditure can only be once, after we use it, we will refer to it as a spent transaction output.

The Basics of Transaction Building

Within each transaction, it will include the version number, input, output, and locktime. 

Inputs in this case contain outpoint(s), sequence numbers, and an unlocking script, aka scriptSig. 

Outputs contain value of the amount that is being spent and the locking script, aka scriptPubKey

We use locktime to determine when the transaction will become valid. 

Every transaction will have a minimum of one input and output. The inputs serve as the logic that tells the network which UTXOs that are needed to be consumed and also makes sure that it is allowed to consume them. The output is the logic used to ensure that future transactions will be able to consume newly created UTXOs. 

The graphic below helps show the transaction relationship:

Steps to Building a Transaction

Step 1:

Identify the previous transaction that contains the UTXOs that you already possess (Bitcoin that you own/possess)

Step 2:

Build the input outpoints for the new transaction in order to identify the previous transactions of UTXOs to spend

Step 3:

In order for the new creation UTXOs to be spent/unlock for the next transaction, you must build the outputs for new transaction so that the locking script will contain the conditions

Step 4:

Lastly, create the unlocking script so that it meets the conditions of the previous transaction locking script. The very last step is the recipient’s signature which will also be contained in the unlocking script, however, this part will be physically put in the middle of the transaction. 

Terms You Need to Know

1. PubkeyScript (also call this scriptPubKey or locking script); and
2. SignatureScript (also call this scriptSig or unlocking script).

A PubkeyScript is a list of recorded instructions that accompany output transactions, it governs how the next person can unlock received Bitcoin and spend it. To do this, the recipient of sent Bitcoin will generate a signature script which must satisfy the parameters of a PubkeyScript created by the last sender.

These parameters are:

1. Public Key Hash (Bitcoin address)
2. Digital Signature

An owner of a P2PKH address can only unlock the PubKey Script and spend sent funds, by providing a public key hash and a private key signature.

In a typical scenario, Bob will give a Bitcoin address to Alice and Alice will send Bitcoin to Bob. So the output of Alice’s transaction will create a PubkeyScript. So, Alice’s job is done and the transaction is broadcasted onto the Bitcoin network and Bitcoin appears in Bob’s wallet.

For Bob to spend his newly received bitcoin, he must prove that he is the owner of the Bitcoin address that Alice attached the BTC too. Bob does this by building a scriptSig with his signature and his public key.

The scriptPubKey places conditions on the transfer of Bitcoin. In P2PKH the condition is “only the person who can prove they own the bitcoin address provided to Alice can pass the bitcoin to someone else”. In conclusion, this person can only ever be Bob.

Thus, the scriptSig is data that is generated by the sender, Bob, to be able to satisfy the scriptPubKey (generated by Alice) to send his Bitcoin to another person creating a scriptPubKey in the process attached to that recipient’s address.

Real Example

To better understand this, let’s look at an example. We took a random transaction from blockchain.com, you can view it here

Input:

Pkscript:

Hex:

76a9144fd31c644c4b46c153601d0e194ab689570f4ce488ac

ASM:

OP_DUP
OP_HASH160
4fd31c644c4b46c153601d0e194ab689570f4ce4
OP_EQUALVERIFY
OP_CHECKSIG

Sigscript:

Hex:

483045022100c71b09c8161ca14b6ef96f155173bda080d72bb77953122f268b9527ff806f9302200fac42255dbe0317bbbea75837fe2e9e9bfdb3fc1161d5c45353dfdf4ecba074012102f4c4c2b0b7c23b472cdc8c27d22a41677d7e69e220675647ccf231831e5113cf

ASM:

3045022100c71b09c8161ca14b6ef96f155173bda080d72bb77953122f268b9527ff806f9302200fac42255dbe0317bbbea75837fe2e9e9bfdb3fc1161d5c45353dfdf4ecba0740102f4c4c2b0b7c23b472cdc8c27d22a41677d7e69e220675647ccf231831e5113cf

In the example above, we can see the PubKeyscript and the scriptSig denote as Pkscript and Sigscript. Pkscript, as before, represents the instructions that are given to the user so that they can unlock the script. You can see that there are two representations of the Pkscript, one in Hex and another in ASM. Hex is the hexadecimal representation of the Pkscript and ASM, assembly, is the representation of Bitcoin’s scripting language op-codes. 

The OP that you see in front of the ASM Pkscript are codes of instruction that execute by the CPU. The Hex and the ASM are also relatively the same except for the “48” and 21” that indicate via (bolded) in the Sigscript for Hex. So these are op-codes that inform the interpreter to put a specific amount of bytes into a stack. Hence, for the Sigscript, it tells you to put 72 bytes (0x48) to the stack and 33 bytes (0x21) for the public key. 

Output:

Hex:

In the example above, one transaction records as “spent” and in red, and the other records as “unspent”, in green. The “spent” transaction means that the output was spent in a subsequent transaction and will not be able to be spent again. The “unspent” transaction (UTXO) is available for spending in future transactions.

ASM:

From the above example, for the Pkscript in ASM, there is once again the “OP” codes which are instructions to be executed by the CPU. In the code you can see that it asks the CPU to “verify” the users and also “checksig” to ensure that the transaction is valid. This is what allows Bitcoin transactions to ensure accuracy while also maintaining security.

Summary

Pay to Public Key Hash is an important concept that simply describes how ownership gets transferred on the blockchain. In addition, P2PKH plays a big role in understanding the fundamentals of transaction building for Bitcoin. It simplifies the process of how Bitcoin is transferred on the blockchain. In addition, there are lots of other variants such as P2PK, P2SH, P2MS etc but they all work on the same basic concept.

References

Newsletter

What is PersistenceOne?

What is PersistenceOne? One of the biggest concerns that people face today is the increasing financial debt of many countries. A lot of people are

Read More »

What is UTU?

What is UTU? In Swahili “UTU” means humanity. It’s also the principle behind UTU the company, which puts people first in their platform that turns

Read More »

What is The Sandbox?

What is the Sandbox? Have you ever imagined a world where players can build, own, and monetize their gaming experiences? Sandbox is a virtual world

Read More »

What is SakeSwap?

SakeSwap, an AMM protocol, brings trading slippage capture and a more efficient incentive mechanism. The TVL on Uniswap reached 2.14 billion US dollars, which means

Read More »