nsec and nprofile support and update dependencies.
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "nos2x",
|
||||
"description": "Nostr Signer Extension",
|
||||
"version": "1.6.1",
|
||||
"version": "1.7.0",
|
||||
"homepage_url": "https://github.com/fiatjaf/nos2x",
|
||||
"manifest_version": 3,
|
||||
"icons": {
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
import browser from 'webextension-polyfill'
|
||||
import React, {useState, useCallback, useEffect} from 'react'
|
||||
import {render} from 'react-dom'
|
||||
import {normalizeRelayURL} from 'nostr-tools/relay'
|
||||
import {generatePrivateKey} from 'nostr-tools/keys'
|
||||
import {generatePrivateKey, nip19} from 'nostr-tools'
|
||||
|
||||
import {getPermissionsString, readPermissions} from './common'
|
||||
|
||||
@@ -20,7 +19,7 @@ function Options() {
|
||||
|
||||
useEffect(() => {
|
||||
browser.storage.local.get(['private_key', 'relays']).then(results => {
|
||||
if (results.private_key) setKey(results.private_key)
|
||||
if (results.private_key) setKey(nip19.nsecEncode(results.private_key))
|
||||
if (results.relays) {
|
||||
let relaysList = []
|
||||
for (let url in results.relays) {
|
||||
@@ -107,10 +106,7 @@ function Options() {
|
||||
/>
|
||||
{key === '' && <button onClick={generate}>generate</button>}
|
||||
</div>
|
||||
<button
|
||||
disabled={!(key.match(/^[a-f0-9]{64}$/) || key === '')}
|
||||
onClick={saveKey}
|
||||
>
|
||||
<button disabled={!isKeyValid()} onClick={saveKey}>
|
||||
save
|
||||
</button>
|
||||
</div>
|
||||
@@ -157,16 +153,39 @@ function Options() {
|
||||
}
|
||||
|
||||
async function generate(e) {
|
||||
setKey(generatePrivateKey())
|
||||
setKey(nip19.nsecEncode(generatePrivateKey()))
|
||||
}
|
||||
|
||||
async function saveKey() {
|
||||
if (!isKeyValid()) return
|
||||
|
||||
let hexOrEmptyKey = key
|
||||
|
||||
try {
|
||||
let {type, data} = nip19.decode(key)
|
||||
if (type === 'nsec') hexOrEmptyKey = data
|
||||
} catch (_) {}
|
||||
|
||||
await browser.storage.local.set({
|
||||
private_key: key
|
||||
private_key: hexOrEmptyKey
|
||||
})
|
||||
|
||||
if (hexOrEmptyKey !== '') {
|
||||
setKey(nip19.nsecEncode(hexOrEmptyKey))
|
||||
}
|
||||
|
||||
showMessage('saved private key!')
|
||||
}
|
||||
|
||||
function isKeyValid() {
|
||||
if (key === '') return true
|
||||
if (key.match(/^[a-f0-9]{64}$/)) return true
|
||||
try {
|
||||
if (nip19.decode(key).type === 'nsec') return true
|
||||
} catch (_) {}
|
||||
return false
|
||||
}
|
||||
|
||||
function changeRelayURL(i, ev) {
|
||||
setRelays([
|
||||
...relays.slice(0, i),
|
||||
@@ -188,7 +207,7 @@ function Options() {
|
||||
|
||||
function addNewRelay() {
|
||||
relays.push({
|
||||
url: normalizeRelayURL(newRelayURL),
|
||||
url: newRelayURL,
|
||||
policy: {read: true, write: true}
|
||||
})
|
||||
setRelays(relays)
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import browser from 'webextension-polyfill'
|
||||
import {render} from 'react-dom'
|
||||
import {getPublicKey} from 'nostr-tools'
|
||||
import {bech32} from 'bech32'
|
||||
import {getPublicKey, nip19} from 'nostr-tools'
|
||||
import React, {useState, useRef, useEffect} from 'react'
|
||||
|
||||
function Popup() {
|
||||
@@ -9,18 +8,32 @@ function Popup() {
|
||||
let keys = useRef([])
|
||||
|
||||
useEffect(() => {
|
||||
browser.storage.local.get('private_key').then(results => {
|
||||
browser.storage.local.get(['private_key', 'relays']).then(results => {
|
||||
if (results.private_key) {
|
||||
let hexKey = getPublicKey(results.private_key)
|
||||
let npubKey = bech32.encode(
|
||||
'npub',
|
||||
bech32.toWords(Buffer.from(hexKey, 'hex'))
|
||||
)
|
||||
let npubKey = nip19.npubEncode(hexKey)
|
||||
|
||||
setKey(npubKey)
|
||||
|
||||
keys.current.push(hexKey)
|
||||
keys.current.push(npubKey)
|
||||
keys.current.push(hexKey)
|
||||
|
||||
if (results.relays) {
|
||||
let relaysList = []
|
||||
for (let url in results.relays) {
|
||||
if (results.relays[url].write) {
|
||||
relaysList.push(url)
|
||||
if (relaysList.length >= 3) break
|
||||
}
|
||||
}
|
||||
if (relaysList.length) {
|
||||
let nprofileKey = nip19.nprofileEncode({
|
||||
pubkey: hexKey,
|
||||
relays: relaysList
|
||||
})
|
||||
keys.current.push(nprofileKey)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
setKey(null)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user