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

View File

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