Script Templates
Overview
Script Templates in the BSV TypeScript SDK provide standardized, reusable patterns for creating locking and unlocking scripts. The ScriptTemplate interface and its implementations (like P2PKH) abstract away the complexity of Bitcoin Script construction, making it easy to create secure, standard transaction types. Templates ensure correct script generation and reduce errors in transaction building.
Purpose
Provide pre-built templates for standard transaction types (P2PKH, P2PK, etc.)
Abstract Bitcoin Script complexity with simple, type-safe interfaces
Enable creation of both locking scripts (outputs) and unlocking script templates (inputs)
Support custom template creation for advanced use cases
Ensure BRC compliance and interoperability
Simplify transaction signing with automatic script generation
Reduce errors through tested, standard implementations
Basic Usage
import { Transaction, P2PKH, PrivateKey } from '@bsv/sdk';
const privKey = PrivateKey.fromWif('L5EY1SbTvvPNSdCYQe1EJHfXCBBT4PmnF6CDbzCm9iifZptUvDGB');
const recipientAddress = '1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa';
// Create P2PKH template instance
const p2pkh = new P2PKH();
const tx = new Transaction();
// Create locking script for output
const lockingScript = p2pkh.lock(recipientAddress);
tx.addOutput({
lockingScript,
satoshis: 10000
});
// Create unlocking script template for input
const unlockingTemplate = p2pkh.unlock(privKey);
tx.addInput({
sourceTXID: '4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b',
sourceOutputIndex: 0,
unlockingScriptTemplate: unlockingTemplate
});
// Sign transaction (unlocking script generated automatically)
await tx.sign();
console.log('Transaction created with P2PKH template');Key Features
1. P2PKH Template (Pay to Public Key Hash)
The most common Bitcoin transaction template:
2. Custom Script Templates
Create your own templates for advanced use cases:
3. Time-Locked Template
Create template with time-based conditions:
4. Multi-Signature Template
M-of-N multisig template:
API Reference
ScriptTemplate Interface
P2PKH Template
Creating Custom Templates
Common Patterns
Pattern 1: Reusable Template Factory
Create template instances with configuration:
Pattern 2: Template Composition
Combine multiple templates:
Pattern 3: Protocol-Specific Templates
Create templates for specific protocols:
Security Considerations
Template Validation: Always validate template parameters before using in production.
Sighash Type Selection: Choose appropriate sighash types for your use case. SIGHASH_ALL is most secure.
Private Key Protection: Never log or expose private keys used in unlock templates.
Custom Template Testing: Thoroughly test custom templates with small amounts before production use.
Script Size Limits: Ensure generated scripts stay within protocol limits.
Standard Compliance: Use standard templates (P2PKH) when possible for maximum compatibility.
Performance Considerations
Template Reuse: Create template instances once and reuse them across multiple transactions.
Unlocking Script Size: Simpler unlock templates result in smaller transactions and lower fees.
Estimation Accuracy: Accurate
estimateLengthimplementations help with fee calculation.Signature Caching: For repeated signatures, consider caching when safe to do so.
Related Components
Transaction - Use templates in transactions
Script - Low-level script building
TransactionInput - Use unlock templates
TransactionOutput - Use lock templates
Signature - Generate signatures for unlocking
Code Examples
See complete working examples in:
Best Practices
Use standard templates (P2PKH) for maximum compatibility
Test custom templates thoroughly before production use
Document template parameters clearly for maintainability
Implement proper error handling in unlock functions
Provide accurate length estimates for fee calculation
Follow BRC standards for protocol-specific templates
Keep templates simple to minimize errors and fees
Reuse template instances across transactions
Validate all inputs before script generation
Include comprehensive unit tests for custom templates
Troubleshooting
Issue: Template unlock fails during signing
Solution: Verify template parameters match the locking script.
Issue: Custom template generates invalid script
Solution: Validate script structure and opcodes.
Issue: Unlock template estimation inaccurate
Solution: Implement precise length estimation.
Issue: Template not working with BRC protocol
Solution: Ensure compliance with BRC specifications.
Further Reading
Script Templates - Standard Bitcoin scripts
P2PKH - Pay to public key hash
BRC-29 - Simple payment protocol
Custom Scripts - Script examples
BSV SDK Documentation - Official SDK docs
Status
✅ Complete
Last updated
