Nostr WoT

Inviare e leggere messaggi privati

Inviare e leggere messaggi privati

@nostr-wot/dm costruisce messaggi NIP-17 e ne verifica l’apertura. Entrambi i partecipanti devono supportare NIP-44. L’esempio prepara un involucro e legge il contenuto con il firmatario del destinatario.

terminale
$npm install @nostr-wot/dm@2.0.0 nostr-wot-sdk@3.0.0 nostr-tools

Esempio

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

Note di integrazione

Pubblica il tipo 1059 sui relay di ricezione scoperti tramite il tipo 10050. Crea un involucro separato per la cronologia del mittente. Non pubblicare direttamente messaggio interno o sigillo. Il tag esterno del destinatario resta visibile. Usa /cache e /react per cronologia e viste; chiudi le sottoscrizioni al logout.

Riferimento e altre opzioni