createKeypair
Generates a Nostr-compatible keypair in specified formats (hex, npub, or both) to facilitate secure interactions with the Nostr social network via the Nostr MCP Server.
Instructions
Generate a new Nostr keypair
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| format | No | Format to return keys in: hex only, npub only, or both | both |
Implementation Reference
- profile/profile-tools.ts:76-102 (handler)The core handler function that generates a new Nostr keypair using snstr's generateKeypair(), formats the output based on the requested format (hex, npub, or both), and returns the keys./** * Generate a new Nostr keypair */ export async function createKeypair( format: "both" | "hex" | "npub" = "both" ): Promise<{ publicKey?: string, privateKey?: string, npub?: string, nsec?: string }> { try { // Generate a new keypair const keys = await generateKeypair(); const result: { publicKey?: string, privateKey?: string, npub?: string, nsec?: string } = {}; if (format === "hex" || format === "both") { result.publicKey = keys.publicKey; result.privateKey = keys.privateKey; } if (format === "npub" || format === "both") { result.npub = encodePublicKey(keys.publicKey); result.nsec = encodePrivateKey(keys.privateKey); } return result; } catch (error) { throw new Error(`Failed to generate keypair: ${error instanceof Error ? error.message : "Unknown error"}`); } }
- profile/profile-tools.ts:9-12 (schema)Zod schema definition for the createKeypair tool input parameters, specifying the output format.// Schema for createKeypair tool export const createKeypairToolConfig = { format: z.enum(["both", "hex", "npub"]).default("both").describe("Format to return keys in: hex only, npub only, or both"), };
- index.ts:1156-1202 (registration)MCP server tool registration for 'createKeypair', which wraps the handler, formats the response as MCP content, and handles errors.server.tool( "createKeypair", "Generate a new Nostr keypair", createKeypairToolConfig, async ({ format }) => { try { const result = await createKeypair(format); let response = "New Nostr keypair generated:\n\n"; if (result.publicKey) { response += `Public Key (hex): ${result.publicKey}\n`; } if (result.privateKey) { response += `Private Key (hex): ${result.privateKey}\n`; } if (result.npub) { response += `Public Key (npub): ${result.npub}\n`; } if (result.nsec) { response += `Private Key (nsec): ${result.nsec}\n`; } response += "\n⚠️ IMPORTANT: Store your private key securely! This is the only copy and cannot be recovered if lost."; return { content: [ { type: "text", text: response, }, ], }; } catch (error) { console.error("Error in createKeypair tool:", error); return { content: [ { type: "text", text: `Error generating keypair: ${error instanceof Error ? error.message : "Unknown error"}`, }, ], }; } }, );