/
cns.ts
203 lines (190 loc) · 5.97 KB
/
cns.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
import { EtheriumNamingService } from './namingService';
import {
NamingServiceSource,
RegistryMap,
ResolutionResponse,
isNullAddress,
} from './types';
import { default as resolverInterface } from './cns/contract/resolver';
import { default as cnsInterface } from './cns/contract/registry';
import { ResolutionError } from '.';
import { ResolutionErrorCode } from './resolutionError';
import Contract from './utils/contract';
/**
* Class to support connection with Crypto 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 Cns extends EtheriumNamingService {
readonly name: string;
readonly network: string;
readonly url: string;
readonly registryAddress?: string;
/** @internal */
readonly RegistryMap: RegistryMap = {
mainnet: '0xD1E5b0FF1287aA9f9A268759062E4Ab08b9Dacbe',
};
/**
* 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();
this.name = 'CNS';
source = this.normalizeSource(source);
this.network = source.network as string;
this.url = source.url;
if (!this.network) {
throw new Error('Unspecified network in Resolution CNS configuration');
}
if (!this.url) {
throw new Error('Unspecified url in Resolution CNS configuration');
}
this.registryAddress = source.registry
? source.registry
: this.RegistryMap[this.network];
if (this.registryAddress) {
this.registryContract = this.buildContract(
cnsInterface,
this.registryAddress,
);
}
}
/**
* Checks if the domain is in valid format
* @param domain - domain name to be checked
* @returns
*/
isSupportedDomain(domain: string): boolean {
return domain === "crypto" || (domain.indexOf('.') > 0 && /^.{1,}\.(crypto)$/.test(domain));
}
/**
* Resolves the given domain.
* @deprecated
* @param domain - domain name to be resolved
* @returns- Returns a promise that resolves in an object
*/
async resolve(domain: string): Promise<ResolutionResponse> {
throw new Error('This method is unsupported for CNS');
}
/**
* Resolves domain to a specific cryptoAddress
* @param domain - domain name to be resolved
* @param currencyTicker currency ticker such as
* - ZIL
* - BTC
* - ETH
* @returns - A promise that resolves in a string
*/
async address(domain: string, currencyTicker: string): Promise<string> {
const [tokenId, _, __, resolver] = await this.getResolutionMeta(domain);
const addr: string = await this.fetchAddress(
resolver,
tokenId,
currencyTicker,
);
if (!addr)
throw new ResolutionError(ResolutionErrorCode.UnspecifiedCurrency, {
domain,
currencyTicker,
});
return addr;
}
/**
* @internal
* @param resolver - Resolver address
* @param tokenId - namehash of a domain name
*/
private async fetchAddress(
resolver: string,
tokenId: string,
coinName?: string,
): Promise<string> {
const resolverContract = this.buildContract(
resolverInterface,
resolver,
);
const addrKey = `crypto.${coinName.toUpperCase()}.address`;
const addr: string = await this.getRecord(resolverContract, 'get', [
addrKey,
tokenId,
]);
return addr;
}
/** @internal */
private getResolver = async (tokenId): Promise<string> =>
await this.callMethod(this.registryContract, 'resolverOf', [tokenId]).catch((err) => {
if (err instanceof ResolutionError && err.code === ResolutionErrorCode.RecordNotFound)
return undefined;
throw err;
});
/** @internal */
async owner(tokenId): Promise<string> {
return await this.callMethod(this.registryContract, 'ownerOf', [tokenId]);
}
private getTtl = async (
contract: Contract,
methodname: string,
params: any[],
): Promise<string> =>
await this.callMethod(contract, methodname, params);
/** @internal */
async record(domain: string, key: string): Promise<string> {
const tokenId = this.namehash(domain);
const resolver: string = await this.getResolver(tokenId);
const resolverContract = this.buildContract(
resolverInterface,
resolver,
);
const record: string = await this.getRecord(resolverContract, 'get', [
key.replace('.value', ''),
tokenId,
]);
// Wrong Record checks
if (!record || isNullAddress(record))
throw new ResolutionError(ResolutionErrorCode.RecordNotFound, {
recordName: key,
domain: domain,
});
return record;
}
/** This is done to make testwriting easy */
private async getRecord(
contract: Contract,
methodname: string,
params: any[],
): Promise<any> {
return await this.callMethod(contract, methodname, params);
}
/**
* @param domain
* @retuns Promise that resolves to [tokenId, owner, ttl, resolver]
*/
private async getResolutionMeta(
domain: string,
): Promise<[string, string, number, string]> {
const tokenId = this.namehash(domain);
const owner: string = await this.owner(tokenId);
const resolver: string = await this.getResolver(tokenId);
if (!resolver || isNullAddress(resolver)) {
if (!owner || isNullAddress(owner))
throw new ResolutionError(ResolutionErrorCode.UnregisteredDomain, {
domain,
});
throw new ResolutionError(ResolutionErrorCode.UnspecifiedResolver, {
domain,
});
}
const resolverContract = this.buildContract(
resolverInterface,
resolver,
);
const ttl = await this.getTtl(resolverContract, 'get', ['ttl', tokenId]);
return [tokenId, owner, parseInt(ttl) || 0, resolver];
}
}