@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,575 @@
1
+ **Duration**: 75 minutes
2
+ **Prerequisites**: Completed "Transaction Types and Data" tutorial, Node.js, basic TypeScript knowledge
3
+
4
+ ## Learning Goals
5
+ - Master multi-input/multi-output transaction construction
6
+ - Implement advanced fee calculation and optimization strategies
7
+ - Handle change outputs efficiently
8
+ - Use advanced `WalletClient` options for transaction control
9
+ - Implement UTXO selection and management strategies
10
+ - Handle complex transaction scenarios and error recovery
11
+
12
+ ## Introduction
13
+
14
+ This tutorial builds on your knowledge of basic `WalletClient` usage to explore advanced transaction construction patterns. You'll learn how to create sophisticated transactions that handle multiple inputs and outputs, optimize fees, and manage UTXOs effectively for production applications.
15
+
16
+ ## Prerequisites
17
+
18
+ - Complete the [Transaction Types and Data](./transaction-types.md) tutorial
19
+ - Have a BRC-100 compliant wallet (such as the [MetaNet Desktop Wallet](https://metanet.bsvb.tech/)) installed and configured
20
+ - Some BSV in your wallet
21
+ - Understanding of Bitcoin transaction fundamentals
22
+
23
+ ## Advanced `WalletClient` Options
24
+
25
+ The `createAction` method supports many advanced options through the `options` parameter. Let's explore these capabilities:
26
+
27
+ ```typescript
28
+ import { WalletClient } from '@bsv/sdk'
29
+
30
+ async function exploreAdvancedOptions() {
31
+ try {
32
+ const wallet = new WalletClient('auto', 'localhost')
33
+
34
+ // Authenticate with the wallet
35
+ const { authenticated } = await wallet.isAuthenticated()
36
+ if (!authenticated) {
37
+ await wallet.waitForAuthentication()
38
+ }
39
+
40
+ // Create a transaction with advanced options
41
+ const actionResult = await wallet.createAction({
42
+ description: 'Advanced options demonstration',
43
+ outputs: [
44
+ {
45
+ satoshis: 100,
46
+ lockingScript: '006a0e416476616e636564206f7074696f6e73', // OP_RETURN "Advanced options"
47
+ outputDescription: 'Advanced options demo'
48
+ }
49
+ ],
50
+ options: {
51
+ // Don't automatically sign and process - gives you more control
52
+ signAndProcess: false,
53
+
54
+ // Accept delayed broadcast for better fee optimization
55
+ acceptDelayedBroadcast: true,
56
+
57
+ // Return only the transaction ID to save bandwidth
58
+ returnTXIDOnly: false,
59
+
60
+ // Don't send the transaction immediately
61
+ noSend: true,
62
+
63
+ // Randomize output order for privacy
64
+ randomizeOutputs: true
65
+ }
66
+ })
67
+
68
+ console.log('Transaction created with advanced options:')
69
+ console.log('Signable transaction available for manual processing')
70
+
71
+ if (actionResult.signableTransaction) {
72
+ console.log('Transaction reference:', actionResult.signableTransaction.reference)
73
+ console.log('Transaction ready for signing')
74
+ }
75
+
76
+ } catch (error: any) {
77
+ console.error('Error:', error)
78
+
79
+ // Provide helpful troubleshooting
80
+ if (error.message?.includes('unlockingScript')) {
81
+ console.log('\nTroubleshooting: When specifying custom inputs, you must provide:')
82
+ console.log('- unlockingScript (valid hexadecimal string, not empty)')
83
+ console.log('- unlockingScriptLength (typically 107 for P2PKH)')
84
+ console.log('\nRecommendation: Let the wallet auto-select inputs by omitting the inputs array')
85
+ }
86
+ }
87
+ }
88
+
89
+ exploreAdvancedOptions().catch(console.error)
90
+ ```
91
+
92
+ ## Multi-Input Transaction Construction
93
+
94
+ Multi-input transactions combine multiple UTXOs to fund larger outputs. The BSV TypeScript SDK provides two approaches:
95
+
96
+ 1. **Automatic Input Selection (Recommended)**: Let the wallet automatically select UTXOs by creating outputs that require more satoshis than any single UTXO can provide.
97
+
98
+ 2. **Manual Input Specification (Advanced)**: Explicitly specify which UTXOs to use as inputs. This requires providing complete unlocking script information and is typically only needed for specialized use cases.
99
+
100
+ ```typescript
101
+ import { WalletClient } from '@bsv/sdk'
102
+
103
+ async function createMultiInputTransaction() {
104
+ try {
105
+ const wallet = new WalletClient('auto', 'localhost')
106
+
107
+ const { authenticated } = await wallet.isAuthenticated()
108
+ if (!authenticated) {
109
+ await wallet.waitForAuthentication()
110
+ }
111
+
112
+ // First, let's see what UTXOs we have available (basket name is 'bsv-tutorial' for consistency with previous tutorials, where we created some outputs)
113
+ const { outputs } = await wallet.listOutputs({
114
+ basket: 'bsv-tutorial',
115
+ include: 'locking scripts',
116
+ limit: 10
117
+ })
118
+
119
+ console.log(`Found ${outputs.length} available outputs:`)
120
+ outputs.forEach((output, index) => {
121
+ console.log(` ${index + 1}. ${output.outpoint}: ${output.satoshis} satoshis (spendable: ${output.spendable})`)
122
+ })
123
+
124
+ // Check if we have enough UTXOs for a multi-input transaction
125
+ const spendableOutputs = outputs.filter(output => output.spendable && output.satoshis >= 100)
126
+
127
+ if (spendableOutputs.length < 2) {
128
+ console.log('Need at least 2 spendable outputs for this demo')
129
+ return
130
+ }
131
+
132
+ // Method 1: Automatic Input Selection (Recommended)
133
+
134
+ // Create a transaction that requires multiple inputs by using a large output amount
135
+ // The wallet will automatically select multiple UTXOs if needed
136
+ const totalAvailable = spendableOutputs.reduce((sum, output) => sum + output.satoshis, 0)
137
+ const largeAmount = Math.min(1500, Math.floor(totalAvailable * 0.8)) // Use 80% of available funds
138
+
139
+ console.log(`Creating transaction requiring ${largeAmount} satoshis (should trigger multi-input selection)`)
140
+
141
+ const actionResult = await wallet.createAction({
142
+ description: 'Multi-input transaction example',
143
+ outputs: [
144
+ {
145
+ satoshis: largeAmount,
146
+ lockingScript: '006a0c4d756c74692d696e707574', // OP_RETURN "Multi-input"
147
+ outputDescription: 'Multi-input demo output'
148
+ }
149
+ ]
150
+ // No inputs specified - let wallet auto-select
151
+ })
152
+
153
+ console.log('Transaction created successfully!')
154
+ console.log('Transaction ID:', actionResult.txid)
155
+ console.log(`View on explorer: https://whatsonchain.com/tx/${actionResult.txid}`)
156
+
157
+ // Method 2: Manual Input Specification (Advanced)
158
+ // Manual input specification requires unlocking script details
159
+ // This is commented out as it requires proper unlocking script construction
160
+ /*
161
+ const manualActionResult = await wallet.createAction({
162
+ description: 'Manual multi-input transaction',
163
+ inputs: [
164
+ {
165
+ outpoint: spendableOutputs[0].outpoint,
166
+ unlockingScript: '00', // Placeholder - would need actual unlocking script
167
+ unlockingScriptLength: 107, // Typical P2PKH unlocking script length
168
+ inputDescription: 'First manually selected input'
169
+ },
170
+ {
171
+ outpoint: spendableOutputs[1].outpoint,
172
+ unlockingScript: '00', // Placeholder - would need actual unlocking script
173
+ unlockingScriptLength: 107, // Typical P2PKH unlocking script length
174
+ inputDescription: 'Second manually selected input'
175
+ }
176
+ ],
177
+ outputs: [
178
+ {
179
+ satoshis: 150,
180
+ lockingScript: '006a104d616e75616c2d6d756c74692d696e707574', // OP_RETURN "Manual-multi-input"
181
+ outputDescription: 'Manual multi-input demo output'
182
+ }
183
+ ]
184
+ })
185
+ */
186
+
187
+ } catch (error: any) {
188
+ console.error('Error:', error)
189
+
190
+ // Provide helpful troubleshooting
191
+ if (error.message?.includes('unlockingScript')) {
192
+ console.log('\nTroubleshooting: When specifying custom inputs, you must provide:')
193
+ console.log('- unlockingScript (valid hexadecimal string, not empty)')
194
+ console.log('- unlockingScriptLength (typically 107 for P2PKH)')
195
+ console.log('\nRecommendation: Let the wallet auto-select inputs by omitting the inputs array')
196
+ }
197
+ }
198
+ }
199
+
200
+ createMultiInputTransaction().catch(console.error)
201
+ ```
202
+
203
+ ## Batch Processing Multiple Payments
204
+
205
+ For businesses and applications that need to send multiple payments efficiently, batch processing reduces fees and blockchain footprint:
206
+
207
+ ```typescript
208
+ import { WalletClient } from '@bsv/sdk'
209
+
210
+ async function createBatchPayment() {
211
+ try {
212
+ const wallet = new WalletClient('auto', 'localhost')
213
+
214
+ const { authenticated } = await wallet.isAuthenticated()
215
+ if (!authenticated) {
216
+ await wallet.waitForAuthentication()
217
+ }
218
+
219
+ // Simulate multiple payment recipients
220
+ const payments = [
221
+ { amount: 100, description: 'Payment to Alice', data: 'Invoice #001' },
222
+ { amount: 150, description: 'Payment to Bob', data: 'Invoice #002' },
223
+ { amount: 200, description: 'Payment to Carol', data: 'Invoice #003' }
224
+ ]
225
+
226
+ // Create outputs for each payment
227
+ const outputs = payments.map((payment, index) => {
228
+ // Create OP_RETURN with payment data
229
+ const dataHex = Buffer.from(payment.data).toString('hex')
230
+ const dataLength = Buffer.from(payment.data).length
231
+ const dataLengthHex = dataLength.toString(16).padStart(2, '0')
232
+ const lockingScript = `006a${dataLengthHex}${dataHex}`
233
+
234
+ return {
235
+ satoshis: payment.amount,
236
+ lockingScript: lockingScript,
237
+ outputDescription: payment.description,
238
+ basket: 'payments', // Organize outputs in a specific basket
239
+ tags: ['batch-payment', `invoice-${index + 1}`] // Tag for easy tracking
240
+ }
241
+ })
242
+
243
+ // Create the batch transaction
244
+ const actionResult = await wallet.createAction({
245
+ description: 'Batch payment transaction',
246
+ outputs: outputs,
247
+ labels: ['batch-processing', 'business-payments'], // Labels for transaction tracking
248
+ options: {
249
+ randomizeOutputs: false // Keep payment order for accounting
250
+ }
251
+ })
252
+
253
+ if (actionResult.txid) {
254
+ console.log(`Batch payment transaction created: ${actionResult.txid}`)
255
+ console.log(`Processed ${payments.length} payments in a single transaction`)
256
+ console.log(`Total amount: ${payments.reduce((sum, p) => sum + p.amount, 0)} satoshis`)
257
+ console.log(`View on explorer: https://whatsonchain.com/tx/${actionResult.txid}`)
258
+ }
259
+
260
+ } catch (error: unknown) {
261
+ console.error('Error:', error)
262
+ }
263
+ }
264
+
265
+ createBatchPayment().catch(console.error)
266
+ ```
267
+
268
+ ## Advanced UTXO Management with Baskets
269
+
270
+ Baskets provide powerful UTXO organization capabilities. Here's how to use them for advanced transaction patterns:
271
+
272
+ ```typescript
273
+ import { WalletClient } from '@bsv/sdk'
274
+
275
+ async function advancedBasketManagement() {
276
+ try {
277
+ const wallet = new WalletClient('auto', 'localhost')
278
+
279
+ const { authenticated } = await wallet.isAuthenticated()
280
+ if (!authenticated) {
281
+ await wallet.waitForAuthentication()
282
+ }
283
+
284
+ // Create outputs in different baskets for different purposes
285
+ console.log('Creating specialized UTXO baskets...')
286
+
287
+ const actionResult = await wallet.createAction({
288
+ description: 'UTXO organization with baskets',
289
+ outputs: [
290
+ {
291
+ satoshis: 500,
292
+ lockingScript: '006a0c48696768207072696f72697479', // OP_RETURN "High priority"
293
+ outputDescription: 'High priority reserve',
294
+ basket: 'high-priority',
295
+ tags: ['reserve', 'urgent-use'],
296
+ customInstructions: 'Reserved for urgent transactions'
297
+ },
298
+ {
299
+ satoshis: 300,
300
+ lockingScript: '006a0d4d656469756d207072696f72697479', // OP_RETURN "Medium priority"
301
+ outputDescription: 'Medium priority operations',
302
+ basket: 'medium-priority',
303
+ tags: ['operations', 'daily-use']
304
+ },
305
+ {
306
+ satoshis: 200,
307
+ lockingScript: '006a0b4c6f77207072696f72697479', // OP_RETURN "Low priority"
308
+ outputDescription: 'Low priority batch',
309
+ basket: 'low-priority',
310
+ tags: ['batch', 'bulk-operations']
311
+ }
312
+ ]
313
+ })
314
+
315
+ if (actionResult.txid) {
316
+ console.log(`Basket organization transaction: ${actionResult.txid}`)
317
+ console.log('Note: OP_RETURN outputs are unspendable by design (for data storage)')
318
+ console.log('Waiting for wallet to process new outputs...')
319
+ // Give the wallet a moment to process the new outputs
320
+ await new Promise(resolve => setTimeout(resolve, 2000))
321
+ }
322
+
323
+ // Now let's examine our organized UTXOs
324
+ console.log('\nExamining basket organization...')
325
+ console.log('Note: OP_RETURN outputs show spendable: false because they are data-only outputs')
326
+ const baskets = ['high-priority', 'medium-priority', 'low-priority']
327
+
328
+ for (const basketName of baskets) {
329
+ const { outputs, totalOutputs } = await wallet.listOutputs({
330
+ basket: basketName,
331
+ includeTags: true,
332
+ includeCustomInstructions: true,
333
+ limit: 10
334
+ })
335
+
336
+ console.log(`\n${basketName.toUpperCase()} Basket:`)
337
+ console.log(` Total outputs: ${totalOutputs}`)
338
+
339
+ if (totalOutputs === 0) {
340
+ console.log(` Note: No outputs found. This could be because:`)
341
+ console.log(` - Outputs need confirmation time`)
342
+ console.log(` - Wallet needs time to process new baskets`)
343
+ console.log(` - OP_RETURN outputs might not be tracked as spendable UTXOs`)
344
+ }
345
+
346
+ outputs.forEach((output, index) => {
347
+ console.log(` Output ${index + 1}:`)
348
+ console.log(` Outpoint: ${output.outpoint}`)
349
+ console.log(` Amount: ${output.satoshis} satoshis`)
350
+ console.log(` Spendable: ${output.spendable}`)
351
+ if (output.tags) {
352
+ console.log(` Tags: ${output.tags.join(', ')}`)
353
+ }
354
+ if (output.customInstructions) {
355
+ console.log(` Instructions: ${output.customInstructions}`)
356
+ }
357
+ })
358
+ }
359
+
360
+ // Add processing delay
361
+ await new Promise(resolve => setTimeout(resolve, 5000));
362
+
363
+ } catch (error: unknown) {
364
+ console.error('Error:', error)
365
+ }
366
+ }
367
+
368
+ advancedBasketManagement().catch(console.error)
369
+ ```
370
+
371
+ ## Transaction Chaining and Dependencies
372
+
373
+ Sometimes you need to create transactions that depend on previous ones. Here's how to handle transaction chaining:
374
+
375
+ ```typescript
376
+ import { WalletClient } from '@bsv/sdk'
377
+
378
+ async function createTransactionChain() {
379
+ try {
380
+ const wallet = new WalletClient('auto', 'localhost')
381
+
382
+ const { authenticated } = await wallet.isAuthenticated()
383
+ if (!authenticated) {
384
+ await wallet.waitForAuthentication()
385
+ }
386
+
387
+ console.log('Creating transaction chain...')
388
+ console.log('Transaction chaining allows you to create a series of transactions that depend on each other.')
389
+ console.log('This is useful for scenarios where you need to ensure that a transaction is only processed after a previous one has been confirmed.')
390
+ console.log('In this example, we will create two transactions: the first one creates an output, and the second one spends that output.')
391
+
392
+ // First transaction - creates outputs for the chain
393
+ const firstTx = await wallet.createAction({
394
+ description: 'Chain starter transaction',
395
+ outputs: [
396
+ {
397
+ satoshis: 400,
398
+ lockingScript: '006a0d436861696e207374617274657220', // OP_RETURN "Chain starter"
399
+ outputDescription: 'Chain starter output',
400
+ basket: 'chain-demo',
401
+ tags: ['chain', 'step-1']
402
+ }
403
+ ],
404
+ options: {
405
+ acceptDelayedBroadcast: true // Allow some delay for better fee optimization
406
+ }
407
+ })
408
+
409
+ if (!firstTx.txid) {
410
+ console.log('First transaction failed')
411
+ return
412
+ }
413
+
414
+ console.log(`First transaction: ${firstTx.txid}`)
415
+
416
+ // Wait a moment for the transaction to be processed
417
+ await new Promise(resolve => setTimeout(resolve, 2000))
418
+
419
+ // Second transaction - demonstrates chaining with sendWith option
420
+ // Note: We use automatic input selection rather than manual specification
421
+ // Manual input specification requires unlockingScript and unlockingScriptLength
422
+ // which adds complexity for tutorial purposes
423
+ const secondTx = await wallet.createAction({
424
+ description: 'Chain continuation transaction',
425
+ outputs: [
426
+ {
427
+ satoshis: 150,
428
+ lockingScript: '006a0c436861696e20636f6e74696e756174696f6e', // OP_RETURN "Chain continuation"
429
+ outputDescription: 'Chain continuation output',
430
+ basket: 'chain-demo',
431
+ tags: ['chain', 'step-2']
432
+ }
433
+ ],
434
+ options: {
435
+ sendWith: [firstTx.txid] // Ensure this transaction is sent with the first one
436
+ }
437
+ })
438
+
439
+ if (secondTx.txid) {
440
+ console.log(`Second transaction: ${secondTx.txid}`)
441
+ console.log('Transaction chain completed successfully')
442
+ }
443
+
444
+ } catch (error: unknown) {
445
+ console.error('Error in transaction chain:', error)
446
+ }
447
+ }
448
+
449
+ createTransactionChain().catch(console.error)
450
+ ```
451
+
452
+
453
+ ## Performance Optimization Strategies
454
+
455
+ For high-throughput applications, consider these optimization techniques:
456
+
457
+ ```typescript
458
+ import { WalletClient } from '@bsv/sdk'
459
+
460
+ async function optimizedTransactionPatterns() {
461
+ try {
462
+ const wallet = new WalletClient('auto', 'localhost')
463
+
464
+ const { authenticated } = await wallet.isAuthenticated()
465
+ if (!authenticated) {
466
+ await wallet.waitForAuthentication()
467
+ }
468
+
469
+ // Strategy 1: Pre-allocate UTXOs for different purposes
470
+ console.log('Pre-allocating UTXOs for optimal performance...')
471
+
472
+ const allocationTx = await wallet.createAction({
473
+ description: 'UTXO pre-allocation for performance',
474
+ outputs: [
475
+ // Create multiple small UTXOs for frequent operations
476
+ ...Array(5).fill(null).map((_, i) => ({
477
+ satoshis: 200,
478
+ lockingScript: `006a0f507265616c6c6f636174656420${i.toString(16).padStart(2, '0')}`, // "Preallocated" + index
479
+ outputDescription: `Pre-allocated UTXO ${i + 1}`,
480
+ basket: 'pre-allocated',
481
+ tags: ['performance', 'ready-to-use']
482
+ }))
483
+ ],
484
+ options: {
485
+ acceptDelayedBroadcast: true
486
+ }
487
+ })
488
+
489
+ if (allocationTx.txid) {
490
+ console.log(`Pre-allocation transaction: ${allocationTx.txid}`)
491
+ }
492
+
493
+ // Strategy 2: Batch multiple operations efficiently
494
+ await new Promise(resolve => setTimeout(resolve, 3000))
495
+
496
+ const operations = [
497
+ { type: 'data', content: 'Operation 1' },
498
+ { type: 'data', content: 'Operation 2' },
499
+ { type: 'data', content: 'Operation 3' }
500
+ ]
501
+
502
+ const batchOutputs = operations.map((op, index) => {
503
+ const dataHex = Buffer.from(op.content).toString('hex')
504
+ const dataLength = Buffer.from(op.content).length
505
+ const dataLengthHex = dataLength.toString(16).padStart(2, '0')
506
+
507
+ return {
508
+ satoshis: 100,
509
+ lockingScript: `006a${dataLengthHex}${dataHex}`,
510
+ outputDescription: `Batch operation ${index + 1}`,
511
+ basket: 'operations',
512
+ tags: ['batch', `op-${index + 1}`]
513
+ }
514
+ })
515
+
516
+ // Use pre-allocated UTXOs for the batch
517
+ const { outputs: preAllocated } = await wallet.listOutputs({
518
+ basket: 'pre-allocated',
519
+ limit: 3
520
+ })
521
+
522
+ console.log(`Found ${preAllocated.length} pre-allocated UTXOs`)
523
+
524
+ if (preAllocated.length > 0) {
525
+ // Let the wallet automatically select inputs instead of manual specification
526
+ const batchTx = await wallet.createAction({
527
+ description: 'Optimized batch operation',
528
+ outputs: batchOutputs,
529
+ options: {
530
+ randomizeOutputs: false, // Maintain order for processing
531
+ acceptDelayedBroadcast: true
532
+ }
533
+ })
534
+
535
+ if (batchTx.txid) {
536
+ console.log(`Optimized batch transaction: ${batchTx.txid}`)
537
+ console.log(`Processed ${operations.length} operations efficiently`)
538
+ }
539
+ }
540
+
541
+ } catch (error: unknown) {
542
+ console.error('Optimization error:', error)
543
+ }
544
+ }
545
+
546
+ optimizedTransactionPatterns().catch(console.error)
547
+ ```
548
+
549
+
550
+
551
+ ## Conclusion
552
+
553
+ You've now mastered advanced transaction construction with the BSV TypeScript SDK's `WalletClient`. You can:
554
+
555
+ - Create sophisticated multi-input/multi-output transactions
556
+ - Implement robust error handling and retry strategies
557
+ - Optimize transaction patterns for performance
558
+ - Use advanced `WalletClient` features like baskets, tags, and options
559
+ - Handle complex scenarios like transaction chaining and batch processing
560
+
561
+ These techniques enable you to build production-ready applications that efficiently manage Bitcoin transactions while maintaining reliability and performance.
562
+
563
+ ## Next Steps
564
+
565
+ - Review the [Transaction Signing Methods](../guides/transaction-signing-methods.md) for custom signing scenarios
566
+ - Check out specialized tutorials for your specific use case
567
+
568
+ - Advanced transaction construction requires robust error handling for production applications. For comprehensive coverage of error handling patterns, retry mechanisms, and recovery strategies, see the dedicated [Error Handling Tutorial](./error-handling.md).
569
+
570
+
571
+ ## Additional Resources
572
+
573
+ - [Wallet Reference](../reference/wallet.md)
574
+ - [BSV Blockchain Documentation](https://docs.bsvblockchain.org/)
575
+ - [MetaNet Desktop Wallet](https://metanet.bsvb.tech/)