Skip to content

Commit

Permalink
Add checkContextVersion & replace hasV1CredentialContext.
Browse files Browse the repository at this point in the history
  • Loading branch information
aljones15 committed Nov 27, 2023
1 parent 6fcf01c commit 844b3c6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
28 changes: 24 additions & 4 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,33 @@ export function assertCredentialContext({context}) {
}

/**
* Checks to see if a VC has a V1 context.
* Turns the first context in a VC into a numbered version.
*
* @param {object} options - Options.
* @param {object} options.credential - A VC.
*
* @returns {boolean} If the VC has a V1 context.
* @returns {number} A number representing the version.
*/
export function hasV1CredentialContext({credential}) {
return credential?.['@context']?.[0] === CREDENTIALS_CONTEXT_V1_URL;
function getContextVersion({credential} = {}) {
const firstContext = credential?.['@context']?.[0];
if(firstContext === CREDENTIALS_CONTEXT_V1_URL) {
return 1.0;
}
if(firstContext === CREDENTIALS_CONTEXT_V2_URL) {
return 2.0;
}
return 0;
}

/**
* Checks if a VC is using a specific context version.
*
* @param {object} options - Options.
* @param {object} options.credential - A VC.
* @param {number} options.version - A VC Context version
*
* @returns {boolean} If the first context matches the version.
*/
export function checkContextVersion({credential, version}) {
return getContextVersion({credential}) === version;
}
9 changes: 6 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@
*/
import {
assertCredentialContext,
checkContextVersion,
CREDENTIALS_CONTEXT_V1_URL,
CREDENTIALS_CONTEXT_V2_URL,
hasV1CredentialContext
} from './helpers.js';
import {documentLoader as _documentLoader} from './documentLoader.js';
import {CredentialIssuancePurpose} from './CredentialIssuancePurpose.js';
Expand Down Expand Up @@ -134,7 +134,10 @@ export async function issue({
if(!credential) {
throw new TypeError('"credential" parameter is required for issuing.');
}
if(hasV1CredentialContext({credential}) && !credential.issuanceDate) {
if(checkContextVersion({
credential,
version: 1.0
}) && !credential.issuanceDate) {
const now = (new Date()).toJSON();
credential.issuanceDate = `${now.slice(0, now.length - 5)}Z`;
}
Expand Down Expand Up @@ -604,7 +607,7 @@ export function _checkCredential({
if(!credential.issuer) {
throw new Error('"issuer" property is required.');
}
if(hasV1CredentialContext({credential})) {
if(checkContextVersion({credential, version: 1.0})) {
// check issued is a date
if(!credential.issuanceDate) {
throw new Error('"issuanceDate" property is required.');
Expand Down

0 comments on commit 844b3c6

Please sign in to comment.