@bsv/sdk 1.9.2 → 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 (93) hide show
  1. package/dist/cjs/package.json +1 -1
  2. package/dist/cjs/src/kvstore/GlobalKVStore.js +116 -98
  3. package/dist/cjs/src/kvstore/GlobalKVStore.js.map +1 -1
  4. package/dist/cjs/src/kvstore/types.js.map +1 -1
  5. package/dist/cjs/src/overlay-tools/index.js +1 -0
  6. package/dist/cjs/src/overlay-tools/index.js.map +1 -1
  7. package/dist/cjs/src/overlay-tools/withDoubleSpendRetry.js +55 -0
  8. package/dist/cjs/src/overlay-tools/withDoubleSpendRetry.js.map +1 -0
  9. package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -1
  10. package/dist/esm/src/kvstore/GlobalKVStore.js +117 -99
  11. package/dist/esm/src/kvstore/GlobalKVStore.js.map +1 -1
  12. package/dist/esm/src/kvstore/types.js.map +1 -1
  13. package/dist/esm/src/overlay-tools/index.js +1 -0
  14. package/dist/esm/src/overlay-tools/index.js.map +1 -1
  15. package/dist/esm/src/overlay-tools/withDoubleSpendRetry.js +48 -0
  16. package/dist/esm/src/overlay-tools/withDoubleSpendRetry.js.map +1 -0
  17. package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
  18. package/dist/types/src/kvstore/GlobalKVStore.d.ts.map +1 -1
  19. package/dist/types/src/kvstore/types.d.ts +2 -0
  20. package/dist/types/src/kvstore/types.d.ts.map +1 -1
  21. package/dist/types/src/overlay-tools/index.d.ts +1 -0
  22. package/dist/types/src/overlay-tools/index.d.ts.map +1 -1
  23. package/dist/types/src/overlay-tools/withDoubleSpendRetry.d.ts +14 -0
  24. package/dist/types/src/overlay-tools/withDoubleSpendRetry.d.ts.map +1 -0
  25. package/dist/types/tsconfig.types.tsbuildinfo +1 -1
  26. package/dist/umd/bundle.js +2 -2
  27. package/dist/umd/bundle.js.map +1 -1
  28. package/docs/fast-docs.png +0 -0
  29. package/docs/index.md +49 -44
  30. package/docs/reference/kvstore.md +9 -0
  31. package/docs/reference/overlay-tools.md +32 -0
  32. package/docs/swagger.png +0 -0
  33. package/package.json +1 -1
  34. package/src/kvstore/GlobalKVStore.ts +134 -114
  35. package/src/kvstore/__tests/GlobalKVStore.test.ts +11 -1
  36. package/src/kvstore/types.ts +2 -0
  37. package/src/overlay-tools/index.ts +1 -0
  38. package/src/overlay-tools/withDoubleSpendRetry.ts +71 -0
  39. package/docs/MARKDOWN_VALIDATION_GUIDE.md +0 -175
  40. package/docs/concepts/beef.md +0 -92
  41. package/docs/concepts/chain-tracking.md +0 -134
  42. package/docs/concepts/decentralized-identity.md +0 -221
  43. package/docs/concepts/fees.md +0 -249
  44. package/docs/concepts/identity-certificates.md +0 -307
  45. package/docs/concepts/index.md +0 -77
  46. package/docs/concepts/key-management.md +0 -185
  47. package/docs/concepts/script-templates.md +0 -176
  48. package/docs/concepts/sdk-philosophy.md +0 -80
  49. package/docs/concepts/signatures.md +0 -194
  50. package/docs/concepts/spv-verification.md +0 -118
  51. package/docs/concepts/transaction-encoding.md +0 -167
  52. package/docs/concepts/transaction-structure.md +0 -67
  53. package/docs/concepts/trust-model.md +0 -139
  54. package/docs/concepts/verification.md +0 -250
  55. package/docs/concepts/wallet-integration.md +0 -101
  56. package/docs/guides/development-wallet-setup.md +0 -374
  57. package/docs/guides/direct-transaction-creation.md +0 -147
  58. package/docs/guides/http-client-configuration.md +0 -488
  59. package/docs/guides/index.md +0 -138
  60. package/docs/guides/large-transactions.md +0 -448
  61. package/docs/guides/multisig-transactions.md +0 -792
  62. package/docs/guides/security-best-practices.md +0 -494
  63. package/docs/guides/transaction-batching.md +0 -132
  64. package/docs/guides/transaction-signing-methods.md +0 -419
  65. package/docs/reference/arc-config.md +0 -698
  66. package/docs/reference/brc-100.md +0 -33
  67. package/docs/reference/configuration.md +0 -835
  68. package/docs/reference/debugging.md +0 -705
  69. package/docs/reference/errors.md +0 -597
  70. package/docs/reference/index.md +0 -111
  71. package/docs/reference/network-config.md +0 -914
  72. package/docs/reference/op-codes.md +0 -325
  73. package/docs/reference/transaction-signatures.md +0 -95
  74. package/docs/tutorials/advanced-transaction.md +0 -572
  75. package/docs/tutorials/aes-encryption.md +0 -949
  76. package/docs/tutorials/authfetch-tutorial.md +0 -986
  77. package/docs/tutorials/ecdh-key-exchange.md +0 -549
  78. package/docs/tutorials/elliptic-curve-fundamentals.md +0 -606
  79. package/docs/tutorials/error-handling.md +0 -1216
  80. package/docs/tutorials/first-transaction-low-level.md +0 -205
  81. package/docs/tutorials/first-transaction.md +0 -275
  82. package/docs/tutorials/hashes-and-hmacs.md +0 -788
  83. package/docs/tutorials/identity-management.md +0 -729
  84. package/docs/tutorials/index.md +0 -219
  85. package/docs/tutorials/key-management.md +0 -538
  86. package/docs/tutorials/protowallet-development.md +0 -743
  87. package/docs/tutorials/script-construction.md +0 -690
  88. package/docs/tutorials/spv-merkle-proofs.md +0 -685
  89. package/docs/tutorials/testnet-transactions-low-level.md +0 -359
  90. package/docs/tutorials/transaction-broadcasting.md +0 -538
  91. package/docs/tutorials/transaction-types.md +0 -420
  92. package/docs/tutorials/type-42.md +0 -568
  93. package/docs/tutorials/uhrp-storage.md +0 -599
@@ -1,420 +0,0 @@
1
- # Transaction Types and Data
2
-
3
- **Duration**: 30 minutes
4
- **Prerequisites**: Completed "Your First BSV Transaction" tutorial, Node.js, basic TypeScript knowledge
5
-
6
- ## Learning Goals
7
-
8
- - Create transactions with multiple outputs
9
- - Add data to transactions
10
- - Work with different output types
11
- - Use advanced `WalletClient` features
12
-
13
- ## Introduction
14
-
15
- In the previous tutorial, you created a simple transaction that sent BSV to a single address. In this tutorial, you'll expand your knowledge by learning how to create more complex transactions using the `WalletClient` interface. You'll learn how to send to multiple recipients, include data in transactions, and explore more advanced `WalletClient` features.
16
-
17
- ## Prerequisites
18
-
19
- - Complete the [Your First BSV Transaction](./first-transaction.md) tutorial
20
- - Have a BRC-100 compliant wallet (such as [MetaNet Desktop Wallet](https://desktop.bsvb.tech/)) installed and configured
21
- - Some BSV in your wallet
22
-
23
- ## Transaction with Multiple Outputs
24
-
25
- One powerful feature of Bitcoin transactions is the ability to send to multiple recipients in a single transaction. Let's create a transaction that sends to two different recipients:
26
-
27
- ```typescript
28
- import { WalletClient } from '@bsv/sdk'
29
-
30
- async function createMultiOutputTransaction() {
31
- try {
32
- // Initialize the WalletClient (use localhost as wallet originator)
33
- const wallet = new WalletClient('auto', 'localhost')
34
-
35
- // Authenticate with the wallet if needed
36
- const { authenticated } = await wallet.isAuthenticated()
37
- if (!authenticated) {
38
- console.log('Please authenticate with your wallet')
39
- await wallet.waitForAuthentication()
40
- }
41
-
42
- // Get network information
43
- const { network } = await wallet.getNetwork()
44
- console.log(`Connected to ${network} network`)
45
-
46
- // Get our own public key for sending back to ourselves
47
- const { publicKey } = await wallet.getPublicKey({ identityKey: true })
48
-
49
- // Create a transaction with multiple outputs
50
- const actionResult = await wallet.createAction({
51
- description: 'Multi-output transaction example',
52
- outputs: [
53
- {
54
- // First output - uses simple OP_RETURN with data
55
- satoshis: 100,
56
- lockingScript: '006a0461626364', // OP_RETURN with data 'abcd'
57
- outputDescription: 'First data output'
58
- },
59
- {
60
- // Second output - also uses OP_RETURN with different data
61
- satoshis: 100,
62
- lockingScript: '006a04656667', // OP_RETURN with data 'efgh'
63
- outputDescription: 'Second data output'
64
- }
65
- ]
66
- })
67
-
68
- console.log('Transaction created:')
69
-
70
- let txReference
71
- let txid
72
-
73
- // If the wallet auto-signed and broadcast the transaction (otherwise, refer to the examples in our first tutorial to see how to explicitly sign it and broadcast it)
74
- if (actionResult.txid) {
75
-
76
- console.log(`Transaction ID: ${actionResult.txid}`)
77
- console.log(`View on explorer: https://whatsonchain.com/tx/${actionResult.txid}`)
78
- console.log('Transaction was automatically signed and broadcast!')
79
- txid = actionResult.txid
80
- }
81
- else {
82
- console.log('Transaction created but no actionable result returned')
83
- }
84
-
85
- console.log(`Transaction ID: ${txid}`)
86
- console.log(`View on explorer: https://whatsonchain.com/tx/${txid}`)
87
-
88
- } catch (error) {
89
- console.error('Error:', error)
90
- }
91
- }
92
-
93
- createMultiOutputTransaction().catch(console.error)
94
- ```
95
-
96
- This transaction creates two outputs, both sending funds back to your own wallet but with different amounts and descriptions. In a real-world scenario, you could send to different recipients by specifying different addresses or public keys.
97
-
98
- ## Adding Data to Transactions
99
-
100
- Another powerful capability of Bitcoin SV is the ability to store data on-chain. Let's create a transaction that includes some data:
101
-
102
- ```typescript
103
- import { WalletClient } from '@bsv/sdk'
104
-
105
- async function createDataTransaction() {
106
- try {
107
- // Initialize the WalletClient
108
- const wallet = new WalletClient('auto', 'localhost')
109
-
110
- // Authenticate with the wallet if needed
111
- const { authenticated } = await wallet.isAuthenticated()
112
- if (!authenticated) {
113
- await wallet.waitForAuthentication()
114
- }
115
-
116
- // Data to store on-chain (in this case, a simple text message)
117
- const message = 'Hello, Bitcoin SV!'
118
-
119
- // Convert the message to a hex string
120
- const messageHex = Buffer.from(message).toString('hex')
121
- console.log(`Message as hex: ${messageHex}`)
122
-
123
- // Create the OP_RETURN script - start with OP_FALSE (00) + OP_RETURN (6a)
124
- // Then add the length of the data as a single byte (hex encoded)
125
- // Our message length is 16 bytes (0x10 in hex)
126
- const dataLength = Buffer.from(message).length
127
- const dataLengthHex = dataLength.toString(16).padStart(2, '0')
128
-
129
- // Complete lockingScript: OP_FALSE + OP_RETURN + data length + data
130
- const lockingScript = `006a${dataLengthHex}${messageHex}`
131
- console.log(`Complete lockingScript: ${lockingScript}`)
132
-
133
- // Create a transaction with an OP_RETURN output containing our data
134
- const actionResult = await wallet.createAction({
135
- description: 'Store data on-chain',
136
- outputs: [
137
- {
138
- satoshis: 100, // Small amount of satoshis
139
- lockingScript: lockingScript, // Dynamic OP_RETURN with our message data
140
- outputDescription: 'Data storage'
141
- }
142
- ]
143
- })
144
-
145
- console.log('Transaction created:')
146
-
147
- let txReference
148
- let txid
149
-
150
- if (actionResult.txid) {
151
- // If the wallet auto-signed and broadcast the transaction
152
- console.log(`Transaction ID: ${actionResult.txid}`)
153
- console.log(`View on explorer: https://whatsonchain.com/tx/${actionResult.txid}`)
154
- console.log('Transaction was automatically signed and broadcast!')
155
- txid = actionResult.txid
156
- }
157
- else {
158
- console.log('Transaction created but no actionable result returned')
159
- }
160
-
161
- console.log(`Transaction ID: ${txid}`)
162
- console.log(`View on explorer: https://whatsonchain.com/tx/${txid}`)
163
- console.log(`Data stored on-chain: "${message}"`)
164
-
165
- } catch (error) {
166
- console.error('Error:', error)
167
- }
168
- }
169
-
170
- createDataTransaction().catch(console.error)
171
- ```
172
-
173
- This transaction includes an OP_RETURN output, which is a special type of output that can contain arbitrary data but cannot be spent. This is a common way to store data on the Bitcoin SV blockchain.
174
-
175
- ## Combining Payment and Data
176
-
177
- You can also combine regular payment outputs with data outputs in the same transaction:
178
-
179
- ```typescript
180
- import { WalletClient } from '@bsv/sdk'
181
-
182
- async function createCombinedTransaction() {
183
- try {
184
- // Initialize the WalletClient
185
- const wallet = new WalletClient('auto', 'localhost')
186
-
187
- // Authenticate with the wallet if needed
188
- const { authenticated } = await wallet.isAuthenticated()
189
- if (!authenticated) {
190
- await wallet.waitForAuthentication()
191
- }
192
-
193
- // Get our own public key for sending back to ourselves
194
- const { publicKey } = await wallet.getPublicKey({ identityKey: true })
195
-
196
- // Message data for our OP_RETURN outputs
197
- const receipt = 'Payment receipt'
198
- const orderRef = 'Order #12345'
199
-
200
- // Convert messages to hex
201
- const receiptHex = Buffer.from(receipt).toString('hex')
202
- const orderHex = Buffer.from(orderRef).toString('hex')
203
-
204
- // Build the OP_RETURN scripts with correct lengths
205
- // Format: OP_FALSE (00) + OP_RETURN (6a) + length byte + data in hex
206
- const receiptLength = Buffer.from(receipt).length
207
- const receiptLengthHex = receiptLength.toString(16).padStart(2, '0') // Ensure even length
208
- const receiptScript = `006a${receiptLengthHex}${receiptHex}`
209
-
210
- const orderLength = Buffer.from(orderRef).length
211
- const orderLengthHex = orderLength.toString(16).padStart(2, '0') // Ensure even length
212
- const orderScript = `006a${orderLengthHex}${orderHex}`
213
-
214
- console.log(`Receipt script: ${receiptScript}`)
215
- console.log(`Order script: ${orderScript}`)
216
-
217
- // Create a transaction with both outputs containing different data
218
- const actionResult = await wallet.createAction({
219
- description: 'Multi-data transaction',
220
- outputs: [
221
- {
222
- // First data output
223
- satoshis: 100,
224
- lockingScript: receiptScript, // OP_RETURN with payment receipt data
225
- outputDescription: 'Payment receipt data'
226
- },
227
- {
228
- // Second data output with different content
229
- satoshis: 100,
230
- lockingScript: orderScript, // OP_RETURN with order reference data
231
- outputDescription: 'Order reference data'
232
- }
233
- ]
234
- })
235
-
236
- console.log('Transaction created:')
237
-
238
- let txReference
239
- let txid
240
-
241
- if (actionResult.txid) {
242
- // If the wallet auto-signed and broadcast the transaction
243
- console.log(`Transaction ID: ${actionResult.txid}`)
244
- console.log(`View on explorer: https://whatsonchain.com/tx/${actionResult.txid}`)
245
- console.log('Transaction was automatically signed and broadcast!')
246
- txid = actionResult.txid
247
- }
248
- else {
249
- console.log('Transaction created but no actionable result returned')
250
- }
251
-
252
- console.log(`Transaction ID: ${txid}`)
253
- console.log(`View on explorer: https://whatsonchain.com/tx/${txid}`)
254
-
255
- } catch (error) {
256
- console.error('Error:', error)
257
- }
258
- }
259
-
260
- createCombinedTransaction().catch(console.error)
261
- ```
262
-
263
- This pattern of combining payments with data is very powerful for business applications, allowing you to create an immutable record of a payment that includes metadata about the purpose of the payment.
264
-
265
- ## Working with Transaction History
266
-
267
- The `WalletClient` also allows you to retrieve your transaction history:
268
-
269
- ```typescript
270
- import { WalletClient } from '@bsv/sdk'
271
-
272
- async function viewTransactionHistory() {
273
- try {
274
- // Initialize the WalletClient
275
- const wallet = new WalletClient('auto', 'localhost')
276
-
277
- // Authenticate with the wallet if needed
278
- const { authenticated } = await wallet.isAuthenticated()
279
- if (!authenticated) {
280
- await wallet.waitForAuthentication()
281
- }
282
-
283
- // Get transaction history
284
- const { totalActions, actions } = await wallet.listActions({
285
- limit: 10, // Limit to the 10 first transactions
286
- offset: 0 // Start from the beginning
287
- })
288
-
289
- console.log(`Found ${totalActions} total transactions`)
290
- console.log('Recent transactions:')
291
-
292
- // Display transaction details
293
- actions.forEach((action, index) => {
294
- console.log(`\nTransaction #${index + 1}:`)
295
- console.log(` TXID: ${action.txid}`)
296
- console.log(` Description: ${action.description}`)
297
- console.log(` Amount: ${action.satoshis} satoshis`)
298
- console.log(` Status: ${action.status}`)
299
- console.log(` Is Outgoing: ${action.isOutgoing ? 'Yes' : 'No'}`)
300
- })
301
-
302
- } catch (error) {
303
- console.error('Error:', error)
304
- }
305
- }
306
-
307
- viewTransactionHistory().catch(console.error)
308
- ```
309
-
310
- This code retrieves and displays your recent transaction history, showing transaction IDs, descriptions, amounts, statuses, and whether each transaction was outgoing or incoming.
311
-
312
- ## Advanced Features: Working with Baskets
313
-
314
- In the BSV SDK, "baskets" are a powerful UTXO management concept that allows you to organize and categorize your unspent transaction outputs (UTXOs) for better control over how they're used in transactions. Think of baskets as labeled containers for your UTXOs. Each basket can have its own purpose - for example, you might have a "savings" basket for long-term storage, a "spending" basket for daily transactions, or special baskets for specific applications or smart contracts.
315
-
316
- Baskets help with:
317
-
318
- - **UTXO organization**: Group outputs for different purposes or applications
319
- - **Transaction optimization**: Control which UTXOs are used for specific transaction types
320
- - **Privacy enhancement**: Segregate UTXOs from different sources or uses
321
- - **Application-specific management**: Maintain dedicated UTXOs for particular applications
322
-
323
- The `WalletClient` provides methods for working with baskets to give you fine-grained control over your UTXO management:
324
-
325
- ```typescript
326
- import { WalletClient } from '@bsv/sdk'
327
-
328
- async function workWithBaskets() {
329
- try {
330
- // Initialize the WalletClient
331
- const wallet = new WalletClient('auto', 'localhost')
332
-
333
- // Authenticate with the wallet if needed
334
- const { authenticated } = await wallet.isAuthenticated()
335
- if (!authenticated) {
336
- await wallet.waitForAuthentication()
337
- }
338
-
339
- // Basket name for our example
340
- const basketName = 'bsv-tutorial'
341
-
342
- // Let's create a transaction that explicitly uses this basket
343
-
344
- console.log('\nCreating a transaction with outputs assigned to our basket...')
345
-
346
- // Get our own public key for the transaction
347
- const { publicKey } = await wallet.getPublicKey({ identityKey: true })
348
-
349
- // Create a transaction with outputs assigned to a specific basket
350
- const actionResult = await wallet.createAction({
351
- description: 'Transaction with basket assignment',
352
- outputs: [
353
- {
354
- satoshis: 100,
355
- lockingScript: '006a0c426173746b657420746573742e', // Simple OP_RETURN
356
- outputDescription: 'Basket demonstration',
357
- basket: basketName // Assign this output to our basket
358
- }
359
- ]
360
- })
361
-
362
- if (actionResult.txid) {
363
- console.log(`Created transaction with ID: ${actionResult.txid}`)
364
- console.log(`Output has been assigned to the "${basketName}" basket`)
365
- }
366
-
367
- // List outputs in the specified basket
368
- const { outputs } = await wallet.listOutputs({
369
- basket: basketName,
370
- include: 'all' // use 'spendable' if you only want to see UTXOs that can be spent
371
- })
372
-
373
- console.log(`Found ${outputs.length} outputs in the "${basketName}" basket`)
374
-
375
- // Display output details
376
- outputs.forEach((output, index) => {
377
- console.log(`\nOutput #${index + 1}:`)
378
- console.log(` Outpoint: ${output.outpoint}`)
379
- console.log(` Value: ${output.satoshis} satoshis`)
380
- console.log(` Spendable: ${output.spendable ? 'Yes' : 'No'}`)
381
- })
382
-
383
- } catch (error) {
384
- console.error('Error:', error)
385
- }
386
- }
387
-
388
- workWithBaskets().catch(console.error)
389
- ```
390
-
391
- > **Important**::
392
- >
393
- > - The `basket` parameter is set at the individual output level, not at the transaction level
394
- > - Each output can be assigned to a different basket if needed
395
- > - The `include` parameter in `listOutputs()` can filter for 'all', 'spendable', or 'unspendable' outputs
396
- > - For a UTXO to appear in a basket, the output must explicitly have a `basket` property when it's created
397
-
398
- ## Conclusion
399
-
400
- Congratulations! You've now learned how to create more complex transactions using the `WalletClient` interface. You can:
401
-
402
- - Send to multiple recipients in a single transaction
403
- - Store data on the blockchain using OP_RETURN outputs
404
- - Combine payments with data in the same transaction
405
- - Retrieve and view your transaction history
406
- - Work with baskets to organize your UTXOs
407
-
408
- These capabilities form the foundation for building sophisticated applications on Bitcoin SV. With the `WalletClient` interface, you can focus on your application logic rather than the low-level details of transaction construction.
409
-
410
- ## Next Steps
411
-
412
- - Learn about [Key Management and Cryptography](./key-management.md)
413
- - Explore [Advanced Transaction Signing](../guides/advanced-transaction-signing.md)
414
- - If you need more control over transaction construction, check out the [Direct Transaction Creation Guide](../guides/direct-transaction-creation.md)
415
-
416
- ## Additional Resources
417
-
418
- - [WalletClient API Reference](../reference/wallet.md#class-walletclient)
419
- - [BSV Transaction Types](https://wiki.bitcoinsv.io/index.php/Bitcoin_Transaction_Types)
420
- - [Data Storage on BSV](https://wiki.bitcoinsv.io/index.php/Data_Insertion_in_Bitcoin)