Enviar y leer mensajes privados
Enviar y leer mensajes privados
Usa @nostr-wot/dm para construir mensajes NIP-17 y verificar su apertura. Ambos participantes necesitan firmantes compatibles con NIP-44. El ejemplo prepara un envoltorio para el destinatario y devuelve contenido legible al descifrarlo.
terminal
$npm install @nostr-wot/dm@2.0.0 nostr-wot-sdk@3.0.0 nostr-tools
Ejemplo
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 };
}Notas de integración
Publica el evento de tipo 1059 en los relays de entrada del destinatario, descubiertos mediante el tipo 10050. Crea otro envoltorio para el historial del emisor. No publiques directamente el mensaje interior ni el sello. La etiqueta exterior del destinatario sigue siendo visible. Usa /cache y /react para una bandeja con historial y cancela las suscripciones al salir.