a thousand fixes and now signEvent and getPublic are fully working.
This commit is contained in:
9
build.js
9
build.js
@@ -2,6 +2,7 @@
|
||||
|
||||
const esbuild = require('esbuild')
|
||||
const alias = require('esbuild-plugin-alias')
|
||||
const nodeGlobals = require('@esbuild-plugins/node-globals-polyfill').default
|
||||
|
||||
esbuild.build({
|
||||
bundle: true,
|
||||
@@ -14,7 +15,11 @@ esbuild.build({
|
||||
plugins: [
|
||||
alias({
|
||||
stream: require.resolve('readable-stream')
|
||||
})
|
||||
}),
|
||||
nodeGlobals()
|
||||
],
|
||||
sourcemap: 'inline'
|
||||
sourcemap: 'inline',
|
||||
define: {
|
||||
global: 'window'
|
||||
}
|
||||
})
|
||||
|
||||
@@ -1,34 +1,41 @@
|
||||
import browser from 'webextension-polyfill'
|
||||
import {validateEvent, signEvent, getPublicKey} from 'nostr-tools'
|
||||
import {Buffer} from 'buffer'
|
||||
import {validateEvent, signEvent, getEventHash, getPublicKey} from 'nostr-tools'
|
||||
|
||||
browser.runtime.onMessage.addListener(async (req, sender, reply) => {
|
||||
browser.runtime.onMessage.addListener(async (req, sender) => {
|
||||
let {type, params, host} = req
|
||||
|
||||
try {
|
||||
switch (type) {
|
||||
case 'getPublicKey': {
|
||||
let results = browser.storage.local.get('private_key')
|
||||
let results = await browser.storage.local.get('private_key')
|
||||
if (results && results.private_key) {
|
||||
reply(getPublicKey(results.private_key))
|
||||
return Buffer.from(getPublicKey(results.private_key)).toString('hex')
|
||||
} else {
|
||||
reply({error: 'no private key found'})
|
||||
return {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')
|
||||
let results = await browser.storage.local.get('private_key')
|
||||
if (results && results.private_key) {
|
||||
reply(signEvent(event, results.private_key))
|
||||
if (!event.pubkey)
|
||||
event.pubkey = Buffer.from(
|
||||
getPublicKey(results.private_key)
|
||||
).toString('hex')
|
||||
if (!event.id) event.id = getEventHash(event)
|
||||
|
||||
if (!validateEvent(event)) return {error: 'invalid event'}
|
||||
|
||||
let signature = await signEvent(event, results.private_key)
|
||||
return Buffer.from(signature).toString('hex')
|
||||
} else {
|
||||
reply({error: 'no private key found'})
|
||||
return {error: 'no private key found'}
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
reply({error})
|
||||
return {error}
|
||||
}
|
||||
})
|
||||
|
||||
@@ -8,22 +8,27 @@ script.setAttribute('src', browser.runtime.getURL('nostr-provider.js'))
|
||||
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 !== 'nos2x') {
|
||||
// pass on to background
|
||||
var response
|
||||
try {
|
||||
response = browser.runtime.sendMessage({
|
||||
type: ev.data.type,
|
||||
params: ev.data.params,
|
||||
host: window.location.host
|
||||
})
|
||||
} catch (error) {
|
||||
response = {error}
|
||||
}
|
||||
window.addEventListener('message', async message => {
|
||||
if (message.source !== window) return
|
||||
if (!message.data) return
|
||||
if (!message.data.params) return
|
||||
if (message.data.ext !== 'nos2x') return
|
||||
|
||||
// return response
|
||||
window.postMessage({id: ev.data.id, ext: 'nos2x', response})
|
||||
// pass on to background
|
||||
var response
|
||||
try {
|
||||
response = await browser.runtime.sendMessage({
|
||||
type: message.data.type,
|
||||
params: message.data.params,
|
||||
host: window.location.host
|
||||
})
|
||||
} catch (error) {
|
||||
response = {error}
|
||||
}
|
||||
|
||||
// return response
|
||||
window.postMessage(
|
||||
{id: message.data.id, ext: 'nos2x', response},
|
||||
message.origin
|
||||
)
|
||||
})
|
||||
|
||||
@@ -4,7 +4,7 @@ window.nostr = {
|
||||
|
||||
async getPublicKey() {
|
||||
if (this._pubkey) return this._pubkey
|
||||
this._pubkey = await this._call('getPublicKey')
|
||||
this._pubkey = await this._call('getPublicKey', {})
|
||||
return this._pubkey
|
||||
},
|
||||
|
||||
@@ -39,7 +39,9 @@ window.addEventListener('message', message => {
|
||||
return
|
||||
|
||||
if (message.data.response.error) {
|
||||
window.nostr._requests[message.data.id].reject(message.data.response.error)
|
||||
window.nostr._requests[message.data.id].reject(
|
||||
new Error(`nos2x returned an error: ${message.data.response.error}`)
|
||||
)
|
||||
} else {
|
||||
window.nostr._requests[message.data.id].resolve(message.data.response)
|
||||
}
|
||||
|
||||
@@ -5,9 +5,15 @@ import browser from 'webextension-polyfill'
|
||||
document
|
||||
.getElementById('privateKeyInput')
|
||||
.addEventListener('input', async ev => {
|
||||
let key = document
|
||||
.getElementById('privateKeyInput')
|
||||
.value.toLowerCase()
|
||||
.trim()
|
||||
if (!key.match(/^[a-f0-9]{64}$/)) return
|
||||
|
||||
try {
|
||||
await browser.storage.local.set({
|
||||
private_key: document.getElementById('privateKeyInput').value
|
||||
private_key: key
|
||||
})
|
||||
showMessage('saved!')
|
||||
} catch (err) {
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
{
|
||||
"dependencies": {
|
||||
"@esbuild-plugins/node-globals-polyfill": "^0.1.1",
|
||||
"buffer": "^6.0.3",
|
||||
"esbuild": "^0.14.11",
|
||||
"esbuild-plugin-alias": "^0.2.1",
|
||||
"eslint": "^8.6.0",
|
||||
|
||||
@@ -2,6 +2,11 @@
|
||||
# yarn lockfile v1
|
||||
|
||||
|
||||
"@esbuild-plugins/node-globals-polyfill@^0.1.1":
|
||||
version "0.1.1"
|
||||
resolved "https://registry.yarnpkg.com/@esbuild-plugins/node-globals-polyfill/-/node-globals-polyfill-0.1.1.tgz#a313ab3efbb2c17c8ce376aa216c627c9b40f9d7"
|
||||
integrity sha512-MR0oAA+mlnJWrt1RQVQ+4VYuRJW/P2YmRTv1AsplObyvuBMnPHiizUF95HHYiSsMGLhyGtWufaq2XQg6+iurBg==
|
||||
|
||||
"@eslint/eslintrc@^1.0.5":
|
||||
version "1.0.5"
|
||||
resolved "https://registry.yarnpkg.com/@eslint/eslintrc/-/eslintrc-1.0.5.tgz#33f1b838dbf1f923bfa517e008362b78ddbbf318"
|
||||
@@ -142,7 +147,7 @@ buffer-xor@^1.0.3:
|
||||
resolved "https://registry.yarnpkg.com/buffer-xor/-/buffer-xor-1.0.3.tgz#26e61ed1422fb70dd42e6e36729ed51d855fe8d9"
|
||||
integrity sha1-JuYe0UIvtw3ULm42cp7VHYVf6Nk=
|
||||
|
||||
buffer@>=5:
|
||||
buffer@>=5, buffer@^6.0.3:
|
||||
version "6.0.3"
|
||||
resolved "https://registry.yarnpkg.com/buffer/-/buffer-6.0.3.tgz#2ace578459cc8fbe2a70aaa8f52ee63b6a74c6c6"
|
||||
integrity sha512-FTiCpNxtwiZZHEZbcbTIcZjERVICn9yq/pDFkTl95/AxzD1naBctN7YO68riM/gLSDY7sdrMby8hofADYuuqOA==
|
||||
|
||||
Reference in New Issue
Block a user