@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.
- package/dist/cjs/package.json +1 -1
- package/dist/cjs/src/kvstore/GlobalKVStore.js +116 -98
- package/dist/cjs/src/kvstore/GlobalKVStore.js.map +1 -1
- package/dist/cjs/src/kvstore/types.js.map +1 -1
- package/dist/cjs/src/overlay-tools/index.js +1 -0
- package/dist/cjs/src/overlay-tools/index.js.map +1 -1
- package/dist/cjs/src/overlay-tools/withDoubleSpendRetry.js +55 -0
- package/dist/cjs/src/overlay-tools/withDoubleSpendRetry.js.map +1 -0
- package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -1
- package/dist/esm/src/kvstore/GlobalKVStore.js +117 -99
- package/dist/esm/src/kvstore/GlobalKVStore.js.map +1 -1
- package/dist/esm/src/kvstore/types.js.map +1 -1
- package/dist/esm/src/overlay-tools/index.js +1 -0
- package/dist/esm/src/overlay-tools/index.js.map +1 -1
- package/dist/esm/src/overlay-tools/withDoubleSpendRetry.js +48 -0
- package/dist/esm/src/overlay-tools/withDoubleSpendRetry.js.map +1 -0
- package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
- package/dist/types/src/kvstore/GlobalKVStore.d.ts.map +1 -1
- package/dist/types/src/kvstore/types.d.ts +2 -0
- package/dist/types/src/kvstore/types.d.ts.map +1 -1
- package/dist/types/src/overlay-tools/index.d.ts +1 -0
- package/dist/types/src/overlay-tools/index.d.ts.map +1 -1
- package/dist/types/src/overlay-tools/withDoubleSpendRetry.d.ts +14 -0
- package/dist/types/src/overlay-tools/withDoubleSpendRetry.d.ts.map +1 -0
- package/dist/types/tsconfig.types.tsbuildinfo +1 -1
- package/dist/umd/bundle.js +2 -2
- package/dist/umd/bundle.js.map +1 -1
- package/docs/fast-docs.png +0 -0
- package/docs/index.md +49 -44
- package/docs/reference/kvstore.md +9 -0
- package/docs/reference/overlay-tools.md +32 -0
- package/docs/swagger.png +0 -0
- package/package.json +1 -1
- package/src/kvstore/GlobalKVStore.ts +134 -114
- package/src/kvstore/__tests/GlobalKVStore.test.ts +11 -1
- package/src/kvstore/types.ts +2 -0
- package/src/overlay-tools/index.ts +1 -0
- package/src/overlay-tools/withDoubleSpendRetry.ts +71 -0
- package/docs/MARKDOWN_VALIDATION_GUIDE.md +0 -175
- package/docs/concepts/beef.md +0 -92
- package/docs/concepts/chain-tracking.md +0 -134
- package/docs/concepts/decentralized-identity.md +0 -221
- package/docs/concepts/fees.md +0 -249
- package/docs/concepts/identity-certificates.md +0 -307
- package/docs/concepts/index.md +0 -77
- package/docs/concepts/key-management.md +0 -185
- package/docs/concepts/script-templates.md +0 -176
- package/docs/concepts/sdk-philosophy.md +0 -80
- package/docs/concepts/signatures.md +0 -194
- package/docs/concepts/spv-verification.md +0 -118
- package/docs/concepts/transaction-encoding.md +0 -167
- package/docs/concepts/transaction-structure.md +0 -67
- package/docs/concepts/trust-model.md +0 -139
- package/docs/concepts/verification.md +0 -250
- package/docs/concepts/wallet-integration.md +0 -101
- package/docs/guides/development-wallet-setup.md +0 -374
- package/docs/guides/direct-transaction-creation.md +0 -147
- package/docs/guides/http-client-configuration.md +0 -488
- package/docs/guides/index.md +0 -138
- package/docs/guides/large-transactions.md +0 -448
- package/docs/guides/multisig-transactions.md +0 -792
- package/docs/guides/security-best-practices.md +0 -494
- package/docs/guides/transaction-batching.md +0 -132
- package/docs/guides/transaction-signing-methods.md +0 -419
- package/docs/reference/arc-config.md +0 -698
- package/docs/reference/brc-100.md +0 -33
- package/docs/reference/configuration.md +0 -835
- package/docs/reference/debugging.md +0 -705
- package/docs/reference/errors.md +0 -597
- package/docs/reference/index.md +0 -111
- package/docs/reference/network-config.md +0 -914
- package/docs/reference/op-codes.md +0 -325
- package/docs/reference/transaction-signatures.md +0 -95
- package/docs/tutorials/advanced-transaction.md +0 -572
- package/docs/tutorials/aes-encryption.md +0 -949
- package/docs/tutorials/authfetch-tutorial.md +0 -986
- package/docs/tutorials/ecdh-key-exchange.md +0 -549
- package/docs/tutorials/elliptic-curve-fundamentals.md +0 -606
- package/docs/tutorials/error-handling.md +0 -1216
- package/docs/tutorials/first-transaction-low-level.md +0 -205
- package/docs/tutorials/first-transaction.md +0 -275
- package/docs/tutorials/hashes-and-hmacs.md +0 -788
- package/docs/tutorials/identity-management.md +0 -729
- package/docs/tutorials/index.md +0 -219
- package/docs/tutorials/key-management.md +0 -538
- package/docs/tutorials/protowallet-development.md +0 -743
- package/docs/tutorials/script-construction.md +0 -690
- package/docs/tutorials/spv-merkle-proofs.md +0 -685
- package/docs/tutorials/testnet-transactions-low-level.md +0 -359
- package/docs/tutorials/transaction-broadcasting.md +0 -538
- package/docs/tutorials/transaction-types.md +0 -420
- package/docs/tutorials/type-42.md +0 -568
- package/docs/tutorials/uhrp-storage.md +0 -599
|
@@ -1,205 +0,0 @@
|
|
|
1
|
-
# Your First BSV Transaction (Low level)
|
|
2
|
-
|
|
3
|
-
**Duration**: 15 minutes
|
|
4
|
-
**Prerequisites**: Node.js, basic TypeScript knowledge
|
|
5
|
-
|
|
6
|
-
## Learning Goals
|
|
7
|
-
|
|
8
|
-
- Install and configure the BSV TypeScript SDK
|
|
9
|
-
- Create a simple P2PKH transaction
|
|
10
|
-
- Understand BSV transaction anatomy
|
|
11
|
-
|
|
12
|
-
## Introduction
|
|
13
|
-
|
|
14
|
-
In this tutorial, you'll learn how to create your first Bitcoin SV transaction using the TypeScript SDK. By the end, you'll understand the basic components of a BSV transaction and how to construct, sign, and broadcast one.
|
|
15
|
-
|
|
16
|
-
## Step 1: Setting Up Your Environment
|
|
17
|
-
|
|
18
|
-
First, create a new Node.js project and install the BSV SDK:
|
|
19
|
-
|
|
20
|
-
```bash
|
|
21
|
-
# Create a new directory for your project
|
|
22
|
-
mkdir my-first-bsv-tx
|
|
23
|
-
cd my-first-bsv-tx
|
|
24
|
-
|
|
25
|
-
# Initialize a new Node.js project
|
|
26
|
-
npm init -y
|
|
27
|
-
|
|
28
|
-
# Install TypeScript and ts-node (TypeScript execution engine)
|
|
29
|
-
npm install typescript ts-node @types/node --save-dev
|
|
30
|
-
# ts-node allows you to run TypeScript files directly without compiling them first
|
|
31
|
-
|
|
32
|
-
# Install the BSV SDK
|
|
33
|
-
npm install @bsv/sdk
|
|
34
|
-
```
|
|
35
|
-
|
|
36
|
-
Create a basic TypeScript configuration file (`tsconfig.json`):
|
|
37
|
-
|
|
38
|
-
```json
|
|
39
|
-
{
|
|
40
|
-
"compilerOptions": {
|
|
41
|
-
"target": "es2020",
|
|
42
|
-
"module": "commonjs",
|
|
43
|
-
"esModuleInterop": true,
|
|
44
|
-
"strict": true,
|
|
45
|
-
"outDir": "./dist"
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
```
|
|
49
|
-
|
|
50
|
-
## Step 2: Understanding Transaction Components
|
|
51
|
-
|
|
52
|
-
Before we write any code, let's understand the basic components of a Bitcoin transaction:
|
|
53
|
-
|
|
54
|
-
- **Inputs**: References to previous transaction outputs that you're spending
|
|
55
|
-
- **Outputs**: New UTXOs (Unspent Transaction Outputs) that define who receives the bitcoins
|
|
56
|
-
- **Locking Scripts**: Scripts that determine the conditions for spending outputs
|
|
57
|
-
- **Unlocking Scripts**: Scripts that satisfy the conditions in locking scripts
|
|
58
|
-
|
|
59
|
-
### Additional Resources
|
|
60
|
-
|
|
61
|
-
For a deeper understanding of Bitcoin transactions, check out these official resources:
|
|
62
|
-
|
|
63
|
-
- [BSV Wiki: Bitcoin Transaction](https://wiki.bitcoinsv.io/index.php/Bitcoin_Transaction) - Comprehensive explanation of transaction structure
|
|
64
|
-
- [BSV Wiki: Script](https://wiki.bitcoinsv.io/index.php/Script) - Detailed information about Bitcoin Script language
|
|
65
|
-
- [Bitcoin Whitepaper](https://craigwright.net/bitcoin-white-paper.pdf) - Section 5 covers the original design of transactions
|
|
66
|
-
- [BSV Academy: Transactions Course](https://bitcoinsv.academy/course/transactions) - In-depth course on Bitcoin transactions
|
|
67
|
-
|
|
68
|
-
## Step 3: Creating Your First Transaction
|
|
69
|
-
|
|
70
|
-
Create a new file called `first-transaction.ts`:
|
|
71
|
-
|
|
72
|
-
```typescript
|
|
73
|
-
import { PrivateKey, P2PKH, Transaction } from '@bsv/sdk'
|
|
74
|
-
|
|
75
|
-
async function createTransaction() {
|
|
76
|
-
// Create a private key (in production, you'd use proper key management)
|
|
77
|
-
const privateKey = PrivateKey.fromRandom()
|
|
78
|
-
console.log(`Private key WIF: ${privateKey.toWif()}`)
|
|
79
|
-
|
|
80
|
-
// Derive the public key and address
|
|
81
|
-
const address = privateKey.toAddress()
|
|
82
|
-
console.log(`Address: ${address.toString()}`)
|
|
83
|
-
|
|
84
|
-
// For a real transaction, you would have a source UTXO
|
|
85
|
-
// For this example, we'll create a transaction without inputs (cannot be broadcast)
|
|
86
|
-
const tx = new Transaction()
|
|
87
|
-
|
|
88
|
-
// Add an output
|
|
89
|
-
tx.addOutput({
|
|
90
|
-
lockingScript: new P2PKH().lock(address),
|
|
91
|
-
satoshis: 100
|
|
92
|
-
})
|
|
93
|
-
|
|
94
|
-
// Serialize the transaction
|
|
95
|
-
const txHex = tx.toHex() // Use toHex() instead of toString()
|
|
96
|
-
console.log(`Transaction (hex): ${txHex}`)
|
|
97
|
-
|
|
98
|
-
// Get transaction ID as a hex string
|
|
99
|
-
const txid = Buffer.from(tx.id()).toString('hex') // Convert the byte array to hex string
|
|
100
|
-
console.log(`Transaction ID: ${txid}`)
|
|
101
|
-
|
|
102
|
-
// Display the transaction structure
|
|
103
|
-
console.log('\nTransaction Structure:')
|
|
104
|
-
console.log(`Version: ${tx.version}`)
|
|
105
|
-
console.log(`Input Count: ${tx.inputs.length}`)
|
|
106
|
-
console.log(`Output Count: ${tx.outputs.length}`)
|
|
107
|
-
|
|
108
|
-
for (let i = 0; i < tx.outputs.length; i++) {
|
|
109
|
-
const output = tx.outputs[i]
|
|
110
|
-
console.log(`\nOutput #${i}:`)
|
|
111
|
-
console.log(` Satoshis: ${output.satoshis}`)
|
|
112
|
-
console.log(` Locking Script: ${output.lockingScript.toHex()}`)
|
|
113
|
-
console.log(` Locking Script (ASM): ${output.lockingScript.toASM()}`)
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
createTransaction().catch(console.error)
|
|
118
|
-
```
|
|
119
|
-
|
|
120
|
-
## Step 4: Run Your Code
|
|
121
|
-
|
|
122
|
-
Execute your code with the following command:
|
|
123
|
-
|
|
124
|
-
```bash
|
|
125
|
-
npx ts-node first-transaction.ts
|
|
126
|
-
```
|
|
127
|
-
|
|
128
|
-
You should see output showing your private key, address, and transaction details.
|
|
129
|
-
|
|
130
|
-
## Step 5: Next Steps for Real Transactions
|
|
131
|
-
|
|
132
|
-
The transaction we created in the previous step doesn't have any inputs, so it can't be broadcast to the network. It serves as a conceptual introduction to transaction structure.
|
|
133
|
-
|
|
134
|
-
In the next tutorial, "[Working with Testnet Transactions (Low-Level)](./testnet-transactions-low-level.md)", you'll learn how to:
|
|
135
|
-
|
|
136
|
-
1. Create a wallet specifically for the BSV testnet
|
|
137
|
-
2. Obtain free testnet coins from a faucet
|
|
138
|
-
3. Create transactions with real inputs and outputs
|
|
139
|
-
4. Calculate transaction fees automatically
|
|
140
|
-
5. Sign and broadcast real transactions to the testnet network
|
|
141
|
-
|
|
142
|
-
### Preview: Transaction Structure for Real Transactions
|
|
143
|
-
|
|
144
|
-
Here's a simplified preview of what a complete transaction looks like. Don't worry about understanding all the details yet - this is just to give you a sense of the structure you'll work with in the next tutorial:
|
|
145
|
-
|
|
146
|
-
```typescript
|
|
147
|
-
// A complete transaction typically follows this structure:
|
|
148
|
-
const tx = new Transaction()
|
|
149
|
-
|
|
150
|
-
// Add an input (where the money comes from)
|
|
151
|
-
tx.addInput({
|
|
152
|
-
sourceTransaction: /* previous transaction containing your funds */,
|
|
153
|
-
sourceOutputIndex: /* which output from that transaction */,
|
|
154
|
-
unlockingScriptTemplate: /* script to unlock those funds */
|
|
155
|
-
})
|
|
156
|
-
|
|
157
|
-
// Add an output (where the money goes)
|
|
158
|
-
tx.addOutput({
|
|
159
|
-
lockingScript: /* script that locks funds to recipient */,
|
|
160
|
-
satoshis: /* amount to send */
|
|
161
|
-
})
|
|
162
|
-
|
|
163
|
-
// Add change output (remaining funds returned to you)
|
|
164
|
-
tx.addOutput({
|
|
165
|
-
lockingScript: /* script that locks funds to you */,
|
|
166
|
-
change: true // Automatically handles change amount
|
|
167
|
-
})
|
|
168
|
-
|
|
169
|
-
// Calculate fee and sign
|
|
170
|
-
await tx.fee()
|
|
171
|
-
await tx.sign()
|
|
172
|
-
|
|
173
|
-
// Broadcast to the network
|
|
174
|
-
const result = await tx.broadcast()
|
|
175
|
-
```
|
|
176
|
-
|
|
177
|
-
### Understanding Transaction Fees
|
|
178
|
-
|
|
179
|
-
Bitcoin transactions require fees to be included in a block by miners. The BSV SDK simplifies fee calculation with the `fee()` method, which:
|
|
180
|
-
|
|
181
|
-
- Calculates the appropriate fee based on transaction size
|
|
182
|
-
- Works with the `change: true` parameter to automatically handle change outputs
|
|
183
|
-
|
|
184
|
-
You'll get hands-on experience with transaction fees in the testnet tutorial.
|
|
185
|
-
|
|
186
|
-
## Conclusion
|
|
187
|
-
|
|
188
|
-
Congratulations! You've learned the basics of creating a BSV transaction using the TypeScript SDK. In this tutorial, you've:
|
|
189
|
-
|
|
190
|
-
- Set up your development environment
|
|
191
|
-
- Learned about transaction components
|
|
192
|
-
- Created a simple transaction
|
|
193
|
-
- Understood how to structure a complete transaction
|
|
194
|
-
|
|
195
|
-
## Next Steps
|
|
196
|
-
|
|
197
|
-
- Ready to try real transactions? Continue with [Working with Testnet Transactions](./testnet-transactions-low-level.md)
|
|
198
|
-
- Learn about [Key Management and Cryptography](./key-management.md)
|
|
199
|
-
- Explore how to [Broadcast Transactions with ARC](./transaction-broadcasting.md)
|
|
200
|
-
|
|
201
|
-
## Additional Resources
|
|
202
|
-
|
|
203
|
-
- [BSV Transaction Reference](../reference/transaction.md)
|
|
204
|
-
- [Script Reference](../reference/script.md)
|
|
205
|
-
- [Bitcoin Transaction Explorer](https://whatsonchain.com) - To view your transactions on the blockchain
|
|
@@ -1,275 +0,0 @@
|
|
|
1
|
-
# Your First BSV Transaction
|
|
2
|
-
|
|
3
|
-
**Duration**: 15 minutes
|
|
4
|
-
**Prerequisites**: Node.js, basic TypeScript knowledge
|
|
5
|
-
|
|
6
|
-
## Learning Goals
|
|
7
|
-
|
|
8
|
-
- Install and configure the BSV TypeScript SDK
|
|
9
|
-
- Create a simple transaction using `WalletClient` interface on the mainnet network. This approach makes it easy to build transactions by abstracting away many of the low-level details. By the end, you'll understand the basic components of a BSV transaction and how to construct, sign, and broadcast one on the BSV blockchain.
|
|
10
|
-
|
|
11
|
-
> **📚 Related Concepts**: Before starting, you may want to review [Transaction Structure](../concepts/transaction-structure.md) and [Wallet Integration](../concepts/wallet-integration.md) to understand the fundamentals.
|
|
12
|
-
|
|
13
|
-
## Introduction
|
|
14
|
-
|
|
15
|
-
In this tutorial, you'll learn how to create your first Bitcoin SV transactions using the TypeScript SDK's `WalletClient` interface on the mainnet network. This approach makes it easy to build transactions by abstracting away many of the low-level details. By the end, you'll understand the basic components of a BSV transaction and how to construct, sign, and broadcast one on the BSV blockchain.
|
|
16
|
-
|
|
17
|
-
> **💡 Try It Interactive**: Want to experiment with the code examples from this tutorial? Check out our [Interactive BSV Coding Environment](https://fast.brc.dev/) where you can run SDK code directly in your browser without any setup!
|
|
18
|
-
|
|
19
|
-
## Precondition
|
|
20
|
-
|
|
21
|
-
Install a BRC-100 compliant wallet such as the [MetaNet Desktop Wallet](https://desktop.bsvb.tech/). When you install it, you'll receive a small amount of funds to play with.
|
|
22
|
-
|
|
23
|
-
## Step 1: Setting Up Your Environment
|
|
24
|
-
|
|
25
|
-
First, create a new Node.js project and install the BSV SDK:
|
|
26
|
-
|
|
27
|
-
```bash
|
|
28
|
-
# Create a new directory for your project
|
|
29
|
-
mkdir my-first-bsv-tx
|
|
30
|
-
cd my-first-bsv-tx
|
|
31
|
-
|
|
32
|
-
# Initialize a new Node.js project
|
|
33
|
-
npm init -y
|
|
34
|
-
|
|
35
|
-
# Install TypeScript and ts-node (TypeScript execution engine)
|
|
36
|
-
npm install typescript ts-node @types/node --save-dev
|
|
37
|
-
# ts-node allows you to run TypeScript files directly without compiling them first
|
|
38
|
-
|
|
39
|
-
# Install the BSV SDK
|
|
40
|
-
npm install @bsv/sdk
|
|
41
|
-
```
|
|
42
|
-
|
|
43
|
-
Create a basic TypeScript configuration file (`tsconfig.json`):
|
|
44
|
-
|
|
45
|
-
```json
|
|
46
|
-
{
|
|
47
|
-
"compilerOptions": {
|
|
48
|
-
"target": "es2020",
|
|
49
|
-
"module": "commonjs",
|
|
50
|
-
"esModuleInterop": true,
|
|
51
|
-
"strict": true,
|
|
52
|
-
"outDir": "./dist"
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
```
|
|
56
|
-
|
|
57
|
-
## Step 2: Understanding Transaction Components
|
|
58
|
-
|
|
59
|
-
Before we write any code, let's understand the basic components of a Bitcoin transaction:
|
|
60
|
-
|
|
61
|
-
- **Inputs**: References to previous transaction outputs that you're spending
|
|
62
|
-
- **Outputs**: New UTXOs (Unspent Transaction Outputs) that define who receives the bitcoins
|
|
63
|
-
- **Locking Scripts**: Scripts that determine the conditions for spending outputs
|
|
64
|
-
- **Unlocking Scripts**: Scripts that satisfy the conditions in locking scripts
|
|
65
|
-
|
|
66
|
-
### Additional Resources
|
|
67
|
-
|
|
68
|
-
For a deeper understanding of Bitcoin transactions, check out these resources:
|
|
69
|
-
|
|
70
|
-
- [Bitcoin SV Protocol Specifications](https://github.com/bitcoin-sv-specs/protocol) - Official Bitcoin SV protocol specifications
|
|
71
|
-
- [WhatsOnChain Explorer](https://whatsonchain.com/) - Standard block explorer for viewing BSV transactions
|
|
72
|
-
- [Bitcoin Developer Reference](https://developer.bitcoin.org/reference/transactions.html) - Comprehensive explanation of transaction structure
|
|
73
|
-
|
|
74
|
-
## Step 3: Your First Transactions - 3 Simple Steps
|
|
75
|
-
|
|
76
|
-
Now let's create your first BSV transactions using three clean, focused steps. Each step builds on the previous one, showing you the core workflow of BSV development.
|
|
77
|
-
|
|
78
|
-
We'll create three separate files to keep things organized and clear:
|
|
79
|
-
|
|
80
|
-
### Example 1: Create a Simple Transaction
|
|
81
|
-
|
|
82
|
-
**What you'll do**: Create your first transaction that stores a simple message on the BSV blockchain. This introduces you to the basic `createAction()` method and shows how data can be permanently stored on-chain.
|
|
83
|
-
|
|
84
|
-
Create a file called `step1-simple-transaction.ts`:
|
|
85
|
-
|
|
86
|
-
```typescript
|
|
87
|
-
import { WalletClient, Script } from '@bsv/sdk'
|
|
88
|
-
|
|
89
|
-
async function createSimpleTransaction() {
|
|
90
|
-
// Connect to user's wallet
|
|
91
|
-
const wallet = new WalletClient('auto', 'localhost')
|
|
92
|
-
|
|
93
|
-
// Create a simple transaction with a data output
|
|
94
|
-
const response = await wallet.createAction({
|
|
95
|
-
description: 'My first BSV transaction',
|
|
96
|
-
outputs: [{
|
|
97
|
-
satoshis: 100,
|
|
98
|
-
lockingScript: Script.fromASM(`OP_RETURN ${Buffer.from('Hello BSV!').toString('hex')}`).toHex(),
|
|
99
|
-
outputDescription: 'My first data output'
|
|
100
|
-
}]
|
|
101
|
-
})
|
|
102
|
-
|
|
103
|
-
console.log('Transaction created:', response)
|
|
104
|
-
if (response.txid) {
|
|
105
|
-
console.log(`View on WhatsOnChain: https://whatsonchain.com/tx/${response.txid}`)
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
return response
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
// Run the function
|
|
112
|
-
createSimpleTransaction().catch(console.error)
|
|
113
|
-
```
|
|
114
|
-
|
|
115
|
-
Run it as follows:
|
|
116
|
-
|
|
117
|
-
```bash
|
|
118
|
-
# Run the TypeScript file directly
|
|
119
|
-
npx ts-node step1-simple-transaction.ts
|
|
120
|
-
```
|
|
121
|
-
|
|
122
|
-
**What's happening here:**
|
|
123
|
-
|
|
124
|
-
- We connect to your BRC-100 wallet (like MetaNet Desktop)
|
|
125
|
-
- Create a transaction with one output containing "Hello BSV!" data
|
|
126
|
-
- The wallet automatically handles inputs, change, and fees
|
|
127
|
-
- We get a transaction ID to view on the blockchain explorer
|
|
128
|
-
|
|
129
|
-
### Example 2: Create and Store a Token
|
|
130
|
-
|
|
131
|
-
**What you'll do**: Create a spendable token and organize it using wallet baskets. This shows you how to create UTXOs that can be spent later and how to use the wallet's organizational features.
|
|
132
|
-
|
|
133
|
-
Create a file called `step2-create-token.ts`:
|
|
134
|
-
|
|
135
|
-
```typescript
|
|
136
|
-
import { WalletClient, Script } from '@bsv/sdk'
|
|
137
|
-
|
|
138
|
-
async function createToken() {
|
|
139
|
-
// Connect to user's wallet
|
|
140
|
-
const wallet = new WalletClient('auto', 'localhost')
|
|
141
|
-
|
|
142
|
-
// Create a token and store it in a specific basket
|
|
143
|
-
const response = await wallet.createAction({
|
|
144
|
-
description: 'Create my first token',
|
|
145
|
-
outputs: [{
|
|
146
|
-
satoshis: 1,
|
|
147
|
-
lockingScript: Script.fromASM('OP_NOP').toHex(),
|
|
148
|
-
basket: 'my-tokens',
|
|
149
|
-
outputDescription: 'My first token'
|
|
150
|
-
}]
|
|
151
|
-
})
|
|
152
|
-
|
|
153
|
-
console.log('Token created:', response)
|
|
154
|
-
if (response.txid) {
|
|
155
|
-
console.log(`Token transaction: https://whatsonchain.com/tx/${response.txid}`)
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
return response
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
// Run the function
|
|
162
|
-
createToken().catch(console.error)
|
|
163
|
-
```
|
|
164
|
-
|
|
165
|
-
Run it as follows:
|
|
166
|
-
|
|
167
|
-
```bash
|
|
168
|
-
# Run the TypeScript file directly
|
|
169
|
-
npx ts-node step2-create-token.ts
|
|
170
|
-
```
|
|
171
|
-
|
|
172
|
-
**What's happening here:**
|
|
173
|
-
|
|
174
|
-
- We create a minimal token (1 satoshi with OP_NOP script)
|
|
175
|
-
- Store it in a wallet basket called 'my-tokens' for organization
|
|
176
|
-
- This creates a spendable output we can use later
|
|
177
|
-
|
|
178
|
-
### Example 3: List and Spend Your Token
|
|
179
|
-
|
|
180
|
-
**What you'll do**: Retrieve your stored token (created in the previous example) and spend it in a new transaction. This demonstrates the complete UTXO lifecycle and shows how to work with transaction inputs using the BEEF format.
|
|
181
|
-
|
|
182
|
-
Create a file called `step3-spend-token.ts`:
|
|
183
|
-
|
|
184
|
-
```typescript
|
|
185
|
-
import { WalletClient, Script } from '@bsv/sdk'
|
|
186
|
-
|
|
187
|
-
async function spendToken() {
|
|
188
|
-
// Connect to user's wallet
|
|
189
|
-
const wallet = new WalletClient('auto', 'localhost')
|
|
190
|
-
|
|
191
|
-
// First, list our tokens
|
|
192
|
-
const tokenList = await wallet.listOutputs({
|
|
193
|
-
basket: 'my-tokens',
|
|
194
|
-
include: 'entire transactions'
|
|
195
|
-
})
|
|
196
|
-
|
|
197
|
-
console.log('Available tokens:', tokenList.outputs.length)
|
|
198
|
-
|
|
199
|
-
let response;
|
|
200
|
-
if (tokenList.outputs.length > 0) {
|
|
201
|
-
// Spend the first token
|
|
202
|
-
response = await wallet.createAction({
|
|
203
|
-
description: 'Spend my first token',
|
|
204
|
-
inputBEEF: tokenList.BEEF,
|
|
205
|
-
inputs: [{
|
|
206
|
-
outpoint: tokenList.outputs[0].outpoint,
|
|
207
|
-
unlockingScript: Script.fromASM('OP_TRUE').toHex(),
|
|
208
|
-
inputDescription: 'My token being spent'
|
|
209
|
-
}]
|
|
210
|
-
})
|
|
211
|
-
|
|
212
|
-
console.log('Token spent:', response)
|
|
213
|
-
if (response.txid) {
|
|
214
|
-
console.log(`Spending transaction: https://whatsonchain.com/tx/${response.txid}`)
|
|
215
|
-
}
|
|
216
|
-
} else {
|
|
217
|
-
console.log('No tokens available to spend. Run step2-create-token.ts first!')
|
|
218
|
-
response = null;
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
return response
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
// Run the function
|
|
225
|
-
spendToken().catch(console.error)
|
|
226
|
-
```
|
|
227
|
-
|
|
228
|
-
Run it as follows:
|
|
229
|
-
|
|
230
|
-
```bash
|
|
231
|
-
# Run the TypeScript file directly
|
|
232
|
-
npx ts-node step3-spend-token.ts
|
|
233
|
-
```
|
|
234
|
-
|
|
235
|
-
**What's happening here:**
|
|
236
|
-
|
|
237
|
-
- We list tokens from our 'my-tokens' basket
|
|
238
|
-
- Spend the first available token by providing its outpoint
|
|
239
|
-
- Create a new output with proof that we spent the token
|
|
240
|
-
- The BEEF (Blockchain Exchange Format) provides the transaction history
|
|
241
|
-
|
|
242
|
-
## What You've Learned
|
|
243
|
-
|
|
244
|
-
Congratulations! You've successfully created your first BSV transactions. Here's what you accomplished:
|
|
245
|
-
|
|
246
|
-
### Core Concepts Mastered
|
|
247
|
-
|
|
248
|
-
1. **WalletClient Usage**: Connected to your BRC-100 wallet and created transactions
|
|
249
|
-
2. **Script Construction**: Used `Script.fromASM()` to create clean, readable Bitcoin scripts
|
|
250
|
-
3. **Transaction Outputs**: Created both data storage and spendable token outputs
|
|
251
|
-
4. **Wallet Baskets**: Organized your tokens using the basket system
|
|
252
|
-
5. **UTXO Management**: Listed and spent existing outputs using BEEF format
|
|
253
|
-
|
|
254
|
-
### Key WalletClient Methods
|
|
255
|
-
|
|
256
|
-
- `createAction()`: Creates transactions with specified outputs and inputs
|
|
257
|
-
- `listOutputs()`: Retrieves spendable outputs from wallet baskets
|
|
258
|
-
- The wallet automatically handles signing, fees, and broadcasting
|
|
259
|
-
|
|
260
|
-
### Script Types You Used
|
|
261
|
-
|
|
262
|
-
- `OP_RETURN "data"`: Store arbitrary data on the blockchain
|
|
263
|
-
- `OP_NOP`: Create simple tokens that can be spent later
|
|
264
|
-
- `OP_TRUE`: Unlock scripts that always validate (for simple spending)
|
|
265
|
-
|
|
266
|
-
## Next Steps
|
|
267
|
-
|
|
268
|
-
- Learn about [Key Management and Cryptography](./key-management.md)
|
|
269
|
-
- Prefer lower-level control? Check out [First Transaction (Low-level API)](./first-transaction-low-level.md)
|
|
270
|
-
|
|
271
|
-
## Additional Resources
|
|
272
|
-
|
|
273
|
-
- [Wallet API Reference](../reference/wallet.md)
|
|
274
|
-
- [BSV Wallet Protocols](https://projectbabbage.com/docs/guides/wallet/transactions)
|
|
275
|
-
- [Transaction Broadcasting](https://projectbabbage.com/docs/guides/wallet/signing)
|