From 2f6875cca604093f4b0a75b2770b1e26fc22d154 Mon Sep 17 00:00:00 2001 From: Jon Staab Date: Fri, 5 Dec 2025 09:44:21 -0800 Subject: [PATCH] Fix zapper loading --- packages/app/src/zappers.ts | 74 +++++++++++++++++-------------------- packages/util/src/Zaps.ts | 5 +-- 2 files changed, 36 insertions(+), 43 deletions(-) diff --git a/packages/app/src/zappers.ts b/packages/app/src/zappers.ts index bb8d4d6..2285e2f 100644 --- a/packages/app/src/zappers.ts +++ b/packages/app/src/zappers.ts @@ -39,59 +39,53 @@ export const fetchZapper = batcher(800, async (lnurls: string[]) => { const base = appContext.dufflepudUrl const result = new Map() + for (const lnurl of lnurls) { + if (!lnurl.startsWith("lnurl1")) { + throw new Error(`Invalid lnurl ${lnurl}`) + } + } + + const addZapper = (lnurl: string, info: any) => { + if (info) { + try { + result.set(lnurl, {...info, lnurl}) + } catch (e) { + // pass + } + } + } + // Use dufflepud if we it's set up to protect user privacy, otherwise fetch directly if (base) { - const hexUrls = removeUndefined(lnurls.map(lnurl => tryCatch(() => bech32ToHex(lnurl)))) + const hexUrls = lnurls.map(bech32ToHex) + const res = await tryCatch(() => postJson(`${base}/zapper/info`, {lnurls: hexUrls})) - if (hexUrls.length > 0) { - const res: any = await tryCatch( - async () => await postJson(`${base}/zapper/info`, {lnurls: hexUrls}), - ) - - for (const {hexUrl, info} of res?.data || []) { - if (info) { - const lnurl = hexToBech32("lnurl", hexUrl) - - tryCatch(() => result.set(lnurl, {...info, lnurl})) - } - } + for (const {lnurl, info} of res?.data || []) { + addZapper(hexToBech32("lnurl", lnurl), info) } } else { - const results = await Promise.all( + await Promise.all( lnurls.map(async lnurl => { - const hexUrl = tryCatch(() => bech32ToHex(lnurl)) - const info = hexUrl ? await tryCatch(async () => await fetchJson(hexUrl)) : undefined - - return {lnurl, hexUrl, info} + addZapper(lnurl, await tryCatch(() => fetchJson(bech32ToHex(lnurl)))) }), ) + } - for (const {lnurl, info} of results) { - if (info) { - result.set(lnurl, {...info, lnurl}) + if (result.size > 0) { + zappersByLnurl.update($zappersByLnurl => { + for (const [lnurl, zapper] of result) { + $zappersByLnurl.set(lnurl, zapper) } + + return $zappersByLnurl + }) + + for (const zapper of result.values()) { + notifyZapper(zapper) } } - zappersByLnurl.update($zappersByLnurl => { - for (const [nip05, info] of result) { - $zappersByLnurl.set(nip05, info) - } - - return $zappersByLnurl - }) - - for (const info of result.values()) { - notifyZapper(info) - } - - return lnurls.map(lnurl => { - const info = result.get(lnurl) - - if (info) { - return {...info, lnurl} - } - }) + return lnurls.map(lnurl => result.get(lnurl)) }) export const forceLoadZapper = makeForceLoadItem(fetchZapper, getZapper) diff --git a/packages/util/src/Zaps.ts b/packages/util/src/Zaps.ts index 3cb1f1e..d36f45d 100644 --- a/packages/util/src/Zaps.ts +++ b/packages/util/src/Zaps.ts @@ -58,11 +58,12 @@ export const getInvoiceAmount = (bolt11: string) => { export const getLnUrl = (address: string) => { address = address.toLowerCase() + // If it's already a lud06 we're good if (address.startsWith("lnurl1")) { return address } - // If it's a regular url, just encode it + // If it's a regular url, encode it if (address.includes("://")) { return hexToBech32("lnurl", address) } @@ -75,8 +76,6 @@ export const getLnUrl = (address: string) => { return hexToBech32("lnurl", `https://${domain}/.well-known/lnurlp/${name}`) } } - - return undefined } export type Zapper = {