From 4a2af45b77bb36a81b7f29908b3b320ddb76a769 Mon Sep 17 00:00:00 2001 From: Adrien Hopkins Date: Fri, 5 Sep 2025 16:51:32 -0500 Subject: Seal with NIP-07 if available This makes the system work with any nostr extensions you might be using. --- src/lib/nip17.ts | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) (limited to 'src/lib') diff --git a/src/lib/nip17.ts b/src/lib/nip17.ts index 66f49639..e0046d47 100644 --- a/src/lib/nip17.ts +++ b/src/lib/nip17.ts @@ -6,9 +6,14 @@ const TWO_DAYS = 2 * 24 * 60 * 60; const randomTimestamp = () => Math.floor( Date.now() / 1000 - Math.random() * TWO_DAYS); -export const createNIP17Message = (text: string, senderSK: Uint8Array, receiverPK: string) => { +export const createNIP17Message = async (text: string, senderSK: Uint8Array, receiverPK: string) => { const rumour = createRumour(text, senderSK, receiverPK); - const sealed = sealRumour(rumour, senderSK, receiverPK); + let sealed; + if (window.nostr) { + sealed = await sealRumourNip07(rumour, receiverPK); + } else { + sealed = sealRumour(rumour, senderSK, receiverPK); + } const wrapped = giftWrap(sealed, receiverPK); return wrapped; } @@ -41,6 +46,20 @@ const sealRumour = (rumour: object, senderSK: Uint8Array, receiverPK: string) => return finalizeEvent(sealEvent, senderSK); } +const sealRumourNip07 = async (rumour: object, receiverPK: string) => { + const encryptedRumour = await window.nostr.nip44.encrypt( + receiverPK, JSON.stringify(rumour)); + + const sealEvent = { + kind: 13, // seal + created_at: randomTimestamp(), + tags: [], + content: encryptedRumour + } + + return window.nostr.signEvent(sealEvent); +} + const giftWrap = (event: object, receiverPK: string) => { const secretKey = generateSecretKey(); const SKString = bytesToHex(secretKey); -- cgit v1.2.3