ethers.js/packages/address/src.ts/checks.ts
2022-04-11 17:09:17 -04:00

51 lines
2.0 KiB
TypeScript

import { getAddress } from "./address.js";
import { logger } from "./logger.js";
import type { Addressable, NameResolver } from "./types.js";
export function isAddressable(value: any): value is Addressable {
return (value && typeof(value.getAddress) === "function");
}
export function isAddress(value: any): boolean {
try {
getAddress(value);
return true;
} catch (error) { }
return false;
}
// Resolves an Ethereum address, ENS name or Addressable object,
// throwing if the result is null; an explicit null returns null
//export async function resolveAddress(target?: null, resolver?: null | NameResolver): Promise<null>;
//export async function resolveAddress(target: string | Addressable, resolver?: null | NameResolver): Promise<string>;
//export async function resolveAddress(target: null | undefined | string | Addressable, resolver?: null | NameResolver) {
export async function resolveAddress(target: string | Addressable, resolver?: null | NameResolver) {
//if (target == null) { return null; }
if (typeof(target) === "string") {
if (target.match(/^0x[0-9a-f]{40}$/i)) { return getAddress(target); }
if (resolver == null) {
return logger.throwError("ENS resolution requires a provider", "UNSUPPORTED_OPERATION", {
operation: "resolveName",
});
}
const result = await resolver.resolveName(target);
if (result == null || result === "0x0000000000000000000000000000000000000000") {
return logger.throwError("unconfigured name", "UNCONFIGURED_NAME", { value: target });
}
return getAddress(result);
} else if (isAddressable(target)) {
const result = await target.getAddress();
if (result == null) {
logger.throwArgumentError("addressable resolved to null", "target", target);
}
return getAddress(result);
}
return logger.throwArgumentError("unsupported addressable value", "target", target);
}