encryptNip04
Encrypt text messages using NIP-04 encryption for secure communication with Nostr contacts, requiring recipient public key and optional private key.
Instructions
Encrypt text with NIP-04 (legacy)
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| plaintext | Yes | Text to encrypt | |
| recipientPubkey | Yes | Recipient pubkey (hex or npub) | |
| privateKey | No | Private key (nsec or hex). Optional when NOSTR_BUNKER_URI is configured. |
Implementation Reference
- src/tools/dm-tools.ts:58-66 (handler)The implementation of the encryptNip04 tool function.
export async function encryptNip04Fn({ plaintext, recipientPubkey, privateKey }: z.infer<typeof encryptNip04Schema>) { const recipient = normalizePubkey(recipientPubkey); if (isBunkerMode()) { return nip04EncryptWithBunker(recipient, plaintext); } if (!privateKey) throw new Error('privateKey is required when NOSTR_BUNKER_URI is not configured'); const sk = normalizePrivateKey(privateKey); return nip04.encrypt(sk, recipient, plaintext); } - src/tools/dm-tools.ts:20-24 (schema)The Zod schema defining the inputs for encryptNip04.
export const encryptNip04Schema = z.object({ plaintext: z.string().describe('Text to encrypt'), recipientPubkey: z.string().describe('Recipient pubkey (hex or npub)'), privateKey: z.string().optional().describe(privateKeyDesc), }); - src/index.ts:125-127 (registration)Registration of the encryptNip04 tool in the MCP server.
server.tool('encryptNip04', 'Encrypt text with NIP-04 (legacy)', encryptNip04Schema.shape, async (params) => { return textResult({ encrypted: await encryptNip04Fn(params) }); });