From 4b2f7b213d07aae542c17e68b33f4fbe61126958 Mon Sep 17 00:00:00 2001 From: smolgrrr Date: Fri, 5 Jan 2024 17:16:55 +1100 Subject: [PATCH] fix shit --- client/src/components/Forms/PostFormCard.tsx | 11 +++-- client/src/components/Forms/handleSubmit.ts | 8 ++-- client/src/powWorker.ts | 4 +- client/src/utils/FileUpload.tsx | 6 +-- client/src/utils/mine.ts | 4 +- client/src/utils/relays.ts | 47 +++++++++----------- 6 files changed, 40 insertions(+), 40 deletions(-) diff --git a/client/src/components/Forms/PostFormCard.tsx b/client/src/components/Forms/PostFormCard.tsx index b92c20d..7f8585b 100644 --- a/client/src/components/Forms/PostFormCard.tsx +++ b/client/src/components/Forms/PostFormCard.tsx @@ -56,10 +56,13 @@ const NewNoteCard = ({ } else { unsigned.tags = refEvent.tags unsigned.tags.push(['p', refEvent.pubkey]); - unsigned.tags.push(['e', refEvent.id]); - } - if (tagType === 'Quote') { - setComment(comment + '\nnostr:' + nip19.noteEncode(refEvent.id)); + + if (tagType === 'Quote') { + setComment(comment + '\nnostr:' + nip19.noteEncode(refEvent.id)); + unsigned.tags.push(['q', refEvent.id]); + } else { + unsigned.tags.push(['e', refEvent.id]); + } } } diff --git a/client/src/components/Forms/handleSubmit.ts b/client/src/components/Forms/handleSubmit.ts index 25a2591..909721f 100644 --- a/client/src/components/Forms/handleSubmit.ts +++ b/client/src/components/Forms/handleSubmit.ts @@ -1,5 +1,5 @@ import { useState, useEffect } from "react"; -import { generatePrivateKey, getPublicKey, finishEvent, UnsignedEvent } from "nostr-tools"; +import { generateSecretKey, getPublicKey, finalizeEvent, UnsignedEvent } from "nostr-tools"; import { publish } from "../../utils/relays"; const useWorkers = (numCores: number, unsigned: UnsignedEvent, difficulty: string, deps: any[]) => { @@ -35,7 +35,7 @@ const useWorkers = (numCores: number, unsigned: UnsignedEvent, difficulty: strin export const useSubmitForm = (unsigned: UnsignedEvent, difficulty: string) => { const [doingWorkProp, setDoingWorkProp] = useState(false); - const [sk, setSk] = useState(generatePrivateKey()); + const [sk, setSk] = useState(generateSecretKey()); const unsignedWithPubkey = { ...unsigned, pubkey: getPublicKey(sk) }; const powServer = useState(localStorage.getItem('powserver') || ''); const [unsignedPoWEvent, setUnsignedPoWEvent] = useState() @@ -49,9 +49,9 @@ export const useSubmitForm = (unsigned: UnsignedEvent, difficulty: string) => { useEffect(() => { if (unsignedPoWEvent) { setDoingWorkProp(false); - const signedEvent = finishEvent(unsignedPoWEvent, sk); + const signedEvent = finalizeEvent(unsignedPoWEvent, sk); publish(signedEvent); - setSk(generatePrivateKey()) + setSk(generateSecretKey()) } }, [unsignedPoWEvent]); diff --git a/client/src/powWorker.ts b/client/src/powWorker.ts index c13bdf3..61c6fce 100644 --- a/client/src/powWorker.ts +++ b/client/src/powWorker.ts @@ -43,10 +43,10 @@ export function getPow(hex: string): number { * * Adapted from Snort: https://git.v0l.io/Kieran/snort/src/commit/4df6c19248184218c4c03728d61e94dae5f2d90c/packages/system/src/pow-util.ts#L14-L36 */ -export function minePow(unsigned: UnsignedEvent, difficulty: number, nonceStart: number, nonceStep: number): { found: boolean, event?: Omit, 'sig'> } { +export function minePow(unsigned: UnsignedEvent, difficulty: number, nonceStart: number, nonceStep: number): { found: boolean, event?: Omit } { let nonce = nonceStart; - const event = unsigned as Omit, 'sig'> + const event = unsigned as Omit const tag = ['nonce', nonce.toString(), difficulty.toString()] event.tags.push(tag); diff --git a/client/src/utils/FileUpload.tsx b/client/src/utils/FileUpload.tsx index 10446cf..b8ca9f8 100644 --- a/client/src/utils/FileUpload.tsx +++ b/client/src/utils/FileUpload.tsx @@ -1,4 +1,4 @@ -import { generatePrivateKey, getPublicKey, finishEvent } from "nostr-tools"; +import { generateSecretKey, getPublicKey, finalizeEvent } from "nostr-tools"; import { base64 } from "@scure/base"; export interface UploadResult { @@ -13,7 +13,7 @@ export interface UploadResult { export default async function FileUpload(file: File): Promise { const buf = await file.arrayBuffer(); - const sk = generatePrivateKey(); + const sk = generateSecretKey(); const auth = async () => { const authEvent = { kind: 27235, @@ -25,7 +25,7 @@ export default async function FileUpload(file: File): Promise { created_at: Math.floor(Date.now() / 1000), pubkey: getPublicKey(sk), } - return `Nostr ${base64.encode(new TextEncoder().encode(JSON.stringify(finishEvent(authEvent, sk))))}`; + return `Nostr ${base64.encode(new TextEncoder().encode(JSON.stringify(finalizeEvent(authEvent, sk))))}`; }; const req = await fetch("https://void.cat/upload", { diff --git a/client/src/utils/mine.ts b/client/src/utils/mine.ts index 1746559..03f388a 100644 --- a/client/src/utils/mine.ts +++ b/client/src/utils/mine.ts @@ -23,10 +23,10 @@ export function getPow(hex: string): number { * * Adapted from Snort: https://git.v0l.io/Kieran/snort/src/commit/4df6c19248184218c4c03728d61e94dae5f2d90c/packages/system/src/pow-util.ts#L14-L36 */ -export function minePow(unsigned: UnsignedEvent, difficulty: number): Omit, 'sig'> { +export function minePow(unsigned: UnsignedEvent, difficulty: number): Omit { let count = 0 - const event = unsigned as Omit, 'sig'> + const event = unsigned as Omit const tag = ['nonce', count.toString(), difficulty.toString()] event.tags.push(tag) diff --git a/client/src/utils/relays.ts b/client/src/utils/relays.ts index 4b64e4c..a81afc1 100644 --- a/client/src/utils/relays.ts +++ b/client/src/utils/relays.ts @@ -1,4 +1,4 @@ -import {Event, Filter, relayInit, Relay, Sub} from 'nostr-tools'; +import {Event, Filter, Relay, Subscription} from 'nostr-tools'; type SubCallback = ( event: Readonly, @@ -11,18 +11,18 @@ type Subscribe = { unsub?: boolean; }; -const subList: Array = []; +const subList: Array = []; const currentSubList: Array = []; const relayMap = new Map(); export const addRelay = async (url: string) => { - const relay = relayInit(url); - relay.on('connect', () => { - console.info(`connected to ${relay.url}`); - }); - relay.on('error', () => { - console.warn(`failed to connect to ${relay.url}`); - }); + const relay = await Relay.connect(url); + // relay.on('connect', () => { + // console.info(`connected to ${relay.url}`); + // }); + // relay.on('error', () => { + // console.warn(`failed to connect to ${relay.url}`); + // }); try { await relay.connect(); currentSubList.forEach(({cb, filter}) => subscribe(cb, filter, relay)); @@ -32,8 +32,8 @@ export const addRelay = async (url: string) => { } }; -export const unsubscribe = (sub: Sub) => { - sub.unsub(); +export const unsubscribe = (sub: Subscription) => { + sub.close(); subList.splice(subList.indexOf(sub), 1); }; @@ -43,17 +43,18 @@ const subscribe = ( relay: Relay, unsub?: boolean ) => { - const sub = relay.sub([filter]); - subList.push(sub); - sub.on('event', (event: Event) => { - cb(event, relay.url); - }); - if (unsub) { - sub.on('eose', () => { - // console.log('eose', relay.url); - unsubscribe(sub); + const sub = relay.subscribe([filter], + { + onevent(event) { + cb(event, relay.url); + }, + oneose() { + if (unsub) { + unsubscribe(sub); + } + } }); - } + subList.push(sub); return sub; }; @@ -68,10 +69,6 @@ export const subOnce = ( const relay = relayMap.get(obj.relay); if (relay) { const sub = subscribe(obj.cb, obj.filter, relay); - sub.on('eose', () => { - // console.log('eose', obj.relay); - unsubscribe(sub); - }); } };