-
Notifications
You must be signed in to change notification settings - Fork 81
/
unstoppableAPI.ts
197 lines (179 loc) · 5.38 KB
/
unstoppableAPI.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
import { toBech32Address } from './zns/utils';
import { ResolutionError, ResolutionErrorCode } from './index';
import NamingService from './namingService';
import {
ResolutionResponse,
NamingServiceSource,
NamingServiceName,
SourceDefinition,
isNullAddress,
} from './types';
import Zns from './zns';
import Ens from './ens';
import Cns from './cns';
import pckg from './package.json';
export default class Udapi extends NamingService {
readonly name = 'UDAPI';
private url: string;
private headers: {
[key: string]: string;
};
constructor(url: string) {
super();
this.url = url;
const DefaultUserAgent = this.isNode()
? 'node-fetch/1.0 (+https://github.com/bitinn/node-fetch)'
: navigator.userAgent;
const version = pckg.version;
const CustomUserAgent = `${DefaultUserAgent} Resolution/${version}`;
this.headers = { 'X-user-agent': CustomUserAgent };
}
/** @internal */
isSupportedDomain(domain: string): boolean {
return !!this.findMethod(domain);
}
/** @internal */
isSupportedNetwork(): boolean {
return true;
}
/** @internal */
namehash(domain: string): string {
const method = this.findMethod(domain);
if (!method)
throw new ResolutionError(ResolutionErrorCode.UnsupportedDomain, {
domain,
});
return method.namehash(domain);
}
/**
* Resolves the domain via UD API mirror
* @param domain - domain name to be resolved
* @param currencyTicker - currencyTicker such as
* - ZIL
* - BTC
* - ETH
*/
async address(domain: string, currencyTicker: string): Promise<string> {
const data = await this.resolve(domain);
if (!data.meta.owner || isNullAddress(data.meta.owner))
throw new ResolutionError(ResolutionErrorCode.UnregisteredDomain, {
domain,
});
const address = data.addresses[currencyTicker.toUpperCase()];
if (!address)
throw new ResolutionError(ResolutionErrorCode.UnspecifiedCurrency, {
domain,
currencyTicker,
});
return address;
}
/**
* Owner of the domain
* @param domain - domain name
* @returns An owner address of the domain
*/
async owner(domain: string): Promise<string | null> {
const { owner } = (await this.resolve(domain)).meta;
if (!owner) return null;
return owner.startsWith('zil1') ? owner : toBech32Address(owner);
}
/**
* Resolves ipfshash from domain
* @param domain - domain name
* @throws ResolutionError.RecordNotFound if not found
*/
async ipfsHash(domain: string): Promise<string> {
const answer = await this.resolve(domain);
if (!answer || !answer.ipfs || !answer.ipfs.html)
throw new ResolutionError(ResolutionErrorCode.RecordNotFound, {
recordName: 'IPFS hash',
domain: domain,
});
return answer.ipfs.html;
}
/**
* Resolves email from domain
* @param domain - domain name
* @throws ResolutionError.RecordNotFound if not found
*/
async email(domain: string): Promise<string> {
const answer = await this.resolve(domain);
if (!answer || !answer.whois || !answer.whois.email)
throw new ResolutionError(ResolutionErrorCode.RecordNotFound, {
recordName: 'email',
domain: domain,
});
return answer.whois.email;
}
/**
* Resolves httpUrl from domain
* @param domain - domain name
* @throws ResolutionError.RecordNotFound if not found
*/
async httpUrl(domain: string): Promise<string> {
const answer = await this.resolve(domain);
if (!answer || !answer.ipfs || !answer.ipfs.redirect_domain)
throw new ResolutionError(ResolutionErrorCode.RecordNotFound, {
recordName: 'httpUrl',
domain: domain,
});
return answer.ipfs.redirect_domain;
}
/**
* Resolves the domain name via UD API mirror
* @param domain - domain name to be resolved
*/
async resolve(domain: string): Promise<ResolutionResponse> {
try {
const response = await this.fetch(`${this.url}/${domain}`, {
method: 'GET',
headers: this.headers,
});
return await response.json();
} catch (error) {
if (error.name !== 'FetchError') throw error;
throw new ResolutionError(ResolutionErrorCode.NamingServiceDown, {
method: this.name,
});
}
}
/**
* Returns naming service name
* @param domain - domain name
*/
serviceName(domain: string): NamingServiceName {
return this.findMethodOrThrow(domain).name;
}
/**
* Not implemented for this naming service
* @param domain
*/
async resolver(domain: string): Promise<string> {
throw new Error('Method not implemented.');
}
/** @internal */
protected normalizeSource(source: NamingServiceSource): SourceDefinition {
throw new Error('Method not implemented.');
}
private findMethod(domain: string) {
return [new Zns(), new Ens(), new Cns()].find(m =>
m.isSupportedDomain(domain),
);
}
private findMethodOrThrow(domain: string) {
const method = this.findMethod(domain);
if (!method)
throw new ResolutionError(ResolutionErrorCode.UnsupportedDomain, {
domain,
});
return method;
}
/**
* Looks up for an arbitrary key inside the records of certain domain
* @param domain - domain name
* @param key - key to look for
*/
async record(domain: string, key: string): Promise<string> {
return await this.findMethodOrThrow(domain).record(domain, key);
}
}