UNPKG

wttp-core

Version:

Core contracts, interfaces, and TypeScript types for the Web3 Transfer Protocol (WTTP).

64 lines 2.11 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.formatEthereumAddress = formatEthereumAddress; exports.resolveEnsName = resolveEnsName; exports.getHostAddress = getHostAddress; const core_1 = require("@wttp/core"); const ethers_1 = require("ethers"); /** * Formats and validates an Ethereum address * * @param address - The Ethereum address to format and validate * @returns The checksummed Ethereum address * @throws Error if the address is invalid */ function formatEthereumAddress(address) { try { // Use ethers to validate the host is a valid address const checksumAddress = ethers_1.ethers.getAddress(String(address)); // ethers.isAddress(checksumAddress); // try/catch implemented, so this is not needed return checksumAddress; } catch (error) { throw `Invalid Ethereum address: ${address} - ${error}`; } } /** * Resolves an ENS (Ethereum Name Service) name to its corresponding Ethereum address * * @param name - The ENS name to resolve (e.g., "example.eth") * @returns Promise resolving to the Ethereum address * @throws Error if the ENS name cannot be resolved */ async function resolveEnsName(name) { const rpcUrl = (0, core_1.getRpcUrl)(1); const provider = new ethers_1.ethers.JsonRpcProvider(rpcUrl); try { const resolved = await provider.resolveName(name); if (resolved) { return resolved; } else { throw `Could not resolve ENS name: ${name}`; } } catch (error) { throw `Invalid ENS name: ${name} - ${error}`; } } /** * Resolves a hostname to an Ethereum address * Handles both ENS names and direct Ethereum addresses * * @param hostname - The hostname to resolve (can be URL, wURL, or hostname string) * @returns Promise resolving to the Ethereum address */ async function getHostAddress(hostname) { if (hostname.endsWith('.eth')) { return await resolveEnsName(hostname); } else { return formatEthereumAddress(hostname); } } //# sourceMappingURL=domains.js.map