@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
80 lines (79 loc) • 4.49 kB
JavaScript
;
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.DID = void 0;
const ssi_sdk_did_utils_1 = require("@sphereon/ssi-sdk-did-utils");
const utils_1 = require("../utils");
const fs_1 = __importDefault(require("fs"));
class DID {
static createDIDFromX509({ domain, privateKeyPEM, certificatePEM, certificateChainPEM, certificateChainURL, kms, kid }, context) {
return __awaiter(this, void 0, void 0, function* () {
const x509 = {
cn: domain,
certificatePEM,
certificateChainPEM,
privateKeyPEM,
certificateChainURL,
};
const privateKeyHex = (0, ssi_sdk_did_utils_1.privateKeyHexFromPEM)(privateKeyPEM);
const meta = { x509 };
const kidResult = kid ? kid : (0, ssi_sdk_did_utils_1.publicKeyHexFromPEM)(privateKeyPEM);
const controllerKeyId = kidResult; //kid ? (kidResult.includes(domain) ? kidResult : `${domain}#${kid}`) : `${domain}#JWK2020-RSA`
return yield context.agent.didManagerImport({
did: `did:web:${domain}`,
provider: 'did:web',
alias: domain,
keys: [{ kid: kid ? kid : kidResult, privateKeyHex, type: 'RSA', meta, kms: kms ? kms : 'local' }],
controllerKeyId,
});
});
}
static exportDocument({ domain, services }, context) {
return __awaiter(this, void 0, void 0, function* () {
const id = yield context.agent.didManagerGet({ did: `did:web:${domain}` });
return yield (0, utils_1.exportToDIDDocument)(id, { services });
});
}
static exportToPath({ domain, services, path }, context) {
return __awaiter(this, void 0, void 0, function* () {
const id = yield context.agent.didManagerGet({ did: `did:web:${domain}` });
// we go through the agent from the context
const doc = yield context.agent.exportDIDDocument({ domain, services });
const basePath = path ? `./${path.replace('.well-known', '')}/${domain}` : `./exported/${domain}`;
const exports = [];
const didPath = `${basePath}/.well-known/did.json`;
exports.push({ file: 'did.json', path: didPath });
fs_1.default.mkdirSync(`${basePath}/.well-known`, { recursive: true });
fs_1.default.writeFileSync(didPath, JSON.stringify(doc, null, 2));
id.keys.forEach((key) => {
var _a, _b, _c, _d;
if (key.type !== 'RSA') {
return;
}
if (((_b = (_a = key.meta) === null || _a === void 0 ? void 0 : _a.x509) === null || _b === void 0 ? void 0 : _b.x5u) && ((_d = (_c = key.meta) === null || _c === void 0 ? void 0 : _c.x509) === null || _d === void 0 ? void 0 : _d.x5c)) {
const x5cPath = `${basePath}/${key.meta.x509.x5u.replace(/https?:\/\/[^/]+\//, '')}`;
const file = x5cPath.split('\\').pop().split('/').pop();
fs_1.default.mkdirSync(x5cPath.replace(file, ''), { recursive: true });
if (!file) {
throw Error(`Could not deduce path for x5 ca chain from x5u URL ${key.meta.x509.x5u}`);
}
fs_1.default.writeFileSync(x5cPath, key.meta.certChainPEM ? key.meta.certChainPEM : (0, ssi_sdk_did_utils_1.x5cToPemCertChain)(key.meta.x509.x5c));
exports.push({ file, path: x5cPath });
}
});
return exports;
});
}
}
exports.DID = DID;