createAccount.ts•2.02 kB
import { Tool } from "@modelcontextprotocol/sdk/types.js";
import { createNewAccount, getAccountAddress, getAccountPrivateKey, getAccountPublicKey } from "../../utils/account.js";
export const CREATE_ACCOUNT: Tool = {
name: "create_account",
description: "Create a new Aptos account with a random private key. This generates a new account that can be used for transactions on the Aptos blockchain. Returns the account address, private key, and public key.",
inputSchema: {
type: "object",
properties: {},
required: [],
},
};
/**
* Creates a new Aptos account
* @param args The arguments (none required)
* @returns The new account details
*/
export async function createAccountHandler(args: Record<string, any> | undefined) {
try {
const results = await performCreateAccount();
return {
content: [{ type: "text", text: results }],
isError: false,
};
} catch (error) {
return {
content: [
{
type: "text",
text: `Error creating account: ${error instanceof Error ? error.message : String(error)}`,
},
],
isError: true,
};
}
}
/**
* Creates a new Aptos account
* @returns The new account details as a formatted string
*/
export async function performCreateAccount(): Promise<string> {
try {
const account = createNewAccount();
const address = getAccountAddress(account);
const privateKey = getAccountPrivateKey(account);
const publicKey = getAccountPublicKey(account);
return `New Aptos Account Created:
Address: ${address}
Private Key: ${privateKey}
Public Key: ${publicKey}
⚠️ IMPORTANT: Save your private key securely! It cannot be recovered if lost.
⚠️ Never share your private key with anyone.
⚠️ This account needs to be funded before it can be used for transactions.`;
} catch (error) {
console.error('Error creating account:', error);
throw new Error(`Failed to create account: ${error instanceof Error ? error.message : String(error)}`);
}
}