2022-09-05 16:57:11 -04:00
|
|
|
"use strict";
|
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
|
exports.resolveAddress = exports.isAddress = exports.isAddressable = void 0;
|
2022-09-15 22:58:45 -04:00
|
|
|
const index_js_1 = require("../utils/index.js");
|
2022-09-05 16:57:11 -04:00
|
|
|
const address_js_1 = require("./address.js");
|
2022-12-02 21:27:06 -05:00
|
|
|
/**
|
|
|
|
* Returns true if %%value%% is an object which implements the
|
|
|
|
* [[Addressable]] interface.
|
2022-12-09 18:24:58 -05:00
|
|
|
*
|
|
|
|
* @example:
|
|
|
|
* // Wallets and AbstractSigner sub-classes
|
|
|
|
* isAddressable(Wallet.createRandom())
|
|
|
|
* //_result:
|
|
|
|
*
|
|
|
|
* // Contracts
|
|
|
|
* contract = new Contract("dai.tokens.ethers.eth", [ ], provider)
|
|
|
|
* isAddressable(contract)
|
|
|
|
* //_result:
|
2022-12-02 21:27:06 -05:00
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
function isAddressable(value) {
|
|
|
|
return (value && typeof (value.getAddress) === "function");
|
|
|
|
}
|
|
|
|
exports.isAddressable = isAddressable;
|
2022-12-02 21:27:06 -05:00
|
|
|
/**
|
|
|
|
* Returns true if %%value%% is a valid address.
|
2022-12-09 18:24:58 -05:00
|
|
|
*
|
|
|
|
* @example:
|
|
|
|
* // Valid address
|
|
|
|
* isAddress("0x8ba1f109551bD432803012645Ac136ddd64DBA72")
|
|
|
|
* //_result:
|
|
|
|
*
|
|
|
|
* // Valid ICAP address
|
|
|
|
* isAddress("XE65GB6LDNXYOFTX0NSV3FUWKOWIXAMJK36")
|
|
|
|
* //_result:
|
|
|
|
*
|
|
|
|
* // Invalid checksum
|
|
|
|
* isAddress("0x8Ba1f109551bD432803012645Ac136ddd64DBa72")
|
|
|
|
* //_result:
|
|
|
|
*
|
|
|
|
* // Invalid ICAP checksum
|
|
|
|
* isAddress("0x8Ba1f109551bD432803012645Ac136ddd64DBA72")
|
|
|
|
* //_result:
|
|
|
|
*
|
|
|
|
* // Not an address (an ENS name requires a provided and an
|
|
|
|
* // asynchronous API to access)
|
|
|
|
* isAddress("ricmoo.eth")
|
|
|
|
* //_result:
|
2022-12-02 21:27:06 -05:00
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
function isAddress(value) {
|
|
|
|
try {
|
|
|
|
(0, address_js_1.getAddress)(value);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch (error) { }
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
exports.isAddress = isAddress;
|
|
|
|
async function checkAddress(target, promise) {
|
|
|
|
const result = await promise;
|
|
|
|
if (result == null || result === "0x0000000000000000000000000000000000000000") {
|
2022-11-09 02:57:02 -05:00
|
|
|
(0, index_js_1.assert)(typeof (target) !== "string", "unconfigured name", "UNCONFIGURED_NAME", { value: target });
|
|
|
|
(0, index_js_1.assertArgument)(false, "invalid AddressLike value; did not resolve to a value address", "target", target);
|
2022-09-05 16:57:11 -04:00
|
|
|
}
|
|
|
|
return (0, address_js_1.getAddress)(result);
|
|
|
|
}
|
2022-12-02 21:27:06 -05:00
|
|
|
/**
|
|
|
|
* Resolves to an address for the %%target%%, which may be any
|
|
|
|
* supported address type, an [[Addressable]] or a Promise which
|
|
|
|
* resolves to an address.
|
|
|
|
*
|
|
|
|
* If an ENS name is provided, but that name has not been correctly
|
|
|
|
* configured a [[UnconfiguredNameError]] is thrown.
|
2022-12-09 18:24:58 -05:00
|
|
|
*
|
|
|
|
* @example:
|
|
|
|
* addr = "0x6B175474E89094C44Da98b954EedeAC495271d0F"
|
|
|
|
*
|
|
|
|
* // Addresses are return synchronously
|
|
|
|
* resolveAddress(addr, provider)
|
|
|
|
* //_result:
|
|
|
|
*
|
|
|
|
* // Address promises are resolved asynchronously
|
|
|
|
* resolveAddress(Promise.resolve(addr))
|
|
|
|
* //_result:
|
|
|
|
*
|
|
|
|
* // ENS names are resolved asynchronously
|
|
|
|
* resolveAddress("dai.tokens.ethers.eth", provider)
|
|
|
|
* //_result:
|
|
|
|
*
|
|
|
|
* // Addressable objects are resolved asynchronously
|
|
|
|
* contract = new Contract(addr, [ ])
|
|
|
|
* resolveAddress(contract, provider)
|
|
|
|
* //_result:
|
|
|
|
*
|
|
|
|
* // Unconfigured ENS names reject
|
|
|
|
* resolveAddress("nothing-here.ricmoo.eth", provider)
|
|
|
|
* //_error:
|
|
|
|
*
|
|
|
|
* // ENS names require a NameResolver object passed in
|
|
|
|
* // (notice the provider was omitted)
|
|
|
|
* resolveAddress("nothing-here.ricmoo.eth")
|
|
|
|
* //_error:
|
2022-12-02 21:27:06 -05:00
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
function resolveAddress(target, resolver) {
|
|
|
|
if (typeof (target) === "string") {
|
|
|
|
if (target.match(/^0x[0-9a-f]{40}$/i)) {
|
|
|
|
return (0, address_js_1.getAddress)(target);
|
|
|
|
}
|
2022-11-09 02:57:02 -05:00
|
|
|
(0, index_js_1.assert)(resolver != null, "ENS resolution requires a provider", "UNSUPPORTED_OPERATION", { operation: "resolveName" });
|
2022-09-05 16:57:11 -04:00
|
|
|
return checkAddress(target, resolver.resolveName(target));
|
|
|
|
}
|
|
|
|
else if (isAddressable(target)) {
|
|
|
|
return checkAddress(target, target.getAddress());
|
|
|
|
}
|
2023-01-15 08:03:21 -05:00
|
|
|
else if (target && typeof (target.then) === "function") {
|
2022-09-05 16:57:11 -04:00
|
|
|
return checkAddress(target, target);
|
|
|
|
}
|
2022-11-09 02:57:02 -05:00
|
|
|
(0, index_js_1.assertArgument)(false, "unsupported addressable value", "target", target);
|
2022-09-05 16:57:11 -04:00
|
|
|
}
|
|
|
|
exports.resolveAddress = resolveAddress;
|
|
|
|
//# sourceMappingURL=checks.js.map
|