Skip to content

Commit

Permalink
feat: [LERX-550] Restore vc-js
Browse files Browse the repository at this point in the history
Reapplying changes to an old fork of the digitalbazaar/jc package since 1EdTech decided to delete their fork
  • Loading branch information
ahripak committed Dec 12, 2024
1 parent 929f939 commit af8063b
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 29 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ npm-debug.log
package-lock.json
.nyc_output
dist
.idea
10 changes: 8 additions & 2 deletions lib/contexts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
* Copyright (c) 2019-2022 Digital Bazaar, Inc. All rights reserved.
*/
import {
contexts as credentialContexts
contexts as credential1Contexts
} from 'credentials-context';
import {
contexts as credential2Contexts
} from '@digitalbazaar/credentials-v2-context';
import {
CONTEXT as vcExamplesV1Context,
CONTEXT_URL as vcExamplesV1ContextUrl
Expand All @@ -18,6 +21,9 @@ export const contexts = {};
contexts[vcExamplesV1ContextUrl] = vcExamplesV1Context;
contexts[odrlContextUrl] = odrlContext;

for(const [url, context] of credentialContexts.entries()) {
for(const [url, context] of credential1Contexts.entries()) {
contexts[url] = context;
}
for(const [url, context] of credential2Contexts.entries()) {
contexts[url] = context;
}
90 changes: 63 additions & 27 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,12 @@ import {CredentialIssuancePurpose} from './CredentialIssuancePurpose.js';
import {documentLoader as _documentLoader} from './documentLoader.js';
export const defaultDocumentLoader =
jsigs.extendContextLoader(_documentLoader);
import * as credentialsContext from 'credentials-context';
import * as credentials1Context from 'credentials-context';
import * as credentials2Context from '@digitalbazaar/credentials-v2-context';

const {AuthenticationProofPurpose} = jsigs.purposes;
const {constants: {CREDENTIALS_CONTEXT_V1_URL}} = credentialsContext;
const {constants: {CREDENTIALS_CONTEXT_V1_URL}} = credentials1Context;
const CREDENTIALS_CONTEXT_V2_URL = credentials2Context.CONTEXT_URL;

export {CredentialIssuancePurpose};

Expand Down Expand Up @@ -134,9 +136,14 @@ export async function issue({
}

// Set the issuance date to now(), if missing
if(!credential.issuanceDate) {
if((credential['@context'][0] === CREDENTIALS_CONTEXT_V1_URL) &&
!credential.issuanceDate) {
const now = (new Date()).toJSON();
credential.issuanceDate = `${now.substr(0, now.length - 5)}Z`;
} else if((credential['@context'][0] === CREDENTIALS_CONTEXT_V2_URL) &&
!credential.validFrom) {
const now = (new Date()).toJSON();
credential.validFrom = `${now.substr(0, now.length - 5)}Z`;
}

// run common credential checks
Expand Down Expand Up @@ -512,10 +519,11 @@ export function _checkPresentation(presentation) {
presentation['@context'] : [presentation['@context']];

// ensure first context is 'https://www.w3.org/2018/credentials/v1'
if(context[0] !== CREDENTIALS_CONTEXT_V1_URL) {
if((context[0] !== CREDENTIALS_CONTEXT_V1_URL) &&
(context[0] !== CREDENTIALS_CONTEXT_V2_URL)) {
throw new Error(
`"${CREDENTIALS_CONTEXT_V1_URL}" needs to be first in the ` +
'list of contexts.');
`"${CREDENTIALS_CONTEXT_V1_URL}" or ${CREDENTIALS_CONTEXT_V2_URL} ` +
'needs to be first in the list of contexts.');
}

const types = jsonld.getValues(presentation, 'type');
Expand All @@ -542,10 +550,11 @@ export function _checkCredential({credential, now = new Date()}) {
now = new Date(now);
}
// ensure first context is 'https://www.w3.org/2018/credentials/v1'
if(credential['@context'][0] !== CREDENTIALS_CONTEXT_V1_URL) {
if((credential['@context'][0] !== CREDENTIALS_CONTEXT_V1_URL) &&
(credential['@context'][0] !== CREDENTIALS_CONTEXT_V2_URL)) {
throw new Error(
`"${CREDENTIALS_CONTEXT_V1_URL}" needs to be first in the ` +
'list of contexts.');
`"${CREDENTIALS_CONTEXT_V1_URL}" or "${CREDENTIALS_CONTEXT_V2_URL}" ` +
'needs to be first in the list of contexts.');
}

// check type presence and cardinality
Expand All @@ -572,27 +581,54 @@ export function _checkCredential({credential, now = new Date()}) {
throw new Error('"issuer" property is required.');
}

// check issuanceDate cardinality
if(jsonld.getValues(credential, 'issuanceDate').length > 1) {
throw new Error('"issuanceDate" property can only have one value.');
}
// ensure first context is 'https://www.w3.org/2018/credentials/v1'
if(credential['@context'][0] === CREDENTIALS_CONTEXT_V1_URL) {
// check issuanceDate cardinality
if(jsonld.getValues(credential, 'issuanceDate').length > 1) {
throw new Error('"issuanceDate" property can only have one value.');
}

// check issued is a date
if(!credential.issuanceDate) {
throw new Error('"issuanceDate" property is required.');
}
// check issued is a date
if(!credential.issuanceDate) {
throw new Error('"issuanceDate" property is required.');
}

if('issuanceDate' in credential) {
let {issuanceDate} = credential;
if(!dateRegex.test(issuanceDate)) {
throw new Error(`"issuanceDate" must be a valid date: ${issuanceDate}`);
if('issuanceDate' in credential) {
let {issuanceDate} = credential;
if(!dateRegex.test(issuanceDate)) {
throw new Error(`"issuanceDate" must be a valid date: ${issuanceDate}`);
}
// check if `now` is before `issuanceDate`
issuanceDate = new Date(issuanceDate);
if(now < issuanceDate) {
throw new Error(
`The current date time (${now.toISOString()}) is before the ` +
`"issuanceDate" (${issuanceDate.toISOString()}).`);
}
}
// check if `now` is before `issuanceDate`
issuanceDate = new Date(issuanceDate);
if(now < issuanceDate) {
throw new Error(
`The current date time (${now.toISOString()}) is before the ` +
`"issuanceDate" (${issuanceDate.toISOString()}).`);
} else if(credential['@context'][0] === CREDENTIALS_CONTEXT_V2_URL) {
// check validFrom cardinality
if(jsonld.getValues(credential, 'validFrom').length > 1) {
throw new Error('"validFrom" property can only have one value.');
}

// check issued is a date
if(!credential.validFrom) {
throw new Error('"validFrom" property is required.');
}

if('validFrom' in credential) {
let {validFrom} = credential;
if(!dateRegex.test(validFrom)) {
throw new Error(`"validFrom" must be a valid date: ${validFrom}`);
}
// check if `now` is before `validFrom`
validFrom = new Date(validFrom);
if(now < validFrom) {
throw new Error(
`The current date time (${now.toISOString()}) is before the ` +
`"validFrom" (${validFrom.toISOString()}).`);
}
}
}

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"lib/**/*.js"
],
"dependencies": {
"@digitalbazaar/credentials-v2-context": "github:digitalbazaar/credentials-v2-context",
"credentials-context": "^2.0.0",
"jsonld": "^8.0.0",
"jsonld-signatures": "^11.0.0"
Expand Down

0 comments on commit af8063b

Please sign in to comment.