@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.
Files changed (60) hide show
  1. package/dist/cjs/package.json +1 -1
  2. package/docs/fast-docs.png +0 -0
  3. package/docs/index.md +49 -44
  4. package/docs/swagger.png +0 -0
  5. package/package.json +1 -1
  6. package/docs/MARKDOWN_VALIDATION_GUIDE.md +0 -175
  7. package/docs/concepts/beef.md +0 -92
  8. package/docs/concepts/chain-tracking.md +0 -134
  9. package/docs/concepts/decentralized-identity.md +0 -221
  10. package/docs/concepts/fees.md +0 -249
  11. package/docs/concepts/identity-certificates.md +0 -307
  12. package/docs/concepts/index.md +0 -77
  13. package/docs/concepts/key-management.md +0 -185
  14. package/docs/concepts/script-templates.md +0 -176
  15. package/docs/concepts/sdk-philosophy.md +0 -80
  16. package/docs/concepts/signatures.md +0 -194
  17. package/docs/concepts/spv-verification.md +0 -118
  18. package/docs/concepts/transaction-encoding.md +0 -167
  19. package/docs/concepts/transaction-structure.md +0 -67
  20. package/docs/concepts/trust-model.md +0 -139
  21. package/docs/concepts/verification.md +0 -250
  22. package/docs/concepts/wallet-integration.md +0 -101
  23. package/docs/guides/development-wallet-setup.md +0 -374
  24. package/docs/guides/direct-transaction-creation.md +0 -147
  25. package/docs/guides/http-client-configuration.md +0 -488
  26. package/docs/guides/index.md +0 -138
  27. package/docs/guides/large-transactions.md +0 -448
  28. package/docs/guides/multisig-transactions.md +0 -792
  29. package/docs/guides/security-best-practices.md +0 -494
  30. package/docs/guides/transaction-batching.md +0 -132
  31. package/docs/guides/transaction-signing-methods.md +0 -419
  32. package/docs/reference/arc-config.md +0 -698
  33. package/docs/reference/brc-100.md +0 -33
  34. package/docs/reference/configuration.md +0 -835
  35. package/docs/reference/debugging.md +0 -705
  36. package/docs/reference/errors.md +0 -597
  37. package/docs/reference/index.md +0 -111
  38. package/docs/reference/network-config.md +0 -914
  39. package/docs/reference/op-codes.md +0 -325
  40. package/docs/reference/transaction-signatures.md +0 -95
  41. package/docs/tutorials/advanced-transaction.md +0 -572
  42. package/docs/tutorials/aes-encryption.md +0 -949
  43. package/docs/tutorials/authfetch-tutorial.md +0 -986
  44. package/docs/tutorials/ecdh-key-exchange.md +0 -549
  45. package/docs/tutorials/elliptic-curve-fundamentals.md +0 -606
  46. package/docs/tutorials/error-handling.md +0 -1216
  47. package/docs/tutorials/first-transaction-low-level.md +0 -205
  48. package/docs/tutorials/first-transaction.md +0 -275
  49. package/docs/tutorials/hashes-and-hmacs.md +0 -788
  50. package/docs/tutorials/identity-management.md +0 -729
  51. package/docs/tutorials/index.md +0 -219
  52. package/docs/tutorials/key-management.md +0 -538
  53. package/docs/tutorials/protowallet-development.md +0 -743
  54. package/docs/tutorials/script-construction.md +0 -690
  55. package/docs/tutorials/spv-merkle-proofs.md +0 -685
  56. package/docs/tutorials/testnet-transactions-low-level.md +0 -359
  57. package/docs/tutorials/transaction-broadcasting.md +0 -538
  58. package/docs/tutorials/transaction-types.md +0 -420
  59. package/docs/tutorials/type-42.md +0 -568
  60. package/docs/tutorials/uhrp-storage.md +0 -599
@@ -1,167 +0,0 @@
1
- # Transaction Encoding
2
-
3
- How Bitcoin transactions are serialized and deserialized in different formats within the BSV TypeScript SDK.
4
-
5
- ## Encoding Formats
6
-
7
- The SDK supports multiple transaction encoding formats:
8
-
9
- ### Raw Transaction Format
10
-
11
- Standard Bitcoin transaction serialization:
12
-
13
- ```typescript
14
- import { Transaction } from '@bsv/sdk'
15
-
16
- // Serialize to hex
17
- const txHex = transaction.toHex()
18
-
19
- // Deserialize from hex
20
- const tx = Transaction.fromHex(txHex)
21
- ```
22
-
23
- ### BEEF Format
24
-
25
- Bitcoin Extras Extension Format for efficient data exchange:
26
-
27
- ```typescript
28
- // Serialize to BEEF
29
- const beefHex = transaction.toBEEF()
30
-
31
- // Deserialize from BEEF
32
- const tx = Transaction.fromHexBEEF(beefHex)
33
- ```
34
-
35
- ### Binary Format
36
-
37
- Raw binary data for maximum efficiency:
38
-
39
- ```typescript
40
- // Serialize to binary
41
- const txBinary = transaction.toBinary()
42
-
43
- // Deserialize from binary
44
- const tx = Transaction.fromBinary(txBinary)
45
- ```
46
-
47
- ## Serialization Structure
48
-
49
- ### Standard Transaction
50
-
51
- ```
52
- Version (4 bytes)
53
- Input Count (varint)
54
- Inputs (variable)
55
- Output Count (varint)
56
- Outputs (variable)
57
- Lock Time (4 bytes)
58
- ```
59
-
60
- ### Input Structure
61
-
62
- ```
63
- Previous TX Hash (32 bytes)
64
- Output Index (4 bytes)
65
- Script Length (varint)
66
- Unlocking Script (variable)
67
- Sequence (4 bytes)
68
- ```
69
-
70
- ### Output Structure
71
-
72
- ```
73
- Value (8 bytes)
74
- Script Length (varint)
75
- Locking Script (variable)
76
- ```
77
-
78
- ## BEEF Enhancements
79
-
80
- BEEF format adds:
81
-
82
- - **Merkle Proofs**: SPV verification data
83
- - **Block Headers**: Chain validation information
84
- - **Metadata**: Additional transaction context
85
- - **Compression**: Efficient encoding for large datasets
86
-
87
- ## Encoding Considerations
88
-
89
- ### Size Optimization
90
-
91
- - Use BEEF for transactions with proofs
92
- - Use raw format for minimal overhead
93
- - Consider compression for large batches
94
-
95
- ### Compatibility
96
-
97
- - Raw format works with all Bitcoin software
98
- - BEEF requires compatible parsers
99
- - Binary format is most efficient but less portable
100
-
101
- ### Performance
102
-
103
- - Binary operations are fastest
104
- - Hex encoding is human-readable
105
- - BEEF provides best feature/size ratio
106
-
107
- ## Working with Encodings
108
-
109
- ### Conversion Between Formats
110
-
111
- ```typescript
112
- // Start with a transaction
113
- const tx = new Transaction()
114
-
115
- // Convert between formats
116
- const hex = tx.toHex()
117
- const binary = tx.toBinary()
118
- const beef = tx.toBEEF()
119
-
120
- // All represent the same transaction
121
- ```
122
-
123
- ### Validation
124
-
125
- ```typescript
126
- // Verify encoding integrity
127
- const originalTx = Transaction.fromHex(hex)
128
- const roundTripHex = originalTx.toHex()
129
- console.log(hex === roundTripHex) // true
130
- ```
131
-
132
- ## Use Cases
133
-
134
- ### Network Transmission
135
-
136
- - Use hex for JSON APIs
137
- - Use binary for efficient protocols
138
- - Use BEEF for SPV applications
139
-
140
- ### Storage
141
-
142
- - Raw format for blockchain storage
143
- - BEEF for application databases
144
- - Binary for memory-constrained environments
145
-
146
- ### Interoperability
147
-
148
- - Hex for debugging and logging
149
- - Raw format for wallet compatibility
150
- - BEEF for modern Bitcoin applications
151
-
152
- ## Error Handling
153
-
154
- Common encoding issues:
155
-
156
- - Invalid hex characters
157
- - Truncated binary data
158
- - Malformed BEEF structures
159
- - Version compatibility problems
160
-
161
- The SDK provides comprehensive validation and error reporting.
162
-
163
- ## Next Steps
164
-
165
- - Understand [BEEF Format](./beef.md) in detail
166
- - Learn about [SPV Verification](./spv-verification.md) with BEEF
167
- - Explore [Transaction Structure](./transaction-structure.md) fundamentals
@@ -1,67 +0,0 @@
1
- # Transaction Structure
2
-
3
- Understanding how Bitcoin transactions work and their representation in the BSV TypeScript SDK.
4
-
5
- ## Basic Transaction Components
6
-
7
- A Bitcoin transaction consists of:
8
-
9
- - **Inputs**: References to previous transaction outputs being spent
10
- - **Outputs**: New transaction outputs being created
11
- - **Metadata**: Version, lock time, and other transaction properties
12
-
13
- ## Transaction in the SDK
14
-
15
- ```typescript
16
- import { Transaction } from '@bsv/sdk'
17
-
18
- // Create a new transaction
19
- const tx = new Transaction()
20
-
21
- // Add inputs and outputs
22
- tx.addInput({
23
- sourceTransaction: previousTx,
24
- sourceOutputIndex: 0,
25
- unlockingScript: unlockingScript
26
- })
27
-
28
- tx.addOutput({
29
- satoshis: 1000,
30
- lockingScript: lockingScript
31
- })
32
- ```
33
-
34
- ## Key Concepts
35
-
36
- ### Inputs
37
-
38
- - Reference previous transaction outputs (UTXOs)
39
- - Include unlocking scripts to prove ownership
40
- - Must be fully consumed (no partial spending)
41
-
42
- ### Outputs
43
-
44
- - Create new UTXOs with specific values
45
- - Include locking scripts that define spending conditions
46
- - Can be spent by future transactions
47
-
48
- ### Transaction ID
49
-
50
- - Unique identifier calculated from transaction data
51
- - Used to reference the transaction in inputs
52
-
53
- ## Working with Transactions
54
-
55
- The SDK provides methods to:
56
-
57
- - Serialize transactions to hex format
58
- - Calculate transaction fees
59
- - Verify transaction validity
60
- - Sign transaction inputs
61
-
62
- ## Next Steps
63
-
64
- - Learn about [BEEF Format](./beef.md) for efficient transaction representation with validation data
65
- - Learn about [Script Templates](./script-templates.md) for locking/unlocking scripts
66
- - Understand [Digital Signatures](./signatures.md) for transaction authorization
67
- - Explore [Transaction Encoding](./transaction-encoding.md) for serialization formats
@@ -1,139 +0,0 @@
1
- # Trust Model
2
-
3
- Understanding the security assumptions and trust relationships in BSV TypeScript SDK applications.
4
-
5
- ## Core Trust Principles
6
-
7
- The SDK is designed around minimizing trust requirements:
8
-
9
- ### Trustless Verification
10
-
11
- - **Cryptographic Proofs**: Verify transactions mathematically
12
- - **SPV Security**: Validate without trusting third parties
13
- - **Self-Sovereign**: Users control their own keys and data
14
-
15
- ### Minimized Dependencies
16
-
17
- - **Zero External Dependencies**: Reduces attack surface
18
- - **Self-Contained**: All cryptographic operations built-in
19
- - **Auditable**: Open source and transparent implementation
20
-
21
- ## Trust Relationships
22
-
23
- ### User Trust
24
-
25
- Users must trust:
26
-
27
- - **The SDK Code**: Open source and auditable
28
- - **Their Wallet**: Manages private keys securely
29
- - **Their Device**: Secure execution environment
30
-
31
- ### Network Trust
32
-
33
- Applications rely on:
34
-
35
- - **Bitcoin Network**: Honest majority of miners
36
- - **Chain Trackers**: Provide accurate blockchain data
37
- - **SPV Assumptions**: Valid merkle proofs and headers
38
-
39
- ### Service Trust
40
-
41
- Optional trust relationships:
42
-
43
- - **Wallet Providers**: If using hosted wallets
44
- - **ARC Services**: For transaction broadcasting
45
- - **Overlay Services**: For additional functionality
46
-
47
- ## Security Assumptions
48
-
49
- ### Cryptographic Security
50
-
51
- - **secp256k1**: Elliptic curve is secure
52
- - **SHA-256**: Hash function is collision-resistant
53
- - **ECDSA**: Digital signature scheme is unforgeable
54
- - **Random Number Generation**: Entropy source is secure
55
-
56
- ### Network Security
57
-
58
- - **Proof of Work**: Mining provides security
59
- - **Longest Chain**: Honest chain has most work
60
- - **Block Finality**: Deep confirmations prevent reorganization
61
- - **Network Connectivity**: Access to honest nodes
62
-
63
- ## Risk Mitigation
64
-
65
- ### Key Management
66
-
67
- ```typescript
68
- // Minimize private key exposure
69
- const wallet = new WalletClient() // Keys stay in wallet
70
-
71
- // Avoid direct key handling
72
- // const privateKey = PrivateKey.fromString() // Higher risk
73
- ```
74
-
75
- ### Transaction Verification
76
-
77
- ```typescript
78
- // Always verify important transactions
79
- const isValid = await transaction.verify(chainTracker, {
80
- merkleProof: proof,
81
- blockHeader: header
82
- })
83
- ```
84
-
85
- ### Multiple Sources
86
-
87
- ```typescript
88
- // Use multiple chain trackers
89
- const config = {
90
- chainTracker: {
91
- primary: 'WhatsOnChain',
92
- fallbacks: ['GorillaPool', 'TAAL']
93
- }
94
- }
95
- ```
96
-
97
- ## Threat Model
98
-
99
- ### Attacks to Consider
100
-
101
- - **Private Key Compromise**: Secure key storage
102
- - **Man-in-the-Middle**: Use HTTPS and verify certificates
103
- - **Service Downtime**: Implement fallback mechanisms
104
- - **Double Spending**: Wait for confirmations
105
- - **Replay Attacks**: Use unique transaction IDs
106
-
107
- ## Application Design
108
-
109
- ### Security-First Design
110
-
111
- ```typescript
112
- // Validate all inputs
113
- function processTransaction(txHex: string) {
114
- if (!isValidHex(txHex)) {
115
- throw new Error('Invalid transaction hex')
116
- }
117
-
118
- const tx = Transaction.fromHex(txHex)
119
- // Process verified transaction
120
- }
121
- ```
122
-
123
- ### Error Handling
124
-
125
- ```typescript
126
- // Handle trust failures gracefully
127
- try {
128
- const result = await chainTracker.getTransaction(txid)
129
- } catch (error) {
130
- // Fallback to alternative source
131
- const backup = await fallbackTracker.getTransaction(txid)
132
- }
133
- ```
134
-
135
- ## Next Steps
136
-
137
- - Review [Key Management](./key-management.md) security practices
138
- - Understand [SPV Verification](./spv-verification.md) assumptions
139
- - Learn about [Wallet Integration](./wallet-integration.md) trust models
@@ -1,250 +0,0 @@
1
- # Transaction Verification
2
-
3
- Understanding how to verify Bitcoin transactions using the BSV TypeScript SDK.
4
-
5
- ## What is Transaction Verification?
6
-
7
- Transaction verification ensures that Bitcoin transactions are valid and can be trusted:
8
-
9
- ```typescript
10
- import { Transaction } from '@bsv/sdk'
11
-
12
- // Verify a transaction
13
- const isValid = await transaction.verify(chainTracker, {
14
- merkleProof: proof,
15
- blockHeader: header
16
- })
17
- ```
18
-
19
- ## Verification Levels
20
-
21
- ### Basic Validation
22
-
23
- Check transaction structure and format:
24
-
25
- - Valid input/output formats
26
- - Correct serialization
27
- - Proper script syntax
28
- - Signature format validation
29
-
30
- ### Script Execution
31
-
32
- Verify that unlocking scripts satisfy locking scripts:
33
-
34
- - Execute Bitcoin script opcodes
35
- - Validate signature operations
36
- - Check script conditions
37
- - Ensure proper stack state
38
-
39
- ### SPV Verification
40
-
41
- Confirm transaction inclusion in the blockchain:
42
-
43
- - Verify merkle proofs
44
- - Validate block headers
45
- - Check proof of work
46
- - Confirm transaction position
47
-
48
- ## SDK Verification Methods
49
-
50
- ### Transaction.verify()
51
-
52
- Complete transaction verification:
53
-
54
- ```typescript
55
- const result = await transaction.verify(chainTracker, {
56
- merkleProof: merkleProof,
57
- blockHeader: blockHeader,
58
- maxMemoryLimit: 100000000
59
- })
60
- ```
61
-
62
- ### Script Verification
63
-
64
- Verify individual scripts:
65
-
66
- ```typescript
67
- const isValid = unlockingScript.verify(
68
- lockingScript,
69
- transaction,
70
- inputIndex
71
- )
72
- ```
73
-
74
- ### Signature Verification
75
-
76
- Check digital signatures:
77
-
78
- ```typescript
79
- const publicKey = PrivateKey.fromWif(wif).toPublicKey()
80
- const isValid = publicKey.verify(messageHash, signature)
81
- ```
82
-
83
- ## Verification Options
84
-
85
- ### Memory Limits
86
-
87
- Control script execution memory usage:
88
-
89
- ```typescript
90
- const options = {
91
- maxMemoryLimit: 50000000 // 50MB limit
92
- }
93
- ```
94
-
95
- ### Scripts-Only Mode
96
-
97
- Skip SPV verification for performance:
98
-
99
- ```typescript
100
- const options = {
101
- scriptsOnly: true
102
- }
103
- ```
104
-
105
- ### Custom Chain Tracker
106
-
107
- Use specific data sources:
108
-
109
- ```typescript
110
- const customTracker = new WhatsOnChain('testnet')
111
- const isValid = await transaction.verify(customTracker)
112
- ```
113
-
114
- ## BEEF Verification
115
-
116
- BEEF format includes verification data:
117
-
118
- ```typescript
119
- // BEEF transactions include proofs
120
- const beefTx = Transaction.fromHexBEEF(beefData)
121
-
122
- // Verify using included proofs
123
- const isValid = await beefTx.verify(chainTracker)
124
- ```
125
-
126
- ## Error Handling
127
-
128
- Common verification failures:
129
-
130
- - Invalid signatures
131
- - Script execution errors
132
- - Missing merkle proofs
133
- - Network connectivity issues
134
-
135
- ```typescript
136
- try {
137
- const isValid = await transaction.verify(chainTracker)
138
- if (!isValid) {
139
- console.log('Transaction verification failed')
140
- }
141
- } catch (error) {
142
- console.error('Verification error:', error.message)
143
- }
144
- ```
145
-
146
- ## Performance Considerations
147
-
148
- ### Batch Verification
149
-
150
- Verify multiple transactions efficiently:
151
-
152
- ```typescript
153
- const results = await Promise.all(
154
- transactions.map(tx => tx.verify(chainTracker))
155
- )
156
- ```
157
-
158
- ### Caching
159
-
160
- Cache verification results:
161
-
162
- ```typescript
163
- const verificationCache = new Map()
164
-
165
- if (!verificationCache.has(txid)) {
166
- const result = await transaction.verify(chainTracker)
167
- verificationCache.set(txid, result)
168
- }
169
- ```
170
-
171
- ## Security Best Practices
172
-
173
- ### Always Verify
174
-
175
- - Verify transactions before trusting them
176
- - Check both script execution and SPV proofs
177
- - Validate input data before verification
178
-
179
- ### Multiple Sources
180
-
181
- - Use multiple chain trackers for redundancy
182
- - Cross-check verification results
183
- - Implement fallback mechanisms
184
-
185
- ### Resource Limits
186
-
187
- - Set appropriate memory limits
188
- - Timeout long-running verifications
189
- - Monitor verification performance
190
-
191
- ## Common Use Cases
192
-
193
- ### Payment Verification
194
-
195
- ```typescript
196
- // Verify received payment
197
- const payment = Transaction.fromHex(paymentHex)
198
- const isValid = await payment.verify(chainTracker)
199
-
200
- if (isValid) {
201
- // Process confirmed payment
202
- }
203
- ```
204
-
205
- ### Historical Transaction Audit
206
-
207
- ```typescript
208
- // Verify old transactions
209
- for (const txHex of historicalTransactions) {
210
- const tx = Transaction.fromHex(txHex)
211
- const result = await tx.verify(chainTracker)
212
- console.log(`Transaction ${tx.id()}: ${result ? 'Valid' : 'Invalid'}`)
213
- }
214
- ```
215
-
216
- ## Integration Patterns
217
-
218
- ### Wallet Integration
219
-
220
- ```typescript
221
- // Wallets typically handle verification
222
- const wallet = new WalletClient()
223
- const action = await wallet.createAction({
224
- outputs: [/* outputs */]
225
- })
226
- // Wallet verifies before broadcasting
227
- ```
228
-
229
- ### Application Verification
230
-
231
- ```typescript
232
- // Applications verify received transactions
233
- async function processIncomingTransaction(txHex: string) {
234
- const tx = Transaction.fromHex(txHex)
235
-
236
- if (await tx.verify(chainTracker)) {
237
- // Process verified transaction
238
- await handleValidTransaction(tx)
239
- } else {
240
- // Reject invalid transaction
241
- throw new Error('Invalid transaction received')
242
- }
243
- }
244
- ```
245
-
246
- ## Next Steps
247
-
248
- - Learn about [SPV Verification](./spv-verification.md) concepts
249
- - Understand [Digital Signatures](./signatures.md) validation
250
- - Explore [BEEF Format](./beef.md) for efficient verification
@@ -1,101 +0,0 @@
1
- # Wallet Integration
2
-
3
- How the BSV TypeScript SDK connects with Bitcoin wallets and manages user authentication.
4
-
5
- ## Wallet Connection Model
6
-
7
- The SDK uses a standardized approach to connect with Bitcoin wallets:
8
-
9
- ```typescript
10
- import { WalletClient } from '@bsv/sdk'
11
-
12
- // Connect to a wallet
13
- const wallet = new WalletClient('https://wallet-url')
14
-
15
- // Authenticate with the wallet
16
- await wallet.authenticate()
17
- ```
18
-
19
- ## BRC-100 Compliance
20
-
21
- The SDK follows the BRC-100 standard for wallet communication:
22
-
23
- - **Standardized APIs**: Consistent interface across different wallets
24
- - **Authentication**: Secure identity verification
25
- - **Transaction Signing**: Wallet handles private key operations
26
- - **UTXO Management**: Wallet manages available funds
27
-
28
- ## Authentication Flow
29
-
30
- 1. **Connection**: Establish connection to wallet service
31
- 2. **Identity**: Wallet provides user identity information
32
- 3. **Capabilities**: Discover what the wallet can do
33
- 4. **Authorization**: User grants permission for specific operations
34
-
35
- ## Transaction Creation
36
-
37
- The wallet handles sensitive operations:
38
-
39
- ```typescript
40
- // Create a transaction action
41
- const action = await wallet.createAction({
42
- description: 'Payment transaction',
43
- outputs: [{
44
- satoshis: 1000,
45
- lockingScript: recipientScript
46
- }]
47
- })
48
-
49
- // Wallet signs and broadcasts automatically
50
- ```
51
-
52
- ## Key Benefits
53
-
54
- ### Security
55
-
56
- - Private keys never leave the wallet
57
- - User controls transaction approval
58
- - Secure authentication protocols
59
-
60
- ### User Experience
61
-
62
- - Familiar wallet interface
63
- - Consistent across applications
64
- - Single sign-on capabilities
65
-
66
- ### Developer Simplicity
67
-
68
- - No key management complexity
69
- - Standardized APIs
70
- - Automatic UTXO handling
71
-
72
- ## Wallet Types
73
-
74
- The SDK works with various wallet implementations:
75
-
76
- - **Desktop Wallets**: Local applications with full control
77
- - **Web Wallets**: Browser-based wallet services
78
- - **Mobile Wallets**: Smartphone applications
79
- - **Hardware Wallets**: Secure hardware devices
80
-
81
- ## Error Handling
82
-
83
- Common wallet integration scenarios:
84
-
85
- - Wallet not available or offline
86
- - User denies transaction approval
87
- - Insufficient funds in wallet
88
- - Network connectivity issues
89
-
90
- ## Best Practices
91
-
92
- - Always handle wallet connection failures gracefully
93
- - Provide clear transaction descriptions to users
94
- - Implement retry logic for network issues
95
- - Cache wallet capabilities to improve performance
96
-
97
- ## Next Steps
98
-
99
- - Learn about [Chain Tracking](./chain-tracking.md) for network data
100
- - Understand [Key Management](./key-management.md) concepts
101
- - Explore [Trust Model](./trust-model.md) considerations