getPublicKey is functional!

This commit is contained in:
Ryan Breen
2023-01-16 22:17:08 -05:00
parent 4fee1c9e81
commit 495cea8560
3 changed files with 38 additions and 3 deletions

View File

@@ -2,6 +2,12 @@ import { generatePrivateKey, getPublicKey } from "nostr-tools";
const storage = browser.storage.local; const storage = browser.storage.local;
browser.runtime.onMessageExternal.addListener(async (message, sender, sendResponse) => {
console.log('External message: ', message);
console.log('External sender: ', sender);
sendResponse('Goodbye!');
});
browser.runtime.onMessage.addListener(async (message, _sender, sendResponse) => { browser.runtime.onMessage.addListener(async (message, _sender, sendResponse) => {
console.log(message); console.log(message);

View File

@@ -1,3 +1,15 @@
let script = document.createElement('script'); let script = document.createElement('script');
script.setAttribute('src', browser.runtime.getURL('nostr.build.js')); script.setAttribute('src', browser.runtime.getURL('nostr.build.js'));
document.body.appendChild(script); document.body.appendChild(script);
window.addEventListener('message', async (message) => {
let {kind, reqId} = message.data;
if (kind !== 'getPublicKey')
return;
console.log(`Event ${reqId}: Content script received message kind ${kind}`);
let publicKey = await browser.runtime.sendMessage({kind: 'getPubKey'});
console.log(`Event ${reqId}: Public key retrieved; ${publicKey}`);
window.postMessage({kind: 'publicKey', reqId, payload: publicKey}, '*');
});

View File

@@ -1,7 +1,13 @@
window.nostr = { window.nostr = {
async getPublicKey() { requests: {},
console.log("getting public key!");
return "285d4ca25cbe209832aa15a4b94353b877a2fe6c3b94dee1a4c8bc36770304db"; getPublicKey() {
let reqId = Math.random().toString();
return new Promise((resolve, _reject) => {
this.requests[reqId] = resolve;
console.log(`Event ${reqId}: Requesting public key.`);
window.postMessage({kind: 'getPublicKey', reqId}, '*');
});
}, },
async signEvent(event) { async signEvent(event) {
@@ -10,3 +16,14 @@ window.nostr = {
return "signed event"; return "signed event";
} }
} }
window.addEventListener('message', (message) => {
let {kind, reqId, payload} = message.data;
if (kind !== 'publicKey')
return;
console.log(`Event ${reqId}: Received public key ${payload}.`);
window.nostr.requests[reqId](payload);
delete window.nostr.requests[reqId];
});