Skip to content

Commit

Permalink
Merge pull request #1789 from Web3Auth:feat/whitelist-optim
Browse files Browse the repository at this point in the history
optimize whitelist config
  • Loading branch information
chaitanyapotti authored Apr 16, 2024
2 parents 7b5142a + a5370d1 commit 35bd1c4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
12 changes: 10 additions & 2 deletions packages/base/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>;
}

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 {
Expand Down Expand Up @@ -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<PROJECT_CONFIG_RESPONSE> => {
export const fetchProjectConfig = async (clientId: string, web3AuthNetwork: OPENLOGIN_NETWORK_TYPE): Promise<PROJECT_CONFIG_RESPONSE> => {
try {
const url = new URL(`${signerHost(web3AuthNetwork)}/api/configuration`);
url.searchParams.append("project_id", clientId);
const res = await get<PROJECT_CONFIG_RESPONSE>(String(url));
url.searchParams.append("network", web3AuthNetwork);
url.searchParams.append("whitelist", "true");
const res = await get<PROJECT_CONFIG_RESPONSE>(url.href);
return res;
} catch (e) {
throw new Error(`Failed to fetch project config: ${(e as Error).message}`);
Expand Down
10 changes: 8 additions & 2 deletions packages/modal/src/modalManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand All @@ -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_ADAPTER_TYPE, ModalConfig> = {
[WALLET_ADAPTERS.OPENLOGIN]: {
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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 });
}
Expand Down
7 changes: 5 additions & 2 deletions packages/no-modal/src/noModal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down Expand Up @@ -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: {
Expand All @@ -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) {
Expand Down

0 comments on commit 35bd1c4

Please sign in to comment.