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.
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.
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:
Identify the previous transaction that contains the UTXOs that you already possess (Bitcoin that you own/possess)
Build the input outpoints for the new transaction in order to identify the previous transactions of UTXOs to spend
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
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.
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.
To better understand this, let’s look at an example. We took a random transaction from blockchain.com, you can view it here:
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.
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.
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.
If you found What is P2PKH? helpful, don’t forget to subscribe below and checkout some of our other articles.