UNPKG

12.9 kBJavaScriptView Raw
1"use strict";
2var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4 return new (P || (P = Promise))(function (resolve, reject) {
5 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8 step((generator = generator.apply(thisArg, _arguments || [])).next());
9 });
10};
11var __generator = (this && this.__generator) || function (thisArg, body) {
12 var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13 return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14 function verb(n) { return function (v) { return step([n, v]); }; }
15 function step(op) {
16 if (f) throw new TypeError("Generator is already executing.");
17 while (_) try {
18 if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19 if (y = 0, t) op = [op[0] & 2, t.value];
20 switch (op[0]) {
21 case 0: case 1: t = op; break;
22 case 4: _.label++; return { value: op[1], done: false };
23 case 5: _.label++; y = op[1]; op = [0]; continue;
24 case 7: op = _.ops.pop(); _.trys.pop(); continue;
25 default:
26 if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27 if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28 if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29 if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30 if (t[2]) _.ops.pop();
31 _.trys.pop(); continue;
32 }
33 op = body.call(thisArg, _);
34 } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35 if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36 }
37};
38var __importDefault = (this && this.__importDefault) || function (mod) {
39 return (mod && mod.__esModule) ? mod : { "default": mod };
40};
41var __importStar = (this && this.__importStar) || function (mod) {
42 if (mod && mod.__esModule) return mod;
43 var result = {};
44 if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
45 result["default"] = mod;
46 return result;
47};
48Object.defineProperty(exports, "__esModule", { value: true });
49var ens_1 = __importDefault(require("./ens"));
50var zns_1 = __importDefault(require("./zns"));
51var cns_1 = __importDefault(require("./cns"));
52var unstoppableAPI_1 = __importDefault(require("./unstoppableAPI"));
53var types_1 = require("./types");
54var resolutionError_1 = __importStar(require("./resolutionError"));
55/**
56 * Blockchain domain Resolution library - Resolution.
57 * @example
58 * ```
59 * let Resolution = new Resolution({blockchain: {ens: {url: 'https://mainnet.infura.io', network: 'mainnet'}}});
60 * let domain = brad.zil
61 * let Resolution = Resolution.address(domain);
62 * ```
63 */
64var Resolution = /** @class */ (function () {
65 /**
66 * Resolution constructor
67 * @property blockchain - main configuration object
68 */
69 function Resolution(_a) {
70 var _b = (_a === void 0 ? {} : _a).blockchain, blockchain = _b === void 0 ? true : _b;
71 this.blockchain = !!blockchain;
72 if (blockchain) {
73 if (blockchain == true) {
74 blockchain = {};
75 }
76 if (blockchain.ens === undefined) {
77 blockchain.ens = true;
78 }
79 if (blockchain.zns === undefined) {
80 blockchain.zns = true;
81 }
82 if (blockchain.cns == undefined) {
83 blockchain.cns = true;
84 }
85 if (blockchain.ens) {
86 this.ens = new ens_1.default(blockchain.ens);
87 }
88 if (blockchain.zns) {
89 this.zns = new zns_1.default(blockchain.zns);
90 }
91 if (blockchain.cns) {
92 this.cns = new cns_1.default(blockchain.cns);
93 }
94 }
95 else {
96 this.api = new unstoppableAPI_1.default();
97 }
98 }
99 /**
100 * Resolves the given domain
101 * @async
102 * @param domain - domain name to be resolved
103 * @returns A promise that resolves in an object
104 */
105 Resolution.prototype.resolve = function (domain) {
106 return __awaiter(this, void 0, void 0, function () {
107 var method, result;
108 return __generator(this, function (_a) {
109 switch (_a.label) {
110 case 0:
111 method = this.getNamingMethodOrThrow(domain);
112 return [4 /*yield*/, method.resolve(domain)];
113 case 1:
114 result = _a.sent();
115 return [2 /*return*/, result || types_1.UnclaimedDomainResponse];
116 }
117 });
118 });
119 };
120 /**
121 * Resolves give domain name to a specific currency address if exists
122 * @async
123 * @param domain - domain name to be resolved
124 * @param currencyTicker - currency ticker like BTC, ETH, ZIL
125 * @returns A promise that resolves in an address or null
126 */
127 Resolution.prototype.address = function (domain, currencyTicker) {
128 return __awaiter(this, void 0, void 0, function () {
129 var error_1;
130 return __generator(this, function (_a) {
131 switch (_a.label) {
132 case 0:
133 _a.trys.push([0, 2, , 3]);
134 return [4 /*yield*/, this.addressOrThrow(domain, currencyTicker)];
135 case 1: return [2 /*return*/, _a.sent()];
136 case 2:
137 error_1 = _a.sent();
138 if (error_1 instanceof resolutionError_1.default) {
139 return [2 /*return*/, null];
140 }
141 else {
142 throw error_1;
143 }
144 return [3 /*break*/, 3];
145 case 3: return [2 /*return*/];
146 }
147 });
148 });
149 };
150 /**
151 * Resolves the ipfs hash configured for domain records on ZNS
152 * @param domain - domain name
153 * @throws ResolutionError
154 * @returns A Promise that resolves in ipfsHash
155 */
156 Resolution.prototype.ipfsHash = function (domain) {
157 return __awaiter(this, void 0, void 0, function () {
158 return __generator(this, function (_a) {
159 switch (_a.label) {
160 case 0: return [4 /*yield*/, this.getNamingMethodOrThrow(domain).record(domain, 'ipfs.html.value')];
161 case 1: return [2 /*return*/, _a.sent()];
162 }
163 });
164 });
165 };
166 /**
167 * Resolves the ipfs redirect url for a supported domain records
168 * @param domain - domain name
169 * @throws ResolutionError
170 * @returns A Promise that resolves in redirect url
171 */
172 Resolution.prototype.ipfsRedirect = function (domain) {
173 return __awaiter(this, void 0, void 0, function () {
174 return __generator(this, function (_a) {
175 switch (_a.label) {
176 case 0: return [4 /*yield*/, this.getNamingMethodOrThrow(domain).record(domain, 'ipfs.redirect_domain.value')];
177 case 1: return [2 /*return*/, _a.sent()];
178 }
179 });
180 });
181 };
182 /**
183 * Resolves the ipfs email field from whois configurations
184 * @param domain - domain name
185 * @throws ResolutionError
186 * @returns A Promise that resolves in an email address configured for this domain whois
187 */
188 Resolution.prototype.email = function (domain) {
189 return __awaiter(this, void 0, void 0, function () {
190 return __generator(this, function (_a) {
191 switch (_a.label) {
192 case 0: return [4 /*yield*/, this.getNamingMethodOrThrow(domain).record(domain, 'whois.email.value')];
193 case 1: return [2 /*return*/, _a.sent()];
194 }
195 });
196 });
197 };
198 /**
199 * Resolves given domain to a specific currency address or throws an error
200 * @param domain - domain name
201 * @param currencyTicker - currency ticker such as
202 * - ZIL
203 * - BTC
204 * - ETH
205 * @throws ResolutionError if address is not found
206 */
207 Resolution.prototype.addressOrThrow = function (domain, currencyTicker) {
208 return __awaiter(this, void 0, void 0, function () {
209 var method;
210 return __generator(this, function (_a) {
211 switch (_a.label) {
212 case 0:
213 method = this.getNamingMethodOrThrow(domain);
214 return [4 /*yield*/, method.address(domain, currencyTicker)];
215 case 1: return [2 /*return*/, _a.sent()];
216 }
217 });
218 });
219 };
220 /**
221 * Owner of the domain
222 * @param domain - domain name
223 * @returns An owner address of the domain
224 */
225 Resolution.prototype.owner = function (domain) {
226 return __awaiter(this, void 0, void 0, function () {
227 var method;
228 return __generator(this, function (_a) {
229 switch (_a.label) {
230 case 0:
231 method = this.getNamingMethod(domain);
232 return [4 /*yield*/, method.owner(domain)];
233 case 1: return [2 /*return*/, (_a.sent()) || null];
234 }
235 });
236 });
237 };
238 /**
239 * This method is only for ens at the moment. Reverse the ens address to a ens registered domain name
240 * @async
241 * @param address - address you wish to reverse
242 * @param currencyTicker - currency ticker like BTC, ETH, ZIL
243 * @returns Domain name attached to this address
244 */
245 Resolution.prototype.reverse = function (address, currencyTicker) {
246 return __awaiter(this, void 0, void 0, function () {
247 return __generator(this, function (_a) {
248 switch (_a.label) {
249 case 0: return [4 /*yield*/, this.ens.reverse(address, currencyTicker)];
250 case 1: return [2 /*return*/, _a.sent()];
251 }
252 });
253 });
254 };
255 /**
256 * Produce a namehash from supported naming service
257 * @param domain - domain name to be hashed
258 * @returns Namehash either for ENS or ZNS
259 */
260 Resolution.prototype.namehash = function (domain) {
261 var method = this.getNamingMethod(domain);
262 if (!method)
263 throw new resolutionError_1.default(resolutionError_1.ResolutionErrorCode.UnsupportedDomain, {
264 domain: domain,
265 });
266 return method.namehash(domain);
267 };
268 /**
269 * Checks if the domain is in valid format
270 * @param domain - domain name to be checked
271 */
272 Resolution.prototype.isSupportedDomain = function (domain) {
273 return !!this.getNamingMethod(domain);
274 };
275 /**
276 * Checks if the domain is supported by the specified network as well as if it is in valid format
277 * @param domain - domain name to be checked
278 */
279 Resolution.prototype.isSupportedDomainInNetwork = function (domain) {
280 var method = this.getNamingMethod(domain);
281 return method && method.isSupportedNetwork();
282 };
283 /**
284 * Used internally to get the right method (ens or zns)
285 * @param domain - domain name
286 */
287 Resolution.prototype.getNamingMethod = function (domain) {
288 var methods = this.blockchain
289 ? [this.ens, this.zns, this.cns]
290 : [this.api];
291 var method = methods.find(function (method) { return method && method.isSupportedDomain(domain); });
292 return method;
293 };
294 Resolution.prototype.getNamingMethodOrThrow = function (domain) {
295 var method = this.getNamingMethod(domain);
296 if (!method)
297 throw new resolutionError_1.default(resolutionError_1.ResolutionErrorCode.UnsupportedDomain, {
298 domain: domain,
299 });
300 return method;
301 };
302 return Resolution;
303}());
304exports.Resolution = Resolution;
305exports.default = Resolution;