createKeypair
Generate a new Nostr keypair for secure authentication and interaction with the decentralized social network, with options to output keys in hex, npub, or both formats.
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:79-102 (handler)Core handler function that generates a new Nostr keypair using generateKeypair from 'snstr' library and returns keys in specified format (hex, npub/nsec, or both).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"}`); } }
- index.ts:1156-1202 (registration)MCP server registration of the 'createKeypair' tool, which calls the core handler and formats the MCP response.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"}`, }, ], }; } }, );
- profile/profile-tools.ts:9-12 (schema)Zod schema defining the input parameters for the createKeypair tool.// 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"), };