mirror of
https://github.com/aljazceru/sendstr-web.git
synced 2025-12-17 06:24:24 +01:00
chore: bump nostr-tools to 1.0.1
This commit is contained in:
38
global.d.ts
vendored
38
global.d.ts
vendored
@@ -3,44 +3,6 @@ declare module "remark-html" {
|
|||||||
export default html
|
export default html
|
||||||
}
|
}
|
||||||
|
|
||||||
declare module "nostr-tools" {
|
|
||||||
export const generatePrivateKey: () => string
|
|
||||||
export const getPublicKey: (priv: string) => string
|
|
||||||
export const relayPool: () => {
|
|
||||||
setPrivateKey: (priv: string) => void
|
|
||||||
addRelay: (url: string, { read: boolean, write: boolean }) => void
|
|
||||||
publish: ({pubkey, created_at, kind, tags, content}: {
|
|
||||||
pubkey: string,
|
|
||||||
created_at: number,
|
|
||||||
kind: number,
|
|
||||||
tags: [[string,string]],
|
|
||||||
content: string,
|
|
||||||
}) => Promise<void>,
|
|
||||||
sub: ({
|
|
||||||
cb,
|
|
||||||
filter,
|
|
||||||
}: {
|
|
||||||
cb: (event: {
|
|
||||||
content: string
|
|
||||||
created_at: number
|
|
||||||
id: string
|
|
||||||
kind: number
|
|
||||||
pubkey: string
|
|
||||||
message: string
|
|
||||||
sig: string
|
|
||||||
tags: [[string, string]]
|
|
||||||
}) => void
|
|
||||||
filter: Record<string, string[]>[]
|
|
||||||
}) => {
|
|
||||||
unsub: () => void
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
declare module "nostr-tools/nip04" {
|
|
||||||
export const decrypt: (priv: string, pub: string, message: string) => string
|
|
||||||
export const encrypt: (priv: string, pub: string, message: string) => string
|
|
||||||
}
|
|
||||||
declare module "toastify-js" {
|
declare module "toastify-js" {
|
||||||
const Toastify: ({
|
const Toastify: ({
|
||||||
text,
|
text,
|
||||||
|
|||||||
1143
package-lock.json
generated
1143
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -18,7 +18,7 @@
|
|||||||
"gray-matter": "^4.0.2",
|
"gray-matter": "^4.0.2",
|
||||||
"next": "latest",
|
"next": "latest",
|
||||||
"next-pwa": "^5.5.4",
|
"next-pwa": "^5.5.4",
|
||||||
"nostr-tools": "^0.23.3",
|
"nostr-tools": "^1.0.1",
|
||||||
"qrcode.react": "^3.0.2",
|
"qrcode.react": "^3.0.2",
|
||||||
"react": "17.0.2",
|
"react": "17.0.2",
|
||||||
"react-device-detect": "^2.2.2",
|
"react-device-detect": "^2.2.2",
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import { getRelays } from "./localStorage"
|
import { getRelays } from "./localStorage"
|
||||||
import { NostrEventType, NostrKeysType, NostrPoolType } from "../types"
|
import { NostrEventType, NostrKeysType, NostrPoolType } from "../types"
|
||||||
|
import { Relay, Sub } from "nostr-tools"
|
||||||
|
|
||||||
export const getLatestEvent = (events: Record<string, NostrEventType>) =>
|
export const getLatestEvent = (events: Record<string, NostrEventType>) =>
|
||||||
Object.entries(events).reduce((acc, x) => {
|
Object.entries(events).reduce((acc, x) => {
|
||||||
@@ -20,30 +21,31 @@ export const subscribe = async (
|
|||||||
peerKey: string,
|
peerKey: string,
|
||||||
cb: (event: NostrEventType) => void,
|
cb: (event: NostrEventType) => void,
|
||||||
) => {
|
) => {
|
||||||
const { decrypt } = await import("nostr-tools/nip04")
|
const { nip04, relayInit } = await import("nostr-tools")
|
||||||
const { relayPool } = await import("nostr-tools")
|
|
||||||
const pool = relayPool()
|
|
||||||
pool.setPrivateKey(keys.priv)
|
|
||||||
const relays = getRelays()
|
const relays = getRelays()
|
||||||
relays.forEach((relay) => relay.enabled && pool.addRelay(relay.url, { read: true, write: true }))
|
.filter((x) => x.enabled)
|
||||||
const sub = pool.sub({
|
.map((x) => relayInit(x.url))
|
||||||
cb: (event: NostrEventType) => {
|
await Promise.allSettled(relays.map((x) => x.connect()))
|
||||||
try {
|
relays.forEach((relay) =>
|
||||||
const p = event.tags.find(([tag]) => tag === "p") || ["p", ""]
|
relay.on("error", () => console.error(`Failed to connect to relay ${relay.url}`)),
|
||||||
const pubkey = event.pubkey === keys.pub ? p[1] : event.pubkey
|
)
|
||||||
const message = decrypt(keys.priv, pubkey, event.content)
|
const subs = relays.map((relay) =>
|
||||||
cb({ ...event, message })
|
relay.sub([{ "#p": [keys.pub, peerKey] }, { authors: [keys.pub, peerKey] }]),
|
||||||
} catch (e) {
|
)
|
||||||
console.warn(e)
|
subs.forEach((x) =>
|
||||||
}
|
x.on("event", async (event: NostrEventType) => {
|
||||||
},
|
const p = event.tags.find(([tag]) => tag === "p") || ["p", ""]
|
||||||
filter: [{ "#p": [keys.pub, peerKey] }, { authors: [keys.pub, peerKey] }],
|
const pubkey = event.pubkey === keys.pub ? p[1] : event.pubkey
|
||||||
})
|
const message = await nip04.decrypt(keys.priv, pubkey, event.content)
|
||||||
return { sub, pool }
|
cb({ ...event, message })
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
return { subs, relays }
|
||||||
}
|
}
|
||||||
|
|
||||||
type SendEncryptedMessage = {
|
type SendEncryptedMessage = {
|
||||||
pool: NostrPoolType | null
|
relays: Relay[]
|
||||||
|
subs: Sub[]
|
||||||
priv: string
|
priv: string
|
||||||
pub: string
|
pub: string
|
||||||
peerKey: string
|
peerKey: string
|
||||||
@@ -51,18 +53,28 @@ type SendEncryptedMessage = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const sendEncryptedMessage = async ({
|
export const sendEncryptedMessage = async ({
|
||||||
pool,
|
relays,
|
||||||
priv,
|
priv,
|
||||||
pub,
|
pub,
|
||||||
peerKey,
|
peerKey,
|
||||||
message,
|
message,
|
||||||
}: SendEncryptedMessage) => {
|
}: SendEncryptedMessage) => {
|
||||||
const { encrypt } = await import("nostr-tools/nip04")
|
const { nip04, getEventHash, signEvent } = await import("nostr-tools")
|
||||||
return pool?.publish({
|
relays.map((relay) => {
|
||||||
pubkey: pub,
|
nip04
|
||||||
created_at: Math.round(Date.now() / 1000),
|
.encrypt(priv, peerKey, message)
|
||||||
kind: 4,
|
.then((content) => {
|
||||||
tags: [["p", peerKey]],
|
const event = {
|
||||||
content: encrypt(priv, peerKey, message),
|
pubkey: pub,
|
||||||
|
created_at: Math.round(Date.now() / 1000),
|
||||||
|
kind: 4,
|
||||||
|
tags: [["p", peerKey]],
|
||||||
|
content,
|
||||||
|
}
|
||||||
|
const id = getEventHash(event)
|
||||||
|
const sig = signEvent(event, priv)
|
||||||
|
relay.publish({ ...event, id, sig })
|
||||||
|
})
|
||||||
|
.catch((e) => console.error(`Failed to send message to relay ${relay.url}`, e))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
10
src/types.ts
10
src/types.ts
@@ -1,3 +1,5 @@
|
|||||||
|
import { Relay, Sub } from "nostr-tools"
|
||||||
|
|
||||||
export type NostrSubType = ({
|
export type NostrSubType = ({
|
||||||
cb,
|
cb,
|
||||||
filter,
|
filter,
|
||||||
@@ -34,17 +36,15 @@ export type NostrPublishType = ({
|
|||||||
export type NostrPoolType = {
|
export type NostrPoolType = {
|
||||||
setPrivateKey: (priv: string) => void
|
setPrivateKey: (priv: string) => void
|
||||||
addRelay: (url: string, { read, write }: { read: boolean; write: boolean }) => void
|
addRelay: (url: string, { read, write }: { read: boolean; write: boolean }) => void
|
||||||
sub: NostrSubType
|
subs: NostrSubType
|
||||||
publish: NostrPublishType
|
publish: NostrPublishType
|
||||||
}
|
}
|
||||||
|
|
||||||
export type NostrType = {
|
export type NostrType = {
|
||||||
priv: string
|
priv: string
|
||||||
pub: string
|
pub: string
|
||||||
pool: NostrPoolType | null
|
subs: Sub[],
|
||||||
sub: {
|
relays: Relay[]
|
||||||
unsub: () => void
|
|
||||||
} | null
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export type NostrEventType = {
|
export type NostrEventType = {
|
||||||
|
|||||||
@@ -45,10 +45,10 @@ export const ReceiveView = ({ keys }: ReceiveViewProps) => {
|
|||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
void (async () => {
|
void (async () => {
|
||||||
const sub = await subscribe(keys, peerKey, processEvent)
|
const { subs, relays } = await subscribe(keys, peerKey, processEvent)
|
||||||
nostr.current = { ...sub, ...keys }
|
nostr.current = { subs, relays, ...keys }
|
||||||
return () => {
|
return () => {
|
||||||
nostr?.current?.sub?.unsub()
|
nostr?.current?.subs.forEach(sub => sub.unsub())
|
||||||
}
|
}
|
||||||
})()
|
})()
|
||||||
}, [peerKey])
|
}, [peerKey])
|
||||||
@@ -56,7 +56,7 @@ export const ReceiveView = ({ keys }: ReceiveViewProps) => {
|
|||||||
const sendMessage = useRef(
|
const sendMessage = useRef(
|
||||||
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
||||||
debounce(async (peerKey: string, message: string) => {
|
debounce(async (peerKey: string, message: string) => {
|
||||||
if (nostr?.current?.pool) {
|
if (nostr?.current?.relays) {
|
||||||
await sendEncryptedMessage({ ...nostr.current, peerKey, message })
|
await sendEncryptedMessage({ ...nostr.current, peerKey, message })
|
||||||
}
|
}
|
||||||
}, 500),
|
}, 500),
|
||||||
|
|||||||
@@ -79,10 +79,10 @@ export const SendView = ({ keys }: SendViewProps) => {
|
|||||||
if (isMobile) {
|
if (isMobile) {
|
||||||
setShowScan(true)
|
setShowScan(true)
|
||||||
}
|
}
|
||||||
const sub = await subscribe(keys, peerKey, processEvent)
|
const { subs, relays } = await subscribe(keys, peerKey, processEvent)
|
||||||
nostr.current = { ...sub, ...keys }
|
nostr.current = { subs, relays, ...keys }
|
||||||
return () => {
|
return () => {
|
||||||
nostr?.current?.sub?.unsub()
|
nostr?.current?.subs.forEach(sub => sub.unsub())
|
||||||
}
|
}
|
||||||
})()
|
})()
|
||||||
}, [])
|
}, [])
|
||||||
@@ -99,7 +99,7 @@ export const SendView = ({ keys }: SendViewProps) => {
|
|||||||
const sendMessage = useRef(
|
const sendMessage = useRef(
|
||||||
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
||||||
debounce(async (peerKey: string, message: string) => {
|
debounce(async (peerKey: string, message: string) => {
|
||||||
if (nostr.current?.pool) {
|
if (nostr.current?.relays) {
|
||||||
await sendEncryptedMessage({ ...nostr.current, peerKey, message })
|
await sendEncryptedMessage({ ...nostr.current, peerKey, message })
|
||||||
}
|
}
|
||||||
}, 500),
|
}, 500),
|
||||||
|
|||||||
Reference in New Issue
Block a user