@ensdomains/ethers-patch-v5 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs ADDED
@@ -0,0 +1,175 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __copyProps = (to, from, except, desc) => {
7
+ if (from && typeof from === "object" || typeof from === "function") {
8
+ for (let key of __getOwnPropNames(from))
9
+ if (!__hasOwnProp.call(to, key) && key !== except)
10
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
11
+ }
12
+ return to;
13
+ };
14
+ var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
15
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
16
+
17
+ // index.ts
18
+ var index_exports = {};
19
+ module.exports = __toCommonJS(index_exports);
20
+ var import_ethers = require("ethers");
21
+ var import_utils = require("ethers/lib/utils");
22
+
23
+ // ../../src/shared.ts
24
+ var UR_PROXY = "0xeEeEEEeE14D718C2B47D9923Deab1335E144EeEe";
25
+ var ABI_FRAGMENTS = [
26
+ // AbstractUniversalResolver
27
+ "function requireResolver(bytes) view returns ((bytes name, uint256 offset, bytes32 node, address resolver, bool extended))",
28
+ // IExtendedResolver
29
+ "function resolve(bytes, bytes) view returns (bytes)",
30
+ // INameResolver
31
+ "function name(bytes32) view returns (string)",
32
+ // IAddrResolver
33
+ "function addr(bytes32) view returns (address)",
34
+ // IAddressResolver
35
+ "function addr(bytes32, uint256) view returns (bytes)"
36
+ ];
37
+ var COIN_TYPE_ETH = 60n;
38
+ var COIN_TYPE_DEFAULT = 1n << 31n;
39
+ function isEVMCoinType(coinType) {
40
+ return coinType === COIN_TYPE_ETH || coinType >= 0n && (coinType ^ COIN_TYPE_DEFAULT) < COIN_TYPE_DEFAULT;
41
+ }
42
+ function getReverseName(lowerAddress, coinType) {
43
+ return `${lowerAddress.slice(2)}.${coinType === COIN_TYPE_ETH ? "addr" : coinType === COIN_TYPE_DEFAULT ? "default" : coinType.toString(16)}.reverse`;
44
+ }
45
+
46
+ // index.ts
47
+ var import_ens_normalize = require("@adraffy/ens-normalize");
48
+ __reExport(index_exports, require("ethers"), module.exports);
49
+ var logger = new import_utils.Logger("ur-patch");
50
+ {
51
+ const { makeError } = import_utils.Logger.prototype;
52
+ import_utils.Logger.prototype.makeError = function() {
53
+ logger = this;
54
+ throw 1;
55
+ };
56
+ try {
57
+ import_ethers.utils.getAddress("abc");
58
+ } catch {
59
+ }
60
+ import_utils.Logger.prototype.makeError = makeError;
61
+ }
62
+ var ABI = new import_utils.Interface(ABI_FRAGMENTS);
63
+ Object.defineProperties(import_ethers.utils, {
64
+ dnsEncode: { value: dnsEncode },
65
+ namehash: { value: namehash },
66
+ ensNormalize: { value: import_ens_normalize.ens_normalize }
67
+ });
68
+ var { getResolver, resolveName, lookupAddress } = import_ethers.providers.BaseProvider.prototype;
69
+ import_ethers.providers.BaseProvider.prototype.getResolverOld = getResolver;
70
+ import_ethers.providers.BaseProvider.prototype.getResolver = async function(name) {
71
+ const UR = new import_ethers.Contract(UR_PROXY, ABI, this);
72
+ try {
73
+ name = (0, import_ens_normalize.ens_normalize)(name);
74
+ const result = await UR.requireResolver(dnsEncode(name));
75
+ const resolver = new import_ethers.providers.Resolver(this, result.resolver, name);
76
+ resolver._supportsEip2544 = Promise.resolve(result.extended);
77
+ return resolver;
78
+ } catch (err) {
79
+ return null;
80
+ }
81
+ };
82
+ import_ethers.providers.BaseProvider.prototype.resolveName = async function(name, coinType = COIN_TYPE_ETH) {
83
+ if (coinType === "old") return resolveName.call(this, name);
84
+ name = await name;
85
+ coinType = import_ethers.BigNumber.from(coinType).toBigInt();
86
+ if ((0, import_utils.isHexString)(name) && name.length === 42) {
87
+ return (0, import_utils.getAddress)(name);
88
+ }
89
+ const fwd = await this.getResolver(name);
90
+ if (!fwd) return null;
91
+ return fetchAddress(fwd, coinType).catch(() => null);
92
+ };
93
+ import_ethers.providers.BaseProvider.prototype.lookupAddress = async function(address, coinType = COIN_TYPE_ETH) {
94
+ if (coinType === "old") return lookupAddress.call(this, address);
95
+ address = await address;
96
+ if (!(0, import_utils.isHexString)(address) || address === "0x") {
97
+ logger.throwArgumentError("invalid address", "address", address);
98
+ }
99
+ address = address.toLowerCase();
100
+ coinType = import_ethers.BigNumber.from(coinType).toBigInt();
101
+ const reverseName = getReverseName(address, coinType);
102
+ try {
103
+ const rev = await this.getResolver(reverseName);
104
+ if (rev) {
105
+ const name = await callResolver(rev, "name");
106
+ if (name && (0, import_ens_normalize.ens_normalize)(name) === name) {
107
+ const fwd = await this.getResolver(name);
108
+ if (fwd) {
109
+ const checked = await fetchAddress(fwd, coinType);
110
+ if (address === checked.toLowerCase()) {
111
+ return name;
112
+ }
113
+ }
114
+ }
115
+ }
116
+ } catch {
117
+ }
118
+ return null;
119
+ };
120
+ function namesplit(name) {
121
+ return name ? name.split(".") : [];
122
+ }
123
+ function dnsEncode(name) {
124
+ const m = namesplit(name).map((x) => (0, import_utils.toUtf8Bytes)(x));
125
+ const v = new Uint8Array(m.reduce((a, x) => a + 1 + x.length, 1));
126
+ let pos = 0;
127
+ for (const x of m) {
128
+ if (x.length > 255) {
129
+ throw new Error("invalid DNS encoded entry; length exceeds 255 bytes");
130
+ }
131
+ v[pos++] = x.length;
132
+ v.set(x, pos);
133
+ pos += x.length;
134
+ }
135
+ return (0, import_utils.hexlify)(v);
136
+ }
137
+ function namehash(name) {
138
+ return namesplit(name).reduceRight(
139
+ (h, x) => (0, import_utils.keccak256)(h + (0, import_utils.id)(x).slice(2)),
140
+ "0x".padEnd(66, "0")
141
+ );
142
+ }
143
+ async function fetchAddress(resolver, coinType) {
144
+ if (coinType === COIN_TYPE_ETH) {
145
+ return callResolver(resolver, "addr(bytes32)");
146
+ }
147
+ const a = await callResolver(
148
+ resolver,
149
+ "addr(bytes32,uint256)",
150
+ coinType
151
+ );
152
+ return isEVMCoinType(coinType) ? a === "0x" ? a.padEnd(42) : (0, import_utils.getAddress)(a) : a;
153
+ }
154
+ async function callResolver(resolver, fragment, ...args) {
155
+ const f = ABI.getFunction(fragment);
156
+ const r = new import_ethers.Contract(resolver.address, ABI, resolver.provider);
157
+ const node = namehash(resolver.name);
158
+ if (await resolver.supportsWildcard()) {
159
+ const res = ABI.decodeFunctionResult(
160
+ f,
161
+ await r.resolve(
162
+ dnsEncode(resolver.name),
163
+ ABI.encodeFunctionData(f, [node, ...args]),
164
+ { ccipReadEnabled: true }
165
+ )
166
+ );
167
+ return f.outputs?.length === 1 ? res[0] : res;
168
+ } else {
169
+ return r[f.format()](node, ...args, { ccipReadEnabled: true });
170
+ }
171
+ }
172
+ // Annotate the CommonJS export names for ESM import in node:
173
+ 0 && (module.exports = {
174
+ ...require("ethers")
175
+ });
@@ -0,0 +1,13 @@
1
+ import { providers, BigNumberish } from 'ethers';
2
+ export * from 'ethers';
3
+
4
+ declare module "ethers/lib/utils" {
5
+ function ensNormalize(name: string): string;
6
+ }
7
+ declare module "@ethersproject/providers" {
8
+ interface BaseProvider {
9
+ getResolverOld(name: string): Promise<providers.Resolver | null>;
10
+ resolveName(name: string | Promise<string>, coinType?: BigNumberish): Promise<string | null>;
11
+ lookupAddress(address: string | Promise<string>, coinType?: BigNumberish): Promise<string | null>;
12
+ }
13
+ }
@@ -0,0 +1,13 @@
1
+ import { providers, BigNumberish } from 'ethers';
2
+ export * from 'ethers';
3
+
4
+ declare module "ethers/lib/utils" {
5
+ function ensNormalize(name: string): string;
6
+ }
7
+ declare module "@ethersproject/providers" {
8
+ interface BaseProvider {
9
+ getResolverOld(name: string): Promise<providers.Resolver | null>;
10
+ resolveName(name: string | Promise<string>, coinType?: BigNumberish): Promise<string | null>;
11
+ lookupAddress(address: string | Promise<string>, coinType?: BigNumberish): Promise<string | null>;
12
+ }
13
+ }
package/dist/index.js ADDED
@@ -0,0 +1,167 @@
1
+ // index.ts
2
+ import {
3
+ BigNumber,
4
+ Contract,
5
+ utils,
6
+ providers
7
+ } from "ethers";
8
+ import {
9
+ getAddress,
10
+ hexlify,
11
+ id as labelhash,
12
+ Interface,
13
+ isHexString,
14
+ keccak256,
15
+ Logger,
16
+ toUtf8Bytes
17
+ } from "ethers/lib/utils";
18
+
19
+ // ../../src/shared.ts
20
+ var UR_PROXY = "0xeEeEEEeE14D718C2B47D9923Deab1335E144EeEe";
21
+ var ABI_FRAGMENTS = [
22
+ // AbstractUniversalResolver
23
+ "function requireResolver(bytes) view returns ((bytes name, uint256 offset, bytes32 node, address resolver, bool extended))",
24
+ // IExtendedResolver
25
+ "function resolve(bytes, bytes) view returns (bytes)",
26
+ // INameResolver
27
+ "function name(bytes32) view returns (string)",
28
+ // IAddrResolver
29
+ "function addr(bytes32) view returns (address)",
30
+ // IAddressResolver
31
+ "function addr(bytes32, uint256) view returns (bytes)"
32
+ ];
33
+ var COIN_TYPE_ETH = 60n;
34
+ var COIN_TYPE_DEFAULT = 1n << 31n;
35
+ function isEVMCoinType(coinType) {
36
+ return coinType === COIN_TYPE_ETH || coinType >= 0n && (coinType ^ COIN_TYPE_DEFAULT) < COIN_TYPE_DEFAULT;
37
+ }
38
+ function getReverseName(lowerAddress, coinType) {
39
+ return `${lowerAddress.slice(2)}.${coinType === COIN_TYPE_ETH ? "addr" : coinType === COIN_TYPE_DEFAULT ? "default" : coinType.toString(16)}.reverse`;
40
+ }
41
+
42
+ // index.ts
43
+ import { ens_normalize } from "@adraffy/ens-normalize";
44
+ export * from "ethers";
45
+ var logger = new Logger("ur-patch");
46
+ {
47
+ const { makeError } = Logger.prototype;
48
+ Logger.prototype.makeError = function() {
49
+ logger = this;
50
+ throw 1;
51
+ };
52
+ try {
53
+ utils.getAddress("abc");
54
+ } catch {
55
+ }
56
+ Logger.prototype.makeError = makeError;
57
+ }
58
+ var ABI = new Interface(ABI_FRAGMENTS);
59
+ Object.defineProperties(utils, {
60
+ dnsEncode: { value: dnsEncode },
61
+ namehash: { value: namehash },
62
+ ensNormalize: { value: ens_normalize }
63
+ });
64
+ var { getResolver, resolveName, lookupAddress } = providers.BaseProvider.prototype;
65
+ providers.BaseProvider.prototype.getResolverOld = getResolver;
66
+ providers.BaseProvider.prototype.getResolver = async function(name) {
67
+ const UR = new Contract(UR_PROXY, ABI, this);
68
+ try {
69
+ name = ens_normalize(name);
70
+ const result = await UR.requireResolver(dnsEncode(name));
71
+ const resolver = new providers.Resolver(this, result.resolver, name);
72
+ resolver._supportsEip2544 = Promise.resolve(result.extended);
73
+ return resolver;
74
+ } catch (err) {
75
+ return null;
76
+ }
77
+ };
78
+ providers.BaseProvider.prototype.resolveName = async function(name, coinType = COIN_TYPE_ETH) {
79
+ if (coinType === "old") return resolveName.call(this, name);
80
+ name = await name;
81
+ coinType = BigNumber.from(coinType).toBigInt();
82
+ if (isHexString(name) && name.length === 42) {
83
+ return getAddress(name);
84
+ }
85
+ const fwd = await this.getResolver(name);
86
+ if (!fwd) return null;
87
+ return fetchAddress(fwd, coinType).catch(() => null);
88
+ };
89
+ providers.BaseProvider.prototype.lookupAddress = async function(address, coinType = COIN_TYPE_ETH) {
90
+ if (coinType === "old") return lookupAddress.call(this, address);
91
+ address = await address;
92
+ if (!isHexString(address) || address === "0x") {
93
+ logger.throwArgumentError("invalid address", "address", address);
94
+ }
95
+ address = address.toLowerCase();
96
+ coinType = BigNumber.from(coinType).toBigInt();
97
+ const reverseName = getReverseName(address, coinType);
98
+ try {
99
+ const rev = await this.getResolver(reverseName);
100
+ if (rev) {
101
+ const name = await callResolver(rev, "name");
102
+ if (name && ens_normalize(name) === name) {
103
+ const fwd = await this.getResolver(name);
104
+ if (fwd) {
105
+ const checked = await fetchAddress(fwd, coinType);
106
+ if (address === checked.toLowerCase()) {
107
+ return name;
108
+ }
109
+ }
110
+ }
111
+ }
112
+ } catch {
113
+ }
114
+ return null;
115
+ };
116
+ function namesplit(name) {
117
+ return name ? name.split(".") : [];
118
+ }
119
+ function dnsEncode(name) {
120
+ const m = namesplit(name).map((x) => toUtf8Bytes(x));
121
+ const v = new Uint8Array(m.reduce((a, x) => a + 1 + x.length, 1));
122
+ let pos = 0;
123
+ for (const x of m) {
124
+ if (x.length > 255) {
125
+ throw new Error("invalid DNS encoded entry; length exceeds 255 bytes");
126
+ }
127
+ v[pos++] = x.length;
128
+ v.set(x, pos);
129
+ pos += x.length;
130
+ }
131
+ return hexlify(v);
132
+ }
133
+ function namehash(name) {
134
+ return namesplit(name).reduceRight(
135
+ (h, x) => keccak256(h + labelhash(x).slice(2)),
136
+ "0x".padEnd(66, "0")
137
+ );
138
+ }
139
+ async function fetchAddress(resolver, coinType) {
140
+ if (coinType === COIN_TYPE_ETH) {
141
+ return callResolver(resolver, "addr(bytes32)");
142
+ }
143
+ const a = await callResolver(
144
+ resolver,
145
+ "addr(bytes32,uint256)",
146
+ coinType
147
+ );
148
+ return isEVMCoinType(coinType) ? a === "0x" ? a.padEnd(42) : getAddress(a) : a;
149
+ }
150
+ async function callResolver(resolver, fragment, ...args) {
151
+ const f = ABI.getFunction(fragment);
152
+ const r = new Contract(resolver.address, ABI, resolver.provider);
153
+ const node = namehash(resolver.name);
154
+ if (await resolver.supportsWildcard()) {
155
+ const res = ABI.decodeFunctionResult(
156
+ f,
157
+ await r.resolve(
158
+ dnsEncode(resolver.name),
159
+ ABI.encodeFunctionData(f, [node, ...args]),
160
+ { ccipReadEnabled: true }
161
+ )
162
+ );
163
+ return f.outputs?.length === 1 ? res[0] : res;
164
+ } else {
165
+ return r[f.format()](node, ...args, { ccipReadEnabled: true });
166
+ }
167
+ }
package/package.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "@ensdomains/ethers-patch-v5",
3
+ "version": "0.0.1",
4
+ "type": "module",
5
+ "main": "dist/index.cjs",
6
+ "module": "dist/index.js",
7
+ "types": "dist/index.d.ts",
8
+ "files": [
9
+ "./dist"
10
+ ],
11
+ "exports": {
12
+ ".": {
13
+ "import": "./dist/index.js",
14
+ "require": "./dist/index.cjs"
15
+ }
16
+ },
17
+ "peerDependencies": {
18
+ "typescript": "^5"
19
+ },
20
+ "dependencies": {
21
+ "@adraffy/ens-normalize": "^1.11.1",
22
+ "ethers": "5",
23
+ "@ethersproject/providers": "5"
24
+ },
25
+ "publishConfig": {
26
+ "access": "public"
27
+ }
28
+ }