From c136e7cf8227bef7b4d5ad9b7a266dc6dd11e20e Mon Sep 17 00:00:00 2001 From: paypes Date: Wed, 4 Jun 2025 16:41:55 +0200 Subject: [PATCH] refactor: use wallet address from env variable instead of parameter --- .github/workflows/publish-docker.yml | 4 +--- src/tools/dataProtectorCore/getUserVoucher.ts | 19 +++++++++-------- .../dataProtectorCore/getWalletBalance.ts | 21 +++++++++---------- src/tools/web3mail/fetchMyContacts.ts | 2 ++ src/tools/web3mail/sendEmail.ts | 12 +---------- 5 files changed, 24 insertions(+), 34 deletions(-) diff --git a/.github/workflows/publish-docker.yml b/.github/workflows/publish-docker.yml index 6e6d821..07edb57 100644 --- a/.github/workflows/publish-docker.yml +++ b/.github/workflows/publish-docker.yml @@ -42,9 +42,7 @@ jobs: with: context: . push: true - tags: | - iexechub/mcp-server:latest - iexechub/mcp-server:v${{ steps.version.outputs.version }} + tags: iexechub/mcp-server:v${{ steps.version.outputs.version }} cache-from: type=gha cache-to: type=gha,mode=max diff --git a/src/tools/dataProtectorCore/getUserVoucher.ts b/src/tools/dataProtectorCore/getUserVoucher.ts index 57c07eb..842205e 100644 --- a/src/tools/dataProtectorCore/getUserVoucher.ts +++ b/src/tools/dataProtectorCore/getUserVoucher.ts @@ -1,5 +1,7 @@ import { McpError, ErrorCode } from "@modelcontextprotocol/sdk/types.js"; import { getIexecProvider } from "../../utils/provider.js"; +import { readWalletPrivateKey } from "../../utils/readWalletKeystore.js"; +import { Wallet } from "ethers"; export const getUserVoucher = { name: "get_user_voucher", @@ -7,18 +9,17 @@ export const getUserVoucher = { inputSchema: { type: "object", properties: { - wallet: { type: "string" } + query: { type: "string" }, }, - required: ["wallet"], + required: [], }, - handler: async (params: any) => { - const { wallet } = params; + handler: async () => { - if (!wallet || !wallet.match(/^0x[a-fA-F0-9]{40}$/)) { - throw new McpError(ErrorCode.InvalidParams, "Invalid or missing wallet address"); - } + const privateKey = await readWalletPrivateKey(); + const wallet = new Wallet(privateKey); try { + const iexecResponse = await getIexecProvider(); if (!iexecResponse.success || !iexecResponse.data) { throw new Error(iexecResponse.error || "Failed to initialize iExec SDK"); @@ -26,7 +27,7 @@ export const getUserVoucher = { const iexec = iexecResponse.data.iexec; - const userVoucher = await iexec.voucher.showUserVoucher(wallet); + const userVoucher = await iexec.voucher.showUserVoucher(wallet.address); return { address: userVoucher.address, @@ -41,7 +42,7 @@ export const getUserVoucher = { } catch (error: any) { if (error.message.includes("No Voucher found")) { return { - message: `No voucher found for wallet ${wallet}. Go to iExec discord to claim your voucher: https://discord.com/invite/aXH5ym5H4k` + message: `No voucher found for wallet ${wallet.address}. Go to iExec discord to claim your voucher: https://discord.com/invite/aXH5ym5H4k` }; } throw new McpError(ErrorCode.InternalError, error.message); diff --git a/src/tools/dataProtectorCore/getWalletBalance.ts b/src/tools/dataProtectorCore/getWalletBalance.ts index daebeaa..6a26370 100644 --- a/src/tools/dataProtectorCore/getWalletBalance.ts +++ b/src/tools/dataProtectorCore/getWalletBalance.ts @@ -1,5 +1,7 @@ import { McpError, ErrorCode } from "@modelcontextprotocol/sdk/types.js"; import { getIexecProvider } from "../../utils/provider.js"; +import { readWalletPrivateKey } from "../../utils/readWalletKeystore.js"; +import { Wallet } from "ethers"; export const getWalletBalance = { name: "get_wallet_balance", @@ -7,16 +9,13 @@ export const getWalletBalance = { inputSchema: { type: "object", properties: { - wallet: { type: "string" } + query: { type: "string" } }, - required: ["wallet"], + required: [], }, - handler: async (params: any) => { - const { wallet } = params; - - if (!wallet || !wallet.match(/^0x[a-fA-F0-9]{40}$/)) { - throw new McpError(ErrorCode.InvalidParams, "Invalid or missing wallet address"); - } + handler: async () => { + const privateKey = await readWalletPrivateKey(); + const wallet = new Wallet(privateKey); try { const iexecResponse = await getIexecProvider(); @@ -26,15 +25,15 @@ export const getWalletBalance = { const iexec = iexecResponse.data.iexec; - const balance = await iexec.account.checkBalance(wallet); + const balance = await iexec.account.checkBalance(wallet.address); const stakeRLC = Number(balance.stake) * 1e-9; const lockedRLC = Number(balance.locked) * 1e-9; - const { nRLC } = await iexec.wallet.checkBalances(wallet); + const { nRLC } = await iexec.wallet.checkBalances(wallet.address); const onChainRLC = Number(nRLC) * 1e-9; return { - wallet, + wallet: wallet.address, onChainRLC, stakeRLC, lockedRLC diff --git a/src/tools/web3mail/fetchMyContacts.ts b/src/tools/web3mail/fetchMyContacts.ts index 77e1585..40aceff 100644 --- a/src/tools/web3mail/fetchMyContacts.ts +++ b/src/tools/web3mail/fetchMyContacts.ts @@ -8,7 +8,9 @@ export const fetchMyContacts = { inputSchema: { type: "object", properties: { + query: { type: "string" }, }, + required: [], }, handler: async () => { try { diff --git a/src/tools/web3mail/sendEmail.ts b/src/tools/web3mail/sendEmail.ts index 8078349..5dc18f9 100644 --- a/src/tools/web3mail/sendEmail.ts +++ b/src/tools/web3mail/sendEmail.ts @@ -1,5 +1,5 @@ import { McpError, ErrorCode } from '@modelcontextprotocol/sdk/types.js'; -import { getWeb3Provider, IExecWeb3mail, WorkflowError } from '@iexec/web3mail'; +import { getWeb3Provider, IExecWeb3mail } from '@iexec/web3mail'; import { readWalletPrivateKey } from '../../utils/readWalletKeystore.js'; export const sendEmail = { @@ -58,21 +58,11 @@ export const sendEmail = { if (contentType !== undefined) sendEmailParams.contentType = contentType; if (label !== undefined) sendEmailParams.label = label; if (useVoucher !== undefined) sendEmailParams.useVoucher = useVoucher; - console.error("Send email params:", sendEmailParams); const response = await web3mail.sendEmail(sendEmailParams); - return response; } catch (error: any) { console.error("Error sending email:", error); throw new McpError(ErrorCode.InternalError, error); - if (error instanceof WorkflowError) { - if (error.isProtocolError) { - throw new McpError(ErrorCode.InternalError, error.message); - } else { - throw new McpError(ErrorCode.InvalidParams, error.message); - } - } - throw new McpError(ErrorCode.InternalError, error.message); } }, };