@sei-js/mcp-server
Version:
Model Context Protocol (MCP) server for interacting with EVM-compatible networks
48 lines (47 loc) • 1.86 kB
JavaScript
import { createWalletClient, http } from 'viem';
import { privateKeyToAccount } from 'viem/accounts';
import { getChain, getRpcUrl } from '../../chains.js';
import { getPrivateKeyAsHex } from '../../config.js';
import { WalletProviderError } from '../types.js';
/**
* Private Key Wallet Provider
* Uses a private key from environment variables
*/
export class PrivateKeyWalletProvider {
constructor() {
this.privateKey = getPrivateKeyAsHex();
}
isAvailable() {
return this.privateKey !== undefined;
}
async getAddress() {
if (!this.privateKey) {
throw new WalletProviderError('Private key not configured. Set PRIVATE_KEY environment variable.', 'private-key', 'MISSING_PRIVATE_KEY');
}
const account = privateKeyToAccount(this.privateKey);
return account.address;
}
async signTransaction(tx) {
if (!this.privateKey) {
throw new WalletProviderError('Private key not configured. Cannot sign transaction.', 'private-key', 'MISSING_PRIVATE_KEY');
}
// For now, return a placeholder - full implementation would involve actual signing
throw new WalletProviderError('Direct transaction signing not implemented for private key provider.', 'private-key', 'NOT_IMPLEMENTED');
}
async getWalletClient(network) {
if (!this.privateKey) {
throw new WalletProviderError('Private key not configured. Cannot create wallet client.', 'private-key', 'MISSING_PRIVATE_KEY');
}
const chain = getChain(network);
const rpcUrl = getRpcUrl(network);
const account = privateKeyToAccount(this.privateKey);
return createWalletClient({
account,
chain,
transport: http(rpcUrl)
});
}
getName() {
return 'private-key';
}
}