From ac43e91b6488dd3e7c665e2a7954f739222e2f33 Mon Sep 17 00:00:00 2001 From: Chaitanya Potti Date: Mon, 23 Sep 2024 11:10:06 +0800 Subject: [PATCH] make injected adapters load sync --- .../adapters/default-evm-adapter/src/index.ts | 49 ++-------------- .../src/injectedAdapters.ts | 41 +++++++++++++ .../default-solana-adapter/src/index.ts | 57 ++----------------- .../src/injectedAdapters.ts | 52 +++++++++++++++++ 4 files changed, 104 insertions(+), 95 deletions(-) create mode 100644 packages/adapters/default-evm-adapter/src/injectedAdapters.ts create mode 100644 packages/adapters/default-solana-adapter/src/injectedAdapters.ts diff --git a/packages/adapters/default-evm-adapter/src/index.ts b/packages/adapters/default-evm-adapter/src/index.ts index de1761413..66ae790b5 100644 --- a/packages/adapters/default-evm-adapter/src/index.ts +++ b/packages/adapters/default-evm-adapter/src/index.ts @@ -1,44 +1,7 @@ -import { - CHAIN_NAMESPACES, - CustomChainConfig, - getChainConfig, - IAdapter, - IProvider, - IWeb3AuthCoreOptions, - normalizeWalletName, - WalletInitializationError, -} from "@web3auth/base"; -import { createStore as createMipd } from "mipd"; +import { CHAIN_NAMESPACES, CustomChainConfig, getChainConfig, IAdapter, IWeb3AuthCoreOptions, WalletInitializationError } from "@web3auth/base"; +import { WalletConnectV2Adapter } from "@web3auth/wallet-connect-v2-adapter"; -import { InjectedEvmAdapter } from "./injectedEvmAdapter"; - -export const getInjectedAdapters = async (params: { options: IWeb3AuthCoreOptions }): Promise[]> => { - const { options } = params; - const { clientId, chainConfig, sessionTime, web3AuthNetwork, useCoreKitKey } = options; - if (!Object.values(CHAIN_NAMESPACES).includes(chainConfig.chainNamespace)) - throw WalletInitializationError.invalidParams(`Invalid chainNamespace: ${chainConfig.chainNamespace}`); - const finalChainConfig = { - ...(getChainConfig(chainConfig.chainNamespace, chainConfig?.chainId) as CustomChainConfig), - ...(chainConfig || {}), - }; - // EIP-6963: multiple injected provider discovery - const mipd = createMipd(); - // We assume that all extensions have emitted by here. - // TODO: Ideally, we must use reactive listening. We will do that with v9 - const injectedProviders = mipd.getProviders().map((providerDetail) => { - return new InjectedEvmAdapter({ - name: normalizeWalletName(providerDetail.info.name), - provider: providerDetail.provider as IProvider, - chainConfig: finalChainConfig, - clientId, - sessionTime, - web3AuthNetwork, - useCoreKitKey, - }); - }); - - return injectedProviders; -}; +import { getInjectedAdapters } from "./injectedAdapters"; export const getDefaultExternalAdapters = async (params: { options: IWeb3AuthCoreOptions }): Promise[]> => { const { options } = params; @@ -50,8 +13,6 @@ export const getDefaultExternalAdapters = async (params: { options: IWeb3AuthCor ...(chainConfig || {}), }; - const [{ WalletConnectV2Adapter }] = await Promise.all([import("@web3auth/wallet-connect-v2-adapter")]); - const wcv2Adapter = new WalletConnectV2Adapter({ chainConfig: finalChainConfig, clientId, @@ -62,7 +23,9 @@ export const getDefaultExternalAdapters = async (params: { options: IWeb3AuthCor walletConnectInitOptions: {}, }, }); - const injectedProviders = await getInjectedAdapters({ options }); + const injectedProviders = getInjectedAdapters({ options }); return [...injectedProviders, wcv2Adapter]; }; + +export { getInjectedAdapters }; diff --git a/packages/adapters/default-evm-adapter/src/injectedAdapters.ts b/packages/adapters/default-evm-adapter/src/injectedAdapters.ts new file mode 100644 index 000000000..343f9f716 --- /dev/null +++ b/packages/adapters/default-evm-adapter/src/injectedAdapters.ts @@ -0,0 +1,41 @@ +import { + CHAIN_NAMESPACES, + CustomChainConfig, + getChainConfig, + IAdapter, + IProvider, + IWeb3AuthCoreOptions, + normalizeWalletName, + WalletInitializationError, +} from "@web3auth/base"; +import { createStore as createMipd } from "mipd"; + +import { InjectedEvmAdapter } from "./injectedEvmAdapter"; + +export const getInjectedAdapters = (params: { options: IWeb3AuthCoreOptions }): IAdapter[] => { + const { options } = params; + const { clientId, chainConfig, sessionTime, web3AuthNetwork, useCoreKitKey } = options; + if (!Object.values(CHAIN_NAMESPACES).includes(chainConfig.chainNamespace)) + throw WalletInitializationError.invalidParams(`Invalid chainNamespace: ${chainConfig.chainNamespace}`); + const finalChainConfig = { + ...(getChainConfig(chainConfig.chainNamespace, chainConfig?.chainId) as CustomChainConfig), + ...(chainConfig || {}), + }; + // EIP-6963: multiple injected provider discovery + const mipd = createMipd(); + // We assume that all extensions have emitted by here. + // TODO: Ideally, we must use reactive listening. We will do that with v9 + const injectedProviders = mipd.getProviders().map((providerDetail) => { + return new InjectedEvmAdapter({ + name: normalizeWalletName(providerDetail.info.name), + provider: providerDetail.provider as IProvider, + chainConfig: finalChainConfig, + clientId, + sessionTime, + web3AuthNetwork, + useCoreKitKey, + }); + }); + + return injectedProviders; +}; diff --git a/packages/adapters/default-solana-adapter/src/index.ts b/packages/adapters/default-solana-adapter/src/index.ts index 97baf5339..84d141076 100644 --- a/packages/adapters/default-solana-adapter/src/index.ts +++ b/packages/adapters/default-solana-adapter/src/index.ts @@ -1,56 +1,9 @@ -import { SolanaSignAndSendTransaction, SolanaSignMessage, SolanaSignTransaction } from "@solana/wallet-standard-features"; -import { getWallets } from "@wallet-standard/app"; -import { StandardConnect } from "@wallet-standard/features"; -import { - BaseAdapter, - CHAIN_NAMESPACES, - CustomChainConfig, - getChainConfig, - IAdapter, - IWeb3AuthCoreOptions, - normalizeWalletName, - WalletInitializationError, -} from "@web3auth/base"; +import { IAdapter, IWeb3AuthCoreOptions } from "@web3auth/base"; -import { WalletStandardAdapter } from "./walletStandardAdapter"; +import { getInjectedAdapters } from "./injectedAdapters"; -export const getInjectedAdapters = async (params: { options: IWeb3AuthCoreOptions }): Promise[]> => { - const { options } = params; - const { clientId, chainConfig, sessionTime, web3AuthNetwork, useCoreKitKey } = options; - if (!Object.values(CHAIN_NAMESPACES).includes(chainConfig.chainNamespace)) - throw WalletInitializationError.invalidParams(`Invalid chainNamespace: ${chainConfig.chainNamespace}`); - const finalChainConfig = { - ...(getChainConfig(chainConfig.chainNamespace, chainConfig?.chainId) as CustomChainConfig), - ...(chainConfig || {}), - }; - - // get installed wallets that support standard wallet - const standardWalletAdapters = [] as BaseAdapter[]; - const wallets = getWallets().get(); - wallets.forEach((wallet) => { - const { name, chains, features } = wallet; - const isSolana = chains.some((chain) => chain.startsWith("solana")); - if (!isSolana) return; - const hasRequiredFeatures = [StandardConnect, SolanaSignMessage, SolanaSignTransaction, SolanaSignAndSendTransaction].every((feature) => - Object.keys(features).includes(feature) - ); - if (!hasRequiredFeatures) return; - - standardWalletAdapters.push( - new WalletStandardAdapter({ - name: normalizeWalletName(name), - wallet, - chainConfig: finalChainConfig, - clientId, - sessionTime, - web3AuthNetwork, - useCoreKitKey, - }) - ); - }); - return standardWalletAdapters; -}; - -export const getDefaultExternalAdapters = async (params: { options: IWeb3AuthCoreOptions }): Promise[]> => { +export const getDefaultExternalAdapters = (params: { options: IWeb3AuthCoreOptions }): IAdapter[] => { return getInjectedAdapters(params); }; + +export { getInjectedAdapters }; diff --git a/packages/adapters/default-solana-adapter/src/injectedAdapters.ts b/packages/adapters/default-solana-adapter/src/injectedAdapters.ts new file mode 100644 index 000000000..c88c0846e --- /dev/null +++ b/packages/adapters/default-solana-adapter/src/injectedAdapters.ts @@ -0,0 +1,52 @@ +import { SolanaSignAndSendTransaction, SolanaSignMessage, SolanaSignTransaction } from "@solana/wallet-standard-features"; +import { getWallets } from "@wallet-standard/app"; +import { StandardConnect } from "@wallet-standard/features"; +import { + BaseAdapter, + CHAIN_NAMESPACES, + CustomChainConfig, + getChainConfig, + IAdapter, + IWeb3AuthCoreOptions, + normalizeWalletName, + WalletInitializationError, +} from "@web3auth/base"; + +import { WalletStandardAdapter } from "./walletStandardAdapter"; + +export const getInjectedAdapters = (params: { options: IWeb3AuthCoreOptions }): IAdapter[] => { + const { options } = params; + const { clientId, chainConfig, sessionTime, web3AuthNetwork, useCoreKitKey } = options; + if (!Object.values(CHAIN_NAMESPACES).includes(chainConfig.chainNamespace)) + throw WalletInitializationError.invalidParams(`Invalid chainNamespace: ${chainConfig.chainNamespace}`); + const finalChainConfig = { + ...(getChainConfig(chainConfig.chainNamespace, chainConfig?.chainId) as CustomChainConfig), + ...(chainConfig || {}), + }; + + // get installed wallets that support standard wallet + const standardWalletAdapters = [] as BaseAdapter[]; + const wallets = getWallets().get(); + wallets.forEach((wallet) => { + const { name, chains, features } = wallet; + const isSolana = chains.some((chain) => chain.startsWith("solana")); + if (!isSolana) return; + const hasRequiredFeatures = [StandardConnect, SolanaSignMessage, SolanaSignTransaction, SolanaSignAndSendTransaction].every((feature) => + Object.keys(features).includes(feature) + ); + if (!hasRequiredFeatures) return; + + standardWalletAdapters.push( + new WalletStandardAdapter({ + name: normalizeWalletName(name), + wallet, + chainConfig: finalChainConfig, + clientId, + sessionTime, + web3AuthNetwork, + useCoreKitKey, + }) + ); + }); + return standardWalletAdapters; +};