bsv-mcp 0.0.26 → 0.0.27

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.
@@ -0,0 +1,249 @@
1
+ import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
2
+ import type { RequestHandlerExtra } from "@modelcontextprotocol/sdk/shared/protocol.js";
3
+ import type {
4
+ ServerNotification,
5
+ ServerRequest,
6
+ } from "@modelcontextprotocol/sdk/types.js";
7
+
8
+ /**
9
+ * BSV SDK Transaction Prompt
10
+ *
11
+ * Provides detailed information about transaction building and management
12
+ * in the BSV SDK, including input/output handling, script integration, and transaction signing.
13
+ */
14
+ export const BSV_SDK_TRANSACTION_PROMPT = `
15
+ # BSV SDK - Transaction Module
16
+
17
+ The Transaction module in the BSV SDK provides comprehensive functionality for creating, manipulating, and signing Bitcoin transactions. It gives developers fine-grained control over transaction construction while abstracting many of the complexities.
18
+
19
+ ## Key Components
20
+
21
+ ### Transaction Class
22
+
23
+ The core \`Transaction\` class represents a Bitcoin transaction and provides methods for manipulating its components:
24
+
25
+ \`\`\`typescript
26
+ import { Transaction, PrivateKey, LockingScript } from "@bsv/sdk";
27
+
28
+ // Create a new transaction
29
+ const tx = new Transaction();
30
+
31
+ // Set transaction properties
32
+ tx.version = 1;
33
+ tx.lockTime = 0;
34
+ \`\`\`
35
+
36
+ ## Building Transactions
37
+
38
+ ### Adding Inputs
39
+
40
+ \`\`\`typescript
41
+ // Add an input by specifying the source transaction and output index
42
+ tx.addInput({
43
+ sourceTXID: "previous_transaction_id_in_hex",
44
+ sourceOutputIndex: 0,
45
+ sequence: 0xffffffff // Optional, defaults to max value
46
+ });
47
+
48
+ // Add multiple inputs
49
+ const inputs = [
50
+ { sourceTXID: "txid1", sourceOutputIndex: 0 },
51
+ { sourceTXID: "txid2", sourceOutputIndex: 1 }
52
+ ];
53
+ inputs.forEach(input => tx.addInput(input));
54
+ \`\`\`
55
+
56
+ ### Adding Outputs
57
+
58
+ \`\`\`typescript
59
+ // Add an output with a locking script and amount
60
+ import { LockingScript } from "@bsv/sdk";
61
+
62
+ // Create from a Bitcoin address
63
+ const lockingScript = LockingScript.fromAddress("recipient_address");
64
+
65
+ // Add the output to the transaction
66
+ tx.addOutput({
67
+ lockingScript,
68
+ satoshis: 5000 // Amount in satoshis
69
+ });
70
+
71
+ // Add a data (OP_RETURN) output
72
+ const dataScript = LockingScript.fromData(Buffer.from("Hello, Bitcoin!"));
73
+ tx.addOutput({
74
+ lockingScript: dataScript,
75
+ satoshis: 0 // OP_RETURN outputs typically have 0 value
76
+ });
77
+ \`\`\`
78
+
79
+ ### Working with UTXOs
80
+
81
+ When building transactions with existing UTXOs:
82
+
83
+ \`\`\`typescript
84
+ import { UnlockingScript } from "@bsv/sdk";
85
+
86
+ // Example UTXO data
87
+ const utxos = [
88
+ {
89
+ txid: "previous_tx_id_in_hex",
90
+ vout: 0,
91
+ satoshis: 10000,
92
+ scriptPubKey: "locking_script_hex"
93
+ }
94
+ ];
95
+
96
+ // Create transaction using UTXOs
97
+ const tx = new Transaction();
98
+
99
+ // Add input from UTXO
100
+ utxos.forEach(utxo => {
101
+ tx.addInput({
102
+ sourceTXID: utxo.txid,
103
+ sourceOutputIndex: utxo.vout
104
+ });
105
+ });
106
+
107
+ // Add output with recipient address
108
+ tx.addOutput({
109
+ lockingScript: LockingScript.fromAddress("recipient_address"),
110
+ satoshis: 9000 // Sending 9000 satoshis (10000 - 1000 fee)
111
+ });
112
+ \`\`\`
113
+
114
+ ## Signing Transactions
115
+
116
+ ### Basic Transaction Signing
117
+
118
+ \`\`\`typescript
119
+ import { PrivateKey, SigningConfig, Utils } from "@bsv/sdk";
120
+
121
+ // Create a private key
122
+ const privateKey = PrivateKey.fromWif("your_private_key_wif");
123
+
124
+ // Sign a specific input
125
+ const inputIndex = 0;
126
+ const signingConfig: SigningConfig = {
127
+ privateKey,
128
+ lockingScript: LockingScript.fromAddress(privateKey.toAddress()),
129
+ satoshis: 10000, // Original amount in the UTXO
130
+ inputIndex,
131
+ sigHashType: Utils.SIGHASH_ALL | Utils.SIGHASH_FORKID // Standard signing algorithm
132
+ };
133
+
134
+ // Apply the signature to the transaction
135
+ tx.sign(signingConfig);
136
+ \`\`\`
137
+
138
+ ### Signing Multiple Inputs
139
+
140
+ \`\`\`typescript
141
+ // Sign multiple inputs with different keys
142
+ const keys = [privateKey1, privateKey2];
143
+ const utxos = [utxo1, utxo2];
144
+
145
+ utxos.forEach((utxo, index) => {
146
+ const signingConfig = {
147
+ privateKey: keys[index],
148
+ lockingScript: LockingScript.fromHex(utxo.scriptPubKey),
149
+ satoshis: utxo.satoshis,
150
+ inputIndex: index,
151
+ sigHashType: Utils.SIGHASH_ALL | Utils.SIGHASH_FORKID
152
+ };
153
+
154
+ tx.sign(signingConfig);
155
+ });
156
+ \`\`\`
157
+
158
+ ## Transaction Serialization
159
+
160
+ \`\`\`typescript
161
+ // Convert transaction to binary format
162
+ const txBinary = tx.toBinary();
163
+
164
+ // Convert to hex string
165
+ const txHex = tx.toHex();
166
+
167
+ // Get transaction ID
168
+ const txid = tx.hash("hex");
169
+
170
+ // Parse an existing transaction
171
+ const parsedTx = Transaction.fromHex("transaction_hex_string");
172
+ \`\`\`
173
+
174
+ ## Fee Calculation
175
+
176
+ \`\`\`typescript
177
+ // Manual fee calculation based on transaction size
178
+ const txSize = tx.toBinary().length;
179
+ const feeRate = 0.5; // satoshis per byte
180
+ const fee = Math.ceil(txSize * feeRate);
181
+
182
+ // Adjust output amount to include fee
183
+ outputAmount = inputAmount - fee;
184
+ \`\`\`
185
+
186
+ ## Advanced Transaction Features
187
+
188
+ ### Time Locks
189
+
190
+ \`\`\`typescript
191
+ // Set absolute locktime (by block height)
192
+ tx.lockTime = 700000; // Transaction can't be mined until block 700000
193
+
194
+ // Set relative locktime using sequence number (BIP 68)
195
+ const sequenceForBlocks = (blocks) => 0xffffffff - blocks;
196
+ tx.inputs[0].sequence = sequenceForBlocks(10); // Locked for 10 blocks
197
+ \`\`\`
198
+
199
+ ### Custom Scripts
200
+
201
+ \`\`\`typescript
202
+ import { Script, OpCodes } from "@bsv/sdk";
203
+
204
+ // Create a custom script
205
+ const customScript = new Script();
206
+ customScript.add(OpCodes.OP_DUP);
207
+ customScript.add(OpCodes.OP_HASH160);
208
+ customScript.add(Buffer.from("public_key_hash", "hex"));
209
+ customScript.add(OpCodes.OP_EQUALVERIFY);
210
+ customScript.add(OpCodes.OP_CHECKSIG);
211
+
212
+ // Create a locking script from the custom script
213
+ const customLockingScript = LockingScript.fromScript(customScript);
214
+ \`\`\`
215
+
216
+ ## Best Practices
217
+
218
+ 1. **Fee Management**: Calculate appropriate fees based on transaction size and network conditions
219
+ 2. **Input/Output Management**: Properly track inputs and outputs to avoid double-spending
220
+ 3. **Change Handling**: Always account for change when not spending the full UTXO amount
221
+ 4. **Testing**: Test transactions on testnet before deploying to mainnet
222
+ 5. **Error Handling**: Implement proper error handling for transaction building and signing
223
+
224
+ For complete API documentation and additional transaction features, refer to the official BSV SDK documentation.
225
+ `;
226
+
227
+ /**
228
+ * Register the BSV SDK Transaction prompt with the MCP server
229
+ * @param server The MCP server instance
230
+ */
231
+ export function registerTransactionPrompt(server: McpServer): void {
232
+ server.prompt(
233
+ "bitcoin_sv_sdk_transaction",
234
+ "Detailed information about transaction building and management in the BSV SDK, including input/output handling, script integration, and transaction signing.",
235
+ async (extra: RequestHandlerExtra<ServerRequest, ServerNotification>) => {
236
+ return {
237
+ messages: [
238
+ {
239
+ role: "assistant",
240
+ content: {
241
+ type: "text",
242
+ text: BSV_SDK_TRANSACTION_PROMPT,
243
+ },
244
+ },
245
+ ],
246
+ };
247
+ },
248
+ );
249
+ }
@@ -0,0 +1,188 @@
1
+ import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
2
+ import type { RequestHandlerExtra } from "@modelcontextprotocol/sdk/shared/protocol.js";
3
+ import type {
4
+ ServerNotification,
5
+ ServerRequest,
6
+ } from "@modelcontextprotocol/sdk/types.js";
7
+
8
+ /**
9
+ * BSV SDK Wallet Prompt
10
+ *
11
+ * Provides detailed information about the wallet functionality in the BSV SDK,
12
+ * including key management, address handling, and UTXO management.
13
+ */
14
+ export const BSV_SDK_WALLET_PROMPT = `
15
+ # BSV SDK - Wallet Module
16
+
17
+ The wallet module in BSV SDK provides comprehensive functionality for managing Bitcoin keys, addresses, and UTXOs (Unspent Transaction Outputs). It forms the foundation for creating and managing Bitcoin wallets in your applications.
18
+
19
+ ## Key Classes and Interfaces
20
+
21
+ ### ProtoWallet
22
+
23
+ The \`ProtoWallet\` class provides a basic implementation of the wallet interface with core functionality:
24
+
25
+ \`\`\`typescript
26
+ import { PrivateKey, ProtoWallet } from "@bsv/sdk";
27
+
28
+ // Create a new wallet with a random private key
29
+ const privateKey = PrivateKey.fromRandom();
30
+ const wallet = new ProtoWallet(privateKey);
31
+ \`\`\`
32
+
33
+ ### WalletInterface
34
+
35
+ The \`WalletInterface\` defines the standard interface that wallet implementations should follow. It includes methods for:
36
+
37
+ - Key management
38
+ - Cryptographic operations
39
+ - Transaction creation and signing
40
+ - Output management
41
+ - Certificate handling
42
+
43
+ ## Key Management
44
+
45
+ ### Generating Keys
46
+
47
+ \`\`\`typescript
48
+ import { PrivateKey } from "@bsv/sdk";
49
+
50
+ // Generate a random private key
51
+ const privateKey = PrivateKey.fromRandom();
52
+
53
+ // Generate from a WIF (Wallet Import Format) string
54
+ const importedKey = PrivateKey.fromWif("your-wif-string");
55
+
56
+ // Generate from a seed
57
+ const seedKey = PrivateKey.fromSeed(Buffer.from("your-seed-data"));
58
+
59
+ // Get the corresponding public key
60
+ const publicKey = privateKey.toPublicKey();
61
+ \`\`\`
62
+
63
+ ### KeyDeriver & CachedKeyDeriver
64
+
65
+ For HD (Hierarchical Deterministic) wallet functionality:
66
+
67
+ \`\`\`typescript
68
+ import { KeyDeriver } from "@bsv/sdk";
69
+
70
+ // Create a key deriver with a seed
71
+ const deriver = new KeyDeriver(seed);
72
+
73
+ // Derive a key at a specific path
74
+ const derivedKey = await deriver.deriveKey("m/44'/0'/0'/0/0");
75
+ \`\`\`
76
+
77
+ ## Address Management
78
+
79
+ \`\`\`typescript
80
+ // Get the address for a private key
81
+ const address = privateKey.toAddress();
82
+
83
+ // Get the address for a public key
84
+ const address = publicKey.toAddress();
85
+
86
+ // Get the address string
87
+ const addressString = address.toString();
88
+ \`\`\`
89
+
90
+ ## UTXO Management
91
+
92
+ Managing UTXOs (Unspent Transaction Outputs) is a critical part of wallet functionality:
93
+
94
+ \`\`\`typescript
95
+ // Example of tracking UTXOs
96
+ class MyWallet extends ProtoWallet {
97
+ private utxos = [];
98
+
99
+ async refreshUtxos(address) {
100
+ // Fetch UTXOs from a service or API
101
+ this.utxos = await fetchUtxosFromService(address);
102
+ }
103
+
104
+ getAvailableUtxos() {
105
+ return this.utxos.filter(utxo => !utxo.spent);
106
+ }
107
+
108
+ getBalance() {
109
+ return this.getAvailableUtxos().reduce((sum, utxo) => sum + utxo.satoshis, 0);
110
+ }
111
+ }
112
+ \`\`\`
113
+
114
+ ## Cryptographic Operations
115
+
116
+ The wallet module provides various cryptographic operations:
117
+
118
+ \`\`\`typescript
119
+ // Signing data
120
+ const signature = await wallet.createSignature({
121
+ data: [1, 2, 3, 4], // Data to sign
122
+ protocolID: [1, "ecdsa"], // Protocol to use
123
+ keyID: "default" // Key identifier
124
+ });
125
+
126
+ // Verifying signatures
127
+ const isValid = await wallet.verifySignature({
128
+ data: [1, 2, 3, 4], // Original data
129
+ signature: signatureBytes, // Signature to verify
130
+ protocolID: [1, "ecdsa"],
131
+ keyID: "default"
132
+ });
133
+
134
+ // Encryption and decryption
135
+ const encrypted = await wallet.encrypt({
136
+ plaintext: [1, 2, 3, 4],
137
+ protocolID: [1, "aes256"],
138
+ keyID: "default"
139
+ });
140
+
141
+ const decrypted = await wallet.decrypt({
142
+ ciphertext: encrypted.ciphertext,
143
+ protocolID: [1, "aes256"],
144
+ keyID: "default"
145
+ });
146
+ \`\`\`
147
+
148
+ ## Best Practices
149
+
150
+ 1. **Key Security**: Always handle private keys securely and never expose them unnecessarily
151
+ 2. **UTXO Management**: Maintain accurate UTXO information for wallet functionality
152
+ 3. **Error Handling**: Implement proper error handling for all wallet operations
153
+ 4. **Testing**: Test wallet functionality thoroughly on testnet before deploying to mainnet
154
+ 5. **Backup**: Provide key backup and recovery mechanisms for users
155
+
156
+ ## Advanced Topics
157
+
158
+ - **Multi-signature wallets**: Implementing wallets requiring multiple signatures
159
+ - **HD Wallets**: Creating hierarchical deterministic wallets for key derivation
160
+ - **Watch-only wallets**: Tracking addresses without private keys
161
+ - **Hardware wallet integration**: Connecting to hardware security devices
162
+
163
+ For complete API documentation and additional wallet features, refer to the official BSV SDK documentation.
164
+ `;
165
+
166
+ /**
167
+ * Register the BSV SDK Wallet prompt with the MCP server
168
+ * @param server The MCP server instance
169
+ */
170
+ export function registerWalletPrompt(server: McpServer): void {
171
+ server.prompt(
172
+ "bitcoin_sv_sdk_wallet",
173
+ "Detailed information about the wallet functionality in the BSV SDK, including key management, address handling, and UTXO management.",
174
+ async (extra: RequestHandlerExtra<ServerRequest, ServerNotification>) => {
175
+ return {
176
+ messages: [
177
+ {
178
+ role: "assistant",
179
+ content: {
180
+ type: "text",
181
+ text: BSV_SDK_WALLET_PROMPT,
182
+ },
183
+ },
184
+ ],
185
+ };
186
+ },
187
+ );
188
+ }
@@ -0,0 +1,23 @@
1
+ import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
2
+ import { registerAllBsvSdkPrompts } from "./bsvSdk";
3
+ import { registerOrdinalsPrompt } from "./ordinals";
4
+
5
+ /**
6
+ * Register all prompts with the MCP server
7
+ * @param server The MCP server instance
8
+ */
9
+ export function registerAllPrompts(server: McpServer): void {
10
+ // Register Ordinals prompt
11
+ registerOrdinalsPrompt(server);
12
+
13
+ // Register all BSV SDK prompts
14
+ registerAllBsvSdkPrompts(server);
15
+
16
+ // Add more prompts registration here as needed
17
+ }
18
+
19
+ /**
20
+ * Export all prompt constants
21
+ */
22
+ export * from "./ordinals";
23
+ export * from "./bsvSdk";
@@ -0,0 +1,103 @@
1
+ import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
2
+ import type { RequestHandlerExtra } from "@modelcontextprotocol/sdk/shared/protocol.js";
3
+ import type {
4
+ ServerNotification,
5
+ ServerRequest,
6
+ } from "@modelcontextprotocol/sdk/types.js";
7
+
8
+ /**
9
+ * 1Sat Ordinals Prompt
10
+ *
11
+ * Provides comprehensive information about Bitcoin SV ordinals,
12
+ * including what they are, how they work, and how to use them.
13
+ */
14
+ export const ORDINALS_PROMPT = `
15
+ # 1Sat Ordinals - Comprehensive Guide
16
+
17
+ Ordinals are a way to uniquely identify and track specific satoshis (the smallest unit of Bitcoin)
18
+ on the blockchain. This concept allows for "inscriptions" - embedding data directly into a satoshi,
19
+ effectively creating NFT-like functionality native to the Bitcoin protocol.
20
+
21
+ ## Key Concepts
22
+
23
+ 1. **Ordinal Theory**: Each satoshi has a unique position in the Bitcoin ledger, determined by the order
24
+ in which they were mined.
25
+
26
+ 2. **Inscriptions**: Content embedded directly into a specific satoshi. Can be any valid content type.
27
+
28
+ 3. **On-chain Storage**: All ordinal data is stored immutably on the blockchain.
29
+
30
+ ## BSV Ordinals (1Sat Ordinals) vs. BTC Ordinals
31
+
32
+ - 1Sat Ordinals leverage the larger block sizes and lower fees of Bitcoin SV, making them more practical
33
+ for storing meaningful data and media.
34
+
35
+ - 1Sat Ordinals can store much larger inscriptions compared to BTC, enabling richer media and applications.
36
+
37
+ - 1Sat Ordinals typically cost a fraction of what BTC ordinals cost to create and transfer.
38
+
39
+ ## Creating Ordinals
40
+
41
+ To create a BSV ordinal:
42
+
43
+ 1. Choose the content to inscribe (image, text, audio, etc.)
44
+ 2. Use a compatible wallet or service that supports ordinal creation
45
+ 3. Pay the transaction fee to inscribe your content on-chain
46
+ 4. Receive a unique ordinal ID that references your specific satoshi
47
+
48
+ ## Transferring Ordinals
49
+
50
+ Ordinals are transferred by sending the specific satoshi that contains the inscription. Compatible wallets
51
+ ensure that when you transfer an ordinal, the specific satoshi containing the inscription is included in
52
+ the transaction.
53
+
54
+ ## Viewing Ordinals
55
+
56
+ Ordinal inscriptions can be viewed through:
57
+
58
+ 1. Specialized ordinal explorers
59
+ 2. Compatible wallets with ordinal support
60
+ 3. Marketplaces that support BSV ordinals
61
+
62
+ ## Use Cases
63
+
64
+ - Digital Art and Collectibles
65
+ - Certificates of Authenticity
66
+ - Domain Names
67
+ - Documentation and Verification
68
+ - Gaming Assets
69
+ - Media Distribution
70
+
71
+ ## Best Practices
72
+
73
+ - Verify file sizes and transaction costs before inscribing
74
+ - Use appropriate file formats optimized for on-chain storage
75
+ - Keep private keys secure to maintain ownership of valuable ordinals
76
+ - Consider using a specialized wallet for managing valuable ordinal collections
77
+
78
+ For technical implementation details, refer to the official documentation and BSV ordinals standards.
79
+ `;
80
+
81
+ /**
82
+ * Register the Ordinals prompt with the MCP server
83
+ * @param server The MCP server instance
84
+ */
85
+ export function registerOrdinalsPrompt(server: McpServer): void {
86
+ server.prompt(
87
+ "bitcoin_sv_ordinals",
88
+ "Comprehensive information about Bitcoin SV ordinals, including what they are, how they work, and how to use them.",
89
+ async (extra: RequestHandlerExtra<ServerRequest, ServerNotification>) => {
90
+ return {
91
+ messages: [
92
+ {
93
+ role: "assistant",
94
+ content: {
95
+ type: "text",
96
+ text: ORDINALS_PROMPT,
97
+ },
98
+ },
99
+ ],
100
+ };
101
+ },
102
+ );
103
+ }