P2PKH
Overview
The P2PKH component implements the most common Bitcoin transaction script template - Pay to Public Key Hash. The P2PKH class provides a simple interface for creating locking scripts that secure coins to a Bitcoin address and unlocking script templates that spend those coins using the corresponding private key.
P2PKH is the standard script template for basic Bitcoin payments, implementing the classic OP_DUP OP_HASH160 <pubKeyHash> OP_EQUALVERIFY OP_CHECKSIG script pattern that has been the foundation of Bitcoin transactions since its inception.
Purpose
P2PKH in the BSV SDK solves several critical problems:
Standard Payments: Implement the most widely-used Bitcoin payment script
Address-Based Transactions: Send and receive coins using Bitcoin addresses
Signature Verification: Cryptographically prove ownership of coins
Template Abstraction: Simplify script creation without manual script construction
SIGHASH Support: Flexible signature types (ALL, NONE, SINGLE, ANYONECANPAY)
Fee Estimation: Accurate script length estimation for fee calculation
This component is fundamental for all standard BSV transactions, providing a secure and well-tested template for everyday Bitcoin payments.
Basic Usage
Create Locking Script (Receive Payment)
Create Unlocking Script (Spend Payment)
Complete Payment Transaction
SIGHASH Flags
Key Features
1. Standard Script Template
P2PKH implements the classic Bitcoin script pattern:
2. Flexible SIGHASH Types
P2PKH supports all standard SIGHASH flags for different signing scenarios:
3. Address and Public Key Support
P2PKH works with both Bitcoin addresses and public keys:
4. Fee Estimation with Unlocking Script Length
P2PKH provides accurate fee estimation by calculating unlocking script size:
API Reference
P2PKH Class
lock() Method
Creates a locking script that secures coins to a Bitcoin address.
Parameters:
address: string- Bitcoin address (base58check encoded) or public key string
Returns:
LockingScript- Script that locks coins to the address
Example:
unlock() Method
Creates an unlocking script template that spends coins locked to an address.
Parameters:
privateKey: PrivateKey- Private key corresponding to the locking addresssignOutputs?: 'all' | 'none' | 'single'- SIGHASH type for outputs (default: 'all')anyoneCanPay?: boolean- Allow additional inputs (default: false)sourceSatoshis?: number- Satoshis in the output being spent (optional)lockingScript?: LockingScript- Locking script being unlocked (optional)
Returns:
Object with
sign()andestimateLength()methods
Example:
ScriptTemplate Interface
P2PKH implements the ScriptTemplate interface:
Common Patterns
1. Simple Payment Workflow
2. Multi-Signature Coordination with SIGHASH
3. Payment Channel with P2PKH
Security Considerations
1. Never Reuse Addresses
BAD - Address reuse destroys privacy:
GOOD - Generate new addresses:
2. Validate Addresses Before Use
BAD - Using unvalidated addresses:
GOOD - Validate and handle errors:
3. Protect Private Keys
BAD - Exposing private keys:
GOOD - Encrypt and secure private keys:
4. Use Appropriate SIGHASH Flags
BAD - Using SIGHASH_NONE without understanding risks:
GOOD - Use SIGHASH_ALL for standard payments:
Performance Considerations
1. Batch Transaction Creation
2. Optimize Script Creation
3. Parallel Transaction Signing
Related Components
Transaction - Use P2PKH templates in transactions
Script - Underlying script implementation
Private Keys - Keys used for P2PKH unlocking
Public Keys - Keys used for P2PKH addresses
Signatures - ECDSA signatures in unlocking scripts
Script Templates - Other script template types
HD Wallets - Generate addresses with key derivation
Best Practices
1. Always Use Change Outputs
2. Use BRC-42 for Address Generation
3. Validate Transaction Before Broadcasting
4. Handle Fee Calculation Errors
5. Use Appropriate SIGHASH for Use Case
Troubleshooting
Invalid Signature Errors
Problem: Transaction fails verification with invalid signature error.
Solution:
Insufficient Funds for Fee
Problem: Cannot create transaction due to insufficient funds.
Solution:
Address Format Errors
Problem: Invalid address format when creating locking script.
Solution:
Further Reading
BRC-29: Simple Payment Protocol - P2PKH-based payment protocol
Bitcoin Script - Understanding Bitcoin script
SIGHASH Flags - Signature hash types
BRC-42: Key Derivation - Derive unique addresses
BSV SDK Documentation - Official SDK documentation
Status
✅ Complete - Comprehensive documentation with P2PKH template, SIGHASH types, and payment patterns.
Last updated
