mirror of
https://github.com/fiatjaf/nak.git
synced 2024-11-25 00:59:08 -05:00
more stuff and some polish on EventSigning.
This commit is contained in:
parent
20ea54a282
commit
78b5cccfc2
9
app.jsx
9
app.jsx
|
@ -1,4 +1,4 @@
|
||||||
import React, {useState} from 'react'
|
import React, {useState, useEffect} from 'react'
|
||||||
import {render} from 'react-dom'
|
import {render} from 'react-dom'
|
||||||
|
|
||||||
import Nothing from './handlers/Nothing'
|
import Nothing from './handlers/Nothing'
|
||||||
|
@ -7,7 +7,11 @@ import EventSigning from './handlers/EventSigning'
|
||||||
const handlers = [EventSigning, Nothing]
|
const handlers = [EventSigning, Nothing]
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
let [value, setValue] = useState(null)
|
let [value, setValue] = useState(localStorage.getItem('value'))
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
localStorage.setItem('value', value)
|
||||||
|
}, [value])
|
||||||
|
|
||||||
let Result
|
let Result
|
||||||
for (let i = 0; i < handlers.length; i++) {
|
for (let i = 0; i < handlers.length; i++) {
|
||||||
|
@ -29,6 +33,7 @@ function App() {
|
||||||
<h1>nostr army knife</h1>
|
<h1>nostr army knife</h1>
|
||||||
paste something nostric
|
paste something nostric
|
||||||
<textarea
|
<textarea
|
||||||
|
value={value}
|
||||||
onChange={e => setValue(e.target.value)}
|
onChange={e => setValue(e.target.value)}
|
||||||
style={{
|
style={{
|
||||||
padding: '7px',
|
padding: '7px',
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import React from 'react'
|
import React from 'react'
|
||||||
|
|
||||||
export default function Item({label, children}) {
|
export default function Item({label, hint, children}) {
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
style={{
|
style={{
|
||||||
|
@ -10,7 +10,9 @@ export default function Item({label, children}) {
|
||||||
wordBreak: 'break-all'
|
wordBreak: 'break-all'
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<b>{label}: </b>
|
<b data-wenk={hint} data-wenk-pos="right">
|
||||||
|
{label}:{' '}
|
||||||
|
</b>
|
||||||
{children}
|
{children}
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
|
|
|
@ -1,17 +1,71 @@
|
||||||
import React from 'react'
|
import React, {useState, useEffect} from 'react'
|
||||||
import {getEventHash, serializeEvent} from 'nostr-tools'
|
import useBooleanState from 'use-boolean-state'
|
||||||
|
import useComputedState from 'use-computed-state'
|
||||||
|
import {
|
||||||
|
getPublicKey,
|
||||||
|
getEventHash,
|
||||||
|
serializeEvent,
|
||||||
|
verifySignature,
|
||||||
|
signEvent
|
||||||
|
} from 'nostr-tools'
|
||||||
|
|
||||||
import Item from '../components/item'
|
import Item from '../components/item'
|
||||||
|
|
||||||
export default function EventSigning({value}) {
|
export default function EventSigning({value}) {
|
||||||
|
let [privateKey, setPrivateKey] = useState(
|
||||||
|
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
|
||||||
|
)
|
||||||
|
let [isValidSignature, signatureGood, signatureBad] = useBooleanState(false)
|
||||||
|
let privateKeyIsValid = useComputedState(
|
||||||
|
() => privateKey.match(/[a-f0-9]{64}/),
|
||||||
|
[privateKey]
|
||||||
|
)
|
||||||
|
let publicKey = useComputedState(
|
||||||
|
() => (privateKeyIsValid ? getPublicKey(privateKey) : null),
|
||||||
|
[privateKeyIsValid]
|
||||||
|
)
|
||||||
|
let signature = useComputedState(async () => {
|
||||||
|
evt.pubkey = publicKey
|
||||||
|
return await signEvent(evt, privateKey)
|
||||||
|
}, [value, privateKey])
|
||||||
|
|
||||||
let evt = JSON.parse(value)
|
let evt = JSON.parse(value)
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
verifySignature(evt)
|
||||||
|
.then(ok => (ok ? signatureGood() : signatureBad()))
|
||||||
|
.catch(signatureBad)
|
||||||
|
}, [value])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Item label="serialized event">{serializeEvent(evt)}</Item>
|
<Item
|
||||||
<Item label="event id" hint="sha256 hash of serialized">
|
label="serialized event"
|
||||||
|
hint="according to nip-01 signature algorithm"
|
||||||
|
>
|
||||||
|
{serializeEvent(evt)}
|
||||||
|
</Item>
|
||||||
|
<Item label="event id" hint="sha256 hash of serialized event">
|
||||||
{getEventHash(evt)}
|
{getEventHash(evt)}
|
||||||
</Item>
|
</Item>
|
||||||
|
{evt.sig ? (
|
||||||
|
<Item label="signature valid">{isValidSignature.toString()}</Item>
|
||||||
|
) : (
|
||||||
|
<>
|
||||||
|
<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>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,7 +6,9 @@
|
||||||
"events": "^3.3.0",
|
"events": "^3.3.0",
|
||||||
"nostr-tools": "^0.21.4",
|
"nostr-tools": "^0.21.4",
|
||||||
"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-computed-state": "1.1.0"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "./build.js prod",
|
"build": "./build.js prod",
|
||||||
|
|
Loading…
Reference in New Issue
Block a user