The lock and unlock scripts for pay-to-pubkey-hash are a more secure way of assigning satoshis to a public key since the public key is not published as part of the lock script. It is only revealed with the unlock script.
The lock script has five components:
OP_DUP OP_HASH160 ⟨hash⟩ OP_EQUALVERIFY OP_CHECKSIG
The following is the lock script from the output of the previous transaction that is referenced by the input of the example transaction. The byte string is color coded to show the underlying data structures of the script:
These components are based on the following data structures:
OpCode : OP_DUP
OpCode : OP_HASH160
VarInt : Length of hash
[Byte] : Hash of public key
OpCode : OP_EQUALVERIFY
OpCode : OP_CHECKSIG
The unlock script has two components:
⟨signature⟩ ⟨pubKey⟩
The following is the unlock script from the input of the example transaction. The byte string is color coded to show the underlying data structures of the script:
These components are based on the following data structures:
VarInt : Length of signature
[Byte] : Serialized signature
VarInt : Length of public key
[Byte] : Serialized public key
The combined script has seven components:
⟨signature⟩ ⟨pubKey⟩ OP_DUP OP_HASH160 ⟨hash⟩ OP_EQUALVERIFY OP_CHECKSIG
The script is parsed and executed from left to right. Every data object is pushed on top of the stack implicitly. Validating the script the following steps are executed:
⟨signature⟩ ⟨pubKey⟩ OP_DUP OP_HASH160 ⟨hash⟩ OP_EQUALVERIFY OP_CHECKSIG
⟨signature⟩
⟨pubKey⟩ OP_DUP OP_HASH160 ⟨hash⟩ OP_EQUALVERIFY OP_CHECKSIG
⟨pubKey⟩⟨signature⟩
OP_DUP
duplicates the top most item and pushes it on the stackOP_DUP OP_HASH160 ⟨hash⟩ OP_EQUALVERIFY OP_CHECKSIG
⟨pubKey⟩
⟨pubKey⟩⟨signature⟩
OP_HASH160
pops the top most item off the stack and pushes the hash of the item on the stackOP_HASH160 ⟨hash⟩ OP_EQUALVERIFY OP_CHECKSIG
⟨hash⟩
⟨pubKey⟩⟨signature⟩
⟨hash⟩ OP_EQUALVERIFY OP_CHECKSIG
⟨hash⟩
⟨hash⟩
⟨pubKey⟩⟨signature⟩
OP_EQUALVERIFY
pops the top two items off the stack and stops execution with an error if the items are not equalOP_EQUALVERIFY OP_CHECKSIG
⟨pubKey⟩⟨signature⟩
OP_CHECKSIG
pops the first two items off the stack, checks the signature and pushes ⟨1⟩
on the stack if the signature is valid (execution stops with an error otherwise)OP_CHECKSIG
⟨1⟩
⟨1⟩
after the execution has finishedThe OpCode OP_EQUALVERIFY
is a combination of the OpCodes OP_EQUAL
and OP_VERIFY
. The first pops the two top items from the stack and pushes ⟨1⟩
on the stack if the items are equal and ⟨0⟩
otherwise. The second pops the top most item from the stack and interrupts execution if the item is not ⟨1⟩
.