diff --git a/lit.config.json b/lit.config.json index 414e3fdf42..41f51f87b5 100644 --- a/lit.config.json +++ b/lit.config.json @@ -46,9 +46,9 @@ "debug": true, "sevAttestation": false, "CONTROLLER_AUTHSIG": { - "sig": "0x6c6447a53fe789fd0ae681c7698cb414985d006a00637906f412d292bbfd2e352e10af744428e3208df35d2d4065c767941eb4d7d502dfe569267705f0d4d5e61c", + "sig": "0x01d7f16d2e1c79846d235c45e506bd866b0c9b0af05736ed6d2cd9de4a08d5dc1156a8884550df267b1cafc9a81bb029ed3348d15d53a6ae421ec848472c2c341b", "derivedVia": "web3.eth.personal.sign", - "signedMessage": "localhost wants you to sign in with your Ethereum account:\n0xeF71c2604f17Ec6Fc13409DF24EfdC440D240d37\n\nThis is a test statement. You can put anything you want here.\n\nURI: https://localhost/login\nVersion: 1\nChain ID: 1\nNonce: 0x6cbca5687b3e317ecd4c2c5ef54aa6f8743c63f80dab435a981fa1fe29a2000f\nIssued At: 2024-03-12T13:34:47.589Z\nExpiration Time: 2024-03-12T14:34:47.584Z", + "signedMessage": "localhost wants you to sign in with your Ethereum account:\n0xeF71c2604f17Ec6Fc13409DF24EfdC440D240d37\n\nThis is a test statement. You can put anything you want here.\n\nURI: https://localhost/login\nVersion: 1\nChain ID: 1\nNonce: 0x6a1c254dc3cad8dcdd02e02ba9d5c0c706ce4b1e040bdfc06e87fb360644d81c\nIssued At: 2024-04-15T17:19:38.839Z\nExpiration Time: 2024-04-15T18:19:38.837Z", "address": "0xeF71c2604f17Ec6Fc13409DF24EfdC440D240d37" }, "CONTROLLER_WALLET": { diff --git a/packages/crypto/src/lib/crypto.ts b/packages/crypto/src/lib/crypto.ts index 46936bd0f0..ea242f6451 100644 --- a/packages/crypto/src/lib/crypto.ts +++ b/packages/crypto/src/lib/crypto.ts @@ -20,6 +20,8 @@ import { nacl } from '@lit-protocol/nacl'; import { SIGTYPE } from '@lit-protocol/constants'; import { CombinedECDSASignature } from '@lit-protocol/types'; +const LIT_CORS_PROXY = `https://cors.litgateway.com`; + // if 'wasmExports' is not available, we need to initialize the BLS SDK if (!globalThis.wasmExports) { blsSdk.initWasmBlsSdk().then((exports) => { @@ -359,16 +361,49 @@ function base64ToBufferAsync(base64) { }); } -async function getAmdCert(url: string) { - // unfortunately, until AMD enables CORS, we have to use a proxy when in the browser - // This project is hosted on heroku and uses this codebase: https://github.com/LIT-Protocol/cors-proxy-amd - if (isBrowser()) { - // CORS proxy url - url = `https://cors.litgateway.com/${url}`; +/** + * Asynchronously fetches an AMD certification from a specified URL using a CORS proxy. + * The primary purpose of using a CORS proxy is to avoid being rate-limited by AMD. + * The function attempts to fetch the AMD cert through a proxy, and if the proxy fetch fails, + * it retries directly from the original URL. + * + * Note: This project is hosted on heroku and uses this codebase: https://github.com/LIT-Protocol/cors-proxy-amd + * + * @param url The URL from which to fetch the AMD cert. + * @returns A Promise that resolves to a Uint8Array containing the AMD certification data. + * @throws An error detailing HTTP or network issues encountered during the fetch process. + */ +async function getAmdCert(url: string): Promise { + const proxyUrl = `${LIT_CORS_PROXY}/${url}`; + + log( + `[getAmdCert] Fetching AMD cert using proxy URL ${proxyUrl} to manage CORS restrictions and to avoid being rate limited by AMD.` + ); + + async function fetchAsUint8Array(targetUrl) { + const res = await fetch(targetUrl); + if (!res.ok) { + throw new Error(`[getAmdCert] HTTP error! status: ${response.status}`); + } + const arrayBuffer = await res.arrayBuffer(); + return new Uint8Array(arrayBuffer); + } + + try { + return await fetchAsUint8Array(proxyUrl); + } catch (e) { + log(`[getAmdCert] Failed to fetch AMD cert from proxy:`, e); + } + + // Try direct fetch only if proxy fails + log('[getAmdCert] Attempting to fetch directly without proxy.'); + + try { + return await fetchAsUint8Array(url); + } catch (e) { + log('[getAmdCert] Direct fetch also failed:', e); + throw e; // Re-throw to signal that both methods failed } - const response = await fetch(url); - const arrayBuffer = await response.arrayBuffer(); - return new Uint8Array(arrayBuffer); } /**