// Runs in the page's own JavaScript world and defines window.nostr (NIP-07). // Calls are forwarded to content.js with postMessage, then to the background // worker, which asks the user before any site can use the key. (() => { if (window.nostr) return; // another signer extension got here first const pending = new Map(); let seq = 0; window.addEventListener("message", (e) => { if (e.source !== window || e.data?.__goostr !== "res") return; const p = pending.get(e.data.id); if (!p) return; pending.delete(e.data.id); if (e.data.error) p.reject(new Error(e.data.error)); else p.resolve(e.data.result); }); const call = (method, params) => new Promise((resolve, reject) => { const id = `goostr-${++seq}-${Math.random().toString(36).slice(2)}`; pending.set(id, { resolve, reject }); window.postMessage({ __goostr: "req", id, method, params }, location.origin); }); window.nostr = { getPublicKey: () => call("getPublicKey"), signEvent: (event) => call("signEvent", { event }), getRelays: () => call("getRelays"), nip04: { encrypt: (pubkey, plaintext) => call("nip04.encrypt", { pubkey, plaintext }), decrypt: (pubkey, ciphertext) => call("nip04.decrypt", { pubkey, ciphertext }), }, nip44: { encrypt: (pubkey, plaintext) => call("nip44.encrypt", { pubkey, plaintext }), decrypt: (pubkey, ciphertext) => call("nip44.decrypt", { pubkey, ciphertext }), }, }; })();