diff options
| author | Adrien Hopkins <[email protected]> | 2025-09-05 16:51:32 -0500 |
|---|---|---|
| committer | Adrien Hopkins <[email protected]> | 2025-09-05 16:51:32 -0500 |
| commit | 4a2af45b77bb36a81b7f29908b3b320ddb76a769 (patch) | |
| tree | d0e953075d66c09a561b6b5bb84f41cd049d7e95 /src/lib | |
| parent | 3d939751f3cdf0684d1de1c7817f7facdb496769 (diff) | |
| download | coinos-ui-4a2af45b77bb36a81b7f29908b3b320ddb76a769.tar.gz coinos-ui-4a2af45b77bb36a81b7f29908b3b320ddb76a769.zip | |
Seal with NIP-07 if available
This makes the system work with any nostr extensions you might be using.
Diffstat (limited to 'src/lib')
| -rw-r--r-- | src/lib/nip17.ts | 23 |
1 files changed, 21 insertions, 2 deletions
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); |
