diff --git a/src/lib/lnurl.ts b/src/lib/lnurl.ts index f4f19c44..75429bbf 100644 --- a/src/lib/lnurl.ts +++ b/src/lib/lnurl.ts @@ -1,5 +1,7 @@ import { bech32, utf8 } from '@scure/base' +const corsProxyUrl = 'https://cors-header-proxy.bordalix.workers.dev/proxy?apiurl=' + const emailRegex = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/ @@ -26,6 +28,13 @@ type LnUrlCallbackResponse = { pr: string } +const fetchWithCorsProxy = (url: string, options?: RequestInit): Promise => { + // don't use proxy in tests to avoid CORS issues with Playwright's request interception + const isPlaywrightTest = typeof process !== 'undefined' && process?.env?.PLAYWRIGHT_TEST === '1' + const proxyUrl = isPlaywrightTest ? url : `${corsProxyUrl}${encodeURIComponent(url)}` + return fetch(proxyUrl, options) +} + const checkResponse = (response: Response): Promise => { if (!response.ok) return Promise.reject(response) return response.json() @@ -41,7 +50,7 @@ const checkLnUrlResponse = (amount: number, data: LnUrlResponse) => { const fetchLnUrlInvoice = async (amount: number, note: string, data: LnUrlResponse) => { let url = `${data.callback}?amount=${amount}` if (note) url += `&comment=${note}` - const res = await fetch(url).then(checkResponse) + const res = await fetchWithCorsProxy(url).then(checkResponse) return res.pr } @@ -78,7 +87,7 @@ export const getCallbackUrl = (lnurl: string): string => { export const checkLnUrlConditions = (lnurl: string): Promise => { return new Promise((resolve, reject) => { const url = getCallbackUrl(lnurl) - fetch(url) + fetchWithCorsProxy(url) .then(checkResponse) .then(resolve) .catch(reject) @@ -89,7 +98,7 @@ export const fetchInvoice = (lnurl: string, sats: number, note: string): Promise return new Promise((resolve, reject) => { const url = getCallbackUrl(lnurl) const amount = Math.round(sats * 1000) // millisatoshis - fetch(url) + fetchWithCorsProxy(url) .then(checkResponse) .then((data) => checkLnUrlResponse(amount, data)) .then((data) => fetchLnUrlInvoice(amount, note, data)) @@ -101,7 +110,7 @@ export const fetchInvoice = (lnurl: string, sats: number, note: string): Promise export const fetchArkAddress = (lnurl: string): Promise => { return new Promise((resolve, reject) => { const url = getCallbackUrl(lnurl) + '?method=ark' - fetch(url) + fetchWithCorsProxy(url) .then(checkResponse) .then(resolve) .catch(reject)