UNPKG

ethr-status-registry

Version:

Verifiable Credential status resolver using an ethereum contract as registry

156 lines 9.17 kB
"use strict"; 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 __spreadArrays = (this && this.__spreadArrays) || function () { for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; for (var r = Array(s), k = 0, i = 0; i < il; i++) for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) r[k] = a[j]; return r; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.EthrStatusRegistry = exports.methodName = void 0; var did_jwt_1 = require("did-jwt"); var revocation_registry_1 = require("revocation-registry"); var configuration_1 = require("./configuration"); var contracts_1 = require("@ethersproject/contracts"); var strings_1 = require("@ethersproject/strings"); var keccak256_1 = require("@ethersproject/keccak256"); exports.methodName = 'EthrStatusRegistry2019'; var EthrStatusRegistry = /** @class */ (function () { function EthrStatusRegistry(conf) { var _this = this; this.asStatusMethod = {}; this.networks = {}; this.asStatusMethod[exports.methodName] = function (cred, doc) { return _this.checkStatus(cred, doc); }; this.networks = configuration_1.configureResolverWithNetworks(conf); } // look for ethereumAddress entries in didDoc EthrStatusRegistry.filterDocForAddresses = function (didDoc) { var keyEntries = didDoc.publicKey .filter(function (entry) { return (entry === null || entry === void 0 ? void 0 : entry.type) === 'Secp256k1VerificationKey2018' && typeof (entry === null || entry === void 0 ? void 0 : entry.ethereumAddress) !== 'undefined'; }) .map(function (entry) { return (entry === null || entry === void 0 ? void 0 : entry.ethereumAddress) || ''; }) .filter(function (address) { return address !== ''; }); return keyEntries; }; EthrStatusRegistry.prototype.checkStatus = function (credential, didDoc) { return __awaiter(this, void 0, void 0, function () { var decodedJWT, statusEntry, _a, registryAddress, networkId, statusReg_1, revokers, asyncChecks, partials, gatherResultsLambda, result; var _this = this; return __generator(this, function (_b) { switch (_b.label) { case 0: decodedJWT = did_jwt_1.decodeJWT(credential).payload; statusEntry = decodedJWT.credentialStatus; if (!statusEntry) { return [2 /*return*/, Promise.resolve({ status: 'NonRevocable' })]; } if (!(statusEntry.type === exports.methodName)) return [3 /*break*/, 2]; _a = this.parseRegistryId(statusEntry.id), registryAddress = _a[0], networkId = _a[1]; if (!this.networks[networkId]) { return [2 /*return*/, Promise.reject("networkId (" + networkId + ") for status check not configured")]; } statusReg_1 = new contracts_1.Contract(registryAddress, revocation_registry_1.abi, this.networks[networkId]); revokers = this.parseRevokers(credential, didDoc, decodedJWT.iss); asyncChecks = revokers.map(function (revoker) { return _this.runCredentialCheck(credential, revoker, statusReg_1); }); return [4 /*yield*/, Promise.all(asyncChecks)]; case 1: partials = _b.sent(); gatherResultsLambda = function (verdict, partial) { verdict.revoked = (verdict === null || verdict === void 0 ? void 0 : verdict.revoked) || (partial === null || partial === void 0 ? void 0 : partial.revoked); return verdict; }; result = partials .filter(function (res) { return res != null && typeof res.revoked !== 'undefined'; }) .reduce(gatherResultsLambda, { revoked: false }); return [2 /*return*/, Promise.resolve(result)]; case 2: return [2 /*return*/, Promise.reject("unsupported credential status method")]; } }); }); }; EthrStatusRegistry.prototype.parseRegistryId = function (id) { var parsedId = id.match(/^((.*):)?(0x[0-9a-fA-F]{40})$/); if (!parsedId) throw new Error("Not a valid status registry ID: " + id); var registryAddress = parsedId[3]; var networkId = !parsedId[1] ? 'mainnet' : parsedId[2]; return [registryAddress, networkId]; }; EthrStatusRegistry.prototype.runCredentialCheck = function (credential, issuerAddress, statusReg // the contract instance as returned by ethjs-contract ) { return __awaiter(this, void 0, void 0, function () { var tokenBytes, credentialHash, revocationBlock, e_1; return __generator(this, function (_a) { switch (_a.label) { case 0: tokenBytes = strings_1.toUtf8Bytes(credential); credentialHash = keccak256_1.keccak256(tokenBytes); _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); return [4 /*yield*/, statusReg.revoked(issuerAddress, credentialHash)]; case 2: revocationBlock = _a.sent(); return [2 /*return*/, { revoked: !revocationBlock.isZero() }]; case 3: e_1 = _a.sent(); if (typeof e_1.statusCode !== 'undefined' || e_1.code === 'NETWORK_ERROR') { return [2 /*return*/, Promise.reject(new Error('CONNECTION ERROR'))]; } return [2 /*return*/, Promise.reject(e_1)]; case 4: return [2 /*return*/]; } }); }); }; EthrStatusRegistry.prototype.parseRevokers = function (credential, didDoc, issuer) { var ethereumAddresses = EthrStatusRegistry.filterDocForAddresses(didDoc); // const derivedAddresses = this.filterDocForSecpKeys(didDoc) var revokers = Array.from(new Set(__spreadArrays(ethereumAddresses // ...derivedAddresses ))); return revokers; }; return EthrStatusRegistry; }()); exports.EthrStatusRegistry = EthrStatusRegistry; //# sourceMappingURL=EthrStatusRegistry.js.map