@ensdomains/ethers-patch-v6 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,151 @@
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
+
22
+ // ../../src/shared.ts
23
+ var UR_PROXY = "0xeEeEEEeE14D718C2B47D9923Deab1335E144EeEe";
24
+ var ABI_FRAGMENTS = [
25
+ // AbstractUniversalResolver
26
+ "function requireResolver(bytes) view returns ((bytes name, uint256 offset, bytes32 node, address resolver, bool extended))",
27
+ // IExtendedResolver
28
+ "function resolve(bytes, bytes) view returns (bytes)",
29
+ // INameResolver
30
+ "function name(bytes32) view returns (string)",
31
+ // IAddrResolver
32
+ "function addr(bytes32) view returns (address)",
33
+ // IAddressResolver
34
+ "function addr(bytes32, uint256) view returns (bytes)"
35
+ ];
36
+ var COIN_TYPE_ETH = 60n;
37
+ var COIN_TYPE_DEFAULT = 1n << 31n;
38
+ function isEVMCoinType(coinType) {
39
+ return coinType === COIN_TYPE_ETH || coinType >= 0n && (coinType ^ COIN_TYPE_DEFAULT) < COIN_TYPE_DEFAULT;
40
+ }
41
+ function getReverseName(lowerAddress, coinType) {
42
+ return `${lowerAddress.slice(2)}.${coinType === COIN_TYPE_ETH ? "addr" : coinType === COIN_TYPE_DEFAULT ? "default" : coinType.toString(16)}.reverse`;
43
+ }
44
+
45
+ // index.ts
46
+ __reExport(index_exports, require("ethers"), module.exports);
47
+ var ABI = new import_ethers.Interface(ABI_FRAGMENTS);
48
+ import_ethers.EnsResolver.fromNameOld = import_ethers.EnsResolver.fromName;
49
+ import_ethers.EnsResolver.fromName = async function(provider, name) {
50
+ if (!name) return null;
51
+ const dns = (0, import_ethers.dnsEncode)(name, 255);
52
+ const UR = new import_ethers.Contract(UR_PROXY, ABI, provider);
53
+ try {
54
+ const result = await UR.requireResolver(dns);
55
+ const resolver = new import_ethers.EnsResolver(provider, result.resolver, name);
56
+ const extended = Promise.resolve(result.extended);
57
+ resolver.supportsWildcard = () => extended;
58
+ return resolver;
59
+ } catch (err) {
60
+ return null;
61
+ }
62
+ };
63
+ var { resolveName, lookupAddress } = import_ethers.AbstractProvider.prototype;
64
+ import_ethers.AbstractProvider.prototype.resolveName = async function(name, coinType = COIN_TYPE_ETH) {
65
+ if (coinType === "old") return resolveName.call(this, name);
66
+ coinType = (0, import_ethers.getBigInt)(coinType, "coinType");
67
+ const fwd = await this.getResolver(name);
68
+ if (!fwd) return null;
69
+ return fetchAddress(fwd, coinType).catch(() => null);
70
+ };
71
+ import_ethers.AbstractProvider.prototype.lookupAddress = async function(address, coinType = COIN_TYPE_ETH) {
72
+ if (coinType === "old") return lookupAddress.call(this, address);
73
+ (0, import_ethers.assertArgument)(
74
+ (0, import_ethers.isHexString)(address) && address !== "0x",
75
+ "invalid address",
76
+ "address",
77
+ address
78
+ );
79
+ address = address.toLowerCase();
80
+ coinType = (0, import_ethers.getBigInt)(coinType, "coinType");
81
+ const reverseName = getReverseName(address, coinType);
82
+ try {
83
+ const rev = await this.getResolver(reverseName);
84
+ if (rev) {
85
+ const name = await callResolver(rev, "name");
86
+ if (name && (0, import_ethers.ensNormalize)(name) === name) {
87
+ const fwd = await this.getResolver(name);
88
+ if (fwd) {
89
+ const checked = await fetchAddress(fwd, coinType).then(
90
+ (x) => x.toLowerCase(),
91
+ () => {
92
+ }
93
+ );
94
+ if (checked) {
95
+ (0, import_ethers.assert)(
96
+ address === checked,
97
+ "address->name->address mismatch",
98
+ "BAD_DATA",
99
+ { value: [address, checked] }
100
+ );
101
+ return name;
102
+ }
103
+ }
104
+ }
105
+ }
106
+ return null;
107
+ } catch (error) {
108
+ if ((0, import_ethers.isError)(error, "BAD_DATA") && error.value === "0x") {
109
+ return null;
110
+ }
111
+ if ((0, import_ethers.isError)(error, "CALL_EXCEPTION")) {
112
+ return null;
113
+ }
114
+ throw error;
115
+ }
116
+ };
117
+ async function fetchAddress(resolver, coinType) {
118
+ if (coinType === COIN_TYPE_ETH) {
119
+ return callResolver(resolver, "addr(bytes32)");
120
+ } else {
121
+ const a = await callResolver(
122
+ resolver,
123
+ "addr(bytes32,uint256)",
124
+ coinType
125
+ );
126
+ return isEVMCoinType(coinType) ? a === "0x" ? a.padEnd(42) : (0, import_ethers.getAddress)(a) : a;
127
+ }
128
+ }
129
+ async function callResolver(resolver, fragment, ...args) {
130
+ const f = ABI.getFunction(fragment);
131
+ const r = new import_ethers.Contract(resolver.address, ABI, resolver.provider);
132
+ if (await resolver.supportsWildcard()) {
133
+ const res = ABI.decodeFunctionResult(
134
+ f,
135
+ await r.resolve(
136
+ (0, import_ethers.dnsEncode)(resolver.name, 255),
137
+ ABI.encodeFunctionData(f, [(0, import_ethers.namehash)(resolver.name), ...args]),
138
+ { enableCcipRead: true }
139
+ )
140
+ );
141
+ return f.outputs.length === 1 ? res[0] : res;
142
+ } else {
143
+ return r[f.format()]((0, import_ethers.namehash)(resolver.name), ...args, {
144
+ enableCcipRead: true
145
+ });
146
+ }
147
+ }
148
+ // Annotate the CommonJS export names for ESM import in node:
149
+ 0 && (module.exports = {
150
+ ...require("ethers")
151
+ });
@@ -0,0 +1,12 @@
1
+ import { BigNumberish } from 'ethers';
2
+ export * from 'ethers';
3
+
4
+ declare module "ethers" {
5
+ namespace EnsResolver {
6
+ function fromNameOld(provider: AbstractProvider, name: string): Promise<EnsResolver | null>;
7
+ }
8
+ interface AbstractProvider {
9
+ resolveName(name: string, coinType?: BigNumberish): Promise<string | null>;
10
+ lookupAddress(address: string, coinType?: BigNumberish): Promise<string | null>;
11
+ }
12
+ }
@@ -0,0 +1,12 @@
1
+ import { BigNumberish } from 'ethers';
2
+ export * from 'ethers';
3
+
4
+ declare module "ethers" {
5
+ namespace EnsResolver {
6
+ function fromNameOld(provider: AbstractProvider, name: string): Promise<EnsResolver | null>;
7
+ }
8
+ interface AbstractProvider {
9
+ resolveName(name: string, coinType?: BigNumberish): Promise<string | null>;
10
+ lookupAddress(address: string, coinType?: BigNumberish): Promise<string | null>;
11
+ }
12
+ }
package/dist/index.js ADDED
@@ -0,0 +1,143 @@
1
+ // index.ts
2
+ import {
3
+ EnsResolver,
4
+ AbstractProvider,
5
+ Contract,
6
+ dnsEncode,
7
+ assert,
8
+ isHexString,
9
+ assertArgument,
10
+ getBigInt,
11
+ Interface,
12
+ isError,
13
+ namehash,
14
+ getAddress,
15
+ ensNormalize
16
+ } from "ethers";
17
+
18
+ // ../../src/shared.ts
19
+ var UR_PROXY = "0xeEeEEEeE14D718C2B47D9923Deab1335E144EeEe";
20
+ var ABI_FRAGMENTS = [
21
+ // AbstractUniversalResolver
22
+ "function requireResolver(bytes) view returns ((bytes name, uint256 offset, bytes32 node, address resolver, bool extended))",
23
+ // IExtendedResolver
24
+ "function resolve(bytes, bytes) view returns (bytes)",
25
+ // INameResolver
26
+ "function name(bytes32) view returns (string)",
27
+ // IAddrResolver
28
+ "function addr(bytes32) view returns (address)",
29
+ // IAddressResolver
30
+ "function addr(bytes32, uint256) view returns (bytes)"
31
+ ];
32
+ var COIN_TYPE_ETH = 60n;
33
+ var COIN_TYPE_DEFAULT = 1n << 31n;
34
+ function isEVMCoinType(coinType) {
35
+ return coinType === COIN_TYPE_ETH || coinType >= 0n && (coinType ^ COIN_TYPE_DEFAULT) < COIN_TYPE_DEFAULT;
36
+ }
37
+ function getReverseName(lowerAddress, coinType) {
38
+ return `${lowerAddress.slice(2)}.${coinType === COIN_TYPE_ETH ? "addr" : coinType === COIN_TYPE_DEFAULT ? "default" : coinType.toString(16)}.reverse`;
39
+ }
40
+
41
+ // index.ts
42
+ export * from "ethers";
43
+ var ABI = new Interface(ABI_FRAGMENTS);
44
+ EnsResolver.fromNameOld = EnsResolver.fromName;
45
+ EnsResolver.fromName = async function(provider, name) {
46
+ if (!name) return null;
47
+ const dns = dnsEncode(name, 255);
48
+ const UR = new Contract(UR_PROXY, ABI, provider);
49
+ try {
50
+ const result = await UR.requireResolver(dns);
51
+ const resolver = new EnsResolver(provider, result.resolver, name);
52
+ const extended = Promise.resolve(result.extended);
53
+ resolver.supportsWildcard = () => extended;
54
+ return resolver;
55
+ } catch (err) {
56
+ return null;
57
+ }
58
+ };
59
+ var { resolveName, lookupAddress } = AbstractProvider.prototype;
60
+ AbstractProvider.prototype.resolveName = async function(name, coinType = COIN_TYPE_ETH) {
61
+ if (coinType === "old") return resolveName.call(this, name);
62
+ coinType = getBigInt(coinType, "coinType");
63
+ const fwd = await this.getResolver(name);
64
+ if (!fwd) return null;
65
+ return fetchAddress(fwd, coinType).catch(() => null);
66
+ };
67
+ AbstractProvider.prototype.lookupAddress = async function(address, coinType = COIN_TYPE_ETH) {
68
+ if (coinType === "old") return lookupAddress.call(this, address);
69
+ assertArgument(
70
+ isHexString(address) && address !== "0x",
71
+ "invalid address",
72
+ "address",
73
+ address
74
+ );
75
+ address = address.toLowerCase();
76
+ coinType = getBigInt(coinType, "coinType");
77
+ const reverseName = getReverseName(address, coinType);
78
+ try {
79
+ const rev = await this.getResolver(reverseName);
80
+ if (rev) {
81
+ const name = await callResolver(rev, "name");
82
+ if (name && ensNormalize(name) === name) {
83
+ const fwd = await this.getResolver(name);
84
+ if (fwd) {
85
+ const checked = await fetchAddress(fwd, coinType).then(
86
+ (x) => x.toLowerCase(),
87
+ () => {
88
+ }
89
+ );
90
+ if (checked) {
91
+ assert(
92
+ address === checked,
93
+ "address->name->address mismatch",
94
+ "BAD_DATA",
95
+ { value: [address, checked] }
96
+ );
97
+ return name;
98
+ }
99
+ }
100
+ }
101
+ }
102
+ return null;
103
+ } catch (error) {
104
+ if (isError(error, "BAD_DATA") && error.value === "0x") {
105
+ return null;
106
+ }
107
+ if (isError(error, "CALL_EXCEPTION")) {
108
+ return null;
109
+ }
110
+ throw error;
111
+ }
112
+ };
113
+ async function fetchAddress(resolver, coinType) {
114
+ if (coinType === COIN_TYPE_ETH) {
115
+ return callResolver(resolver, "addr(bytes32)");
116
+ } else {
117
+ const a = await callResolver(
118
+ resolver,
119
+ "addr(bytes32,uint256)",
120
+ coinType
121
+ );
122
+ return isEVMCoinType(coinType) ? a === "0x" ? a.padEnd(42) : getAddress(a) : a;
123
+ }
124
+ }
125
+ async function callResolver(resolver, fragment, ...args) {
126
+ const f = ABI.getFunction(fragment);
127
+ const r = new Contract(resolver.address, ABI, resolver.provider);
128
+ if (await resolver.supportsWildcard()) {
129
+ const res = ABI.decodeFunctionResult(
130
+ f,
131
+ await r.resolve(
132
+ dnsEncode(resolver.name, 255),
133
+ ABI.encodeFunctionData(f, [namehash(resolver.name), ...args]),
134
+ { enableCcipRead: true }
135
+ )
136
+ );
137
+ return f.outputs.length === 1 ? res[0] : res;
138
+ } else {
139
+ return r[f.format()](namehash(resolver.name), ...args, {
140
+ enableCcipRead: true
141
+ });
142
+ }
143
+ }
package/package.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "name": "@ensdomains/ethers-patch-v6",
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
+ "ethers": "6"
22
+ },
23
+ "publishConfig": {
24
+ "access": "public"
25
+ }
26
+ }