Skip to content

Commit befc357

Browse files
committed
feat: adding get_address_from_private_key tool
1 parent 3938549 commit befc357

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

src/core/operations/clients.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import {
44
http,
55
type PublicClient,
66
type WalletClient,
7-
type Hex
7+
type Hex,
8+
type Address
89
} from 'viem';
910
import { privateKeyToAccount } from 'viem/accounts';
1011
import { getChain, getRpcUrl } from '../chains.js';
@@ -51,4 +52,14 @@ export function getWalletClient(privateKey: Hex, network = 'ethereum'): WalletCl
5152
chain,
5253
transport: http(rpcUrl)
5354
});
55+
}
56+
57+
/**
58+
* Get an Ethereum address from a private key
59+
* @param privateKey The private key in hex format (with or without 0x prefix)
60+
* @returns The Ethereum address derived from the private key
61+
*/
62+
export function getAddressFromPrivateKey(privateKey: Hex): Address {
63+
const account = privateKeyToAccount(privateKey);
64+
return account.address;
5465
}

src/core/tools.ts

+37
Original file line numberDiff line numberDiff line change
@@ -1156,4 +1156,41 @@ export function registerEVMTools(server: McpServer) {
11561156
}
11571157
}
11581158
);
1159+
1160+
// WALLET TOOLS
1161+
1162+
// Get address from private key
1163+
server.tool(
1164+
"get_address_from_private_key",
1165+
"Get the EVM address derived from a private key",
1166+
{
1167+
privateKey: z.string().describe("Private key in hex format (with or without 0x prefix). SECURITY: This is used only for address derivation and is not stored.")
1168+
},
1169+
async ({ privateKey }) => {
1170+
try {
1171+
// Ensure the private key has 0x prefix
1172+
const formattedKey = privateKey.startsWith('0x') ? privateKey as Hex : `0x${privateKey}` as Hex;
1173+
1174+
const address = operations.getAddressFromPrivateKey(formattedKey);
1175+
1176+
return {
1177+
content: [{
1178+
type: "text",
1179+
text: JSON.stringify({
1180+
address,
1181+
privateKey: "0x" + privateKey.replace(/^0x/, '')
1182+
}, null, 2)
1183+
}]
1184+
};
1185+
} catch (error) {
1186+
return {
1187+
content: [{
1188+
type: "text",
1189+
text: `Error deriving address from private key: ${error instanceof Error ? error.message : String(error)}`
1190+
}],
1191+
isError: true
1192+
};
1193+
}
1194+
}
1195+
);
11591196
}

0 commit comments

Comments
 (0)