anon-identity
Version:
Decentralized identity framework with DIDs, Verifiable Credentials, and privacy-preserving selective disclosure
108 lines • 3.8 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.migrateCredentialToV2 = migrateCredentialToV2;
exports.migratePresentationToV2 = migratePresentationToV2;
exports.createV2Context = createV2Context;
const vc2_1 = require("../types/vc2");
/**
* Migrates a W3C VC 1.1 credential to VC 2.0 format
* @param vc11 The VC 1.1 credential
* @returns The migrated VC 2.0 credential
*/
function migrateCredentialToV2(vc11) {
// If already V2, return as-is
if ((0, vc2_1.isVerifiableCredentialV2)(vc11)) {
return vc11;
}
const credential = vc11;
// Create a new context array with V2 context
const contexts = Array.isArray(credential["@context"])
? [...credential["@context"]]
: [credential["@context"]];
// Replace V1 context with V2
const v2Contexts = contexts.map(ctx => ctx === vc2_1.VC_V2_CONTEXTS.CREDENTIALS_V1
? vc2_1.VC_V2_CONTEXTS.CREDENTIALS_V2
: ctx);
// If no V2 context found, add it
if (!v2Contexts.includes(vc2_1.VC_V2_CONTEXTS.CREDENTIALS_V2)) {
v2Contexts.unshift(vc2_1.VC_V2_CONTEXTS.CREDENTIALS_V2);
}
const vc2 = {
"@context": v2Contexts,
type: credential.type,
issuer: credential.issuer,
credentialSubject: credential.credentialSubject,
// Map issuanceDate to validFrom
validFrom: credential.issuanceDate,
// Keep issuanceDate for backward compatibility
issuanceDate: credential.issuanceDate,
// Copy optional fields
...(credential.id && { id: credential.id }),
...(credential.proof && { proof: credential.proof })
};
return vc2;
}
/**
* Migrates a W3C VP 1.1 to VP 2.0 format
* @param vp11 The VP 1.1 presentation
* @returns The migrated VP 2.0 presentation
*/
function migratePresentationToV2(vp11) {
// Create a new context array with V2 context
const contexts = Array.isArray(vp11["@context"])
? [...vp11["@context"]]
: [vp11["@context"]];
// Replace V1 context with V2
const v2Contexts = contexts.map(ctx => ctx === vc2_1.VC_V2_CONTEXTS.CREDENTIALS_V1
? vc2_1.VC_V2_CONTEXTS.CREDENTIALS_V2
: ctx);
// If no V2 context found, add it
if (!v2Contexts.includes(vc2_1.VC_V2_CONTEXTS.CREDENTIALS_V2)) {
v2Contexts.unshift(vc2_1.VC_V2_CONTEXTS.CREDENTIALS_V2);
}
const vp2 = {
"@context": v2Contexts,
type: vp11.type,
verifiableCredential: vp11.verifiableCredential?.map((vc) => {
if (typeof vc === 'string')
return vc;
// Check if it's a SelectivelyDisclosedCredential
if ('disclosureProof' in vc) {
// For now, keep as-is until we implement BBS+
return vc;
}
return migrateCredentialToV2(vc);
})
};
// Copy optional fields
if (vp11.proof)
vp2.proof = vp11.proof;
return vp2;
}
/**
* Creates a VC 2.0 context based on the features used
* @param options Configuration for which contexts to include
* @returns An array of context URLs
*/
function createV2Context(options = {}) {
const contexts = [];
// Always include base V2 context
contexts.push(vc2_1.VC_V2_CONTEXTS.CREDENTIALS_V2);
if (options.ed25519) {
contexts.push(vc2_1.VC_V2_CONTEXTS.ED25519_2020);
}
if (options.bbs) {
contexts.push(vc2_1.VC_V2_CONTEXTS.BBS_2023);
}
if (options.statusList) {
contexts.push(vc2_1.VC_V2_CONTEXTS.STATUS_LIST_2021);
}
if (options.termsOfUse) {
contexts.push(vc2_1.VC_V2_CONTEXTS.TERMS_OF_USE);
}
if (options.customContexts) {
contexts.push(...options.customContexts);
}
return contexts;
}
//# sourceMappingURL=vc-migration.js.map