checks.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import { assert, assertArgument } from "../utils/index.js";
  2. import { getAddress } from "./address.js";
  3. /**
  4. * Returns true if %%value%% is an object which implements the
  5. * [[Addressable]] interface.
  6. *
  7. * @example:
  8. * // Wallets and AbstractSigner sub-classes
  9. * isAddressable(Wallet.createRandom())
  10. * //_result:
  11. *
  12. * // Contracts
  13. * contract = new Contract("dai.tokens.ethers.eth", [ ], provider)
  14. * isAddressable(contract)
  15. * //_result:
  16. */
  17. export function isAddressable(value) {
  18. return (value && typeof (value.getAddress) === "function");
  19. }
  20. /**
  21. * Returns true if %%value%% is a valid address.
  22. *
  23. * @example:
  24. * // Valid address
  25. * isAddress("0x8ba1f109551bD432803012645Ac136ddd64DBA72")
  26. * //_result:
  27. *
  28. * // Valid ICAP address
  29. * isAddress("XE65GB6LDNXYOFTX0NSV3FUWKOWIXAMJK36")
  30. * //_result:
  31. *
  32. * // Invalid checksum
  33. * isAddress("0x8Ba1f109551bD432803012645Ac136ddd64DBa72")
  34. * //_result:
  35. *
  36. * // Invalid ICAP checksum
  37. * isAddress("0x8Ba1f109551bD432803012645Ac136ddd64DBA72")
  38. * //_result:
  39. *
  40. * // Not an address (an ENS name requires a provided and an
  41. * // asynchronous API to access)
  42. * isAddress("ricmoo.eth")
  43. * //_result:
  44. */
  45. export function isAddress(value) {
  46. try {
  47. getAddress(value);
  48. return true;
  49. }
  50. catch (error) { }
  51. return false;
  52. }
  53. async function checkAddress(target, promise) {
  54. const result = await promise;
  55. if (result == null || result === "0x0000000000000000000000000000000000000000") {
  56. assert(typeof (target) !== "string", "unconfigured name", "UNCONFIGURED_NAME", { value: target });
  57. assertArgument(false, "invalid AddressLike value; did not resolve to a value address", "target", target);
  58. }
  59. return getAddress(result);
  60. }
  61. /**
  62. * Resolves to an address for the %%target%%, which may be any
  63. * supported address type, an [[Addressable]] or a Promise which
  64. * resolves to an address.
  65. *
  66. * If an ENS name is provided, but that name has not been correctly
  67. * configured a [[UnconfiguredNameError]] is thrown.
  68. *
  69. * @example:
  70. * addr = "0x6B175474E89094C44Da98b954EedeAC495271d0F"
  71. *
  72. * // Addresses are return synchronously
  73. * resolveAddress(addr, provider)
  74. * //_result:
  75. *
  76. * // Address promises are resolved asynchronously
  77. * resolveAddress(Promise.resolve(addr))
  78. * //_result:
  79. *
  80. * // ENS names are resolved asynchronously
  81. * resolveAddress("dai.tokens.ethers.eth", provider)
  82. * //_result:
  83. *
  84. * // Addressable objects are resolved asynchronously
  85. * contract = new Contract(addr, [ ])
  86. * resolveAddress(contract, provider)
  87. * //_result:
  88. *
  89. * // Unconfigured ENS names reject
  90. * resolveAddress("nothing-here.ricmoo.eth", provider)
  91. * //_error:
  92. *
  93. * // ENS names require a NameResolver object passed in
  94. * // (notice the provider was omitted)
  95. * resolveAddress("nothing-here.ricmoo.eth")
  96. * //_error:
  97. */
  98. export function resolveAddress(target, resolver) {
  99. if (typeof (target) === "string") {
  100. if (target.match(/^0x[0-9a-f]{40}$/i)) {
  101. return getAddress(target);
  102. }
  103. assert(resolver != null, "ENS resolution requires a provider", "UNSUPPORTED_OPERATION", { operation: "resolveName" });
  104. return checkAddress(target, resolver.resolveName(target));
  105. }
  106. else if (isAddressable(target)) {
  107. return checkAddress(target, target.getAddress());
  108. }
  109. else if (target && typeof (target.then) === "function") {
  110. return checkAddress(target, target);
  111. }
  112. assertArgument(false, "unsupported addressable value", "target", target);
  113. }
  114. //# sourceMappingURL=checks.js.map