@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
83 lines (82 loc) • 4.75 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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.asDID = exports.exportToDIDDocument = exports.extractSignInfo = exports.convertDidWebToHost = void 0;
const ssi_sdk_did_utils_1 = require("@sphereon/ssi-sdk-did-utils");
function convertDidWebToHost(did) {
did = did.substring(8);
did = did.replace(/:/g, '/');
did = did.replace(/%/g, ':');
return did;
}
exports.convertDidWebToHost = convertDidWebToHost;
function extractSignInfo({ did, section, keyRef }, context) {
return __awaiter(this, void 0, void 0, function* () {
const verificationRelationship = section ? section : 'verificationMethod';
const id = yield context.agent.didManagerGet({ did });
const didDoc = yield exportToDIDDocument(id);
const keys = yield (0, ssi_sdk_did_utils_1.mapIdentifierKeysToDocWithJwkSupport)(id, verificationRelationship, context, didDoc);
if (!keys || keys.length === 0) {
throw Error(`No suitable keys found to issue a verifiable credential for ${did} and ${verificationRelationship} Verification Relationship`);
}
const key = keyRef ? keys.find((key) => key.kid === keyRef) : keys[0];
if (!key) {
// Can only be keyref, as we already checked for an empty array above
throw Error(`Could not find key with keyref ${keyRef}`);
}
return {
keyRef: keyRef ? keyRef : key.kid,
keys,
key,
participantDID: did,
participantDomain: convertDidWebToHost(did),
verificationRelationship,
};
});
}
exports.extractSignInfo = extractSignInfo;
function exportToDIDDocument(identifier, opts) {
return __awaiter(this, void 0, void 0, function* () {
const keyMapping = {
Secp256k1: 'EcdsaSecp256k1VerificationKey2019',
Secp256r1: 'EcdsaSecp256r1VerificationKey2019',
Ed25519: 'Ed25519VerificationKey2018',
X25519: 'X25519KeyAgreementKey2019',
Bls12381G1: 'Bls12381G1Key2020',
Bls12381G2: 'Bls12381G2Key2020',
RSA: 'JsonWebKey2020',
};
const allKeys = identifier.keys.map((key) => {
var _a, _b, _c;
return (Object.assign(Object.assign({ id: identifier.did + '#' + key.kid, type: keyMapping[key.type], controller: identifier.did }, (((_a = key === null || key === void 0 ? void 0 : key.meta) === null || _a === void 0 ? void 0 : _a.publicKeyJwk) ? { publicKeyJwk: (_b = key === null || key === void 0 ? void 0 : key.meta) === null || _b === void 0 ? void 0 : _b.publicKeyJwk } : {})), (!((_c = key === null || key === void 0 ? void 0 : key.meta) === null || _c === void 0 ? void 0 : _c.publicKeyJwk) ? { publicKeyHex: key.publicKeyHex } : {})));
});
// ed25519 keys can also be converted to x25519 for key agreement
const keyAgreementKeyIds = allKeys
.filter((key) => ['Ed25519VerificationKey2018', 'X25519KeyAgreementKey2019'].includes(key.type))
.map((key) => key.id);
const signingKeyIds = allKeys.filter((key) => key.type !== 'X25519KeyAgreementKey2019').map((key) => key.id);
const didDoc = Object.assign(Object.assign(Object.assign({ '@context': 'https://w3id.org/did/v1', id: identifier.did, verificationMethod: allKeys }, (signingKeyIds.length > 0
? {
authentication: signingKeyIds,
assertionMethod: signingKeyIds,
}
: {})), (keyAgreementKeyIds.length > 0 ? { keyAgreement: keyAgreementKeyIds } : {})), { service: [...((opts === null || opts === void 0 ? void 0 : opts.services) || []), ...((identifier === null || identifier === void 0 ? void 0 : identifier.services) || [])] });
return didDoc;
});
}
exports.exportToDIDDocument = exportToDIDDocument;
function asDID(input) {
if (input.startsWith(`did:web:`)) {
return input;
}
return `did:web:${input.replace(/https?:\/\/([^/?#]+).*/i, '$1').toLowerCase()}`;
}
exports.asDID = asDID;