Skip to content

Commit

Permalink
Merge pull request #428 from LIT-Protocol/feature/lit-2915-js-sdk-alw…
Browse files Browse the repository at this point in the history
…ays-get-the-amdcert-from-the-lit-cos-proxy

feat(AMD Cert): Always fetch from CORS
  • Loading branch information
Ansonhkg authored Apr 16, 2024
2 parents 62efadf + 2de96b9 commit 1286138
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 11 deletions.
4 changes: 2 additions & 2 deletions lit.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
53 changes: 44 additions & 9 deletions packages/crypto/src/lib/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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<Uint8Array> {
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);
}

/**
Expand Down

0 comments on commit 1286138

Please sign in to comment.