Skip to main content
Glama

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
NameRequiredDescriptionDefault
plaintextYesText to encrypt
recipientPubkeyYesRecipient pubkey (hex or npub)
privateKeyNoPrivate key (nsec or hex). Optional when NOSTR_BUNKER_URI is configured.

Implementation Reference

  • 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);
    }
  • 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) });

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/jorgenclaw/nostr-mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server