This project provides a comprehensive, hands-on implementation of Bitcoin scripting, exploring the intricacies of Bitcoin's transaction and script validation mechanisms. It serves as an educational resource for developers and blockchain enthusiasts interested in understanding Bitcoin's low-level scripting system.
- Legacy Scripts
- P2PKH (Pay to Public Key Hash)
- P2SH (Pay to Script Hash)
- Segwit Scripts
- P2WPKH (Pay to Witness Public Key Hash)
- P2WSH (Pay to Witness Script Hash)
- Transaction creation and signing
- Script validation mechanisms
- Cryptographic primitives
- Visualization of script execution
- Language: JavaScript
- Libraries:
bitcoinjs-libnoble-secp256k1- Custom cryptographic utilities
git clone https://github.com/Quadwinner/bitcoin-script.git
cd bitcoin-script
npm installExplore different script types and transaction scenarios by running the example scripts:
node bitcoin-script-lab.js
node transaction-examples.js- Bitcoin Script Reference
- Transaction Validation Techniques
- Cryptographic Signature Schemes
Contributions, issues, and feature requests are welcome! Feel free to check issues page.
Open-source project for educational purposes.
Disclaimer: This is a learning project and should not be used for production cryptocurrency transactions.
- Segwit Scripts
- P2WPKH (Pay to Witness Public Key Hash)
- P2WSH (Pay to Witness Script Hash)
- Interactive Learning Interface: Menu-driven application for exploring different aspects of Bitcoin scripting
- Visual Script Execution: Step-by-step visualization of how scripts are executed on the stack
- Transaction Examples: Real-world transaction examples for each script type
- Size Comparison: Comparison of transaction sizes between legacy and segwit formats
- Manual Script Simulation: Low-level manual execution of Bitcoin scripts to understand the stack operations
- Node.js (v14 or higher)
- npm (Node Package Manager)
- Clone this repository or download the files
- Navigate to the project directory
- Install the required dependencies:
npm installTo start the interactive Bitcoin Script Lab:
node bitcoin-script-lab.jsThis will display a menu with various options:
- Bitcoin Script Basics
- Legacy Scripts (P2PKH and P2SH)
- Segwit Scripts (P2WPKH and P2WSH)
- Visual Script Execution Simulation
- Transaction Examples and Size Comparison
- Exit
- bitcoin-script-lab.js: Main entry point with an interactive menu
- index.js: Implementation of Bitcoin script using bitcoinjs-lib
- bitcoin-script-manual.js: Manual implementation of script execution
- visualization.js: ASCII art visualization of stack operations
- transaction-examples.js: Examples of creating transactions with different script types
Bitcoin Script is a stack-based, forth-like programming language used in Bitcoin to determine if a transaction is valid. It consists of opcodes (operations) that manipulate data on a stack.
Segwit (Segregated Witness) is an upgrade to the Bitcoin protocol that separates signature data from transaction data. Benefits include:
- Transaction Malleability Fix: Transaction IDs cannot be modified by third parties
- Smaller Transaction Size: Signatures are moved to a separate "witness" section
- Lower Fees: Due to smaller transaction size
- Increased Block Capacity: Without changing the block size limit
- Standard transaction type in early Bitcoin
- Locking Script:
OP_DUP OP_HASH160 <publicKeyHash> OP_EQUALVERIFY OP_CHECKSIG - Unlocking Script:
<signature> <publicKey>
- Allows for more complex scripts like multisig
- Locking Script:
OP_HASH160 <redeemScriptHash> OP_EQUAL - Unlocking Script:
<signatures...> <redeemScript>
- Segwit version of P2PKH with improved efficiency
- Locking Script:
OP_0 <publicKeyHash> - Witness Data:
<signature> <publicKey>
- Segwit version of P2SH for complex scripts
- Locking Script:
OP_0 <witnessScriptHash> - Witness Data:
<signatures...> <witnessScript>
The project includes comprehensive examples and visualizations of each script type in action, showing:
- How scripts are executed step-by-step
- How the stack changes during execution
- How transactions are constructed and validated
- Size comparisons between different transaction types
After working with this implementation, you will understand:
- How Bitcoin's script system works
- The differences between legacy and segwit transactions
- How different script types are structured and executed
- The benefits of segwit in terms of transaction size and fees
- How multisignature scripts are implemented #