basic functionality, untested.
This commit is contained in:
@@ -1,9 +1,34 @@
|
||||
browser.runtime.onMessage.addListener((req, sender, reply) => {
|
||||
switch (req.type) {
|
||||
case 'getPublicKey':
|
||||
reply({})
|
||||
break
|
||||
case 'signEvent':
|
||||
break
|
||||
import browser from 'webextension-polyfill'
|
||||
import {validateEvent, signEvent, getPublicKey} from 'nostr-tools'
|
||||
|
||||
browser.runtime.onMessage.addListener(async (req, sender, reply) => {
|
||||
let {type, params, host} = req
|
||||
|
||||
try {
|
||||
switch (type) {
|
||||
case 'getPublicKey': {
|
||||
let results = browser.storage.local.get('private_key')
|
||||
if (results && results.private_key) {
|
||||
reply(getPublicKey(results.private_key))
|
||||
} else {
|
||||
reply({error: 'no private key found'})
|
||||
}
|
||||
break
|
||||
}
|
||||
case 'signEvent': {
|
||||
let {event} = params
|
||||
if (!validateEvent(event)) return reply({error: 'invalid event'})
|
||||
|
||||
let results = browser.storage.local.get('private_key')
|
||||
if (results && results.private_key) {
|
||||
reply(signEvent(event, results.private_key))
|
||||
} else {
|
||||
reply({error: 'no private key found'})
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
reply({error})
|
||||
}
|
||||
})
|
||||
|
||||
@@ -6,19 +6,20 @@ document.head.appendChild(script)
|
||||
// listen for messages from that script
|
||||
window.addEventListener('message', async ev => {
|
||||
if (ev.source !== window) return
|
||||
if (!ev.data || ev.data.ext !== 'nostr') {
|
||||
if (!ev.data || ev.data.ext !== 'nos2x') {
|
||||
// pass on to background
|
||||
var reply
|
||||
var response
|
||||
try {
|
||||
reply = browser.runtime.sendMessage({
|
||||
...ev.data,
|
||||
response = browser.runtime.sendMessage({
|
||||
type: ev.data.type,
|
||||
params: ev.data.params,
|
||||
host: window.location.host
|
||||
})
|
||||
} catch (error) {
|
||||
reply = {error}
|
||||
response = {error}
|
||||
}
|
||||
|
||||
// return response
|
||||
window.postMessage({id: ev.data.id, reply})
|
||||
window.postMessage({id: ev.data.id, ext: 'nos2x', response})
|
||||
}
|
||||
})
|
||||
|
||||
@@ -3,15 +3,10 @@
|
||||
"description": "Nostr Signer Extension",
|
||||
"version": "0.0.1",
|
||||
"manifest_version": 2,
|
||||
"icons": {
|
||||
"16": "icons/icon-16x16.png",
|
||||
"48": "icons/icon-48x48.png",
|
||||
"128": "icons/icon-128x128.png"
|
||||
},
|
||||
"options_page": "options.html",
|
||||
"background": {
|
||||
"scripts": [
|
||||
"background.js"
|
||||
"background.build.js"
|
||||
],
|
||||
"persistent": false
|
||||
},
|
||||
|
||||
@@ -1,4 +1,45 @@
|
||||
window.nostr = {
|
||||
getPublicKey() {},
|
||||
signEvent(event) {}
|
||||
_requests: {},
|
||||
_pubkey: null,
|
||||
|
||||
async getPublicKey() {
|
||||
if (this._pubkey) return this._pubkey
|
||||
this._pubkey = await this._call('getPublicKey')
|
||||
return this._pubkey
|
||||
},
|
||||
|
||||
async signEvent(event) {
|
||||
return this._call('signEvent', {event})
|
||||
},
|
||||
|
||||
_call(type, params) {
|
||||
return new Promise((resolve, reject) => {
|
||||
let id = Math.random().toString().slice(4)
|
||||
this._requests[id] = {resolve, reject}
|
||||
window.postMessage(
|
||||
{
|
||||
id,
|
||||
ext: 'nos2x',
|
||||
type,
|
||||
params
|
||||
},
|
||||
'*'
|
||||
)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
window.addEventListener('message', message => {
|
||||
if (
|
||||
!message.data ||
|
||||
message.data.ext !== 'nos2x' ||
|
||||
!window.nostr._requests[message.data.id]
|
||||
)
|
||||
return
|
||||
|
||||
if (message.data.response.error) {
|
||||
window.nostr._requests[message.data.id].reject(message.data.response.error)
|
||||
} else {
|
||||
window.nostr._requests[message.data.id].resolve(message.data.response)
|
||||
}
|
||||
})
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
<!DOCTYPE html>
|
||||
|
||||
<meta charset="utf-8" />
|
||||
<title>nos2x</title>
|
||||
|
||||
<label>
|
||||
Private Key:
|
||||
<input id="privateKeyInput" />
|
||||
</label>
|
||||
|
||||
<script src="options.build.js"></script>
|
||||
|
||||
15
extension/options.js
Normal file
15
extension/options.js
Normal file
@@ -0,0 +1,15 @@
|
||||
/* global document */
|
||||
|
||||
import browser from 'webextension-polyfill'
|
||||
|
||||
document.getElementById('privateKeyInput').addEventListener('input', ev => {
|
||||
browser.storage.local
|
||||
.set({private_key: document.getElementById('privateKeyInput').value})
|
||||
.then(() => {
|
||||
console.log('success')
|
||||
})
|
||||
})
|
||||
|
||||
browser.storage.local.get('private_key').then(results => {
|
||||
document.getElementById('privateKeyInput').value = results.private_key
|
||||
})
|
||||
Reference in New Issue
Block a user