Skip to content

Commit

Permalink
make injected adapters load sync
Browse files Browse the repository at this point in the history
  • Loading branch information
chaitanyapotti committed Sep 23, 2024
1 parent dda0f94 commit ac43e91
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 95 deletions.
49 changes: 6 additions & 43 deletions packages/adapters/default-evm-adapter/src/index.ts
Original file line number Diff line number Diff line change
@@ -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<IAdapter<unknown>[]> => {
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<IAdapter<unknown>[]> => {
const { options } = params;
Expand All @@ -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,
Expand All @@ -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 };
41 changes: 41 additions & 0 deletions packages/adapters/default-evm-adapter/src/injectedAdapters.ts
Original file line number Diff line number Diff line change
@@ -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<unknown>[] => {
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;
};
57 changes: 5 additions & 52 deletions packages/adapters/default-solana-adapter/src/index.ts
Original file line number Diff line number Diff line change
@@ -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<IAdapter<unknown>[]> => {
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<void>[];
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<IAdapter<unknown>[]> => {
export const getDefaultExternalAdapters = (params: { options: IWeb3AuthCoreOptions }): IAdapter<unknown>[] => {
return getInjectedAdapters(params);
};

export { getInjectedAdapters };
52 changes: 52 additions & 0 deletions packages/adapters/default-solana-adapter/src/injectedAdapters.ts
Original file line number Diff line number Diff line change
@@ -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<unknown>[] => {
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<void>[];
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;
};

0 comments on commit ac43e91

Please sign in to comment.