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

  1. Template Validation: Always validate template parameters before using in production.

  2. Sighash Type Selection: Choose appropriate sighash types for your use case. SIGHASH_ALL is most secure.

  3. Private Key Protection: Never log or expose private keys used in unlock templates.

  4. Custom Template Testing: Thoroughly test custom templates with small amounts before production use.

  5. Script Size Limits: Ensure generated scripts stay within protocol limits.

  6. Standard Compliance: Use standard templates (P2PKH) when possible for maximum compatibility.

Performance Considerations

  1. Template Reuse: Create template instances once and reuse them across multiple transactions.

  2. Unlocking Script Size: Simpler unlock templates result in smaller transactions and lower fees.

  3. Estimation Accuracy: Accurate estimateLength implementations help with fee calculation.

  4. Signature Caching: For repeated signatures, consider caching when safe to do so.

Code Examples

See complete working examples in:

Best Practices

  1. Use standard templates (P2PKH) for maximum compatibility

  2. Test custom templates thoroughly before production use

  3. Document template parameters clearly for maintainability

  4. Implement proper error handling in unlock functions

  5. Provide accurate length estimates for fee calculation

  6. Follow BRC standards for protocol-specific templates

  7. Keep templates simple to minimize errors and fees

  8. Reuse template instances across transactions

  9. Validate all inputs before script generation

  10. 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

Status

✅ Complete

Last updated