当我们讨论比特币交易的运作原理时,P2PKH 在比特币交易中发挥着很重要的作用。它有助于确保比特币的交易和存储。 P2PKH的全写是 “Pay to Public Key Hash”。公钥哈希是比特币地址的多种格式之一,另一种是P2SH,“Pay to Script Hash”。 基本上,P2PKH就是代表着“支付给这个比特币地址”。这是在区块链上的指令,将比特币从目前拥有者转移到新拥有人地址。每个比特币交易的背后,都有一些代码在后台运行,该代码是比特币编码语言。

基本原理

在深入讨论P2PKH概念之前,令一个极其重要的概念是Unspent Transaction Outputs(UTXO),即是未消费的交易输出。在比特币交易中,UTXO是我们经常使用和消费的东西。 UTXO的支出只能是一次,在我们使用它之后,我们将其称为 「支出交易输出」.

交易运作的概念

在每个交易中,它将包括数字、输入、输出和锁定时间。

Inputs 在这种情况下,包含 端点、序列号解锁编码 (又称为 scriptSig)。 

输出: 包含 value 所花费的金额和 locking script,也称 scriptPubKey

我们使用 locktime 设定交易在什么时间执行。 

每笔交易至少要有一个输入和输出。输入是用作告知网络需要消耗那种UTXO,并确保允许使用它们。输出是用于确保未来的交易将能够使用新创建的UTXO。 

下图显示交易关系:

建立交易的步骤

步骤 1:

确定先前包含UTXO的交易(您拥有的比特币)

步骤 2:

新交易输入点的设立是用于识别前交易已使用的UTXOs

步骤 3:

最后,如创建新的UTXOs去解锁下一个交易,您必须为新交易建立输出,这样锁定编码值便会包含条件

步骤 4:

最后,建立一个符合先前交易锁定编码的解锁编码。最后一步是收件人的签名,该签名也将包含在解锁编码中,但是,这部分将实际放在交易的中间。 

术语

1. PubkeyScript (另称为 scriptPubKey 或 locking script);
2. SignatureScript (亦称为 scriptSig 或 unlocking script)

PubkeyScript是一张记录了交易记录的指令列表,它控制了下一名使用者如何解锁已接收的比特币并传送。收款人会制造一个 signature script,而该文件必须满足最后一个发送者创建的PubkeyScript的参数。

PubkeyScript的参数:

1. 公钥杂凑值 (Public Key Hash) (比特币地址)
2. 数码签署

P2PKH地址的拥有者只能透过提供公钥杂凑值与私钥签名来解锁 PubkeyScript和使用已发送的资金。

在一般情况下,Bob 会提供比特币地址给 Alice,而Alice 会将比特币发送给Bob。 Alice 输出的时候会设立一个PubkeyScript。 Alice 完成交易后,交易被发送到了比特币网络上,比特币便会出现在Bob 的钱包里。

如果Bob想使用他刚刚收到的比特币,他必须证明自己是Alice 指定的比特币地址的拥有人。 Bob 需要建立一个包含他的电子签署和公钥的scriptSig。

ScriptPubKey 制定比特币转移的条件。在 P2PKH,条件就是「比特币地址的拥有人能证明自己拥有该钱包地址,才能将比特币传递给其他人」,而这个人只能是Bob。

因此,scriptSig是由 Bob 制定的数据,它能够配合由Alice 制定的 scriptPubKey的要求,他就可以将比特币发送给另一个人。

真实例子

为了更好地理解这一点,让我们看一个例子。我们从blockchain.com进行了随机抽取一个交易,您可以按 按此浏览

输入:

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.

总结

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

通讯