@@ -6,6 +6,7 @@ import { isSphinx } from "./detect"
66const sphinx = require ( "sphinx-bridge" )
77
88let signingPromise : Promise < SignedMessage > | null = null
9+ let l402Promise : Promise < string > | null = null
910
1011export async function enable ( ) : Promise < { pubkey : string } | null > {
1112 try {
@@ -63,26 +64,33 @@ export async function getL402(): Promise<string> {
6364
6465 if ( ! isSphinx ( ) ) return ""
6566
66- try {
67- console . log ( "[getL402] calling sphinx.getLsat..." )
68- const token = await sphinx . getLsat ( window . location . host )
69- console . log ( "[getL402] sphinx.getLsat result:" , token ? "got token" : "no token" )
70- if ( token ?. macaroon ) {
71- localStorage . setItem (
72- "l402" ,
73- JSON . stringify ( {
74- macaroon : token . macaroon ,
75- identifier : token . identifier ,
76- preimage : token . preimage ,
77- } )
78- )
79- return `L402 ${ token . macaroon } :${ token . preimage } `
80- }
81- return ""
82- } catch ( error ) {
83- console . warn ( "Failed to get L402:" , error )
84- return ""
67+ // Queue — sphinx bridge handles only one request at a time
68+ if ( ! l402Promise ) {
69+ l402Promise = ( async ( ) => {
70+ try {
71+ const token = await sphinx . getLsat ( window . location . host )
72+ if ( token ?. macaroon ) {
73+ localStorage . setItem (
74+ "l402" ,
75+ JSON . stringify ( {
76+ macaroon : token . macaroon ,
77+ identifier : token . identifier ,
78+ preimage : token . preimage ,
79+ } )
80+ )
81+ return `L402 ${ token . macaroon } :${ token . preimage } `
82+ }
83+ return ""
84+ } catch ( error ) {
85+ console . warn ( "Failed to get L402:" , error )
86+ return ""
87+ } finally {
88+ l402Promise = null
89+ }
90+ } ) ( )
8591 }
92+
93+ return l402Promise
8694}
8795
8896export function hasWebLN ( ) : boolean {
0 commit comments