-
Notifications
You must be signed in to change notification settings - Fork 81
/
ens.ts
287 lines (269 loc) · 8.41 KB
/
ens.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import { default as ensInterface } from './ens/contract/ens';
import { default as resolverInterface } from './ens/contract/resolver';
import { hash } from 'eth-ens-namehash';
import { formatsByCoinType } from '@ensdomains/address-encoder';
import {
ResolutionResponse,
EthCoinIndex,
NamingServiceSource,
Bip44Constants,
isNullAddress,
} from './types';
import { EtheriumNamingService } from './namingService';
import { ResolutionError, ResolutionErrorCode } from './index';
import Contract from './utils/contract';
const RegistryMap = {
mainnet: '0x314159265dd8dbb310642f98f50c066173c1259b',
ropsten: '0x112234455c3a32fd11230c42e7bccd4a84e02010',
};
/**
* Class to support connection with Etherium naming service
* @param network - network string such as
* - mainnet
* - ropsten
* @param url - main api url such as
* - https://mainnet.infura.io
* @param registryAddress - address for a registry contract
*/
export default class Ens extends EtheriumNamingService {
name = 'ENS';
readonly network: string;
readonly url: string;
readonly registryAddress?: string;
/**
* Source object describing the network naming service operates on
* @param source - if specified as a string will be used as main url, if omited then defaults are used
* @throws ConfigurationError - when either network or url is setup incorrectly
*/
constructor(source: NamingServiceSource = true) {
super();
source = this.normalizeSource(source);
this.network = <string>source.network;
this.url = source.url;
if (!this.network) {
throw new Error('Unspecified network in Resolution ENS configuration');
}
if (!this.url) {
throw new Error('Unspecified url in Resolution ENS configuration');
}
this.registryAddress = source.registry
? source.registry
: RegistryMap[this.network];
if (this.registryAddress) {
this.registryContract = this.buildContract(
ensInterface,
this.registryAddress,
);
}
}
/**
* Checks if the domain is in valid format
* @param domain - domain name to be checked
*/
isSupportedDomain(domain: string): boolean {
return (
domain.indexOf('.') > 0 && /^.{1,}\.(eth|luxe|xyz|test)$/.test(domain)
);
}
/**
* Checks if the current network is supported
*/
isSupportedNetwork(): boolean {
return this.registryAddress != null;
}
/** @internal */
record(domain: string, key: string): Promise<string> {
throw new Error('Method not implemented.');
}
/**
* Reverse the ens address to a ens registered domain name
* @async
* @param address - address you wish to reverse
* @param currencyTicker - currency ticker like BTC, ETH, ZIL
* @returns Domain name attached to this address
*/
async reverse(address: string, currencyTicker: string): Promise<string> {
if (currencyTicker != 'ETH') {
throw new Error(`Ens doesn't support any currency other than ETH`);
}
if (address.startsWith('0x')) {
address = address.substr(2);
}
const reverseAddress = address + '.addr.reverse';
const nodeHash = hash(reverseAddress);
const resolverAddress = await this.getResolver(nodeHash);
if (isNullAddress(resolverAddress)) {
return null;
}
const resolverContract = this.buildContract(
resolverInterface(resolverAddress, EthCoinIndex),
resolverAddress,
);
return await this.resolverCallToName(resolverContract, nodeHash);
}
/**
* Resolves domain to a specific cryptoAddress
* @param domain - domain name to be resolved
* @param currencyTicker - specific currency ticker such as
* - ZIL
* - BTC
* - ETH
* @returns A promise that resolves in a string
* @throws ResolutionError
*/
async address(domain: string, currencyTicker: string): Promise<string> {
const nodeHash = this.namehash(domain);
const ownerPromise = this.owner(domain);
const resolver = await this.getResolver(nodeHash);
if (
!resolver ||
isNullAddress(resolver)
) {
const owner = await ownerPromise;
if (!owner || isNullAddress(owner) )
throw new ResolutionError(ResolutionErrorCode.UnregisteredDomain, {
domain,
});
throw new ResolutionError(ResolutionErrorCode.UnspecifiedResolver, {
domain,
});
}
const coinType = this.getCoinType(currencyTicker.toUpperCase());
var addr = await this.fetchAddress(resolver, nodeHash, coinType);
if (!addr)
throw new ResolutionError(ResolutionErrorCode.UnspecifiedCurrency, {
domain,
currencyTicker,
});
return addr;
}
/**
* Owner of the domain
* @param domain - domain name
* @returns An owner address of the domain
*/
async owner(domain: string): Promise<string | null> {
const nodeHash = this.namehash(domain);
try {
return await this.getOwner(nodeHash);
} catch (err) {
if (
err instanceof ResolutionError &&
err.code === ResolutionErrorCode.RecordNotFound
)
return null;
throw err;
}
}
/**
* Resolves the given domain
* @async
* @param domain - domain name to be resolved
* @returns A promise that resolves in an object
*/
async resolve(domain: string): Promise<ResolutionResponse | null> {
if (!this.isSupportedDomain(domain) || !this.isSupportedNetwork()) {
return null;
}
const nodeHash = this.namehash(domain);
var [owner, ttl, resolver] = await this.getResolutionInfo(domain);
if (isNullAddress(owner)) owner = null;
const address = await this.fetchAddress(resolver, nodeHash, EthCoinIndex);
return {
addresses: {
ETH: address,
},
meta: {
owner,
type: this.name,
ttl: Number(ttl),
},
};
}
/**
* Produces ENS namehash
* @param domain - domain to be hashed
* @returns ENS namehash of a domain
*/
namehash(domain: string): string {
this.ensureSupportedDomain(domain);
return hash(domain);
}
/**
* This was done to make automated tests more configurable
*/
private resolverCallToName(resolverContract: Contract, nodeHash) {
return this.callMethod(resolverContract, 'name', [nodeHash]);
}
private async getTTL(nodeHash) {
try {
return await this.callMethod(this.registryContract, 'ttl', [nodeHash]);
} catch (err) {
if ( err instanceof ResolutionError && err.code === ResolutionErrorCode.RecordNotFound)
return 0;
throw err;
}
}
/**
* This was done to make automated tests more configurable
*/
private async getResolver(nodeHash) {
try {
return await this.callMethod(this.registryContract, 'resolver', [nodeHash]);
} catch (err) {
if (
err instanceof ResolutionError &&
err.code === ResolutionErrorCode.RecordNotFound
)
return null;
throw err;
}
}
/**
* This was done to make automated tests more configurable
*/
private async getOwner(nodeHash) {
return await this.callMethod(this.registryContract, 'owner', [nodeHash]);
}
/**
* This was done to make automated tests more configurable
*/
private async getResolutionInfo(domain: string) {
const nodeHash = this.namehash(domain);
return await Promise.all([this.owner(domain), this.getTTL(nodeHash), this.getResolver(nodeHash)]);
}
/** @internal */
protected getCoinType(currencyTicker: string): number {
const constants: Bip44Constants[] = require('bip44-constants');
const coin = constants.findIndex(
item =>
item[1] === currencyTicker.toUpperCase() ||
item[2] === currencyTicker.toUpperCase(),
);
if (coin < 0 || !formatsByCoinType[coin])
throw new ResolutionError(ResolutionErrorCode.UnsupportedCurrency, {
currencyTicker,
});
return coin;
}
/**
* @param resolver - resolver address
* @param nodeHash - namehash of a domain name
*/
private async fetchAddress(resolver, nodeHash, coinType?: number) {
if (!resolver || isNullAddress(resolver)) {
return null;
}
const resolverContract = this.buildContract(
resolverInterface(resolver, coinType),
resolver,
);
const addr: string =
coinType != EthCoinIndex
? await this.callMethod(resolverContract, 'addr', [nodeHash, coinType])
: await this.callMethod(resolverContract, 'addr', [nodeHash]);
if (!addr) return null;
const data = Buffer.from(addr.replace('0x', ''), 'hex');
return formatsByCoinType[coinType].encoder(data);
}
}