Skip to content

Commit

Permalink
Switch to strings for vc version to support semantic versioning.
Browse files Browse the repository at this point in the history
  • Loading branch information
aljones15 committed Jul 21, 2024
1 parent 7496bfb commit 773229a
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 31 deletions.
32 changes: 13 additions & 19 deletions test/10-verify.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@
* Copyright (c) 2019-2024 Digital Bazaar, Inc. All rights reserved.
*/
import * as vc from '../lib/index.js';
import {
createIssuerSuite,
createVerifySuite,
generateCredential
} from './helpers.js';
import {assertionController} from './mocks/assertionController.js';
import chai from 'chai';
import {DataIntegrityProof} from '@digitalbazaar/data-integrity';
import {documentLoader} from './testDocumentLoader.js';
import {generateCredential} from './helpers.js';
import {invalidContexts} from './contexts/index.js';
import {setupSuites} from './mocks/suites.js';
import {v4 as uuid} from 'uuid';
Expand All @@ -32,20 +36,20 @@ function _runSuite({
suite, version,
credentialFactory, cryptosuite
}) {
const title = `VC ${version.toFixed(1)} Suite ${suiteName} ` +
`KeyType ${keyType}`;
const title = `VC ${version} Suite ${suiteName} KeyType ${keyType}`;
const generateDefaults = {
credentialFactory,
suite,
issuer
};
const verifySuite = createVerifySuite({Suite, cryptosuite, derived});
const issuerSuite = createIssuerSuite({Suite, cryptosuite, derived, signer: keyPair.signer()});

Check failure on line 46 in test/10-verify.spec.js

View workflow job for this annotation

GitHub Actions / lint (20.x)

This line has a length of 97. Maximum allowed is 80
describe(title, async function() {
describe('verify API (credentials)', () => {
it('should verify a vc', async () => {
const verifiableCredential = await vc.issue({
credential: credentialFactory(),
suite,
suite: issuerSuite,
documentLoader
});
const result = await vc.verifyCredential({
Expand Down Expand Up @@ -91,7 +95,7 @@ function _runSuite({
createSignCryptosuite,
} = cryptosuite;
const proofId = `urn:uuid:${uuid()}`;
const mandatoryPointers = (version === 1.0) ?
const mandatoryPointers = (version === '1.0') ?
['/issuer', '/issuanceDate'] : ['/issuer'];
const sdSignSuite = new DataIntegrityProof({
signer: keyPair.signer(),
Expand Down Expand Up @@ -150,7 +154,7 @@ function _runSuite({
};
const verifiableCredential = await vc.issue({
credential,
suite,
suite: issuerSuite,
documentLoader
});
const result = await vc.verifyCredential({
Expand Down Expand Up @@ -264,7 +268,7 @@ function _runSuite({
};
const verifiableCredential = await vc.issue({
credential,
suite,
suite: issuerSuite,
documentLoader
});
const result = await vc.verifyCredential({
Expand All @@ -285,7 +289,7 @@ function _runSuite({
const credential = credentialFactory();
const verifiableCredential = await vc.issue({
credential,
suite,
suite: issuerSuite,
documentLoader
});
const result = await vc.verifyCredential({
Expand All @@ -306,10 +310,9 @@ function _runSuite({
const {
createDiscloseCryptosuite,
createSignCryptosuite,
createVerifyCryptosuite
} = cryptosuite;
const proofId = `urn:uuid:${uuid()}`;
const mandatoryPointers = (version === 1.0) ?
const mandatoryPointers = (version === '1.0') ?
['/issuer', '/issuanceDate'] : ['/issuer'];
// setup ecdsa-sd-2023 suite for signing selective disclosure VCs
const sdSignSuite = new DataIntegrityProof({
Expand Down Expand Up @@ -354,12 +357,3 @@ function _runSuite({
});
});
}

function createVerifySuite({Suite, cryptosuite, derived}) {
if(!derived) {
return new Suite({cryptosuite});
}
return new Suite({
cryptosuite: cryptosuite.createVerifyCryptosuite()
});
}
2 changes: 1 addition & 1 deletion test/30-presentations.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ function _runSuite({keyType, suite, version, credentialFactory}) {
credential.credentialId.should.be.a('string');
}
});
if(version === 2.0) {
if(version === '2.0') {
it(`should not error when credentials have mixed contexts ` +
`[${count}]`, async () => {
const challenge = uuid();
Expand Down
10 changes: 5 additions & 5 deletions test/40-credentials.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ function _runSuite({mockCredential, version}) {
.contain('"evidence" must be a URI');
});

if(version === 1.0) {
if(version === '1.0') {
it('should reject if "expirationDate" has passed', () => {
const credential = mockCredential();
credential.issuer = 'did:example:12345';
Expand Down Expand Up @@ -134,7 +134,7 @@ function _runSuite({mockCredential, version}) {
`"issuanceDate" (${credential.issuanceDate}).`);
});
}
if(version === 2.0) {
if(version === '2.0') {
it('should reject "validFrom" in the future', () => {
const credential = mockCredential();
credential.issuer = 'did:example:12345';
Expand Down Expand Up @@ -267,7 +267,7 @@ function _runSuite({mockCredential, version}) {
const credential = mockCredential();
credential.credentialSubject = {};
credential.issuer = 'did:example:12345';
if(version === 1.0) {
if(version === '1.0') {
credential.issuanceDate = '2022-10-31T19:21:25Z';
}
let error;
Expand All @@ -285,7 +285,7 @@ function _runSuite({mockCredential, version}) {
const credential = mockCredential();
credential.credentialSubject = [{}, {id: 'did:key:zFoo'}];
credential.issuer = 'did:example:12345';
if(version === 1.0) {
if(version === '1.0') {
credential.issuanceDate = '2022-10-31T19:21:25Z';
}
let error;
Expand All @@ -307,7 +307,7 @@ function _runSuite({mockCredential, version}) {
{name: 'did key'}
];
credential.issuer = 'did:example:12345';
if(version === 1.0) {
if(version === '1.0') {
credential.issuanceDate = '2022-10-31T19:21:25Z';
}
let error;
Expand Down
4 changes: 2 additions & 2 deletions test/50-issue.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ function _runSuite({version, keyType, suite, mockCredential}) {
error.message.should
.contain('"suite.verificationMethod" property is required.');
});
if(version === 1.0) {
if(version === '1.0') {
it('should issue an expired verifiable credential', async () => {
const fp = Ed25519VerificationKey2018
.fingerprintFromPublicKey({
Expand Down Expand Up @@ -113,7 +113,7 @@ function _runSuite({version, keyType, suite, mockCredential}) {
);
});
}
if(version === 2.0) {
if(version === '2.0') {
it('should issue "validUntil" in the future', async () => {
const credential = mockCredential();
credential.issuer = 'did:example:12345';
Expand Down
21 changes: 21 additions & 0 deletions test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,24 @@ export async function generateCredential({
});
return {credential};
}

export function createVerifySuite({Suite, cryptosuite, derived}) {
if(!derived) {
return new Suite({cryptosuite});
}
return new Suite({
cryptosuite: cryptosuite.createVerifyCryptosuite()
});
}
export function createIssuerSuite({
Suite, cryptosuite, signer,
derived, mandatoryPointers = ['/issuer']
}) {
if(!derived) {
return new Suite({signer, cryptosuite});
}
return new Suite({
signer,
cryptosuite: cryptosuite.createSignCryptosuite({mandatoryPointers})
});
}
4 changes: 2 additions & 2 deletions test/mocks/credential.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import {klona} from 'klona';

export const versionedCredentials = new Map([
[1.0, () => klona({
['1.0', () => klona({
'@context': [
'https://www.w3.org/2018/credentials/v1',
'https://www.w3.org/2018/credentials/examples/v1'
Expand All @@ -18,7 +18,7 @@ export const versionedCredentials = new Map([
alumniOf: '<span lang="en">Example University</span>'
}
})],
[2.0, () => klona({
['2.0', () => klona({
'@context': [
'https://www.w3.org/ns/credentials/v2',
'https://www.w3.org/ns/credentials/examples/v2'
Expand Down
4 changes: 2 additions & 2 deletions test/mocks/mock.data.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ const _mixedCredential = () => {
return () => {
if((count % 2) === 0) {
count++;
return versionedCredentials.get(1.0)();
return versionedCredentials.get('1.0')();
}
count ++;
return versionedCredentials.get(2.0)();
return versionedCredentials.get('2.0')();
};
};

Expand Down

0 comments on commit 773229a

Please sign in to comment.