UNPKG

@klayr-did/klayr-verifiable-credentials

Version:

A library for working with W3C verifiable credentials (VC) and verifiable presentations (VP) using Klayr DID

62 lines 3.11 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.verifyCredential = exports.issueCredential = 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 issueCredential(credential, privateKey, options) { if (credential.issuer === undefined) { throw new Error('credential.issuer must be defined'); } const documentLoader = (0, documentLoader_1.buildOffChainDocumentLoader)(options); const issuer = typeof credential.issuer === 'string' ? credential.issuer : credential.issuer.id; const didDocument = await (0, klayr_decentralized_identifier_1.getDIDDocument)(issuer, options); if (didDocument == null) { throw new Error('issuer DID not registered yet on the chain'); } const privateKeyMultibase = klayr_decentralized_identifier_1.cryptography.codec.encodePrivateKey(privateKey); const matchedKey = await klayr_decentralized_identifier_1.cryptography.method.getVerificationMethod(didDocument, { relationship: ['assertionMethod'], privateKey, }); if (matchedKey.length === 0) { throw new Error("specified private key doesn't have neccessary permission to act as issuer"); } 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 issuedCredential = { ...credential }; (0, util_1.preprocessCredentials)(issuedCredential); const signedVC = await vc.issue({ credential: issuedCredential, suite, documentLoader, }); return signedVC; } exports.issueCredential = issueCredential; async function verifyCredential(credential, publicKey, options) { if (credential.proof === undefined) { throw new Error('credential.proof must be defined'); } if (credential.issuer === undefined) { throw new Error('credential.issuer must be defined'); } const documentLoader = (0, documentLoader_1.buildOffChainDocumentLoader)(options); const issuer = typeof credential.issuer === 'string' ? credential.issuer : credential.issuer.id; const key = await klayr_decentralized_identifier_1.cryptography.key.createEd25519KeyPair({ id: credential.proof.verificationMethod, type: klayr_decentralized_identifier_1.utils.constant.ED25519_VERIFICATION_KEY_2020_TYPE, controller: issuer, publicKeyMultibase: klayr_decentralized_identifier_1.cryptography.codec.encodePublicKey(publicKey), }); const suite = await klayr_decentralized_identifier_1.cryptography.key.getEd25519SignatureSuite(key); const result = await vc.verifyCredential({ credential, suite, documentLoader, }); return result; } exports.verifyCredential = verifyCredential; //# sourceMappingURL=credentials.js.map