pontus-x_cli
Version:
Command Line Interface for the Pontus-X Data Space Ecosystem.
161 lines • 8.15 kB
JavaScript
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateParticipantCredentials = generateParticipantCredentials;
exports.checkCompliance = checkCompliance;
const path_1 = require("path");
const fs_1 = require("fs");
const Handlebars = __importStar(require("handlebars"));
const crypto_1 = require("crypto");
const json_web_signature_2020_1 = require("@gaia-x/json-web-signature-2020");
const axios_1 = __importDefault(require("axios"));
function generateSignedDocument(data, templateFile, signer) {
const fileContent = (0, fs_1.readFileSync)((0, path_1.resolve)('src/gaia-x_compliance/', templateFile), 'utf-8');
const template = Handlebars.compile(fileContent, { noEscape: true });
const document = JSON.parse(template(data));
return signer.sign(document);
}
async function generateParticipantVC(data, signer) {
console.log(__dirname);
const templateFile = require.resolve('./templates/participant.hbs');
const participantVCSigned = await generateSignedDocument(data, templateFile, signer);
console.log(`Verifiable Credential for participant ${data.participant_legal_name} ` +
`successfully generated`);
return participantVCSigned;
}
async function generateParticipantTandCVC(data, signer) {
const templateFile = require.resolve('./templates/tandc.hbs');
const tandcVCSigned = await generateSignedDocument(data, templateFile, signer);
console.log(`Signed Terms and Conditions for ${data.participant_legal_name} ` +
`successfully generated`);
return tandcVCSigned;
}
async function generateParticipantLNRVC(data) {
var _a;
const templateFile = require.resolve('./templates/lrn-request.hbs');
const notaryService = "https://registrationnumber.notary.lab.gaia-x.eu/v1-staging/registrationNumberVC";
const fileContent = (0, fs_1.readFileSync)((0, path_1.resolve)(templateFile), 'utf-8');
const lnrRequestTemplate = Handlebars.compile(fileContent);
const lnrRequest = lnrRequestTemplate(data);
try {
const response = await axios_1.default.post(`${notaryService}?vcid=https://${data.issuer_domain}/.well-known/${data.participant_name}.vp.json%23lrn`, JSON.parse(lnrRequest));
if (response.status === 200) {
console.log(`Verifiable Credential for ${data.participant_legal_name} Legal Registration Number` +
` successfully generated`);
return response.data;
}
else {
console.error(`Error generating Legal Registration Number for ${data.participant_legal_name}`);
console.error(response === null || response === void 0 ? void 0 : response.data);
}
}
catch (e) {
console.error(`Error generating Legal Registration Number for ${data.participant_legal_name}`);
console.error((_a = e.response) === null || _a === void 0 ? void 0 : _a.data);
}
return null;
}
function generateParticipantVP(participantVC, tandcVC, lrnVC, folder, data) {
const vp = {
"@context": "https://www.w3.org/2018/credentials/v1",
"type": "VerifiablePresentation",
"verifiableCredential": [
participantVC,
lrnVC,
tandcVC
]
};
(0, fs_1.writeFileSync)((0, path_1.resolve)(folder, data.participant_name + '.vp.json'), JSON.stringify(vp, null, 2), 'utf-8');
console.log(`Verifiable Presentation of the credentials for ${data.participant_legal_name} ` +
`generated and stored in ${data.participant_name}.vp.json`);
}
async function generateParticipantCredentials(participantDataFile, didFile, certificateKeyFile) {
const folder = (0, path_1.dirname)(participantDataFile);
const participantData = JSON.parse((0, fs_1.readFileSync)(participantDataFile, 'utf-8'));
participantData.issuance_date = new Date().toISOString();
const didJson = JSON.parse((0, fs_1.readFileSync)(didFile, 'utf-8'));
participantData.issuer_domain = didJson.id.substring(didJson.id.lastIndexOf(':') + 1);
console.log(`Generating Gaia-X credentials for participant ${participantData.participant_legal_name} ` +
`with issuer ${participantData.issuer_domain}`);
const pkcs1 = (0, fs_1.readFileSync)(certificateKeyFile, 'utf-8');
const key = (0, crypto_1.createPrivateKey)(pkcs1);
const signer = new json_web_signature_2020_1.GaiaXSignatureSigner({
privateKey: key,
privateKeyAlg: didJson.verificationMethod[0].publicKeyJwk.alg,
verificationMethod: didJson.verificationMethod[0].id
});
await Promise.all([
generateParticipantVC(participantData, signer),
generateParticipantTandCVC(participantData, signer),
generateParticipantLNRVC(participantData)
])
.then(([participantVC, tandcVC, lrnVC]) => {
if (participantVC && tandcVC && lrnVC) {
generateParticipantVP(participantVC, tandcVC, lrnVC, folder, participantData);
}
else {
console.error('Error with one of the credentials, therefore the Verifiable Presentation was not generated');
}
});
}
async function checkCompliance(participantDataFile, vpFile) {
const complianceService = "https://compliance.lab.gaia-x.eu/v1-staging/api/credential-offers";
const folder = (0, path_1.dirname)(participantDataFile);
const participantData = JSON.parse((0, fs_1.readFileSync)(participantDataFile, 'utf-8'));
const vp = JSON.parse((0, fs_1.readFileSync)(vpFile, 'utf-8'));
axios_1.default.post(`${complianceService}?` +
`vcid=https://${participantData.issuer_domain}/.well-known/${participantData.participant_name}.compliance.json`, vp)
.then(response => {
if (response.status === 201) {
const compliance = response.data;
(0, fs_1.writeFileSync)((0, path_1.resolve)(folder, participantData.participant_name + '.compliance.json'), JSON.stringify(compliance, null, 2), 'utf-8');
console.log(`Gaia-X Compliance verified for ${participantData.participant_legal_name} credentials ` +
`and stored in ${participantData.participant_name}.compliance.json`);
}
else {
console.error(`Error generating Compliance for ${participantData.participant_legal_name}`);
console.error(response === null || response === void 0 ? void 0 : response.data);
}
})
.catch(reason => {
var _a;
console.error(`Error generating Compliance for ${participantData.participant_legal_name}`);
console.error((_a = reason.response) === null || _a === void 0 ? void 0 : _a.data);
});
}
//# sourceMappingURL=index.js.map
;