From 3cebd961cab9142aeda4f15761545670e222c9b6 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Wed, 4 Oct 2023 17:19:19 -0500 Subject: [PATCH] useSignerStream: rework with nostr-machina --- package.json | 1 + src/api/hooks/nostr/useSignerStream.ts | 82 ++++++++++++++------------ yarn.lock | 25 ++++++++ 3 files changed, 69 insertions(+), 39 deletions(-) diff --git a/package.json b/package.json index cfe53c48b..3b7c598f5 100644 --- a/package.json +++ b/package.json @@ -123,6 +123,7 @@ "localforage": "^1.10.0", "lodash": "^4.7.11", "mini-css-extract-plugin": "^2.6.0", + "nostr-machina": "^0.1.0", "nostr-tools": "^1.14.2", "path-browserify": "^1.0.1", "postcss": "^8.4.29", diff --git a/src/api/hooks/nostr/useSignerStream.ts b/src/api/hooks/nostr/useSignerStream.ts index b64fc132b..6780351c8 100644 --- a/src/api/hooks/nostr/useSignerStream.ts +++ b/src/api/hooks/nostr/useSignerStream.ts @@ -1,6 +1,7 @@ -import { relayInit, type Relay } from 'nostr-tools'; -import { useEffect } from 'react'; +import { NiceRelay } from 'nostr-machina'; +import { useEffect, useMemo } from 'react'; +import { nip04, signEvent } from 'soapbox/features/nostr/sign'; import { useInstance } from 'soapbox/hooks'; import { connectRequestSchema } from 'soapbox/schemas/nostr'; import { jsonSchema } from 'soapbox/schemas/utils'; @@ -11,47 +12,50 @@ function useSignerStream() { const relayUrl = instance.nostr?.relay; const pubkey = instance.nostr?.pubkey; - useEffect(() => { - let relay: Relay | undefined; - - if (relayUrl && pubkey && window.nostr?.nip04) { - relay = relayInit(relayUrl); - relay.connect(); - - relay - .sub([{ kinds: [24133], authors: [pubkey], limit: 0 }]) - .on('event', async (event) => { - if (!relay || !window.nostr?.nip04) return; - - const decrypted = await window.nostr.nip04.decrypt(pubkey, event.content); - const reqMsg = jsonSchema.pipe(connectRequestSchema).safeParse(decrypted); - - if (!reqMsg.success) { - console.warn(decrypted); - console.warn(reqMsg.error); - return; - } - - const signed = await window.nostr.signEvent(reqMsg.data.params[0]); - const respMsg = { - id: reqMsg.data.id, - result: signed, - }; - - const respEvent = await window.nostr.signEvent({ - kind: 24133, - content: await window.nostr.nip04.encrypt(pubkey, JSON.stringify(respMsg)), - tags: [['p', pubkey]], - created_at: Math.floor(Date.now() / 1000), - }); - - relay.publish(respEvent); - }); + const relay = useMemo(() => { + if (relayUrl) { + return new NiceRelay(relayUrl); } + }, [relayUrl]); + + useEffect(() => { + if (!relay || !pubkey) return; + + const sub = relay.req([{ kinds: [24133], authors: [pubkey], limit: 0 }]); + + const readEvents = async () => { + for await (const event of sub) { + const decrypted = await nip04.decrypt(pubkey, event.content); + + const reqMsg = jsonSchema.pipe(connectRequestSchema).safeParse(decrypted); + if (!reqMsg.success) { + console.warn(decrypted); + console.warn(reqMsg.error); + return; + } + + const respMsg = { + id: reqMsg.data.id, + result: await signEvent(reqMsg.data.params[0]), + }; + + const respEvent = await signEvent({ + kind: 24133, + content: await nip04.encrypt(pubkey, JSON.stringify(respMsg)), + tags: [['p', pubkey]], + created_at: Math.floor(Date.now() / 1000), + }); + + relay.send(['EVENT', respEvent]); + } + }; + + readEvents(); + return () => { relay?.close(); }; - }, [relayUrl, pubkey]); + }, [relay, pubkey]); } export { useSignerStream }; diff --git a/yarn.lock b/yarn.lock index 838d535f5..5c8d8ca93 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6512,6 +6512,26 @@ normalize-url@^6.0.1: resolved "https://registry.yarnpkg.com/normalize-url/-/normalize-url-6.1.0.tgz#40d0885b535deffe3f3147bec877d05fe4c5668a" integrity sha512-DlL+XwOy3NxAQ8xuC0okPgK46iuVNAK01YN7RueYBqqFeGsBjV9XmCAzAdgt+667bCl5kPh9EqKKDwnaPG1I7A== +nostr-machina@^0.1.0: + version "0.1.0" + resolved "https://registry.yarnpkg.com/nostr-machina/-/nostr-machina-0.1.0.tgz#e111e86eb51655e5de31862174d23de184e6e98a" + integrity sha512-sNswM9vgq7R/96YIJKZOlG0M/m2mZrb1TiPA7hpOMrnWHBGdDuAeON0vLWJaGbvpuDKYQ1b5ZiLZ8HM3EZPevw== + dependencies: + nostr-tools "^1.14.0" + zod "^3.21.0" + +nostr-tools@^1.14.0: + version "1.16.0" + resolved "https://registry.yarnpkg.com/nostr-tools/-/nostr-tools-1.16.0.tgz#5867f1d8bd055a5a3b27aadb199457dceb244314" + integrity sha512-sx/aOl0gmkeHVoIVbyOhEQhzF88NsrBXMC8bsjhPASqA6oZ8uSOAyEGgRLMfC3SKgzQD5Gr6KvDoAahaD6xKcg== + dependencies: + "@noble/ciphers" "^0.2.0" + "@noble/curves" "1.1.0" + "@noble/hashes" "1.3.1" + "@scure/base" "1.1.1" + "@scure/bip32" "1.3.1" + "@scure/bip39" "1.2.1" + nostr-tools@^1.14.2: version "1.14.2" resolved "https://registry.yarnpkg.com/nostr-tools/-/nostr-tools-1.14.2.tgz#161c9401467725e87c07fcf1c9924d31b12fd45c" @@ -9674,6 +9694,11 @@ yocto-queue@^1.0.0: resolved "https://registry.yarnpkg.com/yocto-queue/-/yocto-queue-1.0.0.tgz#7f816433fb2cbc511ec8bf7d263c3b58a1a3c251" integrity sha512-9bnSc/HEW2uRy67wc+T8UwauLuPJVn28jb+GtJY16iiKWyvmYJRXVT4UamsAEGQfPohgr2q4Tq0sQbQlxTfi1g== +zod@^3.21.0: + version "3.22.3" + resolved "https://registry.yarnpkg.com/zod/-/zod-3.22.3.tgz#2fbc96118b174290d94e8896371c95629e87a060" + integrity sha512-EjIevzuJRiRPbVH4mGc8nApb/lVLKVpmUhAaR5R5doKGfAnGJ6Gr3CViAVjP+4FWSxCsybeWQdcgCtbX+7oZug== + zod@^3.21.4: version "3.21.4" resolved "https://registry.yarnpkg.com/zod/-/zod-3.21.4.tgz#10882231d992519f0a10b5dd58a38c9dabbb64db"