Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 27 additions & 19 deletions src/lib/sphinx/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { isSphinx } from "./detect"
const sphinx = require("sphinx-bridge")

let signingPromise: Promise<SignedMessage> | null = null
let l402Promise: Promise<string> | null = null

export async function enable(): Promise<{ pubkey: string } | null> {
try {
Expand Down Expand Up @@ -63,26 +64,33 @@ export async function getL402(): Promise<string> {

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 {
Expand Down
Loading