fix: save key and load key
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import browser from 'webextension-polyfill'
|
||||
import { useState, useCallback, useEffect } from 'react'
|
||||
import { render } from 'react-dom'
|
||||
import { generateSecretKey, nip19, utils } from 'nostr-tools'
|
||||
import { generateSecretKey, nip19 } from 'nostr-tools'
|
||||
import QRCode from 'react-qr-code'
|
||||
import * as Tabs from '@radix-ui/react-tabs'
|
||||
import { LogoIcon } from './icons'
|
||||
@@ -56,8 +56,11 @@ function Options() {
|
||||
.get(['private_key', 'relays', 'protocol_handler', 'notifications'])
|
||||
.then((results) => {
|
||||
if (results.private_key) {
|
||||
setPrivKey(nip19.nsecEncode(results.private_key))
|
||||
const pkey = results.private_key
|
||||
const nsec = nip19.nsecEncode(hexToBytes(pkey))
|
||||
setPrivKey(nsec)
|
||||
}
|
||||
|
||||
if (results.relays) {
|
||||
const relaysList = []
|
||||
for (const url in results.relays) {
|
||||
@@ -68,11 +71,13 @@ function Options() {
|
||||
}
|
||||
setRelays(relaysList)
|
||||
}
|
||||
|
||||
if (results.protocol_handler) {
|
||||
setProtocolHandler(results.protocol_handler)
|
||||
setHandleNostrLinks(true)
|
||||
setShowProtocolHandlerHelp(false)
|
||||
}
|
||||
|
||||
if (results.notifications) {
|
||||
setNotifications(true)
|
||||
}
|
||||
@@ -519,8 +524,7 @@ examples:
|
||||
}
|
||||
|
||||
async function generate() {
|
||||
const sk = generateSecretKey()
|
||||
setPrivKey(nip19.nsecEncode(utils.bytesToHex(sk)))
|
||||
setPrivKey(nip19.nsecEncode(generateSecretKey()))
|
||||
addUnsavedChanges('private_key')
|
||||
}
|
||||
|
||||
@@ -529,23 +533,26 @@ examples:
|
||||
showMessage('PRIVATE KEY IS INVALID! did not save private key.')
|
||||
return
|
||||
}
|
||||
|
||||
let hexOrEmptyKey = privKey
|
||||
|
||||
try {
|
||||
const { type, data } = nip19.decode(privKey)
|
||||
if (type === 'nsec') hexOrEmptyKey = bytesToHex(data)
|
||||
} catch (_) {}
|
||||
} catch (_) {
|
||||
showMessage('Invalid private key format.')
|
||||
return
|
||||
}
|
||||
|
||||
await browser.storage.local.set({
|
||||
private_key: hexOrEmptyKey
|
||||
})
|
||||
if (hexOrEmptyKey !== '') {
|
||||
setPrivKey(nip19.nsecEncode(hexToBytes(hexOrEmptyKey)))
|
||||
}
|
||||
showMessage('saved private key!')
|
||||
|
||||
showMessage('Private Key has been saved.')
|
||||
}
|
||||
|
||||
function isKeyValid() {
|
||||
if (privKey === '') return true
|
||||
if (privKey.match(/^[a-f0-9]{64}$/)) return true
|
||||
try {
|
||||
if (nip19.decode(privKey).type === 'nsec') return true
|
||||
} catch (_) {}
|
||||
|
||||
Reference in New Issue
Block a user