Locking and Unlocking Scripts in Bitcoin
Locking and Unlocking Scripts in Bitcoin
Definition:
A locking script is a line of code at the end of a transaction. Such a code specifies the conditions/elements
that a user has to provide to unlock the transaction. For example, in the Thor movie, this is the statement
that goes “who ever wants to wield this hammer must show it is worthy”. So, “showing the worthiness” is
the locking script. That is the condition that anu user has to show to unlock the transaction.
In the bitcoin network, a locking script has:
[DUP] [HASH160] [PubkHash] [Equalverify] [Checksig]
Operation to duplicate
Hash160: it is a hash function that has an output of 160 bits.
PubkHash: The Hash160 of the public key of the person that received the transaction.
Equalverify: statement that checks if two inputs are equal
Checksig: statement that verifies the signature.
An unlocking script is a line of code that allows a user to use a transaction. In the Thor example, is the act
of showing one’s worthiness.
In the bitcoin network, an unlocking script has:
[Signature] [Public Key]
Signature of the person wanting to use the transaction
Public key of the person wanting to use the transaction.
Example:
Mrs A received 5 BTC
Public key of Mrs A: 1234
Hash of the public key of Mrs A: a1a1
So, the transaction that Mrs A received will have the following locking script:
[DUP] [OP HASH160] [a1a1] [Equalverify] [Checksig]
If Mrs A wants to use the transaction above, she needs to provide the code to unlock her transaction (or
show that it is her transaction), or
[Signature] [1234]
So, to use the transaction, she needs to put the unlocking and locking script together, or:
[Signature] [1234] [DUP] [OP HASH160] [a1a1] [Equalverify] [Checksig]
To see how these two pieces actually unlock the transaction, one needs to work with Script. The script is a
stacking language. Basically, two pieces together will disappear and move the entire column. Think of it
as candy crush, oranges together will collapse and disappear, and the rest of the color will shift.
This is the process:
1. The DUP will duplicate the public key hash, the DUP will disappear
[Signature] [1234] [1234] [OP HASH160] [a1a1] [Equalverify] [Checksig]
2. The OP HASH 160 will compute the hash of the public key (recall that the hash is equal to a1a1),
the OP HASH160 will disappear:
[Signature] [1234] [a1a1] [a1a1] [Equalverify] [Checksig]
3. The equalverify will check if the previous two arguments are equal, then disappear.
4. The checksig will verify the signature. Remember that to verify a signature one needs: the
signature, the message and a public key. In bitcoin, the message is the public key itself. So, since
message=public key, we only need signature and public key to verify. If the signature is verified
and the stack is emptied (i.e., we match all the colors in candy crush), the transaction can be used.