Module2_Lect6and7
Module2_Lect6and7
Blockchain
and DLT
(BLCH)
ITC801
Subject In-charge
Dr. Joanne Gomes
Professor Dept. of Information Technology SFIT
Room No. 317
email: jgomes@sfit.ac.in
Module 2
Lecture 6
Bitcoin
Topics:
• Bitcoin Concepts: Scripting Language and Transaction Scripts
• Block Structure
Revision: UTXO
• Your wallet consists of bitcoins in the form of Unspent transactions (UTXO).
• After an output has been “used up” in a transaction, it cannot be used again.
• Unspent outputs (UTXO), however, are alive. They are available to be used in new
transactions
Revision: Verifying Transactions
• A node will verify the transactions it receives by checking that its inputs have not
already been spent.
• If you try and use an output that has already been used in another transaction, your
transaction will be rejected by nodes.
Validated Rejected
Revision: Wallet Balance
• Address Balances:
• If you want to work out the balance of an
address, add up all of the unspent outputs that
are locked to that address.
Revision: Transactions Inputs and Outputs.
• Transactions Have Inputs and Outputs.
• Transaction inputs are the UTXOs being used, and transaction outputs are the
UTXO payment to the recipient plus any change.
• The difference between inputs and outputs is the fee, which miners collect for doing
the work.
• It’s these locks that prevent us from spending each other’s outputs in a transaction,
as every output we receive is encumbered by a lock.
• A transaction is the process of taking existing outputs and creating new ones from
them; it’s during the creation of these outputs that we give each one a “lock”.
Transaction Data Store on Blockchain
• So even though the blockchain is file of transactions, on a practical level you can
think of it as a storage unit for outputs.
• And when you want to send “your” bitcoins to someone, you simply refer to the
outputs in the blockchain that you are able to unlock:
• And when this transaction gets mined in to the blockchain, the outputs you used (as
inputs) will not be able to be used again.
.
locking scripts
Bob’s Bitcoin
address
locking scripts
Alice’s Bitcoin
address
unlocking
scripts
Script Language
• Bitcoin uses a scripting language called script to implement transactions on the blockchain.
Scripting language is turing INCOMPLETE system (does not allow infinite loops.).
• Bitcoin Script is a simple, stack-based (LIFO) programming language that enables the
processing and validation of transactions on the Bitcoin blockchain.
• A script is simply a set of instructions that specifies how a UTXO is able to be spent.
• Script is used exclusively to lock and unlock bitcoin, not to build applications or run
programs.
• They include opcodes to represent Constants, Flow Control operations, Stack Operations,
Splicing Operations, Bitwise Operations, Arithmetic Operations, Crypto Operations.
unlocking
scripts locking
scripts
Transaction Scripts for Validation
• Every bitcoin client will validate transactions by executing locking and unlocking scripts together.
• For each transaction, the validation software will first retrieve the UTXO referenced by the input. That UTXO
contains a locking script defining the conditions required to spend it.
• The validation software will then take the unlocking script contained in the input that is attempting to spend
this UTXO and execute the two scripts.
• In the bitcoin client, the unlocking and locking scripts are executed separately with the stack transferred
between the two executions.
• First, the unlocking script is executed, using stack execution engine. If unlocking script executed without
errors, main stack (not the alternate stack) is copied and then locking script is executed.
• If result of executing the locking script with stack data copied from the unlocking script is “TRUE,” the
unlocking script has succeeded in resolving conditions and the input is a valid authorization to spend UTXO.
Transaction Scripts
• The five standard types of transaction scripts are
– pay-to-public-key-hash (P2PKH),
– pay-to-public-key (P2PK)
– Pay-to-multi-signature (limited to 15 keys),
– pay-to-script-hash (P2SH), and
– data output (OP_RETURN)
Pay-to-Public-Key-Hash (P2PKH)
• P2PKH is most commonly used script in bitcoin transactions
• These contain a locking script that restricts the output with a public key hash, more
commonly known as a bitcoin address.
• Transactions that pay a bitcoin address contain P2PKH scripts.
• An output locked by a P2PKH script can be unlocked (spent) by presenting a public key and
a digital signature created by the corresponding private key.
Bob’s UTXO
Bob’s New
Transactions
<Cafe Signature> <Cafe Public Key> OP_DUP OP_HASH160 <Cafe Public Key Hash>
OP_EQUAL OP_CHECKSIG
• <Cafe Signature>
• <Cafe Public Key>
• OP_DUP
• OP_HASH160
• <Cafe Public Key Hash>
• OP_EQUAL
• OP_CHECKSIG
• <Cafe Signature>
• <Cafe Public Key>
• OP_DUP
• OP_HASH160
• <Cafe Public Key Hash>
• OP_EQUAL
• OP_CHECKSIG
Pay-to-Public-Key (P2PK)
• Pay-to-public-key (P2PK) is a simpler form of a bitcoin payment than P2PKH.
• With this script form, the long public key itself is stored in the locking script, rather
than a public-key-hash as with P2PKH earlier, which was much shorter.
• Pay-to-public-key-hash was invented by Satoshi to make bitcoin addresses shorter,
for ease of use. Pay-to-public-key is now most often seen in coinbase transactions,
generated by older mining software that has not been updated to use P2PKH.
M <Public Key 1> <Public Key 2> ... <Public Key N> N OP_CHECKMULTISIG
• The two scripts together would form the combined validation script:
OP_0 <Signature B> <Signature C> 2 <Public Key A> <Public Key B> <Public Key C> 3
OP_CHECKMULTISIG
• The condition is whether the unlocking script has a valid signature from the two
private keys that correspond to two of the three public keys set as an encumbrance.
Disadvantage of pay to multi-signature script
• Ex: Mohammed, an electronics importer based in Dubai. Mohammed’s company
uses bitcoin’s multi-signature feature extensively for its corporate accounts.
• Mohammed’s company uses a multi-signature script for all customer payments,
known in accounting terms as “accounts receivable,” or AR.
• With the multi-signature scheme, any payments made by customers are locked in
such a way that they require at least two signatures to release, from Mohammed
and one of his partners or from his attorney who has a backup key.
• Pay-to-Multi-signature-Script is quite long (each key 520 bit long) and looks like this:
2 <Mohammed's Public Key> <Partner1 Public Key> <Partner2 Public Key> <Partner3
Public Key> <Attorney Public Key> 5 OP_CHECKMULTISIG
Each customer would have to use special bitcoin wallet software with the ability to
create custom transaction scripts.
Furthermore, the resulting transaction would be about five times larger than a simple
payment transaction, because this script contains very long public keys. The burden of
that extra-large transaction would be borne by the customer in the form of fees.
Pay-to-Script-Hash (P2SH)
• Pay-to-script-hash (P2SH) was developed to resolve the practical difficulties of Pay-
to-Multi-signature-Script and to make the use of complex scripts as easy as a
payment to a bitcoin address.
• With P2SH payments, the complex locking script is replaced with its digital
fingerprint, i.e. a cryptographic hash.
• When a transaction attempting to spend the UTXO, it must contain the script that
matches the hash, in addition to the unlocking script.
• In simple terms, P2SH means “pay to a script matching this hash, a script that will
be presented later when this output is spent.”
• Since the redeem script itself is presented later, as part of the unlocking script when
the output is spent, it shifts the burden in fees and complexity from the sender to the
recipient (spender) of the transaction.
Pay-to-Script-Hash (P2SH)
• Redeem Script hash (20-byte): 54c557e07dde5bb6cb791c7a540e0a4796f5e97e
• A customer making a payment to Mohammed’s company need only include this
much shorter locking script in his payment.
• When Mohammed wants to spend this UTXO, they must present the original
redeem script (the one whose hash locked the UTXO) and the signatures necessary
to unlock it, like this:
<Sig1> <Sig2> <2 PK1 PK2 PK3 PK4 PK5 5 OP_CHECKMULTISIG>
• The two scripts are combined, First, the redeem script is checked against the
locking script to make sure the hash matches:
<2 PK1 PK2 PK3 PK4 PK5 5 OP_CHECKMULTISIG> OP_HASH160 <redeem
scriptHash> OP_EQUAL
• If the redeem script hash matches, the unlocking script is executed on its own, to
unlock the redeem script
<Sig1> <Sig2> 2 PK1 PK2 PK3 PK4 PK5 5 OP_CHECKMULTISIG
Pay-to-Data Output (OP_RETURN)
• Bitcoin’s distributed and timestamped ledger, the blockchain, has potential uses far beyond
payments for applications such as digital notary services, stock certificates & smart contracts.
• Early attempts to use bitcoin’s script language for these purposes involved creating
transaction outputs that recorded data on the blockchain; for example, to record a digital
fingerprint of a file in such a way that anyone could establish proof-of-existence of that file on
a specific date by reference to that transaction.
• The use of bitcoin’s blockchain to store data unrelated to bitcoin payments was discouraged
because
– It burdened those running full bitcoin nodes with carrying the cost of disk storage for data that the
blockchain was not intended to carry.
– Moreover, such transactions create UTXO that cannot be spent, using the destination bitcoin
address as a free-form 20-byte field.
– Because the address is used for data, it doesn’t correspond to a private key and the resulting UTXO
can never be spent; it’s a fake payment.
– These transactions that can never be spent are therefore never removed from the UTXO set and
cause the size of the UTXO database to forever increase, or “bloat.”
Pay-to-Data Output (OP_RETURN)
• In version 0.9 of the Bitcoin Core client, a compromise was reached with the introduction of
the OP_RETURN operator.
• OP_RETURN allows developers to add 80 bytes of nonpayment data to a transaction output.
• However, unlike the use of “fake” UTXO, the OP_RETURN operator creates an explicitly
provably unspendable output, which does not need to be stored in the UTXO set.
• OP_RETURN outputs are recorded on the blockchain, so they consume disk space and
contribute to the increase in the blockchain’s size, but they are not stored in the UTXO set
and therefore do not bloat the UTXO memory pool and burden full nodes with the cost of
more expensive RAM.