@hyperlane-xyz/widgets
Version:
Common react components for Hyperlane projects
43 lines • 2.03 kB
JavaScript
import { cosmoshub } from '@hyperlane-xyz/registry';
import { ProtocolType, } from '@hyperlane-xyz/utils';
import { getAddressForChain } from './walletAddresses.js';
export function getAccountAddressForChain(multiProvider, chainName, accounts) {
if (!chainName || !accounts)
return undefined;
const protocol = multiProvider.getProtocol(chainName);
const account = accounts[protocol];
return getAddressForChain(account?.addresses, protocol, chainName);
}
export function getAddressFromAccountAndChain(account, chainName) {
if (!account) {
return 'Unknown';
}
// only in cosmos there are multiple addresses per account, in this
// case we display the cosmos hub address by default. If the user
// selects a cosmos based origin chain in the swap form that cosmos
// address is displayed instead
if (account.protocol === ProtocolType.Cosmos ||
account.protocol === ProtocolType.CosmosNative) {
// chainName can be an EVM chain here, therefore if no
// cosmos address was found we search for the cosmos hub
// address below
const cosmosAddress = account?.addresses?.find((a) => a.chainName === chainName)?.address;
// if no cosmos address was found for the chain name we search
// for the cosmos hub address as fallback
return (cosmosAddress ??
account?.addresses?.find((a) => a.chainName === cosmoshub.name)
?.address ??
'Unknown');
}
// by default display the first address of the account
return account.addresses[0]?.address ?? 'Unknown';
}
export function getAccountAddressAndPubKey(multiProvider, chainName, accounts) {
const address = getAccountAddressForChain(multiProvider, chainName, accounts);
if (!accounts || !chainName || !address)
return {};
const protocol = multiProvider.getProtocol(chainName);
const publicKey = accounts[protocol]?.publicKey;
return { address, publicKey };
}
//# sourceMappingURL=accountUtils.js.map