ethers.js/lib.esm/address/checks.js

46 lines
1.7 KiB
JavaScript
Raw Normal View History

2022-09-16 05:58:45 +03:00
import { throwArgumentError, throwError } from "../utils/index.js";
2022-09-05 23:57:11 +03:00
import { getAddress } from "./address.js";
export function isAddressable(value) {
return (value && typeof (value.getAddress) === "function");
}
export function isAddress(value) {
try {
getAddress(value);
return true;
}
catch (error) { }
return false;
}
async function checkAddress(target, promise) {
const result = await promise;
if (result == null || result === "0x0000000000000000000000000000000000000000") {
if (typeof (target) === "string") {
2022-09-16 05:58:45 +03:00
return throwError("unconfigured name", "UNCONFIGURED_NAME", { value: target });
2022-09-05 23:57:11 +03:00
}
2022-09-16 05:58:45 +03:00
return throwArgumentError("invalid AddressLike value; did not resolve to a value address", "target", target);
2022-09-05 23:57:11 +03:00
}
return getAddress(result);
}
// Resolves an Ethereum address, ENS name or Addressable object,
// throwing if the result is null.
export function resolveAddress(target, resolver) {
if (typeof (target) === "string") {
if (target.match(/^0x[0-9a-f]{40}$/i)) {
return getAddress(target);
}
if (resolver == null) {
2022-09-16 05:58:45 +03:00
return throwError("ENS resolution requires a provider", "UNSUPPORTED_OPERATION", {
2022-09-05 23:57:11 +03:00
operation: "resolveName",
});
}
return checkAddress(target, resolver.resolveName(target));
}
else if (isAddressable(target)) {
return checkAddress(target, target.getAddress());
}
else if (typeof (target.then) === "function") {
return checkAddress(target, target);
}
2022-09-16 05:58:45 +03:00
return throwArgumentError("unsupported addressable value", "target", target);
2022-09-05 23:57:11 +03:00
}
//# sourceMappingURL=checks.js.map