sign events working!

This commit is contained in:
Ryan Breen
2023-01-16 23:11:51 -05:00
parent 495cea8560
commit ffbf59ad17
3 changed files with 33 additions and 25 deletions

View File

@@ -1,29 +1,31 @@
window.nostr = {
requests: {},
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 getPublicKey() {
return await this.broadcast('getPubKey');
},
async signEvent(event) {
console.log("Signing event");
console.log(event);
return "signed event";
return await this.broadcast('signEvent', event);
},
broadcast(kind, payload) {
let reqId = Math.random().toString();
return new Promise((resolve, _reject) => {
this.requests[reqId] = resolve;
console.log(`Event ${reqId}: ${kind}, payload: `, payload);
window.postMessage({kind, reqId, payload}, '*');
});
}
}
window.addEventListener('message', (message) => {
let {kind, reqId, payload} = message.data;
if (kind !== 'publicKey')
if (kind !== 'return_getPubKey' && kind !== 'return_signEvent')
return;
console.log(`Event ${reqId}: Received public key ${payload}.`);
console.log(`Event ${reqId}: Received payload:`, payload);
window.nostr.requests[reqId](payload);
delete window.nostr.requests[reqId];
});