Aalthough there are new scripts in Bitcoin which have been gaining popularity, the most common way in the network to carry out transactions is through scripts Pay to Public Key Hash, or better known as P2PKH. In the beginning the network used the script Pay to Public Key (P2PK), but due to the vulnerability to suffer a future attack by quantum computers, it was changed to P2PKH for greater security and network protection. And since then, this script plays a huge role in most transactions made in Bitcoin.
The truth is that both P2PK and P2PKH are very similar. Well, in both P2PK and P2PKH, users must demonstrate a complex cryptographic mechanism. This mechanism is based on proving that they are the true owners of the public key to receive payments and of the private key to spend the payment they receive. But the main difference between P2PK and P2PKH, is that the latter allows that at the time of receiving a transaction the user does not have to provide their full public key to the scriptPubKey. A situation that did occur within the P2PK scripts. That it directly sent the public key of the recipient of the transaction.
This simple change greatly improved Bitcoin's security against possible attacks that could be carried out with quantum computers. But at the same time, it also improved the privacy of transactions, in addition to reducing the size of each transaction, something great for Bitcoin's scalability. This is due to the fact provide only a hash of 20 bytes in length instead of the public key complete. A situation that prevented it from being known or disclosed, increasing the overall security of the network.
But surely you wonder how does P2PKH make all this possible? What mechanisms allow its operation? Well, we will examine all this below.
How does a P2PKH script work?
The operation of a P2PKH script begins at the moment, in which the receiver and sender prepare to carry out a transaction. At that point, the recipient must create a public and private key pair. Once these keys are created, the recipient will share their public key with the issuer of the transaction. In the process of encoding these keys, the receiver creates a hash of the public key that it will transmit to the sender in a secure way to carry out the transaction. That hash is our cryptocurrency or Bitcoin address. If you want to know a little about this generation process you can read our article on How is a Bitcoin address created?
Now, the moment the issuer has the Bitcoin address, it begins with the process of creating the transaction. This process leads to the use of Bitcoin Script, to generate a special sub-script that will be part of the P2PKH. This sub-script is named scriptSig and its function is to verify that the issuer really has the ownership of the cryptocurrencies that it is going to spend. To do this, it takes the public key with which the issuer received the cryptocurrencies at the time and verifies that it has the private key that enables it to spend. When this step is verified, the issuer can then continue with the process of generating the P2PKH script.
The second part of the process generates the scripPubKey sub-script. This part of the script delivers the power of cryptocurrencies sent to the public key hash that the issuer has received. By joining the scriptSig and the scriptPubKey we have a complete P2PKH, whose function can be summarized as follows:
An issuer can only spend bitcoins that they have received at a public address they own. But to spend them, you must prove that that address really is yours. So you need to supply the correct public key and private key to unlock the expense.
Confirmed this point, the issuer sends to another address a certain amount of coins that cease to be his property to become the property of another user, who can only spend those coins following the same process.
Example of a P2PKH
Let's look at an example to have a more detailed explanation of how this whole process occurs in Bitcoin. If Maria wants to send a standard transaction in P2PKH format to Pedro, then Pedro must start the process by creating the set of private key and public key where he wants to receive the transaction.
In the Bitcoin system, this set of keys is created securely using the elliptic curve digital signature algorithm (ECDSA) with the curve secp256k1. Then the generated public key is transformed using the functions SHA-256 and RIPEMD-160.
At this point, Pedro sends Maria the address, which then begins with the process of generating the transaction and the P2PKH script. Maria enters the transaction details into her wallet, which will then translate this data into a P2PKH script. And from the moment Maria presses "Send" the process begins. The transaction is sent to the network, where the miners will take it and include it in a block. At the end of the generation of this block, the network nodes will begin to perform the individual actions of each of these transactions.
It is at this moment that Maria's payment to Pedro becomes a reality. This, then, the nodes will execute the P2PKH script passing the ownership of the coins that Maria has sent to Pedro, making Pedro take full ownership of those coins. In the event that the operation generates a change, this change is sent to María who will maintain ownership of it. The rest of the coins that are not part of the sending of coins to Pedro or the change of María are the commissions that the miners took for their work.
The script P2PKH is present by default in wallets or purses that implement the Bitcoin client from practically the beginning of the system. Thus, just a few days after the launch of Bitcoin and having mined the first blocks of the network, on January 16, 2009, the P2PKH format began to be implemented in Bitcoin transactions. So this script is found in most of the mined blocks that are inside the blockchain.
Its creation is mainly due to two reasons:
- Satoshi Nakamoto I wanted a simpler and easier way for two users to share their public keys to transact. Thus, to make the public key a shorter and easier to use expression, a hash function was applied that makes this key a much more manageable and usable random set of alphanumeric characters. It is used as an address that can be shared with any user easily and reliably.
- Because ECDSA is vulnerable to the modified Shor algorithm. This algorithm can deduce the private key from the public key using a quantum computer. However, the implementation of P2PKH prevents this attack. By preventing the complete public key from being revealed to another user when making a transaction. Creating an additional layer of security over the private key and the public network, providing a higher level of protection. Although this is somewhat distant since the quantum power necessary to execute a successful attack of this type is close to 1500 qubits.