2.3 Bitcoin Blockchain: Scripts in Bitcoin
Features of Bitcoin Scripts:
Bitcoin Script is a stack-based programming language like Forth
A list of instructions is recorded with each transaction
Operations in Bitcoin are composed of opcodes
Bitcoin Script describes how the person can access the Bitcoins if they want to spend them
Bitcoin Script is turing incomplete
Turing incomplete means that one cannot have looping conditions in the code thus, the code is like a boolean (true or false).
All Bitcoin transactions have scripts embedded into their inputs and outputs
Example of Bitcoin Script:
Scenario: Person X verifies the transaction is initiated by Person Y
1. Transaction from Person Y is initiated
2. Public key of Person Y is sent along with the signature which person X can verify Bitcoin transfer scripts (scriptsig, scriptPubkey) instead of signatures and the public key.
Person X can spend Bitcoins only if both the scripts return true after execution.
The script consists of a transaction input and a transaction output. The two kinds of scripts are called the locking script (scriptsig) and unlocking script (scriptPubKey). Unlock script is provided by the user to resolve the encumbrance. It consists of the person’s public key and private key.
Lock script is found in a transaction output and is the encumbrance that must be fulfilled to spend the output. The locking script is created as follows:
The receiver’s public key is duplicated. A hash is generated for the user’s public key. This hash is compared with the public key hash of the received transaction. The two are compared and if they’re equal, the reciever’s public is used to unlock the transaction, thus making it available in their wallet.
Bitcoin Script Construction: A script is a list of instructions recorded with each transaction.
Bitcoin Script Instructions:
Bitcoin scripting language is small
It consists of 256 opcodes out of which 15 are disabled and 75 are reserved
Each instruction is given one byte
Opcodes have the following: constants, flow control, stack, bitwise logic, arithmetic, crypto, lock time, pseudowords, reserved words
Evaluating Bitcoin Transaction Script
Step-by-step execution of the combined script will prove this is a valid transaction:
1. Execution starts and value <sig> is pushed to the top of the stack.
2. Execution continues, moving to the right with each step. Value <PubK> is pushed to the top of the stack, on top of <sig>.
3. DUP operator duplicates at the top item in the stack, the resulting value is pushed to the top of the stack.
Features of Bitcoin Scripts:
Bitcoin Script is a stack-based programming language like Forth
A list of instructions is recorded with each transaction
Operations in Bitcoin are composed of opcodes
Bitcoin Script describes how the person can access the Bitcoins if they want to spend them
Bitcoin Script is turing incomplete
Turing incomplete means that one cannot have looping conditions in the code thus, the code is like a boolean (true or false).
All Bitcoin transactions have scripts embedded into their inputs and outputs
Example of Bitcoin Script:
Scenario: Person X verifies the transaction is initiated by Person Y
1. Transaction from Person Y is initiated
2. Public key of Person Y is sent along with the signature which person X can verify Bitcoin transfer scripts (scriptsig, scriptPubkey) instead of signatures and the public key.
Person X can spend Bitcoins only if both the scripts return true after execution.
The script consists of a transaction input and a transaction output. The two kinds of scripts are called the locking script (scriptsig) and unlocking script (scriptPubKey). Unlock script is provided by the user to resolve the encumbrance. It consists of the person’s public key and private key.
Lock script is found in a transaction output and is the encumbrance that must be fulfilled to spend the output. The locking script is created as follows:
The receiver’s public key is duplicated. A hash is generated for the user’s public key. This hash is compared with the public key hash of the received transaction. The two are compared and if they’re equal, the reciever’s public is used to unlock the transaction, thus making it available in their wallet.
Bitcoin Script Construction: A script is a list of instructions recorded with each transaction.
Bitcoin Script Instructions:
Bitcoin scripting language is small
It consists of 256 opcodes out of which 15 are disabled and 75 are reserved
Each instruction is given one byte
Opcodes have the following: constants, flow control, stack, bitwise logic, arithmetic, crypto, lock time, pseudowords, reserved words
Evaluating Bitcoin Transaction Script
Step-by-step execution of the combined script will prove this is a valid transaction:
1. Execution starts and value <sig> is pushed to the top of the stack.
2. Execution continues, moving to the right with each step. Value <PubK> is pushed to the top of the stack, on top of <sig>.
3. DUP operator duplicates at the top item in the stack, the resulting value is pushed to the top of the stack.