-
Notifications
You must be signed in to change notification settings - Fork 81
/
unstoppableAPI.ts
138 lines (123 loc) · 3.77 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
import { toBech32Address } from './zns/utils';
import { ResolutionError, ResolutionErrorCode } from './index';
import NamingService from './namingService';
import {
ResolutionResponse,
NamingServiceSource,
SourceDefinition,
isNullAddress,
} from './types';
import Zns from './zns';
import Ens from './ens';
import Cns from './cns';
// import * as pckg from '../package.json';
const DefaultUrl = 'https://unstoppabledomains.com/api/v1';
export default class Udapi extends NamingService {
private url: string;
private headers: {
[key: string]: string;
};
constructor() {
super();
this.url = DefaultUrl;
const DefaultUserAgent = this.isNode()
? 'node-fetch/1.0 (+https://github.com/bitinn/node-fetch)'
: navigator.userAgent;
const version = process.env.npm_package_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 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: 'UD',
});
}
}
serviceName(domain: string): string {
return this.findMethodOrThrow(domain).name;
}
/** @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);
}
}