UNPKG

@pushchain/ui-kit

Version:
72 lines 2.43 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.chainToNamespace = void 0; exports.fromCAIPFormat = fromCAIPFormat; exports.toCAIPFormat = toCAIPFormat; const wallet_types_1 = require("../../../types/wallet.types"); exports.chainToNamespace = { EVM: 'eip155', SOL: 'solana', }; function fromCAIPFormat(caipAddress) { const parts = caipAddress.split(':'); if (parts.length !== 3) { throw new Error('Invalid CAIP-10 address format'); } const namespace = parts[0]; const chainId = parts[1]; const rawAddress = parts[2]; let chain = null; if (namespace === 'eip155') { if (chainId === '1') { chain = wallet_types_1.ChainType.ETHEREUM; } else if (chainId === '56') { chain = wallet_types_1.ChainType.BINANCE; } else if (chainId === '42161') { chain = wallet_types_1.ChainType.ARBITRUM; } else if (chainId === '43114') { chain = wallet_types_1.ChainType.AVALANCHE; } else { chain = wallet_types_1.ChainType.ETHEREUM; } } else if (namespace === 'solana') { chain = wallet_types_1.ChainType.SOLANA; } else { throw new Error('Unsupported namespace'); } return { chain, chainId, rawAddress, }; } function toCAIPFormat(rawAddress, chain, chainId) { const formattedAddress = rawAddress; let formattedChainId = chainId; let namespace = ''; if (chain.toLowerCase() === wallet_types_1.ChainType.ETHEREUM || chain.toLowerCase() === wallet_types_1.ChainType.BINANCE || chain.toLowerCase() === wallet_types_1.ChainType.ARBITRUM || chain.toLowerCase() === wallet_types_1.ChainType.AVALANCHE) { namespace = 'eip155'; if (typeof chainId === 'string' && chainId.startsWith('0x')) { formattedChainId = parseInt(chainId, 16); } } else if (chain.toLowerCase() === wallet_types_1.ChainType.SOLANA) { namespace = 'solana'; // TODO: Find a method to get the solana chain id in caip format formattedChainId = 'EtWTRABZaYq6iMfeYKouRu166VU2xqa1'; //devnet } else { throw new Error("Unsupported chain. Use 'ethereum' or 'solana'."); } return `${namespace}:${formattedChainId}:${formattedAddress}`; } //# sourceMappingURL=address.js.map