fix some things.

This commit is contained in:
fiatjaf 2022-02-11 16:31:42 -03:00
parent 78b5cccfc2
commit e7027c169a
2 changed files with 19 additions and 13 deletions

View File

@ -1,5 +1,4 @@
import React, {useState, useEffect} from 'react'
import useBooleanState from 'use-boolean-state'
import React, {useState} from 'react'
import useComputedState from 'use-computed-state'
import {
getPublicKey,
@ -12,10 +11,10 @@ import {
import Item from '../components/item'
export default function EventSigning({value}) {
let evt = JSON.parse(value)
let [privateKey, setPrivateKey] = useState(
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
)
let [isValidSignature, signatureGood, signatureBad] = useBooleanState(false)
let privateKeyIsValid = useComputedState(
() => privateKey.match(/[a-f0-9]{64}/),
[privateKey]
@ -25,16 +24,23 @@ export default function EventSigning({value}) {
[privateKeyIsValid]
)
let signature = useComputedState(async () => {
try {
evt.pubkey = publicKey
return await signEvent(evt, privateKey)
} catch (err) {
return null
}
}, [value, privateKey])
let evt = JSON.parse(value)
useEffect(() => {
verifySignature(evt)
.then(ok => (ok ? signatureGood() : signatureBad()))
.catch(signatureBad)
let isValidSignature = useComputedState(async () => {
if (evt.id && evt.signature) {
try {
return await verifySignature(evt)
} catch (err) {
return false
}
} else {
return null
}
}, [value])
return (

View File

@ -4,7 +4,7 @@
"esbuild": "^0.14.21",
"esbuild-plugin-alias": "^0.2.1",
"events": "^3.3.0",
"nostr-tools": "^0.21.4",
"nostr-tools": "^0.22.1",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"use-boolean-state": "^1.0.2",