UNPKG

@sphereon/ssi-sdk.ebsi-support

Version:

108 lines 5.34 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 __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ebsiListDidDocuments = exports.ebsiWaitTillDocumentAnchored = exports.ebsiGetDidDocument = void 0; const cross_fetch_1 = __importDefault(require("cross-fetch")); const functions_1 = require("../../functions"); const index_1 = require("../../index"); const functions_2 = require("../functions"); /** * Gets the DID document corresponding to the DID. * @param {{ params: GetDidDocumentParams, apiOpts?: ApiOpts }} args * @returns a did document */ const ebsiGetDidDocument = (args) => __awaiter(void 0, void 0, void 0, function* () { const { params, apiOpts } = args; const { did, validAt } = params; if (!did) { throw new Error('did parameter is required'); } const query = validAt ? `?valid_at=${validAt}` : ''; const response = yield (0, cross_fetch_1.default)(`${(0, functions_2.ebsiGetRegistryAPIUrls)(Object.assign({}, apiOpts)).query}/${did}${query}`); const textBody = yield response.text(); const json = textBody.startsWith('{') ? JSON.parse(textBody) : undefined; if (response.status >= 300 || !json) { return Promise.reject(new Error(json !== null && json !== void 0 ? json : textBody)); } return json; }); exports.ebsiGetDidDocument = ebsiGetDidDocument; /** * Wait up to the number of MS for a DID Document or Verification methods and relationships to be registered. This is needed, as the EBSI blockchain does not directly propagate across all nodes, since it needs to mine for consensus first * @param args */ const ebsiWaitTillDocumentAnchored = (args) => __awaiter(void 0, void 0, void 0, function* () { const { did, startIntervalMS = 2000, minIntervalMS = 500, decreaseIntervalMSPerStep = 250, maxWaitTime = 60000, version = 'v5', environment = 'pilot', searchForObject, } = args; if (startIntervalMS < minIntervalMS) { return Promise.reject(Error(`min interval ${minIntervalMS} needs to be smaller or equal to the start interval ${startIntervalMS}`)); } else if (decreaseIntervalMSPerStep < 0) { return Promise.reject(Error(`decrease interval per step ${decreaseIntervalMSPerStep} needs to be bigger than zero`)); } let interval = startIntervalMS; let totalWaitTime = 0; let didDocument; let count = 0; function logCalback() { index_1.logger.debug(`Get DID Document; count ${count}: wait time: ${totalWaitTime}, ${did}`); } while (!didDocument && totalWaitTime <= maxWaitTime) { ++count; try { logCalback(); didDocument = yield (0, exports.ebsiGetDidDocument)({ params: { did }, apiOpts: { environment, version } }); if (searchForObject && didDocument) { const didDocAsStr = JSON.stringify(didDocument); const search = JSON.stringify(searchForObject); const found = didDocAsStr.includes(search.substring(1, search.length - 1)); if (!found) { index_1.logger.debug(`We did not find VM relationship or key ${JSON.stringify(searchForObject)} in DID document ${did}`); didDocument = undefined; } } } catch (e) { } if (!didDocument) { yield (0, functions_1.wait)(interval); totalWaitTime += interval; interval = Math.max(interval - decreaseIntervalMSPerStep, minIntervalMS); } } logCalback(); return { didDocument, totalWaitTime, count }; }); exports.ebsiWaitTillDocumentAnchored = ebsiWaitTillDocumentAnchored; /** * listDidDocuments - Returns a list of identifiers. * @param {{ params: GetDidDocumentsParams; apiOpts?: ApiOpts }} args * @returns a list of identifiers */ const ebsiListDidDocuments = (args) => __awaiter(void 0, void 0, void 0, function* () { const { params, apiOpts } = args; const { offset, size, controller } = params; const queryParams = []; if (offset) { queryParams.push(`page[after]=${offset}`); } if (size) { queryParams.push(`page[size]=${size}`); } if (controller) { queryParams.push(`controller=${controller}`); } const query = `?${queryParams.filter(Boolean).join('&')}`; return yield (yield (0, cross_fetch_1.default)(`${(0, functions_2.ebsiGetRegistryAPIUrls)(Object.assign({}, apiOpts)).query}/${query}`)).json(); }); exports.ebsiListDidDocuments = ebsiListDidDocuments; //# sourceMappingURL=EbsiRestService.js.map