diff --git a/README.md b/README.md index 42e4853..cf55159 100644 --- a/README.md +++ b/README.md @@ -75,7 +75,7 @@ Make sure to replace `` with the path to the `dist/index. "command": "node", "args": [""], "env": { - "AKASH_MNEMONIC": "", + "AKASH_MNEMONIC": "", // OR "AKASH_PRIVATE_KEY": "" "AKASH_RPC_URL": "https://rpc.akashnet.net:443" // optional, defaults to https://rpc.akashnet.net:443 } } diff --git a/src/AkashMCP.ts b/src/AkashMCP.ts index 057fb7c..e2e03d9 100644 --- a/src/AkashMCP.ts +++ b/src/AkashMCP.ts @@ -1,4 +1,4 @@ -import type { DirectSecp256k1HdWallet } from '@cosmjs/proto-signing'; +import type { DirectSecp256k1HdWallet, DirectSecp256k1Wallet } from '@cosmjs/proto-signing'; import type { SigningStargateClient } from '@cosmjs/stargate'; import { loadWalletAndClient, loadCertificate } from './utils/index.js'; import { SERVER_CONFIG } from './config.js'; @@ -22,7 +22,7 @@ import type { ToolContext } from './types/index.js'; import type { CertificatePem } from '@akashnetwork/akashjs/build/certificates/certificate-manager/CertificateManager.js'; class AkashMCP extends McpServer { - private wallet: DirectSecp256k1HdWallet | null = null; + private wallet: DirectSecp256k1HdWallet | DirectSecp256k1Wallet | null = null; private client: SigningStargateClient | null = null; private certificate: CertificatePem | null = null; diff --git a/src/config.ts b/src/config.ts index 164f2cc..40d9f9e 100644 --- a/src/config.ts +++ b/src/config.ts @@ -5,6 +5,7 @@ export const SERVER_CONFIG = { environment: process.env.NODE_ENV || 'development', rpcEndpoint: process.env.RPC_ENDPOINT || 'https://rpc.akashnet.net:443', mnemonic: process.env.AKASH_MNEMONIC || '', + privateKey: process.env.AKASH_PRIVATE_KEY || '', } as const; export type ServerConfig = typeof SERVER_CONFIG; diff --git a/src/types/index.ts b/src/types/index.ts index ed52f59..f2e056d 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -1,13 +1,13 @@ import type { z } from 'zod'; import type { SigningStargateClient } from '@cosmjs/stargate'; -import type { DirectSecp256k1HdWallet } from '@cosmjs/proto-signing'; +import type { DirectSecp256k1HdWallet, DirectSecp256k1Wallet } from '@cosmjs/proto-signing'; import type { ReadResourceCallback } from '@modelcontextprotocol/sdk/server/mcp.js'; import type { CertificatePem } from '@akashnetwork/akashjs/build/certificates/certificate-manager/CertificateManager.js'; // Tool related types export interface ToolContext { client: SigningStargateClient; - wallet: DirectSecp256k1HdWallet; + wallet: DirectSecp256k1HdWallet | DirectSecp256k1Wallet; certificate: CertificatePem; } diff --git a/src/utils/load-certificate.ts b/src/utils/load-certificate.ts index e646528..706e055 100644 --- a/src/utils/load-certificate.ts +++ b/src/utils/load-certificate.ts @@ -3,7 +3,7 @@ import path from 'path'; import { fileURLToPath } from 'url'; import * as cert from '@akashnetwork/akashjs/build/certificates/index.js'; import { certificateManager } from '@akashnetwork/akashjs/build/certificates/certificate-manager/index.js'; -import type { DirectSecp256k1HdWallet } from '@cosmjs/proto-signing'; +import type { DirectSecp256k1HdWallet, DirectSecp256k1Wallet } from '@cosmjs/proto-signing'; import type { SigningStargateClient } from '@cosmjs/stargate'; import type { CertificatePem } from '@akashnetwork/akashjs/build/certificates/certificate-manager/CertificateManager.js'; @@ -11,7 +11,7 @@ const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); export async function loadCertificate( - wallet: DirectSecp256k1HdWallet, + wallet: DirectSecp256k1HdWallet | DirectSecp256k1Wallet, client: SigningStargateClient ): Promise { const accounts = await wallet.getAccounts(); diff --git a/src/utils/load-wallet.ts b/src/utils/load-wallet.ts index cf2bb91..fbc4565 100644 --- a/src/utils/load-wallet.ts +++ b/src/utils/load-wallet.ts @@ -1,12 +1,22 @@ import { GasPrice, SigningStargateClient } from '@cosmjs/stargate'; import { getAkashTypeRegistry } from '@akashnetwork/akashjs/build/stargate/index.js'; -import { DirectSecp256k1HdWallet, Registry } from '@cosmjs/proto-signing'; +import { DirectSecp256k1HdWallet, DirectSecp256k1Wallet, Registry } from '@cosmjs/proto-signing'; import { SERVER_CONFIG } from '../config.js'; export async function loadWalletAndClient() { - const wallet = await DirectSecp256k1HdWallet.fromMnemonic(SERVER_CONFIG.mnemonic, { - prefix: 'akash', - }); + let wallet: DirectSecp256k1HdWallet | DirectSecp256k1Wallet; + + if (SERVER_CONFIG.mnemonic) { + wallet = await DirectSecp256k1HdWallet.fromMnemonic(SERVER_CONFIG.mnemonic, { + prefix: 'akash', + }); + } else if (SERVER_CONFIG.privateKey) { + const privateKeyBytes = Uint8Array.from(Buffer.from(SERVER_CONFIG.privateKey, 'hex')); + wallet = await DirectSecp256k1Wallet.fromKey(privateKeyBytes, 'akash'); + } else { + throw new Error('Either AKASH_MNEMONIC or AKASH_PRIVATE_KEY must be provided'); + } + const registry = getAkashTypeRegistry(); const client = await SigningStargateClient.connectWithSigner(SERVER_CONFIG.rpcEndpoint, wallet, {