Understanding Bitcoin Scripts Guide
Understanding Bitcoin Scripts Guide
GUIDE
INTRODUCTION TO BITCOIN USAGE SCRIPTS
Bitcoin usage scripts, often simply referred to as "scripts," are a crucial
component of the Bitcoin protocol that enable more complex transaction
functionalities beyond simple transfers. At their core, scripts are a set of
instructions written in a Forth-like language that specify how funds can be
spent. They provide the framework for implementing conditional
cryptocurrency transactions, enhancing Bitcoin's utility in various practical
applications.
The primary purpose of Bitcoin scripts is to define the conditions under which
a Bitcoin transaction can be executed. For instance, a script may require
multiple signatures from different parties before funds can be spent,
enabling multi-signature wallets often employed by businesses for added
security. Other scripts can specify time-locks, allowing transactions to occur
only after a designated period, thus serving functions akin to “smart
contracts.”
PAY-TO-PUBLIC-KEY-HASH (P2PKH)
Use Case: This script is predominantly used for standard Bitcoin transactions.
It forms the basis upon which the vast majority of Bitcoin transactions
operate.
Advantages:
Disadvantages:
• Single Signature: Requires one signature, which may not be suitable for
all users, especially for high-value transactions requiring added security.
PAY-TO-SCRIPT-HASH (P2SH)
Use Case: Commonly used in multi-signature wallets where more than one
private key is required to spend the funds.
Advantages:
Disadvantages:
Use Case: P2WPKH is used for standard transactions, while P2WSH supports
scripts requiring more complex conditions.
Advantages:
Disadvantages:
OP_RETURN
Definition: This script type allows users to store data on the Bitcoin
blockchain without making any funds spendable. It effectively marks a
transaction as 'non-spendable.'
Disadvantages:
Understanding these script types is vital for anyone looking to utilize Bitcoin
effectively, whether for regular transactions, enhanced security, or unique
applications involving data storage.
1. Bitcoin Core: This is the full node software that you will use to interact
with the Bitcoin network.
2. Text Editor: Any code-friendly text editor (such as Visual Studio Code)
where you will write your scripts.
3. Basic Knowledge of Command Line: Familiarity with command-line
operations is helpful for executing Bitcoin commands.
• First, you require the public key hash of the recipient. You can generate
a Bitcoin address using your Bitcoin wallet or the Bitcoin command line:
bitcoin-cli getnewaddress
This simple script checks whether the provided signature matches the public
key. Here’s the code snippet for a basic P2PKH script:
• OP_DUP: Duplicates the top item on the stack (the public key).
• OP_HASH160: Computes the hash of the public key.
• <PubKeyHash>: This placeholder is replaced with the actual public key
hash of the recipient.
• OP_EQUALVERIFY: Checks if the duplicated hash matches the provided
public key hash.
• OP_CHECKSIG: Validates the signature against the public key.
Step 3: Deploying the Script
To deploy this script for sending Bitcoin, utilize the following command,
replacing <RecipientAddress> with the address obtained in Step 1:
bitcoin-cli createrawtransaction
'[{"txid":"<TxID>","vout":<VoutIndex>}]'
'{"<RecipientAddress>":<Amount>}'
COMPLETE EXAMPLE
bitcoin-cli getnewaddress
bitcoin-cli createrawtransaction
'[{"txid":"abcd1234","vout":0}]'
'{"<RecipientAddress>":0.001}'
bitcoin-cli signrawtransactionwithwallet
"<HexString>"
4. Broadcast the transaction:
These steps outline the process of creating a basic Bitcoin script and
deploying it effectively. Understanding and becoming comfortable with these
operations can empower individuals and developers to take full advantage of
Bitcoin's scripting capabilities.
To create a Bitcoin transaction that utilizes a script, the following steps are
typically involved:
STEP-BY-STEP PROCESS
Before constructing the transaction, you need to determine which UTXOs will
be used. You can do this by checking the balance of your wallet and selecting
the appropriate outputs. Use the command:
bitcoin-cli listunspent
Once you have your inputs ready, define the outputs for the transaction. The
outputs specify how much Bitcoin to send and the recipient's address.
bitcoin-cli createrawtransaction
'[{"txid":"<TxID>","vout":<VoutIndex>}]'
'{"<RecipientAddress>":<Amount>}'
In the context of this transaction, the script often highlights the required
conditions (e.g., single signature, multi-signature, etc.). As previously
mentioned, a simple Pay-to-Public-Key-Hash (P2PKH) script might look like
this:
OP_DUP OP_HASH160 <PubKeyHash> OP_EQUALVERIFY OP_CHECKSIG
This instructs the Bitcoin network to verify that the right signature is provided
for the transferred funds.
After the raw transaction is prepared, it must be signed using the private key
associated with the inputs. Use the following command:
Once the transaction is signed, it's time to broadcast it to the Bitcoin network.
Execute the command:
This command sends the signed transaction, where <SignedHex> is the hex
representation of your signed transaction.
SUMMARY OF COMMANDS
Step Command
List Unspent
bitcoin-cli listunspent
Outputs
bitcoin-cli createrawtransaction
Create Raw
'[{"txid":"<TxID>","vout":<VoutIndex>}]'
Transaction
'{"<RecipientAddress>":<Amount>}'
Sign the
bitcoin-cli signrawtransactionwithwallet "<HexString>"
Transaction
Step Command
Broadcast the
bitcoin-cli sendrawtransaction "<SignedHex>"
Transaction
MULTI-SIGNATURE SETUPS
Example: A 2-of-3 multisig setup allows any two out of three parties to sign
off on a transaction. The script looks like this:
TIME-LOCK SCRIPTS
In this script:
CONDITIONAL LOGIC
OP_IF
<PubKeyA> OP_CHECKSIG
OP_ELSE
OP_2 <PubKeyB> <PubKeyC> OP_3 OP_CHECKMULTISIG
OP_ENDIF
Time-Lock
Technique Description Benefits
Conditional
Executes based on defined conditions Flexible transaction handling
Logic
3. Hardcoding Values:
5. Lack of Testing:
Improper Script
Failed transactions Validate all inputs and outputs
Validation
KEY TAKEAWAYS