@authereum/resolution
Version:
Domain Resolution for blockchain domains
294 lines (293 loc) • 14.7 kB
JavaScript
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
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;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
var EthereumNamingService_1 = require("./EthereumNamingService");
var types_1 = require("./types");
var proxyReader_1 = __importDefault(require("./cns/contract/proxyReader"));
var resolver_1 = __importDefault(require("./cns/contract/resolver"));
var resolutionError_1 = __importStar(require("./errors/resolutionError"));
var standardKeys_1 = __importDefault(require("./utils/standardKeys"));
var utils_1 = require("./utils");
var publicTypes_1 = require("./publicTypes");
var TwitterSignatureValidator_1 = require("./utils/TwitterSignatureValidator");
var network_config_json_1 = __importDefault(require("./config/network-config.json"));
var Cns = /** @class */ (function (_super) {
__extends(Cns, _super);
function Cns(source) {
if (source === void 0) { source = {}; }
return _super.call(this, source, publicTypes_1.NamingServiceName.CNS) || this;
}
Cns.prototype.readerAbi = function () {
return proxyReader_1.default;
};
Cns.prototype.defaultRegistry = function (network) {
return Cns.ProxyReaderMap[network];
};
Cns.prototype.isSupportedDomain = function (domain) {
return (domain === 'crypto' ||
(domain.indexOf('.') > 0 &&
/^.{1,}\.(crypto)$/.test(domain) &&
domain.split('.').every(function (v) { return !!v.length; })));
};
Cns.prototype.resolver = function (domain) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getVerifiedData(domain)];
case 1: return [2 /*return*/, (_a.sent()).resolver];
}
});
});
};
Cns.prototype.owner = function (domain) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getVerifiedData(domain)];
case 1: return [2 /*return*/, (_a.sent()).owner];
}
});
});
};
Cns.prototype.allRecords = function (domain) {
return __awaiter(this, void 0, void 0, function () {
var tokenId, resolver, resolverContract;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
tokenId = this.namehash(domain);
return [4 /*yield*/, this.resolver(domain)];
case 1:
resolver = _a.sent();
resolverContract = this.buildContract(resolver_1.default, resolver);
if (!utils_1.isLegacyResolver(resolver)) return [3 /*break*/, 3];
return [4 /*yield*/, this.getStandardRecords(tokenId)];
case 2: return [2 /*return*/, _a.sent()];
case 3: return [4 /*yield*/, this.getAllRecords(resolverContract, tokenId)];
case 4: return [2 /*return*/, _a.sent()];
}
});
});
};
Cns.prototype.twitter = function (domain) {
return __awaiter(this, void 0, void 0, function () {
var tokenId, keys, data, records, validationSignature, twitterHandle, owner;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
tokenId = this.namehash(domain);
keys = [
standardKeys_1.default.validation_twitter_username,
standardKeys_1.default.twitter_username,
];
return [4 /*yield*/, this.getVerifiedData(domain, keys)];
case 1:
data = _a.sent();
records = data.records;
validationSignature = records[standardKeys_1.default.validation_twitter_username];
twitterHandle = records[standardKeys_1.default.twitter_username];
if (types_1.isNullAddress(validationSignature)) {
throw new resolutionError_1.default(resolutionError_1.ResolutionErrorCode.RecordNotFound, { domain: domain, recordName: standardKeys_1.default.validation_twitter_username });
}
if (!twitterHandle) {
throw new resolutionError_1.default(resolutionError_1.ResolutionErrorCode.RecordNotFound, { domain: domain, recordName: standardKeys_1.default.twitter_username });
}
owner = data.owner;
if (!TwitterSignatureValidator_1.isValidTwitterSignature({
tokenId: tokenId,
owner: owner,
twitterHandle: twitterHandle,
validationSignature: validationSignature,
})) {
throw new resolutionError_1.default(resolutionError_1.ResolutionErrorCode.InvalidTwitterVerification, {
domain: domain,
});
}
return [2 /*return*/, twitterHandle];
}
});
});
};
Cns.prototype.records = function (domain, keys) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getVerifiedData(domain, keys)];
case 1: return [2 /*return*/, (_a.sent()).records];
}
});
});
};
Cns.prototype.resolve = function (_) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
throw new Error('This method is unsupported for CNS');
});
});
};
Cns.prototype.getVerifiedData = function (domain, keys) {
return __awaiter(this, void 0, void 0, function () {
var tokenId, data;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
tokenId = this.namehash(domain);
return [4 /*yield*/, this.get(tokenId, keys)];
case 1:
data = _a.sent();
if (types_1.isNullAddress(data.resolver)) {
if (types_1.isNullAddress(data.owner)) {
throw new resolutionError_1.default(resolutionError_1.ResolutionErrorCode.UnregisteredDomain, { domain: domain });
}
throw new resolutionError_1.default(resolutionError_1.ResolutionErrorCode.UnspecifiedResolver, { domain: domain });
}
return [2 /*return*/, data];
}
});
});
};
Cns.prototype.getStandardRecords = function (tokenId) {
return __awaiter(this, void 0, void 0, function () {
var keys;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
keys = Object.values(standardKeys_1.default);
return [4 /*yield*/, this.getMany(tokenId, keys)];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
};
Cns.prototype.getAllRecords = function (resolverContract, tokenId) {
return __awaiter(this, void 0, void 0, function () {
var startingBlock, logs, keyTopics;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, utils_1.getStartingBlock(resolverContract, tokenId)];
case 1:
startingBlock = _a.sent();
return [4 /*yield*/, resolverContract.fetchLogs('NewKey', tokenId, startingBlock)];
case 2:
logs = _a.sent();
keyTopics = logs.map(function (event) { return event.topics[2]; });
if (!(keyTopics.length === 0)) return [3 /*break*/, 4];
return [4 /*yield*/, this.getStandardRecords(tokenId)];
case 3: return [2 /*return*/, _a.sent()];
case 4: return [4 /*yield*/, this.getManyByHash(tokenId, keyTopics)];
case 5: return [2 /*return*/, _a.sent()];
}
});
});
};
Cns.prototype.getMany = function (tokenId, keys) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.get(tokenId, keys)];
case 1: return [2 /*return*/, (_a.sent()).records];
}
});
});
};
Cns.prototype.getManyByHash = function (tokenId, hashes) {
return __awaiter(this, void 0, void 0, function () {
var _a, keys, values;
return __generator(this, function (_b) {
switch (_b.label) {
case 0: return [4 /*yield*/, this.readerContract.call('getManyByHash', [hashes, tokenId])];
case 1:
_a = _b.sent(), keys = _a[0], values = _a[1];
return [2 /*return*/, this.constructRecords(keys, values)];
}
});
});
};
Cns.prototype.get = function (tokenId, keys) {
if (keys === void 0) { keys = []; }
return __awaiter(this, void 0, void 0, function () {
var _a, resolver, owner, values;
return __generator(this, function (_b) {
switch (_b.label) {
case 0: return [4 /*yield*/, this.readerContract.call('getData', [
keys,
tokenId,
])];
case 1:
_a = _b.sent(), resolver = _a[0], owner = _a[1], values = _a[2];
return [2 /*return*/, { owner: owner, resolver: resolver, records: this.constructRecords(keys, values) }];
}
});
});
};
Cns.TwitterVerificationAddress = '0x12cfb13522F13a78b650a8bCbFCf50b7CB899d82';
Cns.ProxyReaderMap = getProxyReaderMap();
return Cns;
}(EthereumNamingService_1.EthereumNamingService));
exports.default = Cns;
function getProxyReaderMap() {
var map = {};
for (var _i = 0, _a = Object.keys(network_config_json_1.default.networks); _i < _a.length; _i++) {
var id = _a[_i];
map[id] = network_config_json_1.default.networks[id].contracts.ProxyReader.address.toLowerCase();
}
return map;
}