UNPKG

@hyperlane-xyz/widgets

Version:

Common react components for Hyperlane projects

77 lines 2.88 kB
import { useChain, useChains } from '@cosmos-kit/react'; import { useMemo } from 'react'; import { cosmoshub } from '@hyperlane-xyz/registry'; import { ProtocolType, ensure0x } from '@hyperlane-xyz/utils'; import { widgetLogger } from '../logger.js'; import { getChainsForProtocol } from './utils.js'; const PLACEHOLDER_COSMOS_CHAIN = cosmoshub.name; const logger = widgetLogger.child({ module: 'widgets/walletIntegrations/cosmosWallet', }); function toHexString(bytes) { return ensure0x(Array.from(bytes, (byte) => byte.toString(16).padStart(2, '0')).join('')); } export function useCosmosAccount(multiProvider) { const cosmosChains = getCosmosChainNames(multiProvider); const chainToContext = useChains(cosmosChains); return useMemo(() => { const addresses = []; let publicKey; let isReady = false; for (const [chainName, context] of Object.entries(chainToContext)) { if (!context.address) continue; addresses.push({ address: context.address, chainName }); // Keep the most recently connected chain's public key. publicKey = context.getAccount().then((acc) => toHexString(acc.pubkey), (error) => { logger.error('Failed to get Cosmos account public key', { error }); return undefined; }); isReady = true; } return { protocol: ProtocolType.Cosmos, addresses, publicKey, isReady, }; }, [chainToContext]); } export function useCosmosWalletDetails() { const { wallet } = useChain(PLACEHOLDER_COSMOS_CHAIN); const { logo, prettyName } = wallet || {}; return useMemo(() => ({ name: prettyName, logoUrl: typeof logo === 'string' ? logo : undefined, }), [prettyName, logo]); } export function useCosmosConnectFn() { const { openView } = useChain(PLACEHOLDER_COSMOS_CHAIN); return openView; } export function useCosmosDisconnectFn() { const { disconnect, address } = useChain(PLACEHOLDER_COSMOS_CHAIN); return async () => { if (address) await disconnect(); }; } export function useCosmosActiveChain(_multiProvider) { // CosmosKit doesn't have the concept of an active chain; // wallets connect to each chain independently. return useMemo(() => ({}), []); } export function getCosmosChains(multiProvider) { const chains = [ ...getChainsForProtocol(multiProvider, ProtocolType.Cosmos), ...getChainsForProtocol(multiProvider, ProtocolType.CosmosNative), ]; if (!chains.some((chain) => chain.name === cosmoshub.name)) { chains.push(cosmoshub); } return chains; } export function getCosmosChainNames(multiProvider) { return getCosmosChains(multiProvider).map((c) => c.name); } //# sourceMappingURL=cosmosWallet.js.map