@mcp-dockmaster/mcp-cryptowallet-evm 1.0.4 → 1.0.6

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.
@@ -1,11 +1,25 @@
1
- import { ethers } from "ethers";
1
+ import { ethers, providers } from "ethers";
2
2
  import { ToolResultSchema } from "../types.js";
3
- import { createSuccessResponse, createErrorResponse, getProvider, getWallet } from "./utils.js";
3
+ import { createSuccessResponse, createErrorResponse, getProvider, getWallet, setProvider } from "./utils.js";
4
4
  import { fromPrivateKeyHandlerInput, createMnemonicPhraseHandlerInput } from "./wallet.types.js";
5
5
  import { generateMnemonic, } from '@scure/bip39';
6
- // Wallet Creation and Management
7
6
 
8
- export const createWalletHandler = async (input: any): Promise<ToolResultSchema<any>> => {
7
+ // Provider handlers
8
+
9
+ export const setProviderHandler = async (input: any): Promise<ToolResultSchema> => {
10
+ try {
11
+ setProvider(input.providerURL);
12
+ return createSuccessResponse(`Provider set successfully:
13
+ Provider URL: ${input.providerURL}
14
+ `);
15
+ } catch (error) {
16
+ return createErrorResponse(`Failed to set provider: ${(error as Error).message}`);
17
+ }
18
+ };
19
+
20
+
21
+ // Wallet Creation and Management
22
+ export const createWalletHandler = async (input: any): Promise<ToolResultSchema> => {
9
23
  try {
10
24
  const options: any = {};
11
25
 
@@ -32,7 +46,7 @@ export const createWalletHandler = async (input: any): Promise<ToolResultSchema<
32
46
  result.encryptedWallet = encryptedWallet;
33
47
  }
34
48
 
35
- return createSuccessResponse(result, `Wallet created successfully:
49
+ return createSuccessResponse(`Wallet created successfully:
36
50
  Address: ${wallet.address}
37
51
  Private Key: ${wallet.privateKey}
38
52
  Public Key: ${wallet.publicKey}
@@ -44,20 +58,17 @@ export const createWalletHandler = async (input: any): Promise<ToolResultSchema<
44
58
  }
45
59
  };
46
60
 
47
- export const fromPrivateKeyHandler = async (input: fromPrivateKeyHandlerInput): Promise<ToolResultSchema<any>> => {
61
+ export const fromPrivateKeyHandler = async (input: fromPrivateKeyHandlerInput): Promise<ToolResultSchema> => {
48
62
  try {
49
63
  if (!input.privateKey) {
50
64
  return createErrorResponse("Private key is required");
51
65
  }
52
66
 
53
- const provider = input.provider ? getProvider(input.provider) : undefined;
67
+ const provider = getProvider()
54
68
  const wallet = new ethers.Wallet(input.privateKey, provider);
55
69
 
56
- return createSuccessResponse({
57
- address: wallet.address,
58
- privateKey: wallet.privateKey,
59
- publicKey: wallet.publicKey
60
- }, `Wallet created from private key successfully:
70
+ return createSuccessResponse(
71
+ `Wallet created from private key successfully:
61
72
  Address: ${wallet.address}
62
73
  Private Key: ${wallet.privateKey}
63
74
  Public Key: ${wallet.publicKey}
@@ -67,7 +78,7 @@ export const fromPrivateKeyHandler = async (input: fromPrivateKeyHandlerInput):
67
78
  }
68
79
  };
69
80
 
70
- export const createMnemonicPhraseHandler = async (input: createMnemonicPhraseHandlerInput): Promise<ToolResultSchema<any>> => {
81
+ export const createMnemonicPhraseHandler = async (input: createMnemonicPhraseHandlerInput): Promise<ToolResultSchema> => {
71
82
  try {
72
83
  const { wordlist } = await import(`@scure/bip39/wordlists/${input.locale || 'english'}`);
73
84
  if (!wordlist) {
@@ -77,9 +88,8 @@ export const createMnemonicPhraseHandler = async (input: createMnemonicPhraseHan
77
88
  const entropyBits = ((input.length ?? 12) / 3) * 32;
78
89
  const mnemonic = generateMnemonic(wordlist, entropyBits);
79
90
 
80
- return createSuccessResponse({
81
- mnemonic: mnemonic
82
- }, `Mnemonic phrase created successfully:
91
+ return createSuccessResponse(
92
+ `Mnemonic phrase created successfully:
83
93
  Mnemonic: "${mnemonic}"
84
94
  `);
85
95
  } catch (error) {
@@ -87,7 +97,7 @@ export const createMnemonicPhraseHandler = async (input: createMnemonicPhraseHan
87
97
  }
88
98
  };
89
99
 
90
- export const fromMnemonicHandler = async (input: any): Promise<ToolResultSchema<any>> => {
100
+ export const fromMnemonicHandler = async (input: any): Promise<ToolResultSchema> => {
91
101
  try {
92
102
  if (!input.mnemonic) {
93
103
  return createErrorResponse("Mnemonic is required");
@@ -98,18 +108,15 @@ export const fromMnemonicHandler = async (input: any): Promise<ToolResultSchema<
98
108
  wordlist: (input.locale && ethers.wordlists[input.locale]) || ethers.wordlists.en
99
109
  };
100
110
 
101
- const provider = input.provider ? getProvider(input.provider) : undefined;
111
+ const provider = getProvider();
102
112
  const wallet = ethers.Wallet.fromMnemonic(input.mnemonic, options.path, options.wordlist);
103
113
 
104
114
  if (provider) wallet.connect(provider);
105
115
 
106
- return createSuccessResponse({
107
- address: wallet.address,
108
- mnemonic: wallet.mnemonic?.phrase,
109
- privateKey: wallet.privateKey,
110
- publicKey: wallet.publicKey
111
- }, `Wallet created from mnemonic successfully:
116
+ return createSuccessResponse(
117
+ `Wallet created from mnemonic successfully:
112
118
  Address: ${wallet.address}
119
+ Mnemonic: ${input.mnemonic}
113
120
  Private Key: ${wallet.privateKey}
114
121
  Public Key: ${wallet.publicKey}
115
122
  `);
@@ -118,7 +125,7 @@ export const fromMnemonicHandler = async (input: any): Promise<ToolResultSchema<
118
125
  }
119
126
  };
120
127
 
121
- export const fromEncryptedJsonHandler = async (input: any): Promise<ToolResultSchema<any>> => {
128
+ export const fromEncryptedJsonHandler = async (input: any): Promise<ToolResultSchema> => {
122
129
  try {
123
130
  if (!input.json) {
124
131
  return createErrorResponse("Encrypted JSON is required");
@@ -129,23 +136,24 @@ export const fromEncryptedJsonHandler = async (input: any): Promise<ToolResultSc
129
136
  }
130
137
 
131
138
  const wallet = await ethers.Wallet.fromEncryptedJson(input.json, input.password);
132
- const provider = input.provider ? getProvider(input.provider) : undefined;
139
+ const provider = getProvider()
133
140
 
134
141
  if (provider) {
135
142
  wallet.connect(provider);
136
143
  }
137
144
 
138
- return createSuccessResponse({
139
- address: wallet.address,
140
- privateKey: wallet.privateKey,
141
- publicKey: wallet.publicKey
142
- }, "Wallet created from encrypted JSON successfully");
145
+ return createSuccessResponse(
146
+ `Wallet created from encrypted JSON successfully
147
+ Address: ${wallet.address}
148
+ Private Key: ${wallet.privateKey}
149
+ Public Key: ${wallet.publicKey}
150
+ `);
143
151
  } catch (error) {
144
152
  return createErrorResponse(`Failed to create wallet from encrypted JSON: ${(error as Error).message}`);
145
153
  }
146
154
  };
147
155
 
148
- export const encryptWalletHandler = async (input: any): Promise<ToolResultSchema<any>> => {
156
+ export const encryptWalletHandler = async (input: any): Promise<ToolResultSchema> => {
149
157
  try {
150
158
  if (!input.wallet) {
151
159
  return createErrorResponse("Wallet is required");
@@ -158,9 +166,10 @@ export const encryptWalletHandler = async (input: any): Promise<ToolResultSchema
158
166
  const wallet = await getWallet(input.wallet);
159
167
  const encryptedWallet = await wallet.encrypt(input.password, input.options);
160
168
 
161
- return createSuccessResponse({
162
- encryptedWallet
163
- }, "Wallet encrypted successfully");
169
+ return createSuccessResponse(
170
+ `Wallet encrypted successfully
171
+ Encrypted Wallet: ${encryptedWallet}
172
+ `);
164
173
  } catch (error) {
165
174
  return createErrorResponse(`Failed to encrypt wallet: ${(error as Error).message}`);
166
175
  }
@@ -168,13 +177,12 @@ export const encryptWalletHandler = async (input: any): Promise<ToolResultSchema
168
177
 
169
178
  // Wallet Properties
170
179
 
171
- export const getAddressHandler = async (input: any): Promise<ToolResultSchema<any>> => {
180
+ export const getAddressHandler = async (input: any): Promise<ToolResultSchema> => {
172
181
  try {
173
182
  const wallet = await getWallet(input.wallet);
174
183
 
175
- return createSuccessResponse({
176
- address: wallet.address
177
- }, `Wallet address retrieved successfully:
184
+ return createSuccessResponse(
185
+ `Wallet address retrieved successfully:
178
186
  Address: ${wallet.address}
179
187
  `);
180
188
  } catch (error) {
@@ -182,13 +190,12 @@ export const getAddressHandler = async (input: any): Promise<ToolResultSchema<an
182
190
  }
183
191
  };
184
192
 
185
- export const getPublicKeyHandler = async (input: any): Promise<ToolResultSchema<any>> => {
193
+ export const getPublicKeyHandler = async (input: any): Promise<ToolResultSchema> => {
186
194
  try {
187
195
  const wallet = await getWallet(input.wallet);
188
196
 
189
- return createSuccessResponse({
190
- publicKey: wallet.publicKey
191
- }, `Wallet public key retrieved successfully:
197
+ return createSuccessResponse(
198
+ `Wallet public key retrieved successfully:
192
199
  Public Key: ${wallet.publicKey}
193
200
  `);
194
201
  } catch (error) {
@@ -196,13 +203,12 @@ export const getPublicKeyHandler = async (input: any): Promise<ToolResultSchema<
196
203
  }
197
204
  };
198
205
 
199
- export const getPrivateKeyHandler = async (input: any): Promise<ToolResultSchema<any>> => {
206
+ export const getPrivateKeyHandler = async (input: any): Promise<ToolResultSchema> => {
200
207
  try {
201
208
  const wallet = await getWallet(input.wallet, input.password);
202
209
 
203
- return createSuccessResponse({
204
- privateKey: wallet.privateKey
205
- }, `Wallet private key retrieved successfully:
210
+ return createSuccessResponse(
211
+ `Wallet private key retrieved successfully:
206
212
  Private Key: ${wallet.privateKey}
207
213
  `);
208
214
  } catch (error) {
@@ -211,20 +217,14 @@ export const getPrivateKeyHandler = async (input: any): Promise<ToolResultSchema
211
217
  };
212
218
  // Blockchain Methods
213
219
 
214
- export const getBalanceHandler = async (input: any): Promise<ToolResultSchema<any>> => {
220
+ export const getBalanceHandler = async (input: any): Promise<ToolResultSchema> => {
215
221
  try {
216
- if (!input.provider) {
217
- return createErrorResponse("Provider URL is required to get balance");
218
- }
219
-
220
- const wallet = await getWallet(input.wallet, input.password, input.provider);
222
+ const wallet = await getWallet(input.wallet, input.password);
221
223
 
222
224
  const balance = await wallet.getBalance(input.blockTag ?? "latest");
223
225
 
224
- return createSuccessResponse({
225
- balance: balance.toString(),
226
- balanceInEth: ethers.utils.formatEther(balance)
227
- }, `Wallet balance retrieved successfully
226
+ return createSuccessResponse(
227
+ `Wallet balance retrieved successfully
228
228
  Balance: ${balance.toString()}
229
229
  Balance in ETH: ${ethers.utils.formatEther(balance)}
230
230
  `);
@@ -233,19 +233,18 @@ export const getBalanceHandler = async (input: any): Promise<ToolResultSchema<an
233
233
  }
234
234
  };
235
235
 
236
- export const getChainIdHandler = async (input: any): Promise<ToolResultSchema<any>> => {
236
+ export const getChainIdHandler = async (input: any): Promise<ToolResultSchema> => {
237
237
  try {
238
- const wallet = await getWallet(input.wallet, input.password, input.provider);
238
+ const wallet = await getWallet(input.wallet, input.password);
239
239
 
240
240
  if (!wallet.provider) {
241
- return createErrorResponse("Provider is required to get chain ID");
241
+ return createErrorResponse("Provider is required to get chain ID, please set the provider URL");
242
242
  }
243
243
 
244
244
  const chainId = await wallet.getChainId();
245
245
 
246
- return createSuccessResponse({
247
- chainId
248
- }, `Chain ID retrieved successfully
246
+ return createSuccessResponse(
247
+ `Chain ID retrieved successfully
249
248
  Chain ID: ${chainId.toString()}
250
249
  `);
251
250
  } catch (error) {
@@ -253,20 +252,18 @@ export const getChainIdHandler = async (input: any): Promise<ToolResultSchema<an
253
252
  }
254
253
  };
255
254
 
256
- export const getGasPriceHandler = async (input: any): Promise<ToolResultSchema<any>> => {
255
+ export const getGasPriceHandler = async (input: any): Promise<ToolResultSchema> => {
257
256
  try {
258
- const wallet = await getWallet(input.wallet, input.password, input.provider);
257
+ const wallet = await getWallet(input.wallet, input.password);
259
258
 
260
259
  if (!wallet.provider) {
261
- return createErrorResponse("Provider is required to get gas price");
260
+ return createErrorResponse("Provider is required to get gas price, please set the provider URL");
262
261
  }
263
262
 
264
263
  const gasPrice = await wallet.getGasPrice();
265
264
 
266
- return createSuccessResponse({
267
- gasPrice: gasPrice.toString(),
268
- gasPriceInGwei: ethers.utils.formatUnits(gasPrice, "gwei")
269
- }, `Gas price retrieved successfully
265
+ return createSuccessResponse(
266
+ `Gas price retrieved successfully
270
267
  Gas price: ${gasPrice.toString()}
271
268
  Gas price in Gwei: ${ethers.utils.formatUnits(gasPrice, "gwei")}
272
269
  `);
@@ -275,19 +272,18 @@ export const getGasPriceHandler = async (input: any): Promise<ToolResultSchema<a
275
272
  }
276
273
  };
277
274
 
278
- export const getTransactionCountHandler = async (input: any): Promise<ToolResultSchema<any>> => {
275
+ export const getTransactionCountHandler = async (input: any): Promise<ToolResultSchema> => {
279
276
  try {
280
- const wallet = await getWallet(input.wallet, input.password, input.provider);
277
+ const wallet = await getWallet(input.wallet, input.password);
281
278
 
282
279
  if (!wallet.provider) {
283
- return createErrorResponse("Provider is required to get transaction count");
280
+ return createErrorResponse("Provider is required to get transaction count, please set the provider URL");
284
281
  }
285
282
 
286
283
  const transactionCount = await wallet.getTransactionCount(input.blockTag);
287
284
 
288
- return createSuccessResponse({
289
- transactionCount
290
- }, `Transaction count retrieved successfully
285
+ return createSuccessResponse(
286
+ `Transaction count retrieved successfully
291
287
  Transaction count: ${transactionCount.toString()}
292
288
  `);
293
289
  } catch (error) {
@@ -295,23 +291,22 @@ export const getTransactionCountHandler = async (input: any): Promise<ToolResult
295
291
  }
296
292
  };
297
293
 
298
- export const callHandler = async (input: any): Promise<ToolResultSchema<any>> => {
294
+ export const callHandler = async (input: any): Promise<ToolResultSchema> => {
299
295
  try {
300
296
  if (!input.transaction) {
301
297
  return createErrorResponse("Transaction is required");
302
298
  }
303
299
 
304
- const wallet = await getWallet(input.wallet, input.password, input.provider);
300
+ const wallet = await getWallet(input.wallet, input.password);
305
301
 
306
302
  if (!wallet.provider) {
307
- return createErrorResponse("Provider is required to call a contract");
303
+ return createErrorResponse("Provider is required to call a contract, please set the provider URL");
308
304
  }
309
305
 
310
306
  const result = await wallet.call(input.transaction, input.blockTag);
311
307
 
312
- return createSuccessResponse({
313
- result
314
- }, `Contract call executed successfully
308
+ return createSuccessResponse(
309
+ `Contract call executed successfully
315
310
  Result: ${result}
316
311
  `);
317
312
  } catch (error) {
@@ -321,32 +316,21 @@ export const callHandler = async (input: any): Promise<ToolResultSchema<any>> =>
321
316
 
322
317
  // Transaction Methods
323
318
 
324
- export const sendTransactionHandler = async (input: any): Promise<ToolResultSchema<any>> => {
319
+ export const sendTransactionHandler = async (input: any): Promise<ToolResultSchema> => {
325
320
  try {
326
321
  if (!input.transaction) {
327
322
  return createErrorResponse("Transaction is required");
328
323
  }
329
324
 
330
- const wallet = await getWallet(input.wallet, input.password, input.provider);
331
-
325
+ const wallet = await getWallet(input.wallet, input.password);
332
326
  if (!wallet.provider) {
333
- return createErrorResponse("Provider is required to send a transaction");
327
+ return createErrorResponse("Provider is required to send a transaction, please set the provider URL");
334
328
  }
335
329
 
336
330
  const tx = await wallet.sendTransaction(input.transaction);
337
331
 
338
- return createSuccessResponse({
339
- hash: tx.hash,
340
- nonce: tx.nonce,
341
- gasLimit: tx.gasLimit.toString(),
342
- gasPrice: tx.gasPrice?.toString(),
343
- data: tx.data,
344
- value: tx.value.toString(),
345
- chainId: tx.chainId,
346
- from: tx.from,
347
- to: tx.to,
348
- type: tx.type
349
- }, `Transaction sent successfully
332
+ return createSuccessResponse(
333
+ `Transaction sent successfully
350
334
  Hash: ${tx.hash}
351
335
  Nonce: ${tx.nonce.toString()}
352
336
  Gas limit: ${tx.gasLimit.toString()}
@@ -358,21 +342,20 @@ export const sendTransactionHandler = async (input: any): Promise<ToolResultSche
358
342
  }
359
343
  };
360
344
 
361
- export const signTransactionHandler = async (input: any): Promise<ToolResultSchema<any>> => {
345
+ export const signTransactionHandler = async (input: any): Promise<ToolResultSchema> => {
362
346
  try {
363
347
  if (!input.transaction) {
364
348
  return createErrorResponse("Transaction is required");
365
349
  }
366
350
 
367
- const wallet = await getWallet(input.wallet, input.password, input.provider);
351
+ const wallet = await getWallet(input.wallet, input.password);
368
352
 
369
353
  // For signing a transaction, we need to populate it first
370
354
  const populatedTx = await wallet.populateTransaction(input.transaction);
371
355
  const signedTx = await wallet.signTransaction(populatedTx);
372
356
 
373
- return createSuccessResponse({
374
- signedTransaction: signedTx
375
- }, `Transaction signed successfully
357
+ return createSuccessResponse(
358
+ `Transaction signed successfully
376
359
  Signed transaction: ${signedTx}
377
360
  `);
378
361
  } catch (error) {
@@ -380,35 +363,22 @@ export const signTransactionHandler = async (input: any): Promise<ToolResultSche
380
363
  }
381
364
  };
382
365
 
383
- export const populateTransactionHandler = async (input: any): Promise<ToolResultSchema<any>> => {
366
+ export const populateTransactionHandler = async (input: any): Promise<ToolResultSchema> => {
384
367
  try {
385
368
  if (!input.transaction) {
386
369
  return createErrorResponse("Transaction is required");
387
370
  }
388
371
 
389
- const wallet = await getWallet(input.wallet, input.password, input.provider);
372
+ const wallet = await getWallet(input.wallet, input.password);
390
373
 
391
374
  if (!wallet.provider) {
392
- return createErrorResponse("Provider is required to populate a transaction");
375
+ return createErrorResponse("Provider is required to populate a transaction, please set the provider URL");
393
376
  }
394
377
 
395
378
  const populatedTx = await wallet.populateTransaction(input.transaction);
396
379
 
397
- return createSuccessResponse({
398
- populatedTransaction: {
399
- to: populatedTx.to,
400
- from: populatedTx.from,
401
- nonce: populatedTx.nonce,
402
- gasLimit: populatedTx.gasLimit?.toString(),
403
- gasPrice: populatedTx.gasPrice?.toString(),
404
- data: populatedTx.data,
405
- value: populatedTx.value?.toString(),
406
- chainId: populatedTx.chainId,
407
- type: populatedTx.type,
408
- maxFeePerGas: populatedTx.maxFeePerGas?.toString(),
409
- maxPriorityFeePerGas: populatedTx.maxPriorityFeePerGas?.toString()
410
- }
411
- }, `Transaction populated successfully
380
+ return createSuccessResponse(
381
+ `Transaction populated successfully
412
382
  To: ${populatedTx.to}
413
383
  From: ${populatedTx.from}
414
384
  Nonce: ${populatedTx.nonce?.toString() ?? "Not specified"}
@@ -422,7 +392,7 @@ export const populateTransactionHandler = async (input: any): Promise<ToolResult
422
392
 
423
393
  // Signing Methods
424
394
 
425
- export const signMessageHandler = async (input: any): Promise<ToolResultSchema<any>> => {
395
+ export const signMessageHandler = async (input: any): Promise<ToolResultSchema> => {
426
396
  try {
427
397
  if (!input.message) {
428
398
  return createErrorResponse("Message is required");
@@ -431,10 +401,7 @@ export const signMessageHandler = async (input: any): Promise<ToolResultSchema<a
431
401
  const wallet = await getWallet(input.wallet, input.password);
432
402
  const signature = await wallet.signMessage(input.message);
433
403
 
434
- return createSuccessResponse({
435
- signature,
436
- message: input.message
437
- }, `Message signed successfully
404
+ return createSuccessResponse(`Message signed successfully
438
405
  Signature: ${signature}
439
406
  Message: "${input.message}"
440
407
  `);
@@ -443,7 +410,7 @@ export const signMessageHandler = async (input: any): Promise<ToolResultSchema<a
443
410
  }
444
411
  };
445
412
 
446
- export const signTypedDataHandler = async (input: any): Promise<ToolResultSchema<any>> => {
413
+ export const signTypedDataHandler = async (input: any): Promise<ToolResultSchema> => {
447
414
  try {
448
415
  if (!input.wallet) {
449
416
  return createErrorResponse("Wallet is required");
@@ -459,12 +426,8 @@ export const signTypedDataHandler = async (input: any): Promise<ToolResultSchema
459
426
  // @ts-ignore - _signTypedData is not in the type definitions but is available
460
427
  const signature = await wallet._signTypedData(input.domain, input.types, input.value);
461
428
 
462
- return createSuccessResponse({
463
- signature,
464
- domain: input.domain,
465
- types: input.types,
466
- value: input.value
467
- }, `Typed data signed successfully
429
+ return createSuccessResponse(
430
+ `Typed data signed successfully
468
431
  Signature: ${signature}
469
432
  Domain: ${input.domain}
470
433
  Types: ${input.types}
@@ -475,7 +438,7 @@ export const signTypedDataHandler = async (input: any): Promise<ToolResultSchema
475
438
  }
476
439
  };
477
440
 
478
- export const verifyMessageHandler = async (input: any): Promise<ToolResultSchema<any>> => {
441
+ export const verifyMessageHandler = async (input: any): Promise<ToolResultSchema> => {
479
442
  try {
480
443
  if (!input.message || !input.signature || !input.address) {
481
444
  return createErrorResponse("Message, signature, and address are required");
@@ -484,10 +447,8 @@ export const verifyMessageHandler = async (input: any): Promise<ToolResultSchema
484
447
  const recoveredAddress = ethers.utils.verifyMessage(input.message, input.signature);
485
448
  const isValid = recoveredAddress.toLowerCase() === input.address.toLowerCase();
486
449
 
487
- return createSuccessResponse({
488
- isValid,
489
- recoveredAddress
490
- }, isValid ? `Signature verified successfully
450
+ return createSuccessResponse(
451
+ isValid ? `Signature verified successfully
491
452
  Message: "${input.message}"
492
453
  Signature: ${input.signature}
493
454
  Address: ${input.address}
@@ -501,7 +462,7 @@ export const verifyMessageHandler = async (input: any): Promise<ToolResultSchema
501
462
  }
502
463
  };
503
464
 
504
- export const verifyTypedDataHandler = async (input: any): Promise<ToolResultSchema<any>> => {
465
+ export const verifyTypedDataHandler = async (input: any): Promise<ToolResultSchema> => {
505
466
  try {
506
467
  if (!input.domain || !input.types || !input.value || !input.signature || !input.address) {
507
468
  return createErrorResponse("Domain, types, value, signature, and address are required");
@@ -517,10 +478,8 @@ export const verifyTypedDataHandler = async (input: any): Promise<ToolResultSche
517
478
 
518
479
  const isValid = recoveredAddress.toLowerCase() === input.address.toLowerCase();
519
480
 
520
- return createSuccessResponse({
521
- isValid,
522
- recoveredAddress
523
- }, isValid ? `Typed data signature is valid
481
+ return createSuccessResponse(
482
+ isValid ? `Typed data signature is valid
524
483
  Domain: ${input.domain}
525
484
  Types: ${input.types}
526
485
  Value: ${input.value}
@@ -534,24 +493,19 @@ export const verifyTypedDataHandler = async (input: any): Promise<ToolResultSche
534
493
 
535
494
  // Provider Methods
536
495
 
537
- export const getBlockHandler = async (input: any): Promise<ToolResultSchema<any>> => {
496
+ export const getBlockHandler = async (input: any): Promise<ToolResultSchema> => {
538
497
  try {
539
- if (!input.provider) {
540
- return createErrorResponse("Provider is required");
541
- }
542
-
543
498
  if (!input.blockHashOrBlockTag) {
544
499
  return createErrorResponse("Block hash or block tag is required");
545
500
  }
546
501
 
547
- const provider = getProvider(input.provider);
502
+ const provider = getProvider();
548
503
  // In ethers.js v5, getBlock can take includeTransactions as a second parameter
549
504
  // but TypeScript definitions might not reflect this
550
505
  const block = await (provider as any).getBlock(input.blockHashOrBlockTag, input.includeTransactions);
551
506
 
552
- return createSuccessResponse({
553
- block
554
- }, `Block retrieved successfully
507
+ return createSuccessResponse(
508
+ `Block retrieved successfully
555
509
  Block hash: ${block.hash}
556
510
  Block number: ${block.number?.toString() ?? "Not specified"}
557
511
  Block timestamp: ${block.timestamp?.toString() ?? "Not specified"}
@@ -562,22 +516,17 @@ export const getBlockHandler = async (input: any): Promise<ToolResultSchema<any>
562
516
  }
563
517
  };
564
518
 
565
- export const getTransactionHandler = async (input: any): Promise<ToolResultSchema<any>> => {
519
+ export const getTransactionHandler = async (input: any): Promise<ToolResultSchema> => {
566
520
  try {
567
- if (!input.provider) {
568
- return createErrorResponse("Provider is required");
569
- }
570
-
571
521
  if (!input.transactionHash) {
572
522
  return createErrorResponse("Transaction hash is required");
573
523
  }
574
524
 
575
- const provider = getProvider(input.provider);
525
+ const provider = getProvider();
576
526
  const transaction = await provider.getTransaction(input.transactionHash);
577
527
 
578
- return createSuccessResponse({
579
- transaction
580
- }, `Transaction retrieved successfully
528
+ return createSuccessResponse(
529
+ `Transaction retrieved successfully
581
530
  Transaction hash: ${input.transactionHash}
582
531
  Transaction: ${transaction}
583
532
  `);
@@ -586,22 +535,17 @@ export const getTransactionHandler = async (input: any): Promise<ToolResultSchem
586
535
  }
587
536
  };
588
537
 
589
- export const getTransactionReceiptHandler = async (input: any): Promise<ToolResultSchema<any>> => {
538
+ export const getTransactionReceiptHandler = async (input: any): Promise<ToolResultSchema> => {
590
539
  try {
591
- if (!input.provider) {
592
- return createErrorResponse("Provider is required");
593
- }
594
-
595
540
  if (!input.transactionHash) {
596
541
  return createErrorResponse("Transaction hash is required");
597
542
  }
598
543
 
599
- const provider = getProvider(input.provider);
544
+ const provider = getProvider();
600
545
  const receipt = await provider.getTransactionReceipt(input.transactionHash);
601
546
 
602
- return createSuccessResponse({
603
- receipt
604
- }, `Transaction receipt retrieved successfully
547
+ return createSuccessResponse(
548
+ `Transaction receipt retrieved successfully
605
549
  Transaction hash: ${input.transactionHash}
606
550
  Transaction receipt: ${receipt}
607
551
  `);
@@ -610,22 +554,17 @@ export const getTransactionReceiptHandler = async (input: any): Promise<ToolResu
610
554
  }
611
555
  };
612
556
 
613
- export const getCodeHandler = async (input: any): Promise<ToolResultSchema<any>> => {
557
+ export const getCodeHandler = async (input: any): Promise<ToolResultSchema> => {
614
558
  try {
615
- if (!input.provider) {
616
- return createErrorResponse("Provider is required");
617
- }
618
-
619
559
  if (!input.address) {
620
560
  return createErrorResponse("Address is required");
621
561
  }
622
562
 
623
- const provider = getProvider(input.provider);
563
+ const provider = getProvider();
624
564
  const code = await provider.getCode(input.address, input.blockTag);
625
565
 
626
- return createSuccessResponse({
627
- code
628
- }, `Code retrieved successfully
566
+ return createSuccessResponse(
567
+ `Code retrieved successfully
629
568
  Address: ${input.address}
630
569
  Code: ${code}
631
570
  `);
@@ -634,12 +573,8 @@ export const getCodeHandler = async (input: any): Promise<ToolResultSchema<any>>
634
573
  }
635
574
  };
636
575
 
637
- export const getStorageAtHandler = async (input: any): Promise<ToolResultSchema<any>> => {
576
+ export const getStorageAtHandler = async (input: any): Promise<ToolResultSchema> => {
638
577
  try {
639
- if (!input.provider) {
640
- return createErrorResponse("Provider is required");
641
- }
642
-
643
578
  if (!input.address) {
644
579
  return createErrorResponse("Address is required");
645
580
  }
@@ -648,12 +583,11 @@ export const getStorageAtHandler = async (input: any): Promise<ToolResultSchema<
648
583
  return createErrorResponse("Position is required");
649
584
  }
650
585
 
651
- const provider = getProvider(input.provider);
586
+ const provider = getProvider();
652
587
  const storage = await provider.getStorageAt(input.address, input.position, input.blockTag);
653
588
 
654
- return createSuccessResponse({
655
- storage
656
- }, `Storage retrieved successfully
589
+ return createSuccessResponse(
590
+ `Storage retrieved successfully
657
591
  Address: ${input.address}
658
592
  Position: ${input.position}
659
593
  Storage: ${storage}
@@ -663,22 +597,20 @@ export const getStorageAtHandler = async (input: any): Promise<ToolResultSchema<
663
597
  }
664
598
  };
665
599
 
666
- export const estimateGasHandler = async (input: any): Promise<ToolResultSchema<any>> => {
600
+ export const estimateGasHandler = async (input: any): Promise<ToolResultSchema> => {
667
601
  try {
668
- if (!input.provider) {
669
- return createErrorResponse("Provider is required");
670
- }
671
-
672
602
  if (!input.transaction) {
673
603
  return createErrorResponse("Transaction is required");
674
604
  }
675
605
 
676
- const provider = getProvider(input.provider);
606
+ const provider = getProvider();
607
+ if (!provider) {
608
+ return createErrorResponse("Provider is required to estimate gas, please set the provider URL");
609
+ }
677
610
  const gasEstimate = await provider.estimateGas(input.transaction);
678
611
 
679
- return createSuccessResponse({
680
- gasEstimate: gasEstimate.toString()
681
- }, `Gas estimate retrieved successfully
612
+ return createSuccessResponse(
613
+ `Gas estimate retrieved successfully
682
614
  Gas estimate: ${gasEstimate.toString()}
683
615
  `);
684
616
  } catch (error) {
@@ -686,22 +618,20 @@ export const estimateGasHandler = async (input: any): Promise<ToolResultSchema<a
686
618
  }
687
619
  };
688
620
 
689
- export const getLogsHandler = async (input: any): Promise<ToolResultSchema<any>> => {
621
+ export const getLogsHandler = async (input: any): Promise<ToolResultSchema> => {
690
622
  try {
691
- if (!input.provider) {
692
- return createErrorResponse("Provider is required");
693
- }
694
-
695
623
  if (!input.filter) {
696
624
  return createErrorResponse("Filter is required");
697
625
  }
698
626
 
699
- const provider = getProvider(input.provider);
627
+ const provider = getProvider();
628
+ if (!provider) {
629
+ return createErrorResponse("Provider is required to get logs, please set the provider URL");
630
+ }
700
631
  const logs = await provider.getLogs(input.filter);
701
632
 
702
- return createSuccessResponse({
703
- logs
704
- }, `Logs retrieved successfully
633
+ return createSuccessResponse(
634
+ `Logs retrieved successfully
705
635
  Logs: ${logs}
706
636
  `);
707
637
  } catch (error) {
@@ -709,27 +639,22 @@ export const getLogsHandler = async (input: any): Promise<ToolResultSchema<any>>
709
639
  }
710
640
  };
711
641
 
712
- export const getEnsResolverHandler = async (input: any): Promise<ToolResultSchema<any>> => {
642
+ export const getEnsResolverHandler = async (input: any): Promise<ToolResultSchema> => {
713
643
  try {
714
- if (!input.provider) {
715
- return createErrorResponse("Provider is required");
716
- }
717
-
718
644
  if (!input.name) {
719
645
  return createErrorResponse("ENS name is required");
720
646
  }
721
647
 
722
- const provider = getProvider(input.provider);
648
+ const provider = getProvider();
649
+ if (!provider) {
650
+ return createErrorResponse("Provider is required to get ENS resolver, please set the provider URL");
651
+ }
723
652
  // In ethers.js v5, getResolver might not be directly on the provider type
724
653
  // but it's available in the implementation
725
654
  const resolver = await (provider as any).getResolver(input.name);
726
655
 
727
- return createSuccessResponse({
728
- resolver: resolver ? {
729
- address: resolver.address,
730
- name: resolver.name
731
- } : null
732
- }, resolver ? `ENS resolver retrieved successfully
656
+ return createSuccessResponse(
657
+ resolver ? `ENS resolver retrieved successfully
733
658
  Address: ${resolver.address}
734
659
  Name: ${resolver.name}
735
660
  ` : "No resolver found for this ENS name");
@@ -738,22 +663,20 @@ export const getEnsResolverHandler = async (input: any): Promise<ToolResultSchem
738
663
  }
739
664
  };
740
665
 
741
- export const lookupAddressHandler = async (input: any): Promise<ToolResultSchema<any>> => {
666
+ export const lookupAddressHandler = async (input: any): Promise<ToolResultSchema> => {
742
667
  try {
743
- if (!input.provider) {
744
- return createErrorResponse("Provider is required");
745
- }
746
-
747
668
  if (!input.address) {
748
669
  return createErrorResponse("Address is required");
749
670
  }
750
671
 
751
- const provider = getProvider(input.provider);
672
+ const provider = getProvider();
673
+ if (!provider) {
674
+ return createErrorResponse("Provider is required to lookup ENS name, please set the provider URL");
675
+ }
752
676
  const name = await provider.lookupAddress(input.address);
753
677
 
754
- return createSuccessResponse({
755
- name
756
- }, name ? `ENS name retrieved successfully
678
+ return createSuccessResponse(
679
+ name ? `ENS name retrieved successfully
757
680
  Name: ${name}
758
681
  ` : "No ENS name found for this address");
759
682
  } catch (error) {
@@ -761,22 +684,20 @@ export const lookupAddressHandler = async (input: any): Promise<ToolResultSchema
761
684
  }
762
685
  };
763
686
 
764
- export const resolveNameHandler = async (input: any): Promise<ToolResultSchema<any>> => {
687
+ export const resolveNameHandler = async (input: any): Promise<ToolResultSchema> => {
765
688
  try {
766
- if (!input.provider) {
767
- return createErrorResponse("Provider is required");
768
- }
769
-
770
689
  if (!input.name) {
771
690
  return createErrorResponse("ENS name is required");
772
691
  }
773
692
 
774
- const provider = getProvider(input.provider);
693
+ const provider = getProvider();
694
+ if (!provider) {
695
+ return createErrorResponse("Provider is required to resolve ENS name, please set the provider URL");
696
+ }
775
697
  const address = await provider.resolveName(input.name);
776
698
 
777
- return createSuccessResponse({
778
- address
779
- }, address ? `ENS name resolved successfully
699
+ return createSuccessResponse(
700
+ address ? `ENS name resolved successfully
780
701
  Name: ${input.name}
781
702
  Address: ${address}
782
703
  ` : "Could not resolve this ENS name");
@@ -787,22 +708,15 @@ export const resolveNameHandler = async (input: any): Promise<ToolResultSchema<a
787
708
 
788
709
  // Network Methods
789
710
 
790
- export const getNetworkHandler = async (input: any): Promise<ToolResultSchema<any>> => {
711
+ export const getNetworkHandler = async (input: any): Promise<ToolResultSchema> => {
791
712
  try {
792
- if (!input.provider) {
793
- return createErrorResponse("Provider is required");
713
+ const provider = getProvider();
714
+ if (!provider) {
715
+ return createErrorResponse("Provider is required to get network information, please set the provider URL");
794
716
  }
795
-
796
- const provider = getProvider(input.provider);
797
717
  const network = await provider.getNetwork();
798
718
 
799
- return createSuccessResponse({
800
- network: {
801
- name: network.name,
802
- chainId: network.chainId,
803
- ensAddress: network.ensAddress
804
- }
805
- }, `Network information retrieved successfully
719
+ return createSuccessResponse(`Network information retrieved successfully
806
720
  Network name: ${network.name}
807
721
  Chain ID: ${network.chainId}
808
722
  ENS address: ${network.ensAddress}
@@ -812,18 +726,16 @@ export const getNetworkHandler = async (input: any): Promise<ToolResultSchema<an
812
726
  }
813
727
  };
814
728
 
815
- export const getBlockNumberHandler = async (input: any): Promise<ToolResultSchema<any>> => {
729
+ export const getBlockNumberHandler = async (input: any): Promise<ToolResultSchema> => {
816
730
  try {
817
- if (!input.provider) {
818
- return createErrorResponse("Provider is required");
731
+ const provider = getProvider();
732
+ if (!provider) {
733
+ return createErrorResponse("Provider is required to get block number, please set the provider URL");
819
734
  }
820
-
821
- const provider = getProvider(input.provider);
822
735
  const blockNumber = await provider.getBlockNumber();
823
736
 
824
- return createSuccessResponse({
825
- blockNumber
826
- }, `Block number retrieved successfully
737
+ return createSuccessResponse(
738
+ `Block number retrieved successfully
827
739
  Block number: ${blockNumber.toString()}
828
740
  `);
829
741
  } catch (error) {
@@ -831,25 +743,17 @@ export const getBlockNumberHandler = async (input: any): Promise<ToolResultSchem
831
743
  }
832
744
  };
833
745
 
834
- export const getFeeDataHandler = async (input: any): Promise<ToolResultSchema<any>> => {
746
+ export const getFeeDataHandler = async (input: any): Promise<ToolResultSchema> => {
835
747
  try {
836
- if (!input.provider) {
837
- return createErrorResponse("Provider is required");
748
+ const provider = getProvider();
749
+ if (!provider) {
750
+ return createErrorResponse("Provider is required to get fee data, please set the provider URL");
838
751
  }
839
-
840
- const provider = getProvider(input.provider);
841
-
842
752
  // getFeeData is available in ethers v5.5.0+
843
753
  // @ts-ignore - getFeeData might not be in the type definitions depending on the version
844
754
  const feeData = await provider.getFeeData();
845
755
 
846
- return createSuccessResponse({
847
- feeData: {
848
- gasPrice: feeData.gasPrice?.toString(),
849
- maxFeePerGas: feeData.maxFeePerGas?.toString(),
850
- maxPriorityFeePerGas: feeData.maxPriorityFeePerGas?.toString()
851
- }
852
- }, `Fee data retrieved successfully
756
+ return createSuccessResponse(`Fee data retrieved successfully
853
757
  Gas price: ${feeData.gasPrice?.toString()}
854
758
  Max fee per gas: ${feeData.maxFeePerGas?.toString()}
855
759
  Max priority fee per gas: ${feeData.maxPriorityFeePerGas?.toString()}