UNPKG

@sphereon/gx-compliance-client

Version:

<!--suppress HtmlDeprecatedAttribute --> <h1 align="center"> <br> <a href="https://www.sphereon.com"><img src="https://sphereon.com/content/themes/sphereon/assets/img/logo.svg" alt="Sphereon" width="400"></a> <br>Gaia-X Compliance client (Typescript

106 lines (105 loc) 5.23 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()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.CredentialHandler = void 0; const GXComplianceClient_1 = require("./GXComplianceClient"); const types_1 = require("../types"); const uuid_1 = require("uuid"); const utils_1 = require("../utils"); class CredentialHandler { constructor(client) { this._client = client; } client() { return this._client; } config() { return this.client().config(); } issueVerifiableCredential(args, context) { var _a; return __awaiter(this, void 0, void 0, function* () { const credential = args.credential; if (!(credential === null || credential === void 0 ? void 0 : credential.credentialSubject)) { throw Error('Credential needs a subject'); } if (!((_a = credential.credentialSubject) === null || _a === void 0 ? void 0 : _a.id)) { if (!args.domain) { throw Error('Either a credentialSubject.id value needs to be set, or a domain value needs to be supplied'); } credential.credentialSubject.id = (0, utils_1.asDID)(args.domain); } const did = (0, utils_1.extractSubjectDIDFromVCs)(credential); const signInfo = yield (0, utils_1.extractSignInfo)({ did, section: 'assertionMethod', keyRef: args.keyRef }, context); const verifiableCredential = yield context.agent.createVerifiableCredentialLDLocal({ credential: credential, keyRef: signInfo.keyRef, // todo: Purpose from signInfo }); let hash = ''; //todo: determine id, without saving if (args.persist) { hash = yield context.agent.dataStoreSaveVerifiableCredential({ verifiableCredential }); } return { verifiableCredential, hash }; }); } checkVerifiableCredential(args, context) { return __awaiter(this, void 0, void 0, function* () { const result = yield context.agent.verifyCredentialLDLocal({ credential: args.verifiableCredential, fetchRemoteContexts: true, }); return result; }); } /** {@inheritDoc IGXComplianceClient.issueVerifiablePresentation} */ issueVerifiablePresentation(args, context) { return __awaiter(this, void 0, void 0, function* () { const did = (0, utils_1.asDID)(args.domain); const signInfo = yield (0, utils_1.extractSignInfo)({ did, section: 'authentication', keyRef: args.keyRef }, context); const verifiablePresentation = yield context.agent.createVerifiablePresentationLDLocal({ presentation: { id: (0, uuid_1.v4)(), issuanceDate: new Date(), type: ['VerifiablePresentation'], '@context': ['https://www.w3.org/2018/credentials/v1'], verifiableCredential: args.verifiableCredentials, holder: did, }, // purpose: args.purpose, // todo: Make dynamic basied on signInfo and arg keyRef: signInfo.keyRef, challenge: args.challenge ? args.challenge : GXComplianceClient_1.GXComplianceClient.getDateChallenge(), domain: this.config().complianceServiceUrl, }); let hash = ''; //todo: determine id, without saving if (args.persist) { hash = yield context.agent.dataStoreSaveVerifiablePresentation({ verifiablePresentation }); } return { verifiablePresentation, hash }; }); } checkVerifiablePresentation(args, context) { return __awaiter(this, void 0, void 0, function* () { const domain = this.config().complianceServiceUrl; const challenge = args.challenge ? args.challenge : GXComplianceClient_1.GXComplianceClient.getDateChallenge(); const result = yield context.agent.verifyPresentationLDLocal({ presentation: args.verifiablePresentation, challenge, domain, fetchRemoteContexts: true, presentationPurpose: new types_1.AuthenticationProofPurpose({ domain: args.verifiablePresentation.holder, challenge }), }); console.log(result); return result; }); } } exports.CredentialHandler = CredentialHandler;