2022-02-11 14:31:42 -05:00
|
|
|
import React, {useState} from 'react'
|
2022-02-10 14:42:17 -05:00
|
|
|
import useComputedState from 'use-computed-state'
|
|
|
|
import {
|
|
|
|
getPublicKey,
|
|
|
|
getEventHash,
|
|
|
|
serializeEvent,
|
|
|
|
verifySignature,
|
|
|
|
signEvent
|
|
|
|
} from 'nostr-tools'
|
2022-02-09 17:40:45 -05:00
|
|
|
|
|
|
|
import Item from '../components/item'
|
|
|
|
|
|
|
|
export default function EventSigning({value}) {
|
2022-02-11 14:31:42 -05:00
|
|
|
let evt = JSON.parse(value)
|
2022-02-10 14:42:17 -05:00
|
|
|
let [privateKey, setPrivateKey] = useState(
|
|
|
|
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
|
|
|
|
)
|
|
|
|
let privateKeyIsValid = useComputedState(
|
2022-02-11 14:47:50 -05:00
|
|
|
() => privateKey.match(/^[a-f0-9]{64}$/),
|
2022-02-10 14:42:17 -05:00
|
|
|
[privateKey]
|
|
|
|
)
|
|
|
|
let publicKey = useComputedState(
|
|
|
|
() => (privateKeyIsValid ? getPublicKey(privateKey) : null),
|
|
|
|
[privateKeyIsValid]
|
|
|
|
)
|
|
|
|
let signature = useComputedState(async () => {
|
2022-02-11 14:31:42 -05:00
|
|
|
try {
|
|
|
|
evt.pubkey = publicKey
|
|
|
|
return await signEvent(evt, privateKey)
|
|
|
|
} catch (err) {
|
|
|
|
return null
|
|
|
|
}
|
2022-02-10 14:42:17 -05:00
|
|
|
}, [value, privateKey])
|
2022-02-11 14:31:42 -05:00
|
|
|
let isValidSignature = useComputedState(async () => {
|
2022-02-12 18:30:18 -05:00
|
|
|
if (evt.id && evt.sig) {
|
2022-02-11 14:31:42 -05:00
|
|
|
try {
|
|
|
|
return await verifySignature(evt)
|
|
|
|
} catch (err) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return null
|
|
|
|
}
|
2022-02-10 14:42:17 -05:00
|
|
|
}, [value])
|
|
|
|
|
2022-02-09 17:40:45 -05:00
|
|
|
return (
|
|
|
|
<>
|
2022-02-10 14:42:17 -05:00
|
|
|
<Item
|
|
|
|
label="serialized event"
|
|
|
|
hint="according to nip-01 signature algorithm"
|
|
|
|
>
|
|
|
|
{serializeEvent(evt)}
|
|
|
|
</Item>
|
|
|
|
<Item label="event id" hint="sha256 hash of serialized event">
|
2022-02-09 17:40:45 -05:00
|
|
|
{getEventHash(evt)}
|
|
|
|
</Item>
|
2022-02-10 14:42:17 -05:00
|
|
|
{evt.sig ? (
|
2022-02-12 18:24:11 -05:00
|
|
|
<Item label="signature valid">
|
|
|
|
{isValidSignature?.toString() || ''}
|
|
|
|
</Item>
|
2022-02-10 14:42:17 -05:00
|
|
|
) : (
|
|
|
|
<>
|
|
|
|
<Item
|
|
|
|
label="private key"
|
|
|
|
hint="paste any private key here (32 bytes hex-encoded)"
|
|
|
|
>
|
|
|
|
<input
|
|
|
|
value={privateKey}
|
|
|
|
onChange={e => setPrivateKey(e.target.value.toLowerCase())}
|
|
|
|
/>{' '}
|
|
|
|
{privateKeyIsValid ? 'valid' : 'invalid'}
|
|
|
|
</Item>
|
|
|
|
<Item label="public key">{publicKey}</Item>
|
|
|
|
<Item label="signature">{privateKeyIsValid ? signature : ''}</Item>
|
|
|
|
</>
|
|
|
|
)}
|
2022-02-09 17:40:45 -05:00
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
EventSigning.match = value => {
|
|
|
|
try {
|
|
|
|
let evt = JSON.parse(value)
|
|
|
|
return evt.kind && evt.content && evt.tags
|
|
|
|
} catch (err) {
|
|
|
|
/**/
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|