@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,359 +0,0 @@
1
- # Working with Testnet Transactions
2
-
3
- **Duration**: 30 minutes
4
- **Prerequisites**: Completed "Your First BSV Transaction" tutorial, Node.js, basic TypeScript knowledge
5
-
6
- ## Learning Goals
7
-
8
- - Set up a BSV testnet environment
9
- - Obtain and manage testnet coins
10
- - Create and broadcast real transactions on the testnet
11
- - Track and verify transaction confirmations
12
- - Configure `WalletClient` for different environments
13
-
14
- ## Introduction
15
-
16
- While the previous tutorial taught you the basics of creating transaction structures, this tutorial takes you to the next level: creating and broadcasting actual transactions on the Bitcoin SV testnet. The testnet is a separate blockchain that functions almost identically to the main BSV network (mainnet), but with coins that have no real-world value, making it perfect for experimentation and learning.
17
-
18
- ## Step 1: Understanding Testnet vs Mainnet
19
-
20
- Before we start, it's important to understand the difference between testnet and mainnet:
21
-
22
- | Feature | Testnet | Mainnet |
23
- |---------|---------|--------|
24
- | Purpose | Testing and development | Real-world transactions |
25
- | Coin value | None (free from faucets) | Real monetary value |
26
- | Network prefix | Different from mainnet | Production network |
27
- | Block difficulty | Lower than mainnet | Higher mining difficulty |
28
- | Explorer URLs | Testnet-specific | Mainnet-specific |
29
-
30
- The code you write for testnet can usually work on mainnet with minimal changes (primarily network configuration), making testnet ideal for development and testing.
31
-
32
- ## Step 2: Setting Up a Testnet Wallet
33
-
34
- First, let's create a new project for our testnet experiments:
35
-
36
- ```bash
37
- # Create a new directory for your testnet project
38
- mkdir bsv-testnet-transactions
39
- cd bsv-testnet-transactions
40
-
41
- # Initialize a new Node.js project
42
- npm init -y
43
-
44
- # Install TypeScript and ts-node
45
- npm install typescript ts-node @types/node --save-dev
46
-
47
- # Install the BSV SDK
48
- npm install @bsv/sdk
49
- ```
50
-
51
- Now, create a basic TypeScript configuration file (`tsconfig.json`):
52
-
53
- ```json
54
- {
55
- "compilerOptions": {
56
- "target": "es2022",
57
- "module": "commonjs",
58
- "esModuleInterop": true,
59
- "strict": true,
60
- "outDir": "./dist"
61
- }
62
- }
63
- ```
64
-
65
- Let's create a file to generate a testnet wallet. Create a new file called `create-wallet.ts`:
66
-
67
- ```typescript
68
- import {
69
- PrivateKey,
70
- P2PKH
71
- } from '@bsv/sdk'
72
-
73
- // Create a new private key
74
- const privateKey = PrivateKey.fromRandom()
75
-
76
- // Derive the public key and create a testnet address
77
- // Use the testnet prefix (0x6f) for the address
78
- const address = privateKey.toAddress([0x6f])
79
-
80
- // Display the information
81
- console.log('===== TESTNET WALLET =====')
82
- console.log(`Address: ${address.toString()}`)
83
- console.log(`Private Key (WIF): ${privateKey.toWif()}`)
84
- console.log('\nIMPORTANT: Save this information securely. You will need the private key to spend any received coins.')
85
- console.log('=============================')
86
- ```
87
-
88
- Run this script to generate your testnet wallet:
89
-
90
- ```bash
91
- npx ts-node create-wallet.ts
92
- ```
93
-
94
- You should see output similar to this:
95
-
96
- ```
97
- ===== TESTNET WALLET =====
98
- Address: mzJR1zKcZCZvMJj87PUeQPPXRi3DEpyEDp
99
- Private Key (WIF): cQcTnoUm6hQCSoGDQQiGfRxLEbsB6Sm3DnQPDrJdHZrP6ufuuJyp
100
-
101
- IMPORTANT: Save this information securely. You will need the private key to spend any received coins.
102
- =============================
103
- ```
104
-
105
- **Important**: Save the displayed address and private key somewhere secure. You'll need them for the rest of this tutorial.
106
-
107
- ## Step 3: Getting Testnet Coins
108
-
109
- Now that you have a testnet address, you need to get some testnet coins. Unlike mainnet, testnet coins are free and can be obtained from "faucets" - services that distribute testnet coins for development purposes.
110
-
111
- Here are some BSV testnet faucets you can use:
112
-
113
- - [Scrypt.io Testnet Faucet](https://scrypt.io/faucet)
114
- - [BSV Testnet Faucet](https://bsvfaucet.org)
115
-
116
- Visit one of these faucets, enter your testnet address, and request some coins. The faucet will send a small amount of testnet BSV to your address.
117
-
118
- ## Step 4: Verifying Your Balance and Finding UTXO Information
119
-
120
- After requesting coins from a faucet, you'll need to verify that you received them and gather information about the UTXO (Unspent Transaction Output) you're going to spend. You can use a testnet block explorer for this purpose:
121
-
122
- - [WhatsOnChain Testnet](https://test.whatsonchain.com/)
123
- - [BitcoinSV Testnet Explorer](https://testnet.bitcoincloud.net/)
124
-
125
- ### Using WhatsOnChain Testnet Explorer
126
-
127
- 1. Go to [WhatsOnChain Testnet Explorer](https://test.whatsonchain.com/)
128
-
129
- 2. Enter your testnet address in the search bar at the top and click the search icon
130
-
131
- 3. You'll see your address details page with any transactions listed. Verify that you have received coins from the faucet. It may take a few minutes for the transaction to be confirmed.
132
-
133
- 4. Once you confirm that you've received coins with at least one confirmed transaction (unspent output/UTXO), you can proceed to gather the information needed for your transaction.
134
-
135
- 5. Look for the incoming transaction from the faucet
136
-
137
- 6. Click on the transaction ID (txid) to view the full transaction details
138
-
139
- 7. On the transaction details page, find the following information:
140
-
141
- - **Transaction ID (txid)**: This is the long hexadecimal string at the top of the page (e.g., `7f4e6ea49a847f557fccd9bf99d4a07ac103e5e8cb3464abb852af552516317e`)
142
- - **Output Index**: In the "Outputs" section, find your address and note its index number (0-based). If your address is the first output, the index is 0.
143
- - **Output Amount**: Note the amount sent to your address in this specific output. WhatsOnChain displays amounts in BSV (e.g., 0.00010000 BSV), but our code needs satoshis. To convert:
144
-
145
- - 1 BSV = 100,000,000 satoshis
146
- - Example: 0.00010000 BSV = 10,000 satoshis (multiply by 100,000,000)
147
- - You can use a calculator or simply move the decimal point 8 places to the right
148
-
149
- 8. Write down or copy these three pieces of information:
150
-
151
- ```
152
- Transaction ID (txid): [your transaction id]
153
- Output Index: [your output index, usually 0]
154
- Output Amount: [amount in BSV shown on WhatsOnChain] = [converted amount in satoshis]
155
- Example: 0.00010000 BSV = 10000 satoshis
156
- ```
157
-
158
- > **Important**: Make sure you're looking at an *unspent* output. If the coins have already been spent, you won't be able to use them in your transaction. WhatsOnChain typically shows if an output has been spent.
159
-
160
- Write down this information as you'll need it for the next step.
161
-
162
- ## Step 5: Creating a Transaction with UTXOs
163
-
164
- ### Understanding UTXO Consumption
165
-
166
- When you spend a UTXO in Bitcoin, the **entire UTXO** must be consumed. You can think of a UTXO like a bill in your wallet - if you want to spend a $20 bill, you must use the entire bill, not just part of it. If you only want to spend $5, you'll spend the entire $20 bill and get $15 back as change.
167
-
168
- In the same way, when creating a Bitcoin transaction:
169
-
170
- 1. **You spend the entire UTXO** (even if you only want to send a portion of it)
171
- 2. **You specify how to distribute those funds**:
172
-
173
- - Some goes to the recipient (the payment)
174
- - Some goes to the miners (the transaction fee)
175
- - The remainder comes back to you (the change)
176
-
177
- ### Prerequisites
178
-
179
- #### Taal API Key for Broadcasting
180
-
181
- To broadcast transactions via the ARC API, you'll need a Taal API key:
182
-
183
- 1. Sign up for an account at [https://console.taal.com](https://console.taal.com)
184
- 2. Once logged in, navigate to the API keys section
185
- 3. Create a new API key for your application
186
- 4. Copy the generated API key - you'll need it for the transaction broadcasting step
187
-
188
- ### Implementation
189
-
190
- Create a file `send-transaction.ts`:
191
-
192
- ```typescript
193
- import { Transaction, PrivateKey, P2PKH, ARC, NodejsHttpClient } from '@bsv/sdk'
194
- import https from 'https'
195
-
196
- async function main() {
197
- try {
198
- // Step 1: Set up your wallet
199
- const privateKey = PrivateKey.fromWif('your_testnet_private_key_here')
200
- const myAddress = privateKey.toAddress([0x6f]) // 0x6f is the testnet prefix
201
- const recipientAddress = 'testnet_address_to_send_coins_to'
202
-
203
- // Step 2: Fetch the full transaction hex
204
- const txid = 'source_transaction_id_here'
205
- const response = await fetch(`https://api.whatsonchain.com/v1/bsv/test/tx/${txid}/hex`)
206
- const sourceTxHex = await response.text()
207
- console.log(`Retrieved transaction hex (first 50 chars): ${sourceTxHex.substring(0, 50)}...`)
208
-
209
- // Step 3: Create a transaction
210
- const tx = new Transaction()
211
-
212
- // Step 4: Add the input
213
- // For testnet, we need the hex of the transaction that contains our UTXO
214
- tx.addInput({
215
- sourceTransaction: Transaction.fromHex(sourceTxHex),
216
- sourceOutputIndex: 0, // The output index from Step 4 (typically 0)
217
- unlockingScriptTemplate: new P2PKH().unlock(privateKey)
218
- })
219
-
220
- // Step 5: Add the recipient output
221
- tx.addOutput({
222
- lockingScript: new P2PKH().lock(recipientAddress),
223
- satoshis: 100 // Amount to send (must be less than input amount)
224
- })
225
-
226
- // Step 6: Add the change output back to our address
227
- tx.addOutput({
228
- lockingScript: new P2PKH().lock(myAddress),
229
- change: true // SDK will automatically calculate the change amount
230
- })
231
-
232
- // Step 7: Calculate fee and sign the transaction
233
- await tx.fee()
234
- await tx.sign()
235
-
236
- // Step 8: Broadcast the transaction to the testnet using ARC
237
- // You need to provide your Taal API key here
238
- // Get it by signing up at https://console.taal.com
239
- const apiKey = 'your_taal_api_key_here' // Replace with your actual API key
240
-
241
- // Create an HTTP client to ensure the transaction can be broadcast
242
- const httpClient = new NodejsHttpClient(https)
243
-
244
- // The SDK automatically appends '/v1/tx' to the base URL when broadcasting
245
- // so we need to use the base URL without the '/arc' suffix
246
- const arc = new ARC('https://arc-test.taal.com', {
247
- apiKey,
248
- httpClient, // Provide an HTTP client to avoid connectivity issues
249
- deploymentId: 'testnet-tutorial-deployment' // Provide a fixed deployment ID to avoid random generation
250
- })
251
- const result = await tx.broadcast(arc)
252
- console.log('ARC Response:', JSON.stringify(result, null, 2)) // Log the full response for debugging
253
-
254
- // Step 9: Display the transaction ID
255
- console.log(`Transaction ID: ${Buffer.from(tx.id()).toString('hex')}`)
256
- console.log(`View on explorer: https://test.whatsonchain.com/tx/${Buffer.from(tx.id()).toString('hex')}`)
257
- console.log('Transaction broadcast successfully!')
258
- } catch (error) {
259
- console.error('Error:', error)
260
- }
261
- }
262
-
263
- // Run the main function
264
- main()
265
- ```
266
-
267
- Before running this script, make sure to replace these values in the code:
268
-
269
- 1. `your_testnet_private_key_here`: Your testnet private key (WIF format) from Step 2
270
- 2. `testnet_address_to_send_coins_to`: The recipient's testnet address - for the purpose of this tutorial, use your own address
271
- 3. `source_transaction_id_here`: The ID of the transaction containing your UTXO
272
- 4. Update the `sourceOutputIndex` value if your output index is not 0
273
- 5. Adjust the recipient output `satoshis` value (currently 100) to be less than your input amount
274
-
275
- Once you've made these changes, run the script:
276
-
277
- ```bash
278
- npx ts-node send-transaction.ts
279
- ```
280
-
281
- ## Step 6: Tracking Your Transaction
282
-
283
- After broadcasting your transaction, you can track its progress using a testnet explorer (note - it can take a few seconds for the transaction to show up). Our code already outputs the transaction ID and a direct link to view it on WhatsOnChain:
284
-
285
- ```typescript
286
- console.log(`Transaction ID: ${Buffer.from(tx.id()).toString('hex')}`)
287
- console.log(`View on explorer: https://test.whatsonchain.com/tx/${Buffer.from(tx.id()).toString('hex')}`)
288
- ```
289
-
290
- Simply click the link or copy the transaction ID to search for it on [WhatsOnChain Testnet](https://test.whatsonchain.com/) or another testnet explorer.
291
-
292
- Initially, your transaction will be unconfirmed. Once it's included in a block, it will show as confirmed. On testnet, confirmations typically happen faster than on mainnet due to lower mining difficulty.
293
-
294
- ## Step 7: Understanding Transaction Fees
295
-
296
- Transaction fees are paid to miners to include your transaction in a block. In our simplified code example, we're using the BSV SDK's automatic fee calculation with:
297
-
298
- ```typescript
299
- await tx.fee()
300
- ```
301
-
302
- This method automatically calculates the appropriate fee based on:
303
-
304
- - The size of your transaction (in bytes)
305
- - Current network fee rates
306
- - Number of inputs and outputs
307
-
308
- The `change: true` parameter in our change output works with the fee calculation to:
309
-
310
- 1. Calculate the appropriate fee amount
311
- 2. Subtract that fee from the total input amount
312
- 3. Allocate the remaining balance to the change address
313
-
314
- This approach ensures your transaction pays an appropriate fee based on its size and current network conditions, without needing to manually calculate fees and change amounts.
315
-
316
- ## Conclusion
317
-
318
- Congratulations! You've successfully created, signed, and broadcast a real transaction on the Bitcoin SV testnet. In this tutorial, you've learned:
319
-
320
- - How to set up a testnet wallet
321
- - How to obtain testnet coins from a faucet
322
- - How to find and use UTXO information
323
- - How to create, sign, and broadcast a real transaction
324
- - How to track transaction confirmations
325
- - How to work with transaction fees
326
-
327
- This practical experience with testnet transactions provides a solid foundation for working with real BSV transactions on the mainnet. With these skills, you're ready to move on to more advanced topics in the BSV TypeScript SDK.
328
-
329
- ## Next Steps
330
-
331
- - Learn about [Key Management and Cryptography](./key-management.md)
332
- - Explore [Advanced Transaction Construction](./advanced-transaction.md)
333
- - Discover how to implement [Multi-signature Transactions](../guides/multisig-transactions.md)
334
-
335
- ## Additional Resources
336
-
337
- - [BSV Testnet Faucet List](https://wiki.bitcoinsv.io/index.php/Testnet)
338
- - [WhatsOnChain Testnet API Documentation](https://developers.whatsonchain.com/#introduction)
339
- - [BSV Transaction Reference](../reference/transaction.md)
340
-
341
- ## Low-Level vs `WalletClient` Approach
342
-
343
- | Feature | Low-Level | `WalletClient` |
344
- |---------|---------|--------|
345
- | Control | Full control over transaction structure | Simplified, high-level interface |
346
- | Complexity | Requires manual UTXO management and fee calculation | Handles UTXO management and fee calculation automatically |
347
- | Use Cases | Advanced applications, custom transaction logic | Most production applications, simple transaction workflows |
348
-
349
- The `WalletClient` approach is generally recommended for production applications, while the low-level approach is valuable for:
350
-
351
- - Advanced applications requiring custom transaction logic
352
- - Educational purposes, to understand the underlying mechanics of Bitcoin transactions
353
- - Specialized use cases where fine-grained control is necessary
354
-
355
- Understanding both approaches will help you choose the best method for your specific use case and provide a deeper understanding of the Bitcoin SV ecosystem.
356
-
357
- This tutorial focuses on low-level transaction construction, which gives you complete control over every aspect of the transaction. For simpler applications, consider using the `WalletClient` approach covered in other tutorials.
358
-
359
- > **Alternative Approach**: For most applications, the `WalletClient` interface provides a simpler way to create transactions. This tutorial focuses on the low-level approach for educational purposes and specialized use cases.