Send and read private messages
Send and read private messages
Use @nostr-wot/dm for NIP-17 message construction and verified unwrapping. Both participants need NIP-44-capable signers. The example prepares a recipient gift wrap and returns readable content when the recipient decrypts it.
terminal
$npm install @nostr-wot/dm@2.0.0 nostr-wot-sdk@3.0.0 nostr-tools
Example
typescript
import { buildChatMessage, sealAndGiftWrap, unwrapGiftWrap } from "@nostr-wot/dm";
import type { NostrSigner } from "nostr-wot-sdk/signers";
export async function prepareMessage(signer: NostrSigner, recipient: string, text: string) {
const me = await signer.getPublicKey();
const message = buildChatMessage(me, recipient, text);
return sealAndGiftWrap(signer, recipient, message);
}
// Run with the recipient's signer after receiving a kind-1059 event.
export async function readMessage(signer: NostrSigner, wrap: Parameters<typeof unwrapGiftWrap>[1]) {
const { message, senderPubkey } = await unwrapGiftWrap(signer, wrap);
return { from: senderPubkey, text: message.content };
}Integration notes
Publish the returned kind-1059 event to the recipient’s inbox relays, discovered from kind 10050. Create a separate wrap for the sender’s history. Never publish the inner message or seal directly. The outer recipient tag remains visible. For an inbox with history and React views, use the /cache and /react entry points; dispose subscriptions on logout.