chore: bump nostr-tools to 1.0.1

This commit is contained in:
vilm3r
2022-12-27 05:59:34 -06:00
parent d03aab45ef
commit 92c42f5077
7 changed files with 324 additions and 953 deletions

38
global.d.ts vendored
View File

@@ -3,44 +3,6 @@ declare module "remark-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" {
const Toastify: ({
text,

1141
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -18,7 +18,7 @@
"gray-matter": "^4.0.2",
"next": "latest",
"next-pwa": "^5.5.4",
"nostr-tools": "^0.23.3",
"nostr-tools": "^1.0.1",
"qrcode.react": "^3.0.2",
"react": "17.0.2",
"react-device-detect": "^2.2.2",

View File

@@ -1,5 +1,6 @@
import { getRelays } from "./localStorage"
import { NostrEventType, NostrKeysType, NostrPoolType } from "../types"
import { Relay, Sub } from "nostr-tools"
export const getLatestEvent = (events: Record<string, NostrEventType>) =>
Object.entries(events).reduce((acc, x) => {
@@ -20,30 +21,31 @@ export const subscribe = async (
peerKey: string,
cb: (event: NostrEventType) => void,
) => {
const { decrypt } = await import("nostr-tools/nip04")
const { relayPool } = await import("nostr-tools")
const pool = relayPool()
pool.setPrivateKey(keys.priv)
const { nip04, relayInit } = await import("nostr-tools")
const relays = getRelays()
relays.forEach((relay) => relay.enabled && pool.addRelay(relay.url, { read: true, write: true }))
const sub = pool.sub({
cb: (event: NostrEventType) => {
try {
.filter((x) => x.enabled)
.map((x) => relayInit(x.url))
await Promise.allSettled(relays.map((x) => x.connect()))
relays.forEach((relay) =>
relay.on("error", () => console.error(`Failed to connect to relay ${relay.url}`)),
)
const subs = relays.map((relay) =>
relay.sub([{ "#p": [keys.pub, peerKey] }, { authors: [keys.pub, peerKey] }]),
)
subs.forEach((x) =>
x.on("event", async (event: NostrEventType) => {
const p = event.tags.find(([tag]) => tag === "p") || ["p", ""]
const pubkey = event.pubkey === keys.pub ? p[1] : event.pubkey
const message = decrypt(keys.priv, pubkey, event.content)
const message = await nip04.decrypt(keys.priv, pubkey, event.content)
cb({ ...event, message })
} catch (e) {
console.warn(e)
}
},
filter: [{ "#p": [keys.pub, peerKey] }, { authors: [keys.pub, peerKey] }],
})
return { sub, pool }
}),
)
return { subs, relays }
}
type SendEncryptedMessage = {
pool: NostrPoolType | null
relays: Relay[]
subs: Sub[]
priv: string
pub: string
peerKey: string
@@ -51,18 +53,28 @@ type SendEncryptedMessage = {
}
export const sendEncryptedMessage = async ({
pool,
relays,
priv,
pub,
peerKey,
message,
}: SendEncryptedMessage) => {
const { encrypt } = await import("nostr-tools/nip04")
return pool?.publish({
const { nip04, getEventHash, signEvent } = await import("nostr-tools")
relays.map((relay) => {
nip04
.encrypt(priv, peerKey, message)
.then((content) => {
const event = {
pubkey: pub,
created_at: Math.round(Date.now() / 1000),
kind: 4,
tags: [["p", peerKey]],
content: encrypt(priv, peerKey, message),
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))
})
}

View File

@@ -1,3 +1,5 @@
import { Relay, Sub } from "nostr-tools"
export type NostrSubType = ({
cb,
filter,
@@ -34,17 +36,15 @@ export type NostrPublishType = ({
export type NostrPoolType = {
setPrivateKey: (priv: string) => void
addRelay: (url: string, { read, write }: { read: boolean; write: boolean }) => void
sub: NostrSubType
subs: NostrSubType
publish: NostrPublishType
}
export type NostrType = {
priv: string
pub: string
pool: NostrPoolType | null
sub: {
unsub: () => void
} | null
subs: Sub[],
relays: Relay[]
}
export type NostrEventType = {

View File

@@ -45,10 +45,10 @@ export const ReceiveView = ({ keys }: ReceiveViewProps) => {
useEffect(() => {
void (async () => {
const sub = await subscribe(keys, peerKey, processEvent)
nostr.current = { ...sub, ...keys }
const { subs, relays } = await subscribe(keys, peerKey, processEvent)
nostr.current = { subs, relays, ...keys }
return () => {
nostr?.current?.sub?.unsub()
nostr?.current?.subs.forEach(sub => sub.unsub())
}
})()
}, [peerKey])
@@ -56,7 +56,7 @@ export const ReceiveView = ({ keys }: ReceiveViewProps) => {
const sendMessage = useRef(
// eslint-disable-next-line @typescript-eslint/no-misused-promises
debounce(async (peerKey: string, message: string) => {
if (nostr?.current?.pool) {
if (nostr?.current?.relays) {
await sendEncryptedMessage({ ...nostr.current, peerKey, message })
}
}, 500),

View File

@@ -79,10 +79,10 @@ export const SendView = ({ keys }: SendViewProps) => {
if (isMobile) {
setShowScan(true)
}
const sub = await subscribe(keys, peerKey, processEvent)
nostr.current = { ...sub, ...keys }
const { subs, relays } = await subscribe(keys, peerKey, processEvent)
nostr.current = { subs, relays, ...keys }
return () => {
nostr?.current?.sub?.unsub()
nostr?.current?.subs.forEach(sub => sub.unsub())
}
})()
}, [])
@@ -99,7 +99,7 @@ export const SendView = ({ keys }: SendViewProps) => {
const sendMessage = useRef(
// eslint-disable-next-line @typescript-eslint/no-misused-promises
debounce(async (peerKey: string, message: string) => {
if (nostr.current?.pool) {
if (nostr.current?.relays) {
await sendEncryptedMessage({ ...nostr.current, peerKey, message })
}
}, 500),