@metamask/design-system-react
Version:
Design system react ui components
67 lines • 2.66 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getCaipNamespaceFromAddress = exports.generateSeedNonEthereum = exports.generateSeedEthereum = void 0;
const utils_1 = require("@metamask/utils");
const addresses_1 = require("@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
*/
function generateSeedEthereum(address) {
// Example: parse the first 8 chars of the address after '0x'
const addr = address.slice(2, 10);
return parseInt(addr, 16);
}
exports.generateSeedEthereum = generateSeedEthereum;
/**
* 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
*/
function generateSeedNonEthereum(address) {
return Array.from((0, utils_1.stringToBytes)(address.normalize('NFKC').toLowerCase()));
}
exports.generateSeedNonEthereum = generateSeedNonEthereum;
/**
* 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
*/
async function getCaipNamespaceFromAddress(address) {
// Check for CAIP-10 formatted addresses
if (address.includes(':')) {
const [namespace] = address.split(':');
if (namespace.toLowerCase() === 'bip122') {
return utils_1.KnownCaipNamespace.Bip122;
}
if (namespace.toLowerCase() === 'solana') {
return utils_1.KnownCaipNamespace.Solana;
}
if (namespace.toLowerCase() === 'eip155') {
return utils_1.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 utils_1.KnownCaipNamespace.Bip122;
}
}
catch {
// If the import fails or 'validate' is not available, fall through
}
if ((0, addresses_1.isAddress)(address)) {
return utils_1.KnownCaipNamespace.Solana;
}
// Default to Ethereum
return utils_1.KnownCaipNamespace.Eip155;
}
exports.getCaipNamespaceFromAddress = getCaipNamespaceFromAddress;
//# sourceMappingURL=Jazzicon.utilities.cjs.map