MENU

Why Understanding P2PKH is Important in Blockchain

January 13, 2017
0
6076
0

Introduction

P2PKH is an important acronym to learn. It stands for “Pay To Public Key Hash” and is the most common form of transaction on the bitcoin blockchain.

Part 1: What on Earth does “Pay to Public Key Hash” mean?

In a bitcoin transaction, the “Public Key Hash” is synonymous to a “bitcoin address”. (A bitcoin address is derived from a the hash of your public key for the technically minded).

So let’s reword P2PKH to mean “Pay to this bitcoin address”. That’s not so scary is it?

P2PKH is therefore an instruction on the blockchain to transfer ownership from the current owner to the new owner of the bitcoin address.

Why this is important to understand?

This instruction can be modified for example to say, “pay to this bitcoin address OR that bitcoin address”. This is basically a smart contract in its simplest form. It is also known as multi-sig. A multiple signature condition that states “please provide any 1 of the 2 provided bitcoin addresses for this transaction to evaluate to true and execute”.

That is the basics of P2PKH. Part 2 tackles this from a technical angle.

Part 2: What does it “technically” really mean?

There are lots of articles explaining what P2PKH is. Here is a nice diagram and this site (https://www.doc.ic.ac.uk/project/2015/163/g1516311/verification.html) explains it as well. The wikipedia is worth a read too.

The problem though is that it doesn’t clearly highlight one important aspect. The ordering.

All the examples show:

scriptPubKey: OP_DUP OP_HASH160 <pubKeyHash> OP_EQUALVERIFY OP_CHECKSIG
scriptSig: <sig> <pubKey>

This line of code allows the owner of a token (say a bitcoin) to transfer it to someone else.

All docs state that the scriptPubKey and the scriptSig are concatenated together and then evaluated and if true, the transaction is valid. Great. What does that mean?

I found the secret to understanding P2PKH is to think of scriptPubKey as going first.

The typical scenario is Alice is sending Bob 1 BTC. Bob creates a public/private key, creates a bitcoin address from his public key and gives this to Alice.

Alice then creates a scriptPubKey and uses Bob’s bitcoin address.

scriptPubKey: OP_DUP OP_HASH160 <Bob's btc address> OP_EQUALVERIFY OP_CHECKSIG

Alice’s job is now done. This transaction gets broadcasted to the blockchain network and 1BTC appears in Bob’s wallet. It looks like Bob now owns 1BTC and he kinda does. This is because when Bob comes to pay for his haircut with bitcoins, he has to transfer 1BTC to his hairdresser. It is at this time Bob has to man up and prove that he is the owner of the bitcoin address that Alice attached the 1BTC to.

Bob does this by building a scriptSig with Bob’s signature and Bob’s public key. The script then gets evaluated as per the diagram below. The diagram was taken from https://bitcoin.org/en/developer-guide#p2pkh-script-validation which is the best place to further understand P2PKH.

Notes: 

The scriptPubKey is often referred to as conditions placed on the transfer. 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”. And this person can only ever be Bob.

Sometimes it is called a “Locking Script” – http://chimera.labs.oreilly.com/books/1234000001802/ch05.html

“A good analogy for how this works is that the output scripts are puzzles that specify in which conditions can those bitcoins be spent. The input scripts provide the correct data to make those output scripts evaluate to true.” – https://bitcore.io/api/lib/script

Summary

Pay to Public Key Hash is an important concept that simply describes how ownership gets transferred on the blockchain. There are lots of other variants such as P2PK, P2SH, P2MS etc  but they all work on the same basic concept. Focus on understand scriptPubKey before scriptSig.

Leave a Comment!

Your email address will not be published. Required fields are marked *