@klayr-did/klayr-verifiable-credentials
Version:
A library for working with W3C verifiable credentials (VC) and verifiable presentations (VP) using Klayr DID
65 lines • 3.25 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.verifyPresentation = exports.issuePresentation = void 0;
const vc = require("@digitalcredentials/vc");
const documentLoader_1 = require("../documentLoader");
const util_1 = require("../util");
const klayr_decentralized_identifier_1 = require("@klayr-did/klayr-decentralized-identifier");
async function issuePresentation(verifiableCredentialsList, holderDid, privateKey, challenge, options) {
if (verifiableCredentialsList.map(t => t.proof !== undefined).includes(false)) {
throw new Error('all verifiable credentials needs to have a proof');
}
const documentLoader = (0, documentLoader_1.buildOffChainDocumentLoader)(options);
const didDocument = await (0, klayr_decentralized_identifier_1.getDIDDocument)(holderDid, options);
if (didDocument == null) {
throw new Error('holder DID not registered yet on the chain');
}
const privateKeyMultibase = await klayr_decentralized_identifier_1.cryptography.codec.encodePrivateKey(privateKey);
const matchedKey = await klayr_decentralized_identifier_1.cryptography.method.getVerificationMethod(didDocument, {
relationship: ['authentication'],
privateKey,
});
if (matchedKey.length === 0) {
throw new Error("specified private key doesn't have neccessary permission to authenticate a presentation");
}
const key = await klayr_decentralized_identifier_1.cryptography.key.createEd25519KeyPair({ ...matchedKey[0], privateKeyMultibase });
const suite = await klayr_decentralized_identifier_1.cryptography.key.getEd25519SignatureSuite(key);
const presentation = vc.createPresentation({
verifiableCredential: verifiableCredentialsList,
holder: holderDid,
});
await (0, util_1.preprocessPresentation)(presentation);
const signedVP = await vc.signPresentation({
presentation,
suite,
challenge,
documentLoader,
});
return signedVP;
}
exports.issuePresentation = issuePresentation;
async function verifyPresentation(presentation, publicKey, options) {
if (presentation.proof === undefined) {
throw new Error('presentation.proof must be defined');
}
if (presentation.holder === undefined) {
throw new Error('presentation.holder must be defined');
}
const documentLoader = (0, documentLoader_1.buildOffChainDocumentLoader)(options);
const key = await klayr_decentralized_identifier_1.cryptography.key.createEd25519KeyPair({
id: presentation.proof.verificationMethod,
type: klayr_decentralized_identifier_1.utils.constant.ED25519_VERIFICATION_KEY_2020_TYPE,
controller: presentation.holder,
publicKeyMultibase: await klayr_decentralized_identifier_1.cryptography.codec.encodePublicKey(publicKey),
});
const suite = await klayr_decentralized_identifier_1.cryptography.key.getEd25519SignatureSuite(key);
const verificationResult = await vc.verify({
presentation,
challenge: presentation.proof.challenge,
suite,
documentLoader,
});
return verificationResult;
}
exports.verifyPresentation = verifyPresentation;
//# sourceMappingURL=presentation.js.map