From 69d96cdefc90ec0b19295c5b3658865185766c63 Mon Sep 17 00:00:00 2001 From: zmeyer44 Date: Sun, 22 Oct 2023 12:48:00 -0400 Subject: [PATCH] fixed decryption issue --- .../(profile)/[npub]/_components/Feed.tsx | 17 ++++---------- app/(app)/(profile)/[npub]/page.tsx | 22 ++++--------------- app/(app)/_layout/components/AuthActions.tsx | 14 ------------ app/_providers/ndk/context/index.tsx | 2 ++ app/_providers/ndk/context/instance.ts | 4 +++- components/KindCard/3745.tsx | 18 +++++++-------- lib/actions/create.ts | 15 +++++++------ lib/hooks/useCurrentUser.ts | 1 + 8 files changed, 30 insertions(+), 63 deletions(-) diff --git a/app/(app)/(profile)/[npub]/_components/Feed.tsx b/app/(app)/(profile)/[npub]/_components/Feed.tsx index 1e752b6..b16d83f 100644 --- a/app/(app)/(profile)/[npub]/_components/Feed.tsx +++ b/app/(app)/(profile)/[npub]/_components/Feed.tsx @@ -1,23 +1,14 @@ import Feed from "@/containers/Feed"; import Spinner from "@/components/spinner"; +import { NDKKind } from "@nostr-dev-kit/ndk"; -export default function ProfileFeed({ - pubkey, - alt, -}: { - pubkey: string; - alt?: string; -}) { - const authors = [pubkey]; - if (alt) { - authors.push(alt); - } +export default function ProfileFeed({ pubkey }: { pubkey: string }) { return (
(
diff --git a/app/(app)/(profile)/[npub]/page.tsx b/app/(app)/(profile)/[npub]/page.tsx index d41599e..1d04ecd 100644 --- a/app/(app)/(profile)/[npub]/page.tsx +++ b/app/(app)/(profile)/[npub]/page.tsx @@ -42,22 +42,12 @@ export default function ProfilePage({ const modal = useModal(); const { currentUser, mySubscription, initSubscriptions } = useCurrentUser(); const [activeTab, setActiveTab] = useState("feed"); - const [delegate, setDelegate] = useState(); const { type, data } = nip19.decode(npub); const pubkey = data.toString(); - useEffect(() => { - console.log("In affectr"); - if (mySubscription) { - const _delegate = getTagValues("delegate", mySubscription.tags); - console.log("Setting"); - setDelegate(_delegate); - } - }, [mySubscription]); - useEffect(() => { - initSubscriptions(pubkey); - }, []); - console.log("Delegate", delegate); + // useEffect(() => { + // initSubscriptions(pubkey); + // }, []); if (type !== "npub") { throw new Error("Invalid list"); } @@ -171,11 +161,7 @@ export default function ProfilePage({ setActiveTab={(t) => setActiveTab(t.name)} />
- {activeTab === "feed" ? ( - - ) : ( - "" - )} + {activeTab === "feed" ? : ""} {activeTab === "subscriptions" ? : ""}
diff --git a/app/(app)/_layout/components/AuthActions.tsx b/app/(app)/_layout/components/AuthActions.tsx index b144061..22b9178 100644 --- a/app/(app)/_layout/components/AuthActions.tsx +++ b/app/(app)/_layout/components/AuthActions.tsx @@ -31,9 +31,6 @@ import { } from "@/components/ui/tooltip"; import { useKeyboardShortcut } from "@/lib/hooks/useKeyboardShortcut"; -import { db } from "@nostr-dev-kit/ndk-cache-dexie"; -import { useLiveQuery } from "dexie-react-hooks"; - const LoginModal = dynamic(() => import("@/components/Modals/Login"), { ssr: false, }); @@ -44,16 +41,6 @@ export default function AuthActions() { const { currentUser, logout, attemptLogin, initSubscriptions } = useCurrentUser(); const { ndk } = useNDK(); - const friends = useLiveQuery( - async () => { - console.log("Calling", db); - if (!db) return; - const friends = await db.users.count(); - return friends; - }, - // specify vars that affect query: - [], - ); useKeyboardShortcut(["shift", "ctrl", "u"], () => { if (currentUser) { @@ -78,7 +65,6 @@ export default function AuthActions() { - {friends} ); } diff --git a/app/_providers/ndk/context/index.tsx b/app/_providers/ndk/context/index.tsx index 48bb8be..e0c1ad1 100644 --- a/app/_providers/ndk/context/index.tsx +++ b/app/_providers/ndk/context/index.tsx @@ -13,6 +13,7 @@ import NDK, { import NDKInstance from "./instance"; import { _loginWithNip07, _loginWithNip46, _loginWithSecret } from "./signers"; import { Users } from "./Users"; +import { log } from "@/lib/utils"; interface NDKContext { ndk: NDK | undefined; @@ -105,6 +106,7 @@ const NDKProvider = ({ } async function loginWithNip07() { + log("func", "loginWithNip07"); const res = await _loginWithNip07(); if (res) { const { signer } = res; diff --git a/app/_providers/ndk/context/instance.ts b/app/_providers/ndk/context/instance.ts index 7847f57..f9a116a 100644 --- a/app/_providers/ndk/context/instance.ts +++ b/app/_providers/ndk/context/instance.ts @@ -68,7 +68,7 @@ export default function NDKInstance(explicitRelayUrls: string[]) { ) { const explicitRelayUrls = await getExplicitRelays(); const dexieAdapter = new NDKCacheAdapterDexie({ - dbName: "ndkcache", + dbName: "flockstr-ndkcache", }); const ndkInstance = new NDK({ explicitRelayUrls, @@ -81,6 +81,7 @@ export default function NDKInstance(explicitRelayUrls: string[]) { } if (signer) { + console.log("SIGNER", signer); _setSigner(signer); } @@ -96,6 +97,7 @@ export default function NDKInstance(explicitRelayUrls: string[]) { async function setSigner( signer: NDKPrivateKeySigner | NDKNip46Signer | NDKNip07Signer, ) { + console.log("SetSigner caled"); loadNdk(signer); } diff --git a/components/KindCard/3745.tsx b/components/KindCard/3745.tsx index 08706a6..5f40704 100644 --- a/components/KindCard/3745.tsx +++ b/components/KindCard/3745.tsx @@ -32,7 +32,7 @@ export default function Kind3745(props: KindCardProps) { const [passphrase, setPassphrase] = useState(""); const [fetchingEvent, setFetchingEvent] = useState(false); const [decryptedEvent, setDecryptedEvent] = useState(); - const { ndk, fetchEvents } = useNDK(); + const { ndk, signer } = useNDK(); useEffect(() => { if (ndk && !fetchingEvent && !decryptedEvent) { void handleFetchEvent(); @@ -40,22 +40,21 @@ export default function Kind3745(props: KindCardProps) { }, [ndk]); async function handleFetchEvent() { - if (!ndk) return; - log("func", `handleFetchEvent(${pubkey})`); - + if (!ndk || !currentUser) return; + log("func", `handleFetchEvent()`); setFetchingEvent(true); try { - const directMessageEvent = await ndk!.fetchEvent({ + const directMessageEvent = await ndk.fetchEvent({ kinds: [4], - authors: [pubkey], ["#e"]: [id], + ["#p"]: [currentUser.pubkey], }); if (directMessageEvent) { log("info", "direct msg decryption"); - console.log(directMessageEvent); + if (!signer) return; await directMessageEvent.decrypt( - new NDKUser({ hexpubkey: pubkey }), - ndk!.signer, + new NDKUser({ hexpubkey: directMessageEvent.pubkey }), + signer, ); const passphrase_ = directMessageEvent.content; if (!passphrase_) { @@ -64,7 +63,6 @@ export default function Kind3745(props: KindCardProps) { } setPassphrase(passphrase_); const decrypedData = await decryptMessage(content, passphrase_); - console.log("Decrypted", decrypedData); const hiddenEvent = EventSchema.safeParse( JSON.parse(decrypedData ?? ""), ); diff --git a/lib/actions/create.ts b/lib/actions/create.ts index fa182b2..1facef3 100644 --- a/lib/actions/create.ts +++ b/lib/actions/create.ts @@ -80,9 +80,6 @@ export async function createEventHandler( rawEventString, passphrase, ); - const signer = delegateSigner ?? ndk.signer!; - const user = await signer.user(); - log("info", "Signer", user.toString()); const newEvent = new NDKEvent(ndk, { content: encryptedRawEventString, kind: 3745, @@ -90,11 +87,15 @@ export async function createEventHandler( ["kind", event.kind.toString()], ["client", "flockstr"], ], - pubkey: user.pubkey, + pubkey, } as NostrEvent); - await newEvent.sign(signer); + await newEvent.sign(); await newEvent.publish(); + const messenger = delegateSigner ?? ndk.signer!; + const user = await messenger.user(); + log("info", "Signer", user.toString()); + if (list) { // Send DMs to subscribers const subscribers = getTagsValues("p", list.tags); @@ -111,9 +112,9 @@ export async function createEventHandler( } as NostrEvent); await messageEvent.encrypt( new NDKUser({ hexpubkey: subscriber }), - signer, + messenger, ); - await messageEvent.sign(signer); + await messageEvent.sign(messenger); await messageEvent.publish(); } } diff --git a/lib/hooks/useCurrentUser.ts b/lib/hooks/useCurrentUser.ts index c4e4bf2..d5a172a 100644 --- a/lib/hooks/useCurrentUser.ts +++ b/lib/hooks/useCurrentUser.ts @@ -28,6 +28,7 @@ export default function useCurrentUser() { const shouldReconnect = localStorage.getItem("shouldReconnect"); if (!shouldReconnect || typeof window.nostr === "undefined") return; const user = await loginWithNip07(); + console.log("Called loginWithNip07"); if (!user) { throw new Error("NO auth"); }