@web3-onboard/capsule
Version:
Capsule SDK wallet module for connecting to Web3-Onboard. Web3-Onboard makes it simple to connect Ethereum hardware and software wallets to your dapp. Features standardised spec compliant web3 providers for all supported wallets, framework agnostic modern
76 lines (75 loc) • 3.27 kB
JavaScript
import { Environment as CapsuleEnvironment, OAuthMethod } from '@usecapsule/react-sdk';
import "@usecapsule/react-sdk/styles.css";
async function buildChainsMap() {
const chains = await import('viem/chains');
const chainEntries = Object.entries(chains);
const chainsMap = new Map();
for (const [, chainObject] of chainEntries) {
if (chainObject && 'id' in chainObject) {
chainsMap.set(chainObject.id, chainObject);
}
}
return chainsMap;
}
function getChainsByIds(chainIds, chainsMap) {
return chainIds.map(id => chainsMap.get(id)).filter((c) => !!c);
}
function convertChainIdToNumber(chainId) {
if (typeof chainId === 'number') {
return chainId;
}
const hexRegex = /^[0-9a-fA-F]+$/;
return hexRegex.test(chainId) ? parseInt(chainId, 16) : Number(chainId);
}
function validateOptions(options, chains, appMetadata) {
if (!(options.environment in CapsuleEnvironment)) {
throw new Error(`Invalid environment. Must be one of the Environment enum values.`);
}
if (appMetadata == null) {
throw new Error('No appMetadata passed into the Onboard object');
}
if (typeof appMetadata.name !== 'string' || appMetadata.name.trim() === '') {
throw new Error('appName must be a non-empty string.');
}
if (!Array.isArray(chains) || chains.length === 0) {
throw new Error('chains must be a non-empty array.');
}
if (chains.some(chain => typeof Number(chain.id) !== 'number')) {
throw new Error('All elements in chains must be numbers.');
}
if (options.apiKey !== undefined &&
(typeof options.apiKey !== 'string' || options.apiKey.trim() === '')) {
throw new Error('apiKey must be a non-empty string.');
}
}
function capsule(options) {
return () => {
return {
label: options.walletLabel || 'Capsule',
getIcon: options.walletIcon || (async () => (await import('./icon')).default),
getInterface: async ({ chains, appMetadata }) => {
const { default: Capsule } = await import('@usecapsule/react-sdk');
const { CapsuleEIP1193Provider } = await import('@usecapsule/wagmi-v2-integration');
validateOptions(options, chains, appMetadata);
const capsule = new Capsule(options.environment, options.apiKey, options.constructorOpts);
const chainsMap = await buildChainsMap();
const providerOpts = {
capsule: capsule,
chainId: convertChainIdToNumber(chains[0].id).toString(),
appName: appMetadata === null || appMetadata === void 0 ? void 0 : appMetadata.name,
chains: getChainsByIds(chains.map(ch => convertChainIdToNumber(ch.id)), chainsMap),
...options.modalProps
};
const provider = new CapsuleEIP1193Provider(providerOpts);
provider.disconnect = () => { capsule.logout(); };
return {
instance: capsule,
provider: provider
};
}
};
};
}
export default capsule;
export { CapsuleEnvironment as Environment };
export { OAuthMethod };