diff --git a/app/(app)/app/_sections/FeaturedLists.tsx b/app/(app)/app/_sections/FeaturedLists.tsx index cdaa5de..86b800a 100644 --- a/app/(app)/app/_sections/FeaturedLists.tsx +++ b/app/(app)/app/_sections/FeaturedLists.tsx @@ -64,7 +64,7 @@ export default function FeaturedLists() { {processedEvents.map((e) => ( - + ))} diff --git a/app/_providers/signer/index.tsx b/app/_providers/signer/index.tsx index b45b511..db2cd9f 100644 --- a/app/_providers/signer/index.tsx +++ b/app/_providers/signer/index.tsx @@ -15,6 +15,7 @@ import { type NDKList, } from "@nostr-dev-kit/ndk"; import { useNDK } from "../ndk"; +import { log } from "@/lib/utils"; export type SignerStoreItem = { signer: NDKPrivateKeySigner; @@ -67,15 +68,19 @@ export default function SignerProvider({ } async function getSigner(list: NDKList): Promise { + log("func", "getSigner"); const id = list.encode(); + log("info", "list ID", id.toString()); let item = signers.get(id); if (item) return item; + log("info", "NO local signer"); + let signer = await findEphemeralSigner(ndk!, ndk!.signer!, { associatedEventNip19: list.encode(), }); if (signer) { - console.log(`found a signer for list ${list.title}`); + log("info", `found a signer for list ${list.title}`, signer.toString()); item = { signer: signer!, user: await signer.user(), @@ -83,9 +88,9 @@ export default function SignerProvider({ id, }; } else { - console.log(`no signer found for list ${list.title}`); + log("info", `no signer found for list ${list.title}`); signer = NDKPrivateKeySigner.generate(); - console.log(`Signer generated ${JSON.stringify(signer)}`); + log("info", "generated signer", signer.toString()); item = { signer, user: await signer.user(), diff --git a/components/Modals/CreateList.tsx b/components/Modals/CreateList.tsx index febd7a7..b9661c0 100644 --- a/components/Modals/CreateList.tsx +++ b/components/Modals/CreateList.tsx @@ -13,6 +13,7 @@ import { getTagValues } from "@/lib/nostr/utils"; import { NDKList } from "@nostr-dev-kit/ndk"; import { saveEphemeralSigner } from "@/lib/actions/ephemeral"; import { useRouter } from "next/navigation"; +import { log } from "@/lib/utils"; const CreateListSchema = z.object({ title: z.string(), @@ -32,6 +33,7 @@ export default function CreateList() { const { currentUser, updateUser } = useCurrentUser(); const { ndk } = useNDK(); const { getSigner } = useSigner()!; + async function handleSubmit(data: CreateListType) { setIsLoading(true); const random = randomId(); @@ -60,7 +62,6 @@ export default function CreateList() { kind: 30001, tags: tags, }); - console.log("EVENT Created", event); if (event && getTagValues("subscriptions", event.tags)) { await getSigner(new NDKList(ndk, event.rawEvent())) .then((delegateSigner) => @@ -75,7 +76,9 @@ export default function CreateList() { }), ) .then( - (savedSigner) => savedSigner, + (savedSigner) => { + log("info", "savedSigner", savedSigner.toString()); + }, // updateList(ndk!, event.rawEvent(), [ // ["delegate", savedSigner.hexpubkey], // ]), diff --git a/components/Modals/ShortTextNote.tsx b/components/Modals/ShortTextNote.tsx index d225588..d542d99 100644 --- a/components/Modals/ShortTextNote.tsx +++ b/components/Modals/ShortTextNote.tsx @@ -126,7 +126,6 @@ export default function ShortTextNoteModal() { slug: "list", options: lists .map((l) => { - console.log("MApping", l); const title = getTagValues("title", l.tags) ?? getTagValues("name", l.tags) ?? diff --git a/lib/actions/create.ts b/lib/actions/create.ts index 62e8382..f4218b3 100644 --- a/lib/actions/create.ts +++ b/lib/actions/create.ts @@ -10,6 +10,7 @@ import NDK, { import { generateRandomString, encryptMessage, randomId } from "@/lib/nostr"; import { unixTimeNowInSeconds } from "@/lib/nostr/dates"; import { getTagsValues } from "@/lib/nostr/utils"; +import { log } from "@/lib/utils"; export async function createEvent( ndk: NDK, @@ -19,6 +20,7 @@ export async function createEvent( tags: string[][]; }, ) { + log("func", "createEvent"); try { const pubkey = await window.nostr?.getPublicKey(); if (!pubkey || !window.nostr) { @@ -29,14 +31,11 @@ export async function createEvent( pubkey, created_at: unixTimeNowInSeconds(), } as NostrEvent); - console.log("eventToPublish ", eventToPublish); - - const signed = await eventToPublish.sign(); - console.log("signed", signed); + await eventToPublish.sign(); await eventToPublish.publish(); return eventToPublish; } catch (err) { - console.log(err); + log("error", err); alert("An error has occured"); return false; } @@ -52,6 +51,7 @@ export async function createEventHandler( list?: NDKList, delegateSigner?: NDKPrivateKeySigner, ) { + log("func", "createEventHandler"); const pubkey = await window.nostr?.getPublicKey(); if (!pubkey || !window.nostr) { throw new Error("No public key provided!"); @@ -68,6 +68,7 @@ export async function createEventHandler( let publishedEvent: NDKEvent | null = null; // Check if is private event if (isPrivate) { + log("info", "isPrivate"); const rawEventString = JSON.stringify(eventToPublish.rawEvent()); const passphrase = generateRandomString(); const encryptedRawEventString = await encryptMessage( @@ -76,7 +77,7 @@ export async function createEventHandler( ); const signer = delegateSigner ?? ndk.signer!; const user = await signer.user(); - + log("info", "Signer", user.toString()); const newEvent = new NDKEvent(ndk, { content: encryptedRawEventString, kind: 3745, @@ -144,21 +145,6 @@ export async function createReaction( ], }); } -export async function createList( - ndk: NDK, - title: string, - description?: string, -) { - return createEvent(ndk, { - content: "", - kind: 30001, - tags: [ - ["name", title], - ["description", description ?? ""], - ["d", randomId()], - ], - }); -} export async function deleteEvent( ndk: NDK, events: [["e", string] | ["a", `${number}:${string}:${string}`]], diff --git a/lib/actions/ephemeral.ts b/lib/actions/ephemeral.ts index d6970e1..036089e 100644 --- a/lib/actions/ephemeral.ts +++ b/lib/actions/ephemeral.ts @@ -9,6 +9,8 @@ import NDK, { } from "@nostr-dev-kit/ndk"; import { getHashedKeyName } from "@/lib/nostr"; import { z } from "zod"; +import { log } from "@/lib/utils"; + const SignerSchema = z.object({ key: z.string(), }); @@ -26,6 +28,7 @@ export async function findEphemeralSigner( mainSigner: NDKSigner, opts: IFindEphemeralSignerLookups, ): Promise { + log("func", "findEphemeralSigner"); const filter: NDKFilter = { kinds: [2600 as number] }; if (opts.name) { @@ -37,13 +40,11 @@ export async function findEphemeralSigner( ); filter["#e"] = [hashedEventReference]; } - console.log("filter", filter); + log("info", "filter", filter.toString()); const event = await ndk.fetchEvent(filter); - if (event) { const decryptEventFunction = async (event: NDKEvent) => { await event.decrypt(await mainSigner.user()); - const content = SignerSchema.parse(JSON.parse(event.content)); return new NDKPrivateKeySigner(content.key as string); }; @@ -99,18 +100,18 @@ function generateContent( return JSON.stringify(content); } async function generateTags(mainSigner: NDKSigner, opts: ISaveOpts = {}) { + log("func", "generateTags", opts.toString()); const mainUser = await mainSigner.user(); const tags = [ ["p", mainUser.pubkey], ["client", "flockstr"], ]; - if (opts.associatedEvent) { const encodedEvent = opts.associatedEvent.encode(); - console.log("encodedEvent", encodedEvent); + log("info", "encodedEvent", encodedEvent.toString()); // TODO: This is trivially reversable; better to encrypt it or hash it with the pubkey const hashedEventReference = await getHashedKeyName(encodedEvent); - console.log("hashedEventReference", hashedEventReference); + log("info", "hashedEventReference", hashedEventReference.toString()); tags.push(["e", hashedEventReference]); } @@ -127,6 +128,7 @@ export async function saveEphemeralSigner( targetSigner: NDKPrivateKeySigner, opts: ISaveOpts = {}, ) { + log("func", "saveEphemeralSigner"); // Determine current user signer const mainSigner = opts.mainSigner || ndk.signer; @@ -145,7 +147,7 @@ export async function saveEphemeralSigner( await event.publish(); // Update Ephemeral signers metadata - console.log("Checking keyProfile", opts.keyProfile); + log("info", "Checking keyProfile", opts.keyProfile?.toString()); const user = await targetSigner.user(); if (opts.keyProfile) { const event = new NDKEvent(ndk, { diff --git a/lib/actions/zap.ts b/lib/actions/zap.ts index 314d154..751eab0 100644 --- a/lib/actions/zap.ts +++ b/lib/actions/zap.ts @@ -13,6 +13,8 @@ import { getTagValues, getTagsAllValues } from "../nostr/utils"; import { unixTimeNowInSeconds } from "../nostr/dates"; import { createEvent } from "./create"; import { findEphemeralSigner } from "@/lib/actions/ephemeral"; +import { log } from "@/lib/utils"; + const fetchWithZod = createZodFetcher(); const ZapEndpointResponseSchema = z.object({ nostrPubkey: z.string(), @@ -24,15 +26,14 @@ export async function sendZap( _event: NostrEvent, comment?: string, ) { - console.log("sendzap called", amount); + log("func", "sendZap"); const event = await new NDKEvent(ndk, _event); - console.log("Event", event); + log("info", event.toString()); const pr = await event.zap(amount * 1000, comment); if (!pr) { - console.log("No PR"); + log("info", "No PR"); return; } - console.log("PR", pr); const webln = await requestProvider(); return await webln.sendPayment(pr); } @@ -105,6 +106,7 @@ export async function updateListUsersFromZaps( tagId: string, event: NostrEvent, ) { + log("func", "updateListUsersFromZaps"); const SECONDS_IN_MONTH = 2_628_000; const SECONDS_IN_YEAR = SECONDS_IN_MONTH * 365; const paymentEvents = await ndk.fetchEvents({ @@ -136,7 +138,6 @@ export async function updateListUsersFromZaps( paymentInvoice.zappee, event, ); - console.log("Is valid?", isValid); if (isValid) { validUsers.push([ paymentInvoice.zappee, @@ -148,15 +149,15 @@ export async function updateListUsersFromZaps( // Send old codes to user } } + log("info", "New users", newUsers.toString()); + await sendCodesToNewUsers(ndk, newUsers, tagId, event); - // Add self - console.log("Adding self"); + // Add self; const selfIndex = validUsers.findIndex(([vu]) => vu === event.pubkey); if (selfIndex === -1) { validUsers.push([event.pubkey, "", "self", "4000000000"]); } - console.log("Valid users", validUsers); return createEvent(ndk, { ...event, kind: event.kind as number, @@ -176,7 +177,9 @@ async function sendCodesToNewUsers( const signer = await findEphemeralSigner(ndk, ndk.signer!, { associatedEventNip19: new NDKEvent(ndk, event).encode(), }); - console.log("Signer", signer); + log("func", "sendCodesToNewUsers"); + log("info", "Signer", signer?.toString()); + if (!signer) return; const delegate = await signer.user(); const messages = await ndk.fetchEvents({ @@ -189,7 +192,7 @@ async function sendCodesToNewUsers( await message.decrypt(); codes.push([getTagValues("e", message.tags) ?? "", message.content]); } - console.log("codes", codes); + log("info", "codes", codes.toString()); for (const user of users) { for (const [event, code] of codes) { @@ -203,7 +206,7 @@ async function sendCodesToNewUsers( ], pubkey: delegate.pubkey, } as NostrEvent); - console.log("Sending message"); + log("info", "sending message"); await messageEvent.encrypt(new NDKUser({ hexpubkey: user }), signer); await messageEvent.sign(signer); await messageEvent.publish(); diff --git a/lib/hooks/useEvents.ts b/lib/hooks/useEvents.ts index c48e502..cdffb97 100644 --- a/lib/hooks/useEvents.ts +++ b/lib/hooks/useEvents.ts @@ -69,7 +69,7 @@ export default function useEvents({ } }); } catch (err) { - log(debug, "error", `❌ nostr (${err})`); + log("error", `❌ nostr (${err})`); } finally { setIsLoading(false); } diff --git a/lib/utils/index.ts b/lib/utils/index.ts index 8b66b58..8331a95 100644 --- a/lib/utils/index.ts +++ b/lib/utils/index.ts @@ -85,12 +85,23 @@ export function formatNumber(number: number) { } else return "not a number"; } export function log( - isOn: boolean | undefined, - type: "info" | "error" | "warn", + type: "info" | "error" | "warn" | "func", ...args: unknown[] ) { + const isOn = true; if (!isOn) return; - console[type](...args); + const consoleType = type === "func" ? "info" : type; + const items = [...args].map((a) => `%c${a}`); + console[consoleType]( + ...items, + type === "info" + ? "color: aqua;" + : type === "warn" + ? "color: yellow;" + : type === "func" + ? "color: green;" + : "color: red;", + ); } export function validateUrl(value: string) {