diff --git a/src/lib/sphinx/bridge.ts b/src/lib/sphinx/bridge.ts index 95089589..cea0489c 100644 --- a/src/lib/sphinx/bridge.ts +++ b/src/lib/sphinx/bridge.ts @@ -6,6 +6,7 @@ import { isSphinx } from "./detect" const sphinx = require("sphinx-bridge") let signingPromise: Promise | null = null +let l402Promise: Promise | null = null export async function enable(): Promise<{ pubkey: string } | null> { try { @@ -63,26 +64,33 @@ export async function getL402(): Promise { if (!isSphinx()) return "" - try { - console.log("[getL402] calling sphinx.getLsat...") - const token = await sphinx.getLsat(window.location.host) - console.log("[getL402] sphinx.getLsat result:", token ? "got token" : "no token") - if (token?.macaroon) { - localStorage.setItem( - "l402", - JSON.stringify({ - macaroon: token.macaroon, - identifier: token.identifier, - preimage: token.preimage, - }) - ) - return `L402 ${token.macaroon}:${token.preimage}` - } - return "" - } catch (error) { - console.warn("Failed to get L402:", error) - return "" + // Queue — sphinx bridge handles only one request at a time + if (!l402Promise) { + l402Promise = (async () => { + try { + const token = await sphinx.getLsat(window.location.host) + if (token?.macaroon) { + localStorage.setItem( + "l402", + JSON.stringify({ + macaroon: token.macaroon, + identifier: token.identifier, + preimage: token.preimage, + }) + ) + return `L402 ${token.macaroon}:${token.preimage}` + } + return "" + } catch (error) { + console.warn("Failed to get L402:", error) + return "" + } finally { + l402Promise = null + } + })() } + + return l402Promise } export function hasWebLN(): boolean {