wallet_get_private_key
Retrieve the private key for a wallet securely by providing the wallet data and optional password. Ensures access to wallet management on Ethereum and EVM-compatible chains.
Instructions
Get the wallet private key (with appropriate security warnings)
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| password | No | The password to decrypt the wallet if it's encrypted | |
| wallet | No | The wallet (private key, mnemonic, or JSON). If not provided, uses PRIVATE_KEY environment variable if set. |
Implementation Reference
- src/handlers/wallet.ts:206-217 (handler)The core handler function for the wallet_get_private_key tool. It retrieves the wallet instance using the getWallet helper and returns the private key in a formatted success response.export const getPrivateKeyHandler = async (input: any): Promise<ToolResultSchema> => { try { const wallet = await getWallet(input.wallet, input.password); return createSuccessResponse( `Wallet private key retrieved successfully: Private Key: ${wallet.privateKey} `); } catch (error) { return createErrorResponse(`Failed to get wallet private key: ${(error as Error).message}`); } };
- src/handlers/utils.ts:74-114 (helper)Key helper utility that instantiates an ethers.Wallet from input wallet data (private key, mnemonic phrase, or encrypted JSON), supports password for decryption, falls back to PRIVATE_KEY env var, and connects to the provider.export const getWallet = async ( walletData?: string, password?: string, ): Promise<ethers.Wallet> => { const provider = getProvider() // If walletData is not provided, check for PRIVATE_KEY environment variable if (!walletData && process.env.PRIVATE_KEY) { const wallet = new ethers.Wallet(process.env.PRIVATE_KEY); return provider ? wallet.connect(provider) : wallet; } // If no walletData and no environment variable, throw an error if (!walletData) { throw new Error("Wallet data is required or set PRIVATE_KEY environment variable"); } try { // Try to parse as JSON first if (walletData.startsWith("{")) { if (!password) { throw new Error("Password is required for encrypted JSON wallets"); } const wallet = await ethers.Wallet.fromEncryptedJson(walletData, password); return provider ? wallet.connect(provider) : wallet; } // Check if it's a mnemonic (12, 15, 18, 21, or 24 words) const words = walletData.trim().split(/\s+/); if ([12, 15, 18, 21, 24].includes(words.length)) { const wallet = ethers.Wallet.fromMnemonic(walletData); return provider ? wallet.connect(provider) : wallet; } // Assume it's a private key const wallet = new ethers.Wallet(walletData); return provider ? wallet.connect(provider) : wallet; } catch (error) { throw new Error(`Invalid wallet data: ${(error as Error).message}`); } };
- src/tools.ts:163-174 (schema)The input schema definition for the wallet_get_private_key tool, specifying optional wallet input and password parameters.{ name: "wallet_get_private_key", description: "Get the wallet private key (with appropriate security warnings)", inputSchema: { type: "object", properties: { wallet: { type: "string", description: "The wallet (private key, mnemonic, or JSON). If not provided, uses PRIVATE_KEY environment variable if set." }, password: { type: "string", description: "The password to decrypt the wallet if it's encrypted" } }, required: [] } },
- src/tools.ts:569-569 (registration)Maps the tool name 'wallet_get_private_key' to its handler function getPrivateKeyHandler in the central handlers dictionary."wallet_get_private_key": getPrivateKeyHandler,