Skip to main content
Glama

Aptos Blockchain MCP

account.ts•2.44 kB
import { Account, Ed25519PrivateKey } from "@aptos-labs/ts-sdk"; import { APTOS_MCP_PRIVATE_KEY } from "../config.js"; // Global account instance let defaultAccount: Account | null = null; /** * Get or create the default account from the private key * @returns Account instance */ export function getDefaultAccount(): Account { if (!defaultAccount) { if (!APTOS_MCP_PRIVATE_KEY) { throw new Error("APTOS_MCP_PRIVATE_KEY environment variable is required"); } try { const privateKey = new Ed25519PrivateKey(APTOS_MCP_PRIVATE_KEY); defaultAccount = Account.fromPrivateKey({ privateKey }); } catch (error) { throw new Error(`Failed to create account from private key: ${error instanceof Error ? error.message : String(error)}`); } } return defaultAccount; } /** * Create a new random account * @returns New Account instance */ export function createNewAccount(): Account { return Account.generate(); } /** * Create account from private key string * @param privateKeyHex Private key in hex format * @returns Account instance */ export function createAccountFromPrivateKey(privateKeyHex: string): Account { try { const privateKey = new Ed25519PrivateKey(privateKeyHex); return Account.fromPrivateKey({ privateKey }); } catch (error) { throw new Error(`Failed to create account from private key: ${error instanceof Error ? error.message : String(error)}`); } } /** * Get account address as string * @param account Account instance * @returns Address as hex string */ export function getAccountAddress(account: Account): string { return account.accountAddress.toString(); } /** * Get account private key as hex string * @param account Account instance * @returns Private key as hex string */ export function getAccountPrivateKey(account: Account): string { // Note: In newer versions of Aptos SDK, private key access might be restricted // This is a placeholder - in production, you should store the private key separately return "Private key access restricted for security"; } /** * Get account public key as hex string * @param account Account instance * @returns Public key as hex string */ export function getAccountPublicKey(account: Account): string { return account.publicKey.toString(); } /** * Reset the default account (useful for testing) */ export function resetDefaultAccount(): void { defaultAccount = null; }

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/punkpeye/aptos-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server