UNPKG

@metamask/design-system-react

Version:
61 lines 2.28 kB
import { stringToBytes, KnownCaipNamespace } from "@metamask/utils"; import { isAddress as isSolanaAddress } from "@solana/addresses"; /** * Generates a numeric seed for Ethereum (eip155) addresses. * * @param address - The Ethereum address to generate a seed from * @returns A numeric seed for jazzicon generation */ export function generateSeedEthereum(address) { // Example: parse the first 8 chars of the address after '0x' const addr = address.slice(2, 10); return parseInt(addr, 16); } /** * Generates a byte-array seed for non-Ethereum addresses (Solana, Bitcoin, etc.). * * @param address - The address to generate a byte array seed from * @returns An array of numbers representing the bytes of the address */ export function generateSeedNonEthereum(address) { return Array.from(stringToBytes(address.normalize('NFKC').toLowerCase())); } /** * Dynamically checks if the address is Bitcoin or Solana; otherwise default to Ethereum. * Returns a Promise that resolves to one of the known CAIP-2 namespaces. * * @param address - The address to determine the CAIP namespace for * @returns A Promise that resolves to a KnownCaipNamespace */ export async function getCaipNamespaceFromAddress(address) { // Check for CAIP-10 formatted addresses if (address.includes(':')) { const [namespace] = address.split(':'); if (namespace.toLowerCase() === 'bip122') { return KnownCaipNamespace.Bip122; } if (namespace.toLowerCase() === 'solana') { return KnownCaipNamespace.Solana; } if (namespace.toLowerCase() === 'eip155') { return KnownCaipNamespace.Eip155; } // Add other namespaces as needed. } try { const { validate, Network } = await import("bitcoin-address-validation"); if (validate(address, Network.mainnet) || validate(address, Network.testnet)) { return KnownCaipNamespace.Bip122; } } catch { // If the import fails or 'validate' is not available, fall through } if (isSolanaAddress(address)) { return KnownCaipNamespace.Solana; } // Default to Ethereum return KnownCaipNamespace.Eip155; } //# sourceMappingURL=Jazzicon.utilities.mjs.map