Skip to content

Commit

Permalink
update nostr-tools.
Browse files Browse the repository at this point in the history
  • Loading branch information
AsaiToshiya authored and fiatjaf committed Sep 20, 2024
1 parent a0f7358 commit 4afc9df
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 97 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"dependencies": {
"dayjs": "^1.11.6",
"esbuild": "^0.17.0",
"nostr-tools": "^1.7.0",
"nostr-tools": "^2.7.2",
"react": "^18.2.0",
"react-dom": "^18.1.0",
"styled-components": "^5.3.6",
Expand Down
39 changes: 22 additions & 17 deletions src/Editor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ import {
Notices
} from './components'
import {
generatePrivateKey,
generateSecretKey,
getPublicKey,
getEventHash,
signEvent
} from 'nostr-tools'
finalizeEvent
} from 'nostr-tools/pure'

export function Editor({
publicKey,
Expand Down Expand Up @@ -98,7 +97,7 @@ export function Editor({
// otherwise use a key from localStorage or generate a new one
let privateKey = localStorage.getItem('nostrkey')
if (!privateKey || privateKey.match(/^[a-f0-9]{64}$/)) {
privateKey = generatePrivateKey()
privateKey = generateSecretKey()
localStorage.setItem('nostrkey', privateKey)
}
setPrivateKey(privateKey)
Expand All @@ -112,7 +111,7 @@ export function Editor({
let rootReference = baseTag?.reference
if (!rootReference) {
// create base event right here
let sk = generatePrivateKey()
let sk = generateSecretKey()
let tags = [['r', url]]
if (ownerTag) {
tags.push(ownerTag)
Expand All @@ -124,14 +123,16 @@ export function Editor({
tags: tags,
content: `Comments on ${url}` + (ownerTag ? ` by #[1]` : '') + ` ↴`
}
root.id = getEventHash(root)
root.sig = signEvent(root, sk)
root = finalizeEvent(root, sk)
rootReference = ['e', root.id, '', 'root']
setBaseTag({filter: {'#e': [root.id]}, reference: rootReference})

pool.current.publish(relays, root)
await Promise.any(pool.current.publish(relays, root))
pool.current.trackRelays = true
await pool.current.get(relays, {ids: [root.id]})
pool.current.trackRelays = false
setBaseTag(prev => {
rootReference[2] = pool.current.seenOn(root.id)[0]
rootReference[2] = Array.from(pool.current.seenOn.get(root.id))[0].url

return {
filter: {'#e': [root.id]},
Expand Down Expand Up @@ -159,8 +160,7 @@ export function Editor({

// if we have a private key that means it was generated locally and we don't have a nip07 extension
if (privateKey) {
event.id = getEventHash(event)
event.sig = signEvent(event, privateKey)
event = finalizeEvent(event, privateKey)
} else {
try {
event = await window.nostr.signEvent(event)
Expand All @@ -182,12 +182,17 @@ export function Editor({

console.log('publishing...')

let pub = pool.current.publish(relays, event)
pub.on('ok', relay => {
let pub = Promise.allSettled(pool.current.publish(relays, event))
pub.then(async _ => {
clearTimeout(publishTimeout)
showNotice(`event ${event.id.slice(0, 5)}… published to ${relay}.`)
setComment('')
setEditable(true)
pool.current.trackRelays = true
await pool.current.querySync(relays, {ids: [event.id]})
pool.current.trackRelays = false
pool.current.seenOn.get(event.id).forEach(relay => {
showNotice(`event ${event.id.slice(0, 5)}… published to ${relay.url}.`)
setComment('')
setEditable(true)
})
})
}

Expand Down
91 changes: 51 additions & 40 deletions src/NoComment.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import React, {useState, useEffect, useRef, useMemo} from 'react'
import {useDebounce} from 'use-debounce'
import {SimplePool, nip05, nip19} from 'nostr-tools'
import {SimplePool} from 'nostr-tools/pool'
import * as nip05 from 'nostr-tools/nip05'
import * as nip19 from 'nostr-tools/nip19'

import {normalizeURL, insertEventIntoDescendingList, getName} from './util'
import {Container} from './components'
Expand Down Expand Up @@ -97,13 +99,12 @@ export function NoComment({
if (baseTag) return

// search for the base event based on the #r tag (url)
pool.current.trackRelays = true
pool.current
.list(chosenRelays, [
{
'#r': [url],
kinds: [1]
}
])
.querySync(chosenRelays, {
'#r': [url],
kinds: [1]
})
.then(events => {
if (events.length === 0) return

Expand All @@ -112,33 +113,40 @@ export function NoComment({
reference: [
'e',
events[0].id,
pool.current.seenOn(events[0].id)[0],
Array.from(pool.current.seenOn.get(events[0].id))[0].url,
'root'
]
})
})
.finally(() => {
pool.current.trackRelays = false
})
}, [chosenRelays.length])

useEffect(() => {
if (!baseTag) return

// query for comments
let sub = pool.current.sub(chosenRelays, [
let sub = pool.current.subscribeMany(chosenRelays,
[
{
...baseTag.filter,
kinds: [1]
}
],
{
...baseTag.filter,
kinds: [1]
onevent(event) {
setEvents(events => insertEventIntoDescendingList(events, event))
fetchMetadata(event.pubkey, i)
i++
}
}
])
)

let i = 0
sub.on('event', event => {
setEvents(events => insertEventIntoDescendingList(events, event))
fetchMetadata(event.pubkey, i)
i++
})

return () => {
sub.unsub()
sub.close()
}
}, [baseTag, chosenRelays.length])

Expand Down Expand Up @@ -200,31 +208,34 @@ export function NoComment({

let done = 0

let sub = pool.current.sub(chosenRelays, [{kinds: [0], authors: [pubkey]}])
done++
sub.on('event', event => {
if (!metadata[pubkey] || metadata[pubkey].created_at < event.created_at) {
setMetadata(curr => {
try {
return {
...curr,
[pubkey]: {
...JSON.parse(event.content),
created_at: event.created_at
let sub = pool.current.subscribeMany(chosenRelays, [{kinds: [0], authors: [pubkey]}],
{
onevent(event) {
if (!metadata[pubkey] || metadata[pubkey].created_at < event.created_at) {
setMetadata(curr => {
try {
return {
...curr,
[pubkey]: {
...JSON.parse(event.content),
created_at: event.created_at
}
}
} catch {
return curr
}
}
} catch {
return curr
})
}
})
},
oneose() {
sub.close()
done--

if (done === 0) fetchNIP05(pubkey, metadata[pubkey])
}
}
})
sub.on('eose', () => {
sub.unsub()
done--

if (done === 0) fetchNIP05(pubkey, metadata[pubkey])
})
)
done++
}

async function fetchNIP05(pubkey, meta) {
Expand Down
99 changes: 60 additions & 39 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -310,41 +310,60 @@
"@jridgewell/resolve-uri" "3.1.0"
"@jridgewell/sourcemap-codec" "1.4.14"

"@noble/[email protected]":
version "1.0.0"
resolved "https://registry.yarnpkg.com/@noble/hashes/-/hashes-1.0.0.tgz#d5e38bfbdaba174805a4e649f13be9a9ed3351ae"
integrity sha512-DZVbtY62kc3kkBtMHqwCOfXrT/hnoORy5BJ4+HU1IR59X0KWAOqsfzQPcUl/lQLlG7qXbe/fZ3r/emxtAl+sqg==
"@noble/ciphers@^0.5.1":
version "0.5.3"
resolved "https://registry.yarnpkg.com/@noble/ciphers/-/ciphers-0.5.3.tgz#48b536311587125e0d0c1535f73ec8375cd76b23"
integrity sha512-B0+6IIHiqEs3BPMT0hcRmHvEj2QHOLu+uwt+tqDDeVd0oyVzh7BPrDcPjRnV1PV/5LaknXJJQvOuRGR0zQJz+w==

"@noble/hashes@~1.2.0":
"@noble/curves@1.2.0":
version "1.2.0"
resolved "https://registry.yarnpkg.com/@noble/hashes/-/hashes-1.2.0.tgz#a3150eeb09cc7ab207ebf6d7b9ad311a9bdbed12"
integrity sha512-FZfhjEDbT5GRswV3C6uvLPHMiVD6lQBmpoX5+eSiPaMTXte/IKqI5dykDxzZB/WBeK/CDuQRBWarPdi3FNY2zQ==
resolved "https://registry.yarnpkg.com/@noble/curves/-/curves-1.2.0.tgz#92d7e12e4e49b23105a2555c6984d41733d65c35"
integrity sha512-oYclrNgRaM9SsBUBVbb8M6DTV7ZHRTKugureoYEncY5c65HOmRzvSiTE3y5CYaPYJA/GVkrhXEoF0M3Ya9PMnw==
dependencies:
"@noble/hashes" "1.3.2"

"@noble/curves@~1.1.0":
version "1.1.0"
resolved "https://registry.yarnpkg.com/@noble/curves/-/curves-1.1.0.tgz#f13fc667c89184bc04cccb9b11e8e7bae27d8c3d"
integrity sha512-091oBExgENk/kGj3AZmtBDMpxQPDtxQABR2B9lb1JbVTs6ytdzZNwvhxQ4MWasRNEzlbEH8jCWFCwhF/Obj5AA==
dependencies:
"@noble/hashes" "1.3.1"

"@noble/[email protected]":
version "1.3.1"
resolved "https://registry.yarnpkg.com/@noble/hashes/-/hashes-1.3.1.tgz#8831ef002114670c603c458ab8b11328406953a9"
integrity sha512-EbqwksQwz9xDRGfDST86whPBgM65E0OH/pCgqW0GBVzO22bNE+NuIbeTb714+IfSjU3aRk47EUvXIb5bTsenKA==

"@noble/[email protected]":
version "1.3.2"
resolved "https://registry.yarnpkg.com/@noble/hashes/-/hashes-1.3.2.tgz#6f26dbc8fbc7205873ce3cee2f690eba0d421b39"
integrity sha512-MVC8EAQp7MvEcm30KWENFjgR+Mkmf+D189XJTkFIlwohU5hcBbn1ZkKq7KVTi2Hme3PMGF390DaL52beVrIihQ==

"@noble/secp256k1@^1.7.1", "@noble/secp256k1@~1.7.0":
version "1.7.1"
resolved "https://registry.yarnpkg.com/@noble/secp256k1/-/secp256k1-1.7.1.tgz#b251c70f824ce3ca7f8dc3df08d58f005cc0507c"
integrity sha512-hOUk6AyBFmqVrv7k5WAw/LpszxVbj9gGN4JRkIX52fdFAj1UA61KXmZDvqVEm+pOyec3+fIeZB02LYa/pWOArw==
"@noble/hashes@~1.3.0", "@noble/hashes@~1.3.1":
version "1.3.3"
resolved "https://registry.yarnpkg.com/@noble/hashes/-/hashes-1.3.3.tgz#39908da56a4adc270147bb07968bf3b16cfe1699"
integrity sha512-V7/fPHgl+jsVPXqqeOzT8egNj2iBIVt+ECeMMG8TdcnTikP3oaBtUVqpT/gYCR68aEBJSF+XbYUxStjbFMqIIA==

"@scure/base@^1.1.1", "@scure/base@~1.1.0":
"@scure/[email protected]", "@scure/base@~1.1.0":
version "1.1.1"
resolved "https://registry.yarnpkg.com/@scure/base/-/base-1.1.1.tgz#ebb651ee52ff84f420097055f4bf46cfba403938"
integrity sha512-ZxOhsSyxYwLJj3pLZCefNitxsj093tb2vq90mp2txoYeBqbcjDjqFhyM8eUjq/uFm6zJ+mUuqxlS2FkuSY1MTA==

"@scure/bip32@^1.1.5":
version "1.1.5"
resolved "https://registry.yarnpkg.com/@scure/bip32/-/bip32-1.1.5.tgz#d2ccae16dcc2e75bc1d75f5ef3c66a338d1ba300"
integrity sha512-XyNh1rB0SkEqd3tXcXMi+Xe1fvg+kUIcoRIEujP1Jgv7DqW2r9lg3Ah0NkFaCs9sTkQAQA8kw7xiRXzENi9Rtw==
"@scure/bip32@1.3.1":
version "1.3.1"
resolved "https://registry.yarnpkg.com/@scure/bip32/-/bip32-1.3.1.tgz#7248aea723667f98160f593d621c47e208ccbb10"
integrity sha512-osvveYtyzdEVbt3OfwwXFr4P2iVBL5u1Q3q4ONBfDY/UpOuXmOlbgwc1xECEboY8wIays8Yt6onaWMUdUbfl0A==
dependencies:
"@noble/hashes" "~1.2.0"
"@noble/secp256k1" "~1.7.0"
"@noble/curves" "~1.1.0"
"@noble/hashes" "~1.3.1"
"@scure/base" "~1.1.0"

"@scure/bip39@^1.1.1":
version "1.1.1"
resolved "https://registry.yarnpkg.com/@scure/bip39/-/bip39-1.1.1.tgz#b54557b2e86214319405db819c4b6a370cf340c5"
integrity sha512-t+wDck2rVkh65Hmv280fYdVdY25J9YeEUIgn2LG1WM6gxFkGzcksoDiUkWVpVp3Oex9xGC68JU2dSbUfwZ2jPg==
"@scure/bip39@1.2.1":
version "1.2.1"
resolved "https://registry.yarnpkg.com/@scure/bip39/-/bip39-1.2.1.tgz#5cee8978656b272a917b7871c981e0541ad6ac2a"
integrity sha512-Z3/Fsz1yr904dduJD0NpiyRHhRYHdcnyh73FZWiV+/qhWi83wNJ3NWolYqCEN+ZWsUz2TWwajJggcRE9r1zUYg==
dependencies:
"@noble/hashes" "~1.2.0"
"@noble/hashes" "~1.3.0"
"@scure/base" "~1.1.0"

acorn-jsx@^5.3.2:
Expand Down Expand Up @@ -1158,17 +1177,24 @@ natural-compare@^1.4.0:
resolved "https://registry.yarnpkg.com/natural-compare/-/natural-compare-1.4.0.tgz#4abebfeed7541f2c27acfb29bdbbd15c8d5ba4f7"
integrity sha512-OWND8ei3VtNC9h7V60qff3SVobHr996CTwgxubgyQYEpg290h9J0buyECNNJexkFm5sOajh5G116RYA1c8ZMSw==

nostr-tools@^1.7.0:
version "1.7.0"
resolved "https://registry.yarnpkg.com/nostr-tools/-/nostr-tools-1.7.0.tgz#66788334f9055e09f582e71b87408fa5db48ef34"
integrity sha512-oG1Ac5UkKcClxfXaHx9kobbNKtRoRs8FrijN8XnHpPp3lRGGkN/Mi3XjPEl8LyuBulshToksLBnnYcqUrxEllA==
dependencies:
"@noble/hashes" "1.0.0"
"@noble/secp256k1" "^1.7.1"
"@scure/base" "^1.1.1"
"@scure/bip32" "^1.1.5"
"@scure/bip39" "^1.1.1"
prettier "^2.8.4"
nostr-tools@^2.7.2:
version "2.7.2"
resolved "https://registry.yarnpkg.com/nostr-tools/-/nostr-tools-2.7.2.tgz#74a6ff543a81da1dcce9563b9317faa17221acce"
integrity sha512-Bq3Ug0SZFtgtL1+0wCnAe8AJtI7yx/00/a2nUug9SkhfOwlKS92Tef12iCK9FdwXw+oFZWMtRnSwcLayQso+xA==
dependencies:
"@noble/ciphers" "^0.5.1"
"@noble/curves" "1.2.0"
"@noble/hashes" "1.3.1"
"@scure/base" "1.1.1"
"@scure/bip32" "1.3.1"
"@scure/bip39" "1.2.1"
optionalDependencies:
nostr-wasm v0.1.0

[email protected]:
version "0.1.0"
resolved "https://registry.yarnpkg.com/nostr-wasm/-/nostr-wasm-0.1.0.tgz#17af486745feb2b7dd29503fdd81613a24058d94"
integrity sha512-78BTryCLcLYv96ONU8Ws3Q1JzjlAt+43pWQhIl86xZmWeegYCNLPml7yQ+gG3vR6V5h4XGj+TxO+SS5dsThQIA==

object-assign@^4.1.1:
version "4.1.1"
Expand Down Expand Up @@ -1291,11 +1317,6 @@ prettier@^2.5.1:
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.6.2.tgz#e26d71a18a74c3d0f0597f55f01fb6c06c206032"
integrity sha512-PkUpF+qoXTqhOeWL9fu7As8LXsIUZ1WYaJiY/a7McAQzxjk82OF0tibkFXVCDImZtWxbvojFjerkiLb0/q8mew==

prettier@^2.8.4:
version "2.8.4"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.8.4.tgz#34dd2595629bfbb79d344ac4a91ff948694463c3"
integrity sha512-vIS4Rlc2FNh0BySk3Wkd6xmwxB0FpOndW5fisM5H8hsZSxU2VWVB5CWIkIjWvrHjIhxk2g3bfMKM87zNTrZddw==

prop-types@^15.8.1:
version "15.8.1"
resolved "https://registry.yarnpkg.com/prop-types/-/prop-types-15.8.1.tgz#67d87bf1a694f48435cf332c24af10214a3140b5"
Expand Down

0 comments on commit 4afc9df

Please sign in to comment.