@bsv/sdk 1.9.3 → 1.9.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/package.json +1 -1
- package/docs/fast-docs.png +0 -0
- package/docs/index.md +49 -44
- package/docs/swagger.png +0 -0
- package/package.json +1 -1
- package/docs/MARKDOWN_VALIDATION_GUIDE.md +0 -175
- package/docs/concepts/beef.md +0 -92
- package/docs/concepts/chain-tracking.md +0 -134
- package/docs/concepts/decentralized-identity.md +0 -221
- package/docs/concepts/fees.md +0 -249
- package/docs/concepts/identity-certificates.md +0 -307
- package/docs/concepts/index.md +0 -77
- package/docs/concepts/key-management.md +0 -185
- package/docs/concepts/script-templates.md +0 -176
- package/docs/concepts/sdk-philosophy.md +0 -80
- package/docs/concepts/signatures.md +0 -194
- package/docs/concepts/spv-verification.md +0 -118
- package/docs/concepts/transaction-encoding.md +0 -167
- package/docs/concepts/transaction-structure.md +0 -67
- package/docs/concepts/trust-model.md +0 -139
- package/docs/concepts/verification.md +0 -250
- package/docs/concepts/wallet-integration.md +0 -101
- package/docs/guides/development-wallet-setup.md +0 -374
- package/docs/guides/direct-transaction-creation.md +0 -147
- package/docs/guides/http-client-configuration.md +0 -488
- package/docs/guides/index.md +0 -138
- package/docs/guides/large-transactions.md +0 -448
- package/docs/guides/multisig-transactions.md +0 -792
- package/docs/guides/security-best-practices.md +0 -494
- package/docs/guides/transaction-batching.md +0 -132
- package/docs/guides/transaction-signing-methods.md +0 -419
- package/docs/reference/arc-config.md +0 -698
- package/docs/reference/brc-100.md +0 -33
- package/docs/reference/configuration.md +0 -835
- package/docs/reference/debugging.md +0 -705
- package/docs/reference/errors.md +0 -597
- package/docs/reference/index.md +0 -111
- package/docs/reference/network-config.md +0 -914
- package/docs/reference/op-codes.md +0 -325
- package/docs/reference/transaction-signatures.md +0 -95
- package/docs/tutorials/advanced-transaction.md +0 -572
- package/docs/tutorials/aes-encryption.md +0 -949
- package/docs/tutorials/authfetch-tutorial.md +0 -986
- package/docs/tutorials/ecdh-key-exchange.md +0 -549
- package/docs/tutorials/elliptic-curve-fundamentals.md +0 -606
- package/docs/tutorials/error-handling.md +0 -1216
- package/docs/tutorials/first-transaction-low-level.md +0 -205
- package/docs/tutorials/first-transaction.md +0 -275
- package/docs/tutorials/hashes-and-hmacs.md +0 -788
- package/docs/tutorials/identity-management.md +0 -729
- package/docs/tutorials/index.md +0 -219
- package/docs/tutorials/key-management.md +0 -538
- package/docs/tutorials/protowallet-development.md +0 -743
- package/docs/tutorials/script-construction.md +0 -690
- package/docs/tutorials/spv-merkle-proofs.md +0 -685
- package/docs/tutorials/testnet-transactions-low-level.md +0 -359
- package/docs/tutorials/transaction-broadcasting.md +0 -538
- package/docs/tutorials/transaction-types.md +0 -420
- package/docs/tutorials/type-42.md +0 -568
- package/docs/tutorials/uhrp-storage.md +0 -599
|
@@ -1,176 +0,0 @@
|
|
|
1
|
-
# Script Templates
|
|
2
|
-
|
|
3
|
-
Standard and custom Bitcoin script patterns available in the BSV TypeScript SDK.
|
|
4
|
-
|
|
5
|
-
## What are Script Templates?
|
|
6
|
-
|
|
7
|
-
Script templates provide reusable patterns for Bitcoin transaction locking and unlocking scripts:
|
|
8
|
-
|
|
9
|
-
```typescript
|
|
10
|
-
import { P2PKH } from '@bsv/sdk'
|
|
11
|
-
|
|
12
|
-
// Use a standard template
|
|
13
|
-
const template = new P2PKH()
|
|
14
|
-
|
|
15
|
-
// Create locking script
|
|
16
|
-
const lockingScript = template.lock(publicKeyHash)
|
|
17
|
-
|
|
18
|
-
// Create unlocking script
|
|
19
|
-
const unlockingScript = template.unlock(privateKey, signature)
|
|
20
|
-
```
|
|
21
|
-
|
|
22
|
-
## Standard Templates
|
|
23
|
-
|
|
24
|
-
### P2PKH (Pay to Public Key Hash)
|
|
25
|
-
|
|
26
|
-
The most common Bitcoin script pattern:
|
|
27
|
-
|
|
28
|
-
```typescript
|
|
29
|
-
const p2pkh = new P2PKH()
|
|
30
|
-
const lock = p2pkh.lock(publicKeyHash)
|
|
31
|
-
const unlock = p2pkh.unlock(privateKey, signature)
|
|
32
|
-
```
|
|
33
|
-
|
|
34
|
-
### P2PK (Pay to Public Key)
|
|
35
|
-
|
|
36
|
-
Direct payment to a public key:
|
|
37
|
-
|
|
38
|
-
```typescript
|
|
39
|
-
const p2pk = new P2PK()
|
|
40
|
-
const lock = p2pk.lock(publicKey)
|
|
41
|
-
const unlock = p2pk.unlock(signature)
|
|
42
|
-
```
|
|
43
|
-
|
|
44
|
-
### OP_RETURN (Data Storage)
|
|
45
|
-
|
|
46
|
-
Store arbitrary data on-chain:
|
|
47
|
-
|
|
48
|
-
```typescript
|
|
49
|
-
const opReturn = new OpReturn()
|
|
50
|
-
const lock = opReturn.lock(data)
|
|
51
|
-
// No unlock needed - unspendable output
|
|
52
|
-
```
|
|
53
|
-
|
|
54
|
-
## Custom Templates
|
|
55
|
-
|
|
56
|
-
Create your own script templates:
|
|
57
|
-
|
|
58
|
-
```typescript
|
|
59
|
-
class TimeLockTemplate implements ScriptTemplate {
|
|
60
|
-
lock(lockTime: number, publicKeyHash: string): Script {
|
|
61
|
-
return new Script()
|
|
62
|
-
.writeNumber(lockTime)
|
|
63
|
-
.writeOpCode(OpCode.OP_CHECKLOCKTIMEVERIFY)
|
|
64
|
-
.writeOpCode(OpCode.OP_DROP)
|
|
65
|
-
.writeOpCode(OpCode.OP_DUP)
|
|
66
|
-
.writeOpCode(OpCode.OP_HASH160)
|
|
67
|
-
.writeBin(publicKeyHash)
|
|
68
|
-
.writeOpCode(OpCode.OP_EQUALVERIFY)
|
|
69
|
-
.writeOpCode(OpCode.OP_CHECKSIG)
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
unlock(signature: string, publicKey: string): Script {
|
|
73
|
-
return new Script()
|
|
74
|
-
.writeBin(signature)
|
|
75
|
-
.writeBin(publicKey)
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
```
|
|
79
|
-
|
|
80
|
-
## Template Interface
|
|
81
|
-
|
|
82
|
-
All templates implement the ScriptTemplate interface:
|
|
83
|
-
|
|
84
|
-
```typescript
|
|
85
|
-
interface ScriptTemplate {
|
|
86
|
-
lock(...args: any[]): Script
|
|
87
|
-
unlock(...args: any[]): Script
|
|
88
|
-
estimateLength?(args: any[]): number
|
|
89
|
-
}
|
|
90
|
-
```
|
|
91
|
-
|
|
92
|
-
## Benefits
|
|
93
|
-
|
|
94
|
-
### Reusability
|
|
95
|
-
|
|
96
|
-
- Standard patterns for common use cases
|
|
97
|
-
- Consistent implementation across applications
|
|
98
|
-
- Reduced development time
|
|
99
|
-
|
|
100
|
-
### Security
|
|
101
|
-
|
|
102
|
-
- Well-tested script patterns
|
|
103
|
-
- Reduced risk of script errors
|
|
104
|
-
- Best practice implementations
|
|
105
|
-
|
|
106
|
-
### Maintainability
|
|
107
|
-
|
|
108
|
-
- Clear separation of script logic
|
|
109
|
-
- Easy to update and modify
|
|
110
|
-
- Testable components
|
|
111
|
-
|
|
112
|
-
## Working with Templates
|
|
113
|
-
|
|
114
|
-
### Transaction Integration
|
|
115
|
-
|
|
116
|
-
```typescript
|
|
117
|
-
// Use template in transaction
|
|
118
|
-
const output = {
|
|
119
|
-
satoshis: 1000,
|
|
120
|
-
lockingScript: template.lock(publicKeyHash)
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
const input = {
|
|
124
|
-
sourceTransaction: prevTx,
|
|
125
|
-
sourceOutputIndex: 0,
|
|
126
|
-
unlockingScript: template.unlock(privateKey, signature)
|
|
127
|
-
}
|
|
128
|
-
```
|
|
129
|
-
|
|
130
|
-
### Fee Estimation
|
|
131
|
-
|
|
132
|
-
```typescript
|
|
133
|
-
// Estimate script size for fee calculation
|
|
134
|
-
const estimatedSize = template.estimateLength([publicKeyHash])
|
|
135
|
-
const fee = estimatedSize * feePerByte
|
|
136
|
-
```
|
|
137
|
-
|
|
138
|
-
## Advanced Patterns
|
|
139
|
-
|
|
140
|
-
### Multi-Signature
|
|
141
|
-
|
|
142
|
-
```typescript
|
|
143
|
-
class MultiSigTemplate implements ScriptTemplate {
|
|
144
|
-
lock(threshold: number, publicKeys: string[]): Script {
|
|
145
|
-
// Implementation for m-of-n multisig
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
unlock(signatures: string[]): Script {
|
|
149
|
-
// Implementation for signature collection
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
```
|
|
153
|
-
|
|
154
|
-
### Conditional Scripts
|
|
155
|
-
|
|
156
|
-
```typescript
|
|
157
|
-
class ConditionalTemplate implements ScriptTemplate {
|
|
158
|
-
lock(condition: Script, trueScript: Script, falseScript: Script): Script {
|
|
159
|
-
// Implementation for IF/ELSE logic
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
```
|
|
163
|
-
|
|
164
|
-
## Best Practices
|
|
165
|
-
|
|
166
|
-
- Use standard templates when possible
|
|
167
|
-
- Test custom templates thoroughly
|
|
168
|
-
- Document template parameters clearly
|
|
169
|
-
- Consider script size and complexity
|
|
170
|
-
- Validate inputs before script creation
|
|
171
|
-
|
|
172
|
-
## Next Steps
|
|
173
|
-
|
|
174
|
-
- Learn about [Digital Signatures](./signatures.md) for script authorization
|
|
175
|
-
- Understand [Transaction Structure](./transaction-structure.md) integration
|
|
176
|
-
- Explore [Key Management](./key-management.md) for script security
|
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
# SDK Design Philosophy
|
|
2
|
-
|
|
3
|
-
This document details the core principles behind the BSV TypeScript SDK architecture and design decisions.
|
|
4
|
-
|
|
5
|
-
## Zero Dependencies
|
|
6
|
-
|
|
7
|
-
The SDK is built without external dependencies to:
|
|
8
|
-
|
|
9
|
-
- Minimize security attack surface
|
|
10
|
-
- Reduce bundle size and complexity
|
|
11
|
-
- Ensure long-term stability
|
|
12
|
-
- Provide predictable behavior
|
|
13
|
-
|
|
14
|
-
## SPV-First Approach
|
|
15
|
-
|
|
16
|
-
The SDK prioritizes Simplified Payment Verification:
|
|
17
|
-
|
|
18
|
-
- **Lightweight**: No need to download the full blockchain
|
|
19
|
-
- **Efficient**: Verify transactions using merkle proofs
|
|
20
|
-
- **Scalable**: Works with millions of transactions
|
|
21
|
-
- **Secure**: Cryptographically verifiable without full nodes
|
|
22
|
-
|
|
23
|
-
## Vendor Neutrality
|
|
24
|
-
|
|
25
|
-
The SDK works with any compliant Bitcoin infrastructure:
|
|
26
|
-
|
|
27
|
-
- **Wallet Agnostic**: Supports any BRC-100 compliant wallet
|
|
28
|
-
- **Network Flexible**: Works with different chain tracking services
|
|
29
|
-
- **Service Independent**: No lock-in to specific providers
|
|
30
|
-
|
|
31
|
-
## Modular Design
|
|
32
|
-
|
|
33
|
-
Components are designed to work independently:
|
|
34
|
-
|
|
35
|
-
- **Composable**: Mix and match functionality as needed
|
|
36
|
-
- **Extensible**: Easy to add custom implementations
|
|
37
|
-
- **Testable**: Each component can be tested in isolation
|
|
38
|
-
- **Maintainable**: Clear separation of concerns
|
|
39
|
-
|
|
40
|
-
## TypeScript-First
|
|
41
|
-
|
|
42
|
-
Built specifically for TypeScript to provide:
|
|
43
|
-
|
|
44
|
-
- **Type Safety**: Catch errors at compile time
|
|
45
|
-
- **Developer Experience**: Rich IDE support and autocomplete
|
|
46
|
-
- **Documentation**: Types serve as living documentation
|
|
47
|
-
- **Reliability**: Reduced runtime errors
|
|
48
|
-
|
|
49
|
-
## Security by Design
|
|
50
|
-
|
|
51
|
-
Security considerations are built into every component:
|
|
52
|
-
|
|
53
|
-
- **Cryptographic Primitives**: Secure implementations of Bitcoin cryptography
|
|
54
|
-
- **Input Validation**: All inputs are validated and sanitized
|
|
55
|
-
- **Error Handling**: Comprehensive error handling prevents information leakage
|
|
56
|
-
- **Best Practices**: Follows established security patterns
|
|
57
|
-
|
|
58
|
-
## Performance Focused
|
|
59
|
-
|
|
60
|
-
Optimized for real-world application needs:
|
|
61
|
-
|
|
62
|
-
- **Memory Efficient**: Minimal memory footprint
|
|
63
|
-
- **Fast Execution**: Optimized critical paths
|
|
64
|
-
- **Batch Processing**: Support for high-throughput scenarios
|
|
65
|
-
- **Caching**: Intelligent caching where appropriate
|
|
66
|
-
|
|
67
|
-
## Developer-Friendly
|
|
68
|
-
|
|
69
|
-
Designed to make Bitcoin development accessible:
|
|
70
|
-
|
|
71
|
-
- **Clear APIs**: Intuitive method names and parameters
|
|
72
|
-
- **Comprehensive Documentation**: Tutorials, guides, and references
|
|
73
|
-
- **Working Examples**: Real code that developers can use immediately
|
|
74
|
-
- **Progressive Complexity**: Start simple, add complexity as needed
|
|
75
|
-
|
|
76
|
-
## Next Steps
|
|
77
|
-
|
|
78
|
-
- Understand [Wallet Integration](./wallet-integration.md) patterns
|
|
79
|
-
- Learn about [SPV Verification](./spv-verification.md) concepts
|
|
80
|
-
- Explore [Key Management](./key-management.md) approaches
|
|
@@ -1,194 +0,0 @@
|
|
|
1
|
-
# Digital Signatures
|
|
2
|
-
|
|
3
|
-
How digital signatures work in Bitcoin and their implementation in the BSV TypeScript SDK.
|
|
4
|
-
|
|
5
|
-
## What are Digital Signatures?
|
|
6
|
-
|
|
7
|
-
Digital signatures prove ownership and authorize Bitcoin transactions:
|
|
8
|
-
|
|
9
|
-
```typescript
|
|
10
|
-
import { PrivateKey, Transaction } from '@bsv/sdk'
|
|
11
|
-
|
|
12
|
-
// Create a signature
|
|
13
|
-
const privateKey = PrivateKey.fromRandom()
|
|
14
|
-
const message = 'transaction data'
|
|
15
|
-
const signature = privateKey.sign(message)
|
|
16
|
-
|
|
17
|
-
// Verify the signature
|
|
18
|
-
const publicKey = privateKey.toPublicKey()
|
|
19
|
-
const isValid = publicKey.verify(message, signature)
|
|
20
|
-
```
|
|
21
|
-
|
|
22
|
-
## Bitcoin Signatures
|
|
23
|
-
|
|
24
|
-
Bitcoin uses ECDSA (Elliptic Curve Digital Signature Algorithm):
|
|
25
|
-
|
|
26
|
-
- **secp256k1**: The elliptic curve used by Bitcoin
|
|
27
|
-
- **SHA-256**: Hash function for message digests
|
|
28
|
-
- **DER Encoding**: Standard format for signature serialization
|
|
29
|
-
|
|
30
|
-
## SIGHASH Types
|
|
31
|
-
|
|
32
|
-
SIGHASH flags determine what parts of a transaction are signed:
|
|
33
|
-
|
|
34
|
-
### SIGHASH_ALL (Default)
|
|
35
|
-
|
|
36
|
-
Signs all inputs and outputs:
|
|
37
|
-
|
|
38
|
-
```typescript
|
|
39
|
-
const signature = privateKey.sign(txHash, 'all')
|
|
40
|
-
```
|
|
41
|
-
|
|
42
|
-
### SIGHASH_NONE
|
|
43
|
-
|
|
44
|
-
Signs all inputs but no outputs:
|
|
45
|
-
|
|
46
|
-
```typescript
|
|
47
|
-
const signature = privateKey.sign(txHash, 'none')
|
|
48
|
-
```
|
|
49
|
-
|
|
50
|
-
### SIGHASH_SINGLE
|
|
51
|
-
|
|
52
|
-
Signs all inputs and one corresponding output:
|
|
53
|
-
|
|
54
|
-
```typescript
|
|
55
|
-
const signature = privateKey.sign(txHash, 'single')
|
|
56
|
-
```
|
|
57
|
-
|
|
58
|
-
### SIGHASH_ANYONECANPAY
|
|
59
|
-
|
|
60
|
-
Can be combined with other flags to sign only one input:
|
|
61
|
-
|
|
62
|
-
```typescript
|
|
63
|
-
const signature = privateKey.sign(txHash, 'all|anyonecanpay')
|
|
64
|
-
```
|
|
65
|
-
|
|
66
|
-
## Transaction Signing
|
|
67
|
-
|
|
68
|
-
The SDK handles transaction signing automatically:
|
|
69
|
-
|
|
70
|
-
```typescript
|
|
71
|
-
// Manual signing (low-level)
|
|
72
|
-
const tx = new Transaction()
|
|
73
|
-
const signature = tx.sign(privateKey, inputIndex, sighashType)
|
|
74
|
-
|
|
75
|
-
// Wallet signing (recommended)
|
|
76
|
-
const wallet = new WalletClient()
|
|
77
|
-
const action = await wallet.createAction({
|
|
78
|
-
outputs: [/* outputs */]
|
|
79
|
-
})
|
|
80
|
-
// Wallet handles signing internally
|
|
81
|
-
```
|
|
82
|
-
|
|
83
|
-
## Signature Verification
|
|
84
|
-
|
|
85
|
-
Verify signatures to ensure transaction validity:
|
|
86
|
-
|
|
87
|
-
```typescript
|
|
88
|
-
// Verify a specific signature
|
|
89
|
-
const isValid = publicKey.verify(messageHash, signature)
|
|
90
|
-
|
|
91
|
-
// Verify entire transaction
|
|
92
|
-
const txValid = await transaction.verify(chainTracker)
|
|
93
|
-
```
|
|
94
|
-
|
|
95
|
-
## DER Encoding
|
|
96
|
-
|
|
97
|
-
Signatures are encoded in DER format:
|
|
98
|
-
|
|
99
|
-
```typescript
|
|
100
|
-
// Get DER-encoded signature
|
|
101
|
-
const derSignature = signature.toDER()
|
|
102
|
-
|
|
103
|
-
// Parse DER signature
|
|
104
|
-
const sig = Signature.fromDER(derBytes)
|
|
105
|
-
|
|
106
|
-
// Get r and s components
|
|
107
|
-
const r = signature.r
|
|
108
|
-
const s = signature.s
|
|
109
|
-
```
|
|
110
|
-
|
|
111
|
-
## Security Considerations
|
|
112
|
-
|
|
113
|
-
### Nonce Security
|
|
114
|
-
|
|
115
|
-
- Each signature must use a unique, random nonce
|
|
116
|
-
- Reusing nonces can leak private keys
|
|
117
|
-
- The SDK handles nonce generation securely
|
|
118
|
-
|
|
119
|
-
### Signature Malleability
|
|
120
|
-
|
|
121
|
-
- Bitcoin signatures can be modified without invalidating them
|
|
122
|
-
- Use canonical signatures to prevent malleability
|
|
123
|
-
- The SDK produces canonical signatures by default
|
|
124
|
-
|
|
125
|
-
### Hash Types
|
|
126
|
-
|
|
127
|
-
- Choose appropriate SIGHASH types for your use case
|
|
128
|
-
- SIGHASH_ALL is safest for most applications
|
|
129
|
-
- Other types enable advanced transaction patterns
|
|
130
|
-
|
|
131
|
-
## Common Patterns
|
|
132
|
-
|
|
133
|
-
### Multi-Input Signing
|
|
134
|
-
|
|
135
|
-
```typescript
|
|
136
|
-
// Sign multiple inputs in a transaction
|
|
137
|
-
for (let i = 0; i < transaction.inputs.length; i++) {
|
|
138
|
-
const signature = privateKey.sign(transaction.getSignatureHash(i))
|
|
139
|
-
transaction.inputs[i].unlockingScript = createUnlockingScript(signature)
|
|
140
|
-
}
|
|
141
|
-
```
|
|
142
|
-
|
|
143
|
-
### Conditional Signatures
|
|
144
|
-
|
|
145
|
-
```typescript
|
|
146
|
-
// Different signatures for different conditions
|
|
147
|
-
const signature1 = privateKey1.sign(txHash, 'all')
|
|
148
|
-
const signature2 = privateKey2.sign(txHash, 'single')
|
|
149
|
-
```
|
|
150
|
-
|
|
151
|
-
## Error Handling
|
|
152
|
-
|
|
153
|
-
Common signature issues:
|
|
154
|
-
|
|
155
|
-
- Invalid private key format
|
|
156
|
-
- Incorrect message hash
|
|
157
|
-
- Malformed signature data
|
|
158
|
-
- Verification failures
|
|
159
|
-
|
|
160
|
-
```typescript
|
|
161
|
-
try {
|
|
162
|
-
const signature = privateKey.sign(message)
|
|
163
|
-
} catch (error) {
|
|
164
|
-
console.error('Signing failed:', error.message)
|
|
165
|
-
}
|
|
166
|
-
```
|
|
167
|
-
|
|
168
|
-
## Best Practices
|
|
169
|
-
|
|
170
|
-
- Always use secure random number generation
|
|
171
|
-
- Verify signatures before trusting them
|
|
172
|
-
- Use appropriate SIGHASH types for your use case
|
|
173
|
-
- Store signatures in DER format for interoperability
|
|
174
|
-
- Never reuse nonces across signatures
|
|
175
|
-
|
|
176
|
-
## Wallet Integration
|
|
177
|
-
|
|
178
|
-
Most applications use wallets for signing:
|
|
179
|
-
|
|
180
|
-
```typescript
|
|
181
|
-
// Wallet handles signature creation
|
|
182
|
-
const wallet = new WalletClient()
|
|
183
|
-
const result = await wallet.createAction({
|
|
184
|
-
description: 'Payment transaction',
|
|
185
|
-
outputs: [/* outputs */]
|
|
186
|
-
})
|
|
187
|
-
// Signatures created automatically
|
|
188
|
-
```
|
|
189
|
-
|
|
190
|
-
## Next Steps
|
|
191
|
-
|
|
192
|
-
- Understand [Key Management](./key-management.md) for signature security
|
|
193
|
-
- Learn about [Script Templates](./script-templates.md) for signature usage
|
|
194
|
-
- Explore [Transaction Structure](./transaction-structure.md) for signature placement
|
|
@@ -1,118 +0,0 @@
|
|
|
1
|
-
# SPV Verification
|
|
2
|
-
|
|
3
|
-
Understanding Simplified Payment Verification and how it enables lightweight Bitcoin applications.
|
|
4
|
-
|
|
5
|
-
## What is SPV?
|
|
6
|
-
|
|
7
|
-
SPV allows verification of Bitcoin transactions without downloading the entire blockchain:
|
|
8
|
-
|
|
9
|
-
- **Lightweight**: Only requires block headers and merkle proofs
|
|
10
|
-
- **Secure**: Cryptographically verifiable using merkle trees
|
|
11
|
-
- **Efficient**: Scales to millions of transactions
|
|
12
|
-
- **Practical**: Enables mobile and web applications
|
|
13
|
-
|
|
14
|
-
## How SPV Works
|
|
15
|
-
|
|
16
|
-
### 1. Block Headers
|
|
17
|
-
|
|
18
|
-
Download only block headers (80 bytes each) instead of full blocks:
|
|
19
|
-
|
|
20
|
-
```typescript
|
|
21
|
-
// Block header contains merkle root
|
|
22
|
-
const header = await chainTracker.getBlockHeader(blockHash)
|
|
23
|
-
```
|
|
24
|
-
|
|
25
|
-
### 2. Merkle Proofs
|
|
26
|
-
|
|
27
|
-
Verify transaction inclusion using merkle proofs:
|
|
28
|
-
|
|
29
|
-
```typescript
|
|
30
|
-
import { MerklePath } from '@bsv/sdk'
|
|
31
|
-
|
|
32
|
-
// Verify transaction is in block
|
|
33
|
-
const proof = MerklePath.fromHex(proofHex)
|
|
34
|
-
const isValid = proof.verify(txid, merkleRoot)
|
|
35
|
-
```
|
|
36
|
-
|
|
37
|
-
### 3. Transaction Verification
|
|
38
|
-
|
|
39
|
-
Combine proofs with block headers for full verification:
|
|
40
|
-
|
|
41
|
-
```typescript
|
|
42
|
-
import { Transaction } from '@bsv/sdk'
|
|
43
|
-
|
|
44
|
-
// Verify transaction with SPV
|
|
45
|
-
const isValid = await Transaction.verify(
|
|
46
|
-
transaction,
|
|
47
|
-
chainTracker,
|
|
48
|
-
{ merkleProof: proof }
|
|
49
|
-
)
|
|
50
|
-
```
|
|
51
|
-
|
|
52
|
-
## Merkle Trees
|
|
53
|
-
|
|
54
|
-
Bitcoin uses merkle trees to efficiently prove transaction inclusion:
|
|
55
|
-
|
|
56
|
-
- **Binary Tree**: Each leaf is a transaction ID
|
|
57
|
-
- **Hash Pairs**: Parent nodes are hashes of child pairs
|
|
58
|
-
- **Root Hash**: Single hash representing all transactions
|
|
59
|
-
- **Proof Path**: Minimal data needed to verify inclusion
|
|
60
|
-
|
|
61
|
-
## Security Model
|
|
62
|
-
|
|
63
|
-
SPV provides strong security guarantees:
|
|
64
|
-
|
|
65
|
-
- **Proof of Work**: Block headers contain proof of work
|
|
66
|
-
- **Cryptographic Hashes**: Merkle proofs use SHA-256
|
|
67
|
-
- **Chain Validation**: Verify header chain integrity
|
|
68
|
-
- **Fraud Detection**: Invalid proofs are cryptographically detectable
|
|
69
|
-
|
|
70
|
-
## Trade-offs
|
|
71
|
-
|
|
72
|
-
### Advantages
|
|
73
|
-
|
|
74
|
-
- **Low Resource Usage**: Minimal storage and bandwidth
|
|
75
|
-
- **Fast Synchronization**: Quick startup time
|
|
76
|
-
- **Scalability**: Works with any blockchain size
|
|
77
|
-
- **Privacy**: No need to reveal which transactions you care about
|
|
78
|
-
|
|
79
|
-
### Limitations
|
|
80
|
-
|
|
81
|
-
- **Trust Assumptions**: Relies on honest majority of miners
|
|
82
|
-
- **Network Dependency**: Requires connection to full nodes
|
|
83
|
-
- **Delayed Detection**: May not immediately detect invalid blocks
|
|
84
|
-
|
|
85
|
-
## SDK Implementation
|
|
86
|
-
|
|
87
|
-
The SDK provides comprehensive SPV support:
|
|
88
|
-
|
|
89
|
-
```typescript
|
|
90
|
-
// Configure SPV verification
|
|
91
|
-
const config = {
|
|
92
|
-
spv: {
|
|
93
|
-
enabled: true,
|
|
94
|
-
maxMemoryLimit: 100000000, // 100MB
|
|
95
|
-
chainTracker: chainTracker
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
// Verify transaction with SPV
|
|
100
|
-
const result = await transaction.verify(chainTracker, {
|
|
101
|
-
merkleProof: proof,
|
|
102
|
-
blockHeader: header
|
|
103
|
-
})
|
|
104
|
-
```
|
|
105
|
-
|
|
106
|
-
## BEEF Integration
|
|
107
|
-
|
|
108
|
-
SPV works seamlessly with BEEF format:
|
|
109
|
-
|
|
110
|
-
- **Efficient Encoding**: BEEF includes merkle proofs
|
|
111
|
-
- **Batch Verification**: Verify multiple transactions together
|
|
112
|
-
- **Standardized Format**: Consistent across applications
|
|
113
|
-
|
|
114
|
-
## Next Steps
|
|
115
|
-
|
|
116
|
-
- Learn about [BEEF Format](./beef.md) for efficient data exchange
|
|
117
|
-
- Understand [Transaction Encoding](./transaction-encoding.md) formats
|
|
118
|
-
- Explore [Trust Model](./trust-model.md) considerations
|