From a5370d187bace08663c0149847513cbcf7d05550 Mon Sep 17 00:00:00 2001 From: Chaitanya Potti Date: Tue, 16 Apr 2024 14:59:59 +0800 Subject: [PATCH] optimize whitelist config --- packages/base/src/utils.ts | 12 ++++++++++-- packages/modal/src/modalManager.ts | 10 ++++++++-- packages/no-modal/src/noModal.ts | 7 +++++-- 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/packages/base/src/utils.ts b/packages/base/src/utils.ts index cb14764c0..95fa15c07 100644 --- a/packages/base/src/utils.ts +++ b/packages/base/src/utils.ts @@ -4,11 +4,17 @@ import type { OPENLOGIN_NETWORK_TYPE, WhiteLabelData } from "@toruslabs/openlogi import { WEB3AUTH_NETWORK } from "./adapter/IAdapter"; +export interface WhitelistResponse { + urls: string[]; + signed_urls: Record; +} + export interface PROJECT_CONFIG_RESPONSE { whitelabel?: WhiteLabelData; sms_otp_enabled: boolean; wallet_connect_enabled: boolean; wallet_connect_project_id?: string; + whitelist?: WhitelistResponse; } export function storageAvailable(type: "sessionStorage" | "localStorage"): boolean { @@ -51,11 +57,13 @@ export const signerHost = (web3AuthNetwork?: OPENLOGIN_NETWORK_TYPE): string => return SIGNER_MAP[web3AuthNetwork ?? WEB3AUTH_NETWORK.SAPPHIRE_MAINNET]; }; -export const fetchProjectConfig = async (clientId: string, web3AuthNetwork?: OPENLOGIN_NETWORK_TYPE): Promise => { +export const fetchProjectConfig = async (clientId: string, web3AuthNetwork: OPENLOGIN_NETWORK_TYPE): Promise => { try { const url = new URL(`${signerHost(web3AuthNetwork)}/api/configuration`); url.searchParams.append("project_id", clientId); - const res = await get(String(url)); + url.searchParams.append("network", web3AuthNetwork); + url.searchParams.append("whitelist", "true"); + const res = await get(url.href); return res; } catch (e) { throw new Error(`Failed to fetch project config: ${(e as Error).message}`); diff --git a/packages/modal/src/modalManager.ts b/packages/modal/src/modalManager.ts index d3946561a..2ead652be 100644 --- a/packages/modal/src/modalManager.ts +++ b/packages/modal/src/modalManager.ts @@ -64,7 +64,7 @@ export class Web3Auth extends Web3AuthNoModal implements IWeb3AuthModal { let projectConfig: PROJECT_CONFIG_RESPONSE; try { - projectConfig = await fetchProjectConfig(this.options.clientId); + projectConfig = await fetchProjectConfig(this.options.clientId, this.options.web3AuthNetwork); } catch (e) { throw WalletInitializationError.notReady("failed to fetch project configurations"); } @@ -80,7 +80,7 @@ export class Web3Auth extends Web3AuthNoModal implements IWeb3AuthModal { }); this.subscribeToLoginModalEvents(); - const { sms_otp_enabled: smsOtpEnabled } = projectConfig; + const { sms_otp_enabled: smsOtpEnabled, whitelist } = projectConfig; if (smsOtpEnabled !== undefined) { const adapterConfig: Record = { [WALLET_ADAPTERS.OPENLOGIN]: { @@ -153,6 +153,9 @@ export class Web3Auth extends Web3AuthNoModal implements IWeb3AuthModal { } as LoginConfig[keyof LoginConfig], }; } + if (whitelist) { + finalOpenloginAdapterSettings.originData = whitelist.signed_urls; + } if (this.options.uiConfig.uxMode) { finalOpenloginAdapterSettings.uxMode = this.options.uiConfig.uxMode; } @@ -213,6 +216,9 @@ export class Web3Auth extends Web3AuthNoModal implements IWeb3AuthModal { }, }); } + if (whitelist) { + openloginAdapter.setAdapterSettings({ originData: whitelist.signed_urls }); + } if (this.options.uiConfig?.uxMode) { openloginAdapter.setAdapterSettings({ uxMode: this.options.uiConfig.uxMode }); } diff --git a/packages/no-modal/src/noModal.ts b/packages/no-modal/src/noModal.ts index cc1f676e3..a9d3e9940 100644 --- a/packages/no-modal/src/noModal.ts +++ b/packages/no-modal/src/noModal.ts @@ -94,7 +94,7 @@ export class Web3AuthNoModal extends SafeEventEmitter implements IWeb3Auth { let projectConfig: PROJECT_CONFIG_RESPONSE; try { - projectConfig = await fetchProjectConfig(this.coreOptions.clientId); + projectConfig = await fetchProjectConfig(this.coreOptions.clientId, this.coreOptions.web3AuthNetwork); } catch (e) { throw WalletInitializationError.notReady("failed to fetch project configurations"); } @@ -127,7 +127,7 @@ export class Web3AuthNoModal extends SafeEventEmitter implements IWeb3Auth { const { whitelabel } = projectConfig; this.coreOptions.uiConfig = merge(clonedeep(whitelabel), this.coreOptions.uiConfig); - const { sms_otp_enabled: smsOtpEnabled } = projectConfig; + const { sms_otp_enabled: smsOtpEnabled, whitelist } = projectConfig; if (smsOtpEnabled !== undefined) { openloginAdapter.setAdapterSettings({ loginConfig: { @@ -140,6 +140,9 @@ export class Web3AuthNoModal extends SafeEventEmitter implements IWeb3Auth { }, }); } + if (whitelist) { + openloginAdapter.setAdapterSettings({ originData: whitelist.signed_urls }); + } if (this.coreOptions.privateKeyProvider) { if (openloginAdapter.currentChainNamespace !== this.coreOptions.privateKeyProvider.currentChainConfig.chainNamespace) {