signNote
Generate signed note events for the Nostr network using a private key. Input includes note content, tags, and metadata to create authenticated text notes compatible with Nostr MCP Server.
Instructions
Sign a note event with a private key
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| noteEvent | Yes | Unsigned note event to sign | |
| privateKey | Yes | Private key to sign the note with (hex format or nsec format) |
Implementation Reference
- note/note-tools.ts:247-292 (handler)The main handler function that implements the signNote tool logic: normalizes private key, verifies pubkey match, computes event hash, signs it, and returns the signed note event.export async function signNote( privateKey: string, noteEvent: { kind: number; content: string; tags: string[][]; created_at: number; pubkey: string; } ): Promise<{ success: boolean, message: string, signedNote?: any }> { try { // Normalize private key const normalizedPrivateKey = normalizePrivateKey(privateKey); // Verify the public key matches the private key const derivedPubkey = getPublicKeyFromPrivate(normalizedPrivateKey); if (derivedPubkey !== noteEvent.pubkey) { return { success: false, message: 'Private key does not match the public key in the note event', }; } // Get event hash and sign it const eventId = await getEventHash(noteEvent); const signature = await signEvent(eventId, normalizedPrivateKey); // Create complete signed event const signedNote = { ...noteEvent, id: eventId, sig: signature }; return { success: true, message: 'Note signed successfully', signedNote: signedNote, }; } catch (error) { return { success: false, message: `Error signing note: ${error instanceof Error ? error.message : "Unknown error"}`, }; } }
- note/note-tools.ts:44-54 (schema)Zod schema defining the input parameters for the signNote tool: privateKey and the structure of the unsigned noteEvent.// Schema for signNote tool export const signNoteToolConfig = { privateKey: z.string().describe("Private key to sign the note with (hex format or nsec format)"), noteEvent: z.object({ kind: z.number().describe("Event kind (should be 1 for text notes)"), content: z.string().describe("Content of the note"), tags: z.array(z.array(z.string())).describe("Tags array"), created_at: z.number().describe("Creation timestamp"), pubkey: z.string().describe("Public key of the author") }).describe("Unsigned note event to sign"), };
- index.ts:1459-1506 (registration)MCP server.tool() registration for the 'signNote' tool, which wraps the imported signNote handler and formats the MCP response.server.tool( "signNote", "Sign a note event with a private key", signNoteToolConfig, async ({ privateKey, noteEvent }) => { try { const result = await signNote(privateKey, noteEvent); if (result.success) { let response = `Note signed successfully!\n\n`; response += `${result.message}\n`; response += `Note ID: ${result.signedNote?.id}\n`; response += `Content: "${noteEvent.content}"\n`; response += `\nSigned Note Event:\n${JSON.stringify(result.signedNote, null, 2)}`; return { content: [ { type: "text", text: response, }, ], }; } else { return { content: [ { type: "text", text: `Failed to sign note: ${result.message}`, }, ], }; } } catch (error) { console.error("Error in signNote tool:", error); return { content: [ { type: "text", text: `Error signing note: ${error instanceof Error ? error.message : "Unknown error"}`, }, ], }; } }, );
- note/note-tools.ts:192-201 (helper)Helper function used by signNote to normalize private key from nsec bech32 to hex format.function normalizePrivateKey(privateKey: string): string { if (privateKey.startsWith('nsec')) { const decoded = nip19decode(privateKey as `${string}1${string}`); if (decoded.type !== 'nsec') { throw new Error('Invalid nsec format'); } return decoded.data; } return privateKey; }
- note/note-tools.ts:204-206 (helper)Helper function used by signNote to derive public key from private key for verification.function getPublicKeyFromPrivate(privateKey: string): string { return Buffer.from(schnorr.getPublicKey(privateKey)).toString('hex'); }