encryptNip04
Encrypt text messages for private communication on Nostr using the NIP-04 standard. Secures plaintext with recipient's public key for confidential direct messaging.
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 encryptNip04Fn function handles the NIP-04 encryption logic, either by delegating to a bunker signer or using the nostr-tools library if a private key is provided.
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 encryptNip04Schema defines the input validation for the encryptNip04 tool, requiring plaintext, a recipient pubkey, and an optional private key.
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-126 (registration)The encryptNip04 tool is registered in the MCP server using server.tool, mapping the schema and handler function.
server.tool('encryptNip04', 'Encrypt text with NIP-04 (legacy)', encryptNip04Schema.shape, async (params) => { return textResult({ encrypted: await encryptNip04Fn(params) });