@bsv/sdk 1.6.8 → 1.6.10

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 (92) hide show
  1. package/README.md +9 -4
  2. package/dist/cjs/package.json +7 -5
  3. package/dist/cjs/src/wallet/substrates/HTTPWalletJSON.js +11 -3
  4. package/dist/cjs/src/wallet/substrates/HTTPWalletJSON.js.map +1 -1
  5. package/dist/cjs/src/wallet/substrates/WalletWireProcessor.js +1 -1
  6. package/dist/cjs/src/wallet/substrates/WalletWireProcessor.js.map +1 -1
  7. package/dist/cjs/src/wallet/substrates/utils/toOriginHeader.js +21 -0
  8. package/dist/cjs/src/wallet/substrates/utils/toOriginHeader.js.map +1 -0
  9. package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -1
  10. package/dist/esm/src/wallet/substrates/HTTPWalletJSON.js +9 -1
  11. package/dist/esm/src/wallet/substrates/HTTPWalletJSON.js.map +1 -1
  12. package/dist/esm/src/wallet/substrates/WalletWireProcessor.js +1 -1
  13. package/dist/esm/src/wallet/substrates/WalletWireProcessor.js.map +1 -1
  14. package/dist/esm/src/wallet/substrates/utils/toOriginHeader.js +17 -0
  15. package/dist/esm/src/wallet/substrates/utils/toOriginHeader.js.map +1 -0
  16. package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
  17. package/dist/types/src/wallet/substrates/HTTPWalletJSON.d.ts.map +1 -1
  18. package/dist/types/src/wallet/substrates/utils/toOriginHeader.d.ts +2 -0
  19. package/dist/types/src/wallet/substrates/utils/toOriginHeader.d.ts.map +1 -0
  20. package/dist/types/tsconfig.types.tsbuildinfo +1 -1
  21. package/dist/umd/bundle.js +1 -1
  22. package/docs/concepts/beef.md +84 -0
  23. package/docs/concepts/chain-tracking.md +122 -0
  24. package/docs/concepts/decentralized-identity.md +184 -0
  25. package/docs/concepts/fees.md +217 -0
  26. package/docs/concepts/identity-certificates.md +255 -0
  27. package/docs/concepts/index.md +62 -0
  28. package/docs/concepts/key-management.md +176 -0
  29. package/docs/concepts/script-templates.md +163 -0
  30. package/docs/concepts/sdk-philosophy.md +72 -0
  31. package/docs/concepts/signatures.md +179 -0
  32. package/docs/concepts/spv-verification.md +106 -0
  33. package/docs/concepts/transaction-encoding.md +148 -0
  34. package/docs/concepts/transaction-structure.md +63 -0
  35. package/docs/concepts/trust-model.md +123 -0
  36. package/docs/concepts/verification.md +219 -0
  37. package/docs/concepts/wallet-integration.md +95 -0
  38. package/docs/guides/direct-transaction-creation.md +137 -0
  39. package/docs/guides/http-client-configuration.md +414 -0
  40. package/docs/guides/index.md +30 -0
  41. package/docs/guides/transaction-signing-methods.md +268 -0
  42. package/docs/index.md +74 -0
  43. package/docs/reference/arc-config.md +698 -0
  44. package/docs/reference/brc-100.md +33 -0
  45. package/docs/reference/configuration.md +829 -0
  46. package/docs/reference/debugging.md +700 -0
  47. package/docs/reference/errors.md +547 -0
  48. package/docs/reference/index.md +98 -0
  49. package/docs/reference/network-config.md +914 -0
  50. package/docs/reference/op-codes.md +306 -0
  51. package/docs/reference/transaction-signatures.md +94 -0
  52. package/docs/{wallet.md → reference/wallet.md} +9 -0
  53. package/docs/requirements.txt +3 -0
  54. package/docs/tutorials/advanced-transaction.md +575 -0
  55. package/docs/tutorials/aes-encryption.md +947 -0
  56. package/docs/tutorials/authfetch-tutorial.md +957 -0
  57. package/docs/tutorials/ecdh-key-exchange.md +547 -0
  58. package/docs/tutorials/elliptic-curve-fundamentals.md +603 -0
  59. package/docs/tutorials/error-handling.md +1215 -0
  60. package/docs/tutorials/first-transaction-low-level.md +204 -0
  61. package/docs/tutorials/first-transaction.md +278 -0
  62. package/docs/tutorials/hashes-and-hmacs.md +814 -0
  63. package/docs/tutorials/identity-management.md +702 -0
  64. package/docs/tutorials/index.md +182 -0
  65. package/docs/tutorials/key-management.md +536 -0
  66. package/docs/tutorials/protowallet-development.md +716 -0
  67. package/docs/tutorials/script-construction.md +690 -0
  68. package/docs/tutorials/spv-merkle-proofs.md +682 -0
  69. package/docs/tutorials/testnet-transactions-low-level.md +352 -0
  70. package/docs/tutorials/transaction-broadcasting.md +535 -0
  71. package/docs/tutorials/transaction-types.md +419 -0
  72. package/docs/tutorials/type-42.md +582 -0
  73. package/docs/tutorials/uhrp-storage.md +579 -0
  74. package/package.json +7 -5
  75. package/src/transaction/__tests/Transaction.test.ts +1 -1
  76. package/src/wallet/substrates/HTTPWalletJSON.ts +11 -1
  77. package/src/wallet/substrates/WalletWireProcessor.ts +1 -1
  78. package/src/wallet/substrates/__tests/toOriginHeader.test.ts +34 -0
  79. package/src/wallet/substrates/utils/toOriginHeader.ts +15 -0
  80. package/docs/README.md +0 -21
  81. /package/docs/{auth.md → reference/auth.md} +0 -0
  82. /package/docs/{compat.md → reference/compat.md} +0 -0
  83. /package/docs/{identity.md → reference/identity.md} +0 -0
  84. /package/docs/{kvstore.md → reference/kvstore.md} +0 -0
  85. /package/docs/{messages.md → reference/messages.md} +0 -0
  86. /package/docs/{overlay-tools.md → reference/overlay-tools.md} +0 -0
  87. /package/docs/{primitives.md → reference/primitives.md} +0 -0
  88. /package/docs/{registry.md → reference/registry.md} +0 -0
  89. /package/docs/{script.md → reference/script.md} +0 -0
  90. /package/docs/{storage.md → reference/storage.md} +0 -0
  91. /package/docs/{totp.md → reference/totp.md} +0 -0
  92. /package/docs/{transaction.md → reference/transaction.md} +0 -0
@@ -0,0 +1,219 @@
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
+ Check transaction structure and format:
23
+ - Valid input/output formats
24
+ - Correct serialization
25
+ - Proper script syntax
26
+ - Signature format validation
27
+
28
+ ### Script Execution
29
+ Verify that unlocking scripts satisfy locking scripts:
30
+ - Execute Bitcoin script opcodes
31
+ - Validate signature operations
32
+ - Check script conditions
33
+ - Ensure proper stack state
34
+
35
+ ### SPV Verification
36
+ Confirm transaction inclusion in the blockchain:
37
+ - Verify merkle proofs
38
+ - Validate block headers
39
+ - Check proof of work
40
+ - Confirm transaction position
41
+
42
+ ## SDK Verification Methods
43
+
44
+ ### Transaction.verify()
45
+ Complete transaction verification:
46
+ ```typescript
47
+ const result = await transaction.verify(chainTracker, {
48
+ merkleProof: merkleProof,
49
+ blockHeader: blockHeader,
50
+ maxMemoryLimit: 100000000
51
+ })
52
+ ```
53
+
54
+ ### Script Verification
55
+ Verify individual scripts:
56
+ ```typescript
57
+ const isValid = unlockingScript.verify(
58
+ lockingScript,
59
+ transaction,
60
+ inputIndex
61
+ )
62
+ ```
63
+
64
+ ### Signature Verification
65
+ Check digital signatures:
66
+ ```typescript
67
+ const publicKey = PrivateKey.fromWif(wif).toPublicKey()
68
+ const isValid = publicKey.verify(messageHash, signature)
69
+ ```
70
+
71
+ ## Verification Options
72
+
73
+ ### Memory Limits
74
+ Control script execution memory usage:
75
+ ```typescript
76
+ const options = {
77
+ maxMemoryLimit: 50000000 // 50MB limit
78
+ }
79
+ ```
80
+
81
+ ### Scripts-Only Mode
82
+ Skip SPV verification for performance:
83
+ ```typescript
84
+ const options = {
85
+ scriptsOnly: true
86
+ }
87
+ ```
88
+
89
+ ### Custom Chain Tracker
90
+ Use specific data sources:
91
+ ```typescript
92
+ const customTracker = new WhatsOnChain('testnet')
93
+ const isValid = await transaction.verify(customTracker)
94
+ ```
95
+
96
+ ## BEEF Verification
97
+
98
+ BEEF format includes verification data:
99
+ ```typescript
100
+ // BEEF transactions include proofs
101
+ const beefTx = Transaction.fromHexBEEF(beefData)
102
+
103
+ // Verify using included proofs
104
+ const isValid = await beefTx.verify(chainTracker)
105
+ ```
106
+
107
+ ## Error Handling
108
+
109
+ Common verification failures:
110
+ - Invalid signatures
111
+ - Script execution errors
112
+ - Missing merkle proofs
113
+ - Network connectivity issues
114
+
115
+ ```typescript
116
+ try {
117
+ const isValid = await transaction.verify(chainTracker)
118
+ if (!isValid) {
119
+ console.log('Transaction verification failed')
120
+ }
121
+ } catch (error) {
122
+ console.error('Verification error:', error.message)
123
+ }
124
+ ```
125
+
126
+ ## Performance Considerations
127
+
128
+ ### Batch Verification
129
+ Verify multiple transactions efficiently:
130
+ ```typescript
131
+ const results = await Promise.all(
132
+ transactions.map(tx => tx.verify(chainTracker))
133
+ )
134
+ ```
135
+
136
+ ### Caching
137
+ Cache verification results:
138
+ ```typescript
139
+ const verificationCache = new Map()
140
+
141
+ if (!verificationCache.has(txid)) {
142
+ const result = await transaction.verify(chainTracker)
143
+ verificationCache.set(txid, result)
144
+ }
145
+ ```
146
+
147
+ ## Security Best Practices
148
+
149
+ ### Always Verify
150
+ - Verify transactions before trusting them
151
+ - Check both script execution and SPV proofs
152
+ - Validate input data before verification
153
+
154
+ ### Multiple Sources
155
+ - Use multiple chain trackers for redundancy
156
+ - Cross-check verification results
157
+ - Implement fallback mechanisms
158
+
159
+ ### Resource Limits
160
+ - Set appropriate memory limits
161
+ - Timeout long-running verifications
162
+ - Monitor verification performance
163
+
164
+ ## Common Use Cases
165
+
166
+ ### Payment Verification
167
+ ```typescript
168
+ // Verify received payment
169
+ const payment = Transaction.fromHex(paymentHex)
170
+ const isValid = await payment.verify(chainTracker)
171
+
172
+ if (isValid) {
173
+ // Process confirmed payment
174
+ }
175
+ ```
176
+
177
+ ### Historical Transaction Audit
178
+ ```typescript
179
+ // Verify old transactions
180
+ for (const txHex of historicalTransactions) {
181
+ const tx = Transaction.fromHex(txHex)
182
+ const result = await tx.verify(chainTracker)
183
+ console.log(`Transaction ${tx.id()}: ${result ? 'Valid' : 'Invalid'}`)
184
+ }
185
+ ```
186
+
187
+ ## Integration Patterns
188
+
189
+ ### Wallet Integration
190
+ ```typescript
191
+ // Wallets typically handle verification
192
+ const wallet = new WalletClient()
193
+ const action = await wallet.createAction({
194
+ outputs: [/* outputs */]
195
+ })
196
+ // Wallet verifies before broadcasting
197
+ ```
198
+
199
+ ### Application Verification
200
+ ```typescript
201
+ // Applications verify received transactions
202
+ async function processIncomingTransaction(txHex: string) {
203
+ const tx = Transaction.fromHex(txHex)
204
+
205
+ if (await tx.verify(chainTracker)) {
206
+ // Process verified transaction
207
+ await handleValidTransaction(tx)
208
+ } else {
209
+ // Reject invalid transaction
210
+ throw new Error('Invalid transaction received')
211
+ }
212
+ }
213
+ ```
214
+
215
+ ## Next Steps
216
+
217
+ - Learn about [SPV Verification](./spv-verification.md) concepts
218
+ - Understand [Digital Signatures](./signatures.md) validation
219
+ - Explore [BEEF Format](./beef.md) for efficient verification
@@ -0,0 +1,95 @@
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
+ - **Standardized APIs**: Consistent interface across different wallets
23
+ - **Authentication**: Secure identity verification
24
+ - **Transaction Signing**: Wallet handles private key operations
25
+ - **UTXO Management**: Wallet manages available funds
26
+
27
+ ## Authentication Flow
28
+
29
+ 1. **Connection**: Establish connection to wallet service
30
+ 2. **Identity**: Wallet provides user identity information
31
+ 3. **Capabilities**: Discover what the wallet can do
32
+ 4. **Authorization**: User grants permission for specific operations
33
+
34
+ ## Transaction Creation
35
+
36
+ The wallet handles sensitive operations:
37
+
38
+ ```typescript
39
+ // Create a transaction action
40
+ const action = await wallet.createAction({
41
+ description: 'Payment transaction',
42
+ outputs: [{
43
+ satoshis: 1000,
44
+ lockingScript: recipientScript
45
+ }]
46
+ })
47
+
48
+ // Wallet signs and broadcasts automatically
49
+ ```
50
+
51
+ ## Key Benefits
52
+
53
+ ### Security
54
+ - Private keys never leave the wallet
55
+ - User controls transaction approval
56
+ - Secure authentication protocols
57
+
58
+ ### User Experience
59
+ - Familiar wallet interface
60
+ - Consistent across applications
61
+ - Single sign-on capabilities
62
+
63
+ ### Developer Simplicity
64
+ - No key management complexity
65
+ - Standardized APIs
66
+ - Automatic UTXO handling
67
+
68
+ ## Wallet Types
69
+
70
+ The SDK works with various wallet implementations:
71
+ - **Desktop Wallets**: Local applications with full control
72
+ - **Web Wallets**: Browser-based wallet services
73
+ - **Mobile Wallets**: Smartphone applications
74
+ - **Hardware Wallets**: Secure hardware devices
75
+
76
+ ## Error Handling
77
+
78
+ Common wallet integration scenarios:
79
+ - Wallet not available or offline
80
+ - User denies transaction approval
81
+ - Insufficient funds in wallet
82
+ - Network connectivity issues
83
+
84
+ ## Best Practices
85
+
86
+ - Always handle wallet connection failures gracefully
87
+ - Provide clear transaction descriptions to users
88
+ - Implement retry logic for network issues
89
+ - Cache wallet capabilities to improve performance
90
+
91
+ ## Next Steps
92
+
93
+ - Learn about [Chain Tracking](./chain-tracking.md) for network data
94
+ - Understand [Key Management](./key-management.md) concepts
95
+ - Explore [Trust Model](./trust-model.md) considerations
@@ -0,0 +1,137 @@
1
+ # Creating Transactions with Direct Interfaces
2
+
3
+ This guide demonstrates how to create Bitcoin SV transactions using the lower-level direct interfaces provided by the BSV TypeScript SDK. This approach gives you more control over transaction construction and is useful for specialized use cases where the WalletClient abstraction isn't suitable.
4
+
5
+ ## When to Use Direct Interfaces
6
+
7
+ - When creating custom transaction types not supported by WalletClient
8
+ - When you need precise control over UTXO selection
9
+ - When building specialized applications like data storage services that require custom optimization
10
+ - When integrating with systems that require direct management of transactions
11
+ - For educational purposes to understand the underlying transaction structure
12
+
13
+ ## Basic Transaction Construction
14
+
15
+ ```typescript
16
+ import { PrivateKey, P2PKH, Transaction } from '@bsv/sdk'
17
+
18
+ async function createBasicTransaction() {
19
+ // Create a private key
20
+ const privateKey = PrivateKey.fromRandom()
21
+ console.log(`Private key WIF: ${privateKey.toWif()}`)
22
+
23
+ // Derive the address
24
+ const address = privateKey.toAddress()
25
+ console.log(`Address: ${address.toString()}`)
26
+
27
+ // Create a new transaction
28
+ const tx = new Transaction()
29
+
30
+ // Add an output
31
+ tx.addOutput({
32
+ lockingScript: new P2PKH().lock(address),
33
+ satoshis: 1000
34
+ })
35
+
36
+ // Serialize the transaction
37
+ const txHex = tx.toHex()
38
+ console.log(`Transaction (hex): ${txHex}`)
39
+
40
+ // Get transaction ID as a hex string
41
+ const txid = Buffer.from(tx.id()).toString('hex')
42
+ console.log(`Transaction ID: ${txid}`)
43
+ }
44
+ ```
45
+
46
+ ## Complete Transaction with Inputs and Outputs
47
+
48
+ For a complete transaction that can be broadcast, you need to add inputs, calculate fees, and sign it:
49
+
50
+ ```typescript
51
+ import { Transaction, PrivateKey, P2PKH, ARC } from '@bsv/sdk'
52
+
53
+ async function createCompleteTransaction() {
54
+ // Set up your wallet
55
+ const privateKey = PrivateKey.fromWif('your_private_key_here')
56
+ const myAddress = privateKey.toAddress()
57
+ const recipientAddress = 'recipient_address_here'
58
+
59
+ // You need the hex of the source transaction
60
+ const sourceTxHex = '...' // Hex string of the source transaction
61
+
62
+ // Create a transaction
63
+ const tx = new Transaction()
64
+
65
+ // Add the input
66
+ tx.addInput({
67
+ sourceTransaction: Transaction.fromHex(sourceTxHex),
68
+ sourceOutputIndex: 0, // The output index you want to spend
69
+ unlockingScriptTemplate: new P2PKH().unlock(privateKey)
70
+ })
71
+
72
+ // Add the recipient output
73
+ tx.addOutput({
74
+ lockingScript: new P2PKH().lock(recipientAddress),
75
+ satoshis: 100 // Amount to send
76
+ })
77
+
78
+ // Add the change output back to our address
79
+ tx.addOutput({
80
+ lockingScript: new P2PKH().lock(myAddress),
81
+ change: true // SDK will automatically calculate the change amount
82
+ })
83
+
84
+ // Calculate fee and sign the transaction
85
+ await tx.fee()
86
+ await tx.sign()
87
+
88
+ // Get the transaction hex ready for broadcasting
89
+ const signedTxHex = tx.toHex()
90
+ console.log(`Signed transaction hex: ${signedTxHex}`)
91
+
92
+ // Get the transaction ID
93
+ const txid = Buffer.from(tx.id()).toString('hex')
94
+ console.log(`Transaction ID: ${txid}`)
95
+ }
96
+ ```
97
+
98
+ ## Broadcasting the Transaction
99
+
100
+ To broadcast your signed transaction:
101
+
102
+ ```typescript
103
+ import { ARC, NodejsHttpClient } from '@bsv/sdk'
104
+ import https from 'https'
105
+
106
+ async function broadcastTransaction(signedTx) {
107
+ // Create an HTTP client
108
+ const httpClient = new NodejsHttpClient(https)
109
+
110
+ // Initialize the ARC client
111
+ const arc = new ARC('https://api.arc.taal.com', {
112
+ apiKey: 'your_api_key_here',
113
+ httpClient,
114
+ deploymentId: 'your-deployment-id'
115
+ })
116
+
117
+ // Broadcast the transaction
118
+ const result = await signedTx.broadcast(arc)
119
+ console.log('Broadcast result:', result)
120
+ }
121
+ ```
122
+
123
+ ## Key Implementation Details
124
+
125
+ When working with direct interfaces, remember these important details:
126
+
127
+ 1. Use `toWif()` (lowercase 'f') not `toWIF()` for private key WIF format
128
+ 2. Use `toHex()` instead of `toString()` for transaction serialization
129
+ 3. Transaction IDs need to be converted from byte arrays: `Buffer.from(tx.id()).toString('hex')`
130
+ 4. For script objects, use `toHex()` or `toASM()` rather than `toString()`
131
+ 5. Method chaining doesn't work well with current API - use separate method calls
132
+
133
+ ## Related Resources
134
+
135
+ - For simpler implementations, see the [Creating Transactions with WalletClient](../tutorials/first-transaction.md) tutorial
136
+ - Learn about [Advanced Transaction Signing](./advanced-transaction-signing.md)
137
+ - Explore [HTTP Client Configuration](./http-client-configuration.md) for optimizing API requests