From e6b2c1d1be977b847dff6570f967b65746560109 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Fri, 24 Nov 2023 13:44:40 +0000 Subject: [PATCH] Use version number for key in credentials map & remove old cred mocks. --- test/10-verify.spec.js | 49 +++++++++++++++++++++++++++------------- test/mocks/credential.js | 34 ++-------------------------- 2 files changed, 35 insertions(+), 48 deletions(-) diff --git a/test/10-verify.spec.js b/test/10-verify.spec.js index a77dbd10..5ec0852c 100644 --- a/test/10-verify.spec.js +++ b/test/10-verify.spec.js @@ -84,6 +84,7 @@ before(async () => { await ecdsaKeyPair.export({publicKey: true})); }); +// run tests on each version of VCs for(const [version, mockCredential] of versionedCredentials) { describe(`Verifiable Credentials Data Model ${version}`, async function() { describe('vc.issue()', () => { @@ -99,7 +100,7 @@ for(const [version, mockCredential] of versionedCredentials) { verifiableCredential.should.have.property('proof'); verifiableCredential.proof.should.be.an('object'); }); - if(version === '1.0') { + if(version === 1.0) { it('should issue an expired verifiable credential', async () => { const keyPair = await Ed25519VerificationKey2018.generate(); const fp = Ed25519VerificationKey2018 @@ -173,7 +174,8 @@ for(const [version, mockCredential] of versionedCredentials) { const presentation = vc.createPresentation({ verifiableCredential: jsonld.clone(mockCredential), id: 'test:ebc6f1c2', - holder: 'did:ex:holder123' + holder: 'did:ex:holder123', + version }); const vp = await vc.signPresentation({ presentation, @@ -215,7 +217,7 @@ for(const [version, mockCredential] of versionedCredentials) { it('should verify a derived vc', async () => { 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 ecdsaSdSignSuite = new DataIntegrityProof({ @@ -381,7 +383,7 @@ for(const [version, mockCredential] of versionedCredentials) { }); it('should verify a changed derived vc', async () => { 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 ecdsaSdSignSuite = new DataIntegrityProof({ @@ -432,7 +434,7 @@ for(const [version, mockCredential] of versionedCredentials) { const challenge = uuid(); const {presentation, suite, documentLoader} = - await _generatePresentation({challenge, mockCredential}); + await _generatePresentation({challenge, mockCredential, version}); const result = await vc.verify({ challenge, @@ -450,8 +452,15 @@ for(const [version, mockCredential] of versionedCredentials) { }); it('verifies an unsigned presentation', async () => { - const {presentation, suite: vcSuite, documentLoader} = - await _generatePresentation({unsigned: true, mockCredential}); + const { + presentation, + suite: vcSuite, + documentLoader + } = await _generatePresentation({ + unsigned: true, + mockCredential, + version + }); const result = await vc.verify({ documentLoader, @@ -479,7 +488,8 @@ for(const [version, mockCredential] of versionedCredentials) { await _generatePresentation({ challenge, credentialsCount: count, - mockCredential + mockCredential, + version }); // tampering with the first two credentials id @@ -528,7 +538,8 @@ for(const [version, mockCredential] of versionedCredentials) { await _generatePresentation({ challenge, credentialsCount: count, - mockCredential + mockCredential, + version }); const result = await vc.verify({ documentLoader, @@ -618,7 +629,7 @@ for(const [version, mockCredential] of versionedCredentials) { error.message.should .contain('Credential has expired.'); }); - if(version === '1.0') { + if(version === 1.0) { it('should reject if "now" is before "issuanceDate"', () => { const credential = jsonld.clone(mockCredential); credential.issuer = 'did:example:12345'; @@ -642,7 +653,7 @@ for(const [version, mockCredential] of versionedCredentials) { const credential = jsonld.clone(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; @@ -660,7 +671,7 @@ for(const [version, mockCredential] of versionedCredentials) { const credential = jsonld.clone(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; @@ -682,7 +693,7 @@ for(const [version, mockCredential] of versionedCredentials) { {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; @@ -720,7 +731,11 @@ async function _generateCredential(_mockCredential) { } async function _generatePresentation({ - challenge, unsigned = false, credentialsCount = 1, mockCredential + challenge, + unsigned = false, + credentialsCount = 1, + mockCredential, + version }) { const {didDocument, documentLoader: didLoader} = await _loadDid(); testLoader.addLoader(didLoader); @@ -737,8 +752,10 @@ async function _generatePresentation({ suite: vcSuite} = await _generateCredential(mockCredential); testLoader.addLoader(dlc); - const presentation = vc.createPresentation( - {verifiableCredential: credentials}); + const presentation = vc.createPresentation({ + verifiableCredential: credentials, + version + }); if(unsigned) { return {presentation, suite: vcSuite, diff --git a/test/mocks/credential.js b/test/mocks/credential.js index 271c8d27..541f0bb2 100644 --- a/test/mocks/credential.js +++ b/test/mocks/credential.js @@ -1,35 +1,5 @@ -export const credentials = { - v1: { - '@context': [ - 'https://www.w3.org/2018/credentials/v1', - 'https://www.w3.org/2018/credentials/examples/v1' - ], - id: 'http://example.edu/credentials/1872', - type: ['VerifiableCredential', 'AlumniCredential'], - issuer: 'https://example.edu/issuers/565049', - issuanceDate: '2010-01-01T19:23:24Z', - credentialSubject: { - id: 'did:example:ebfeb1f712ebc6f1c276e12ec21', - alumniOf: 'Example University' - } - }, - v2: { - '@context': [ - 'https://www.w3.org/ns/credentials/v2', - 'https://www.w3.org/2018/credentials/examples/v1' - ], - id: 'http://example.edu/credentials/1872', - type: ['VerifiableCredential', 'AlumniCredential'], - issuer: 'https://example.edu/issuers/565049', - credentialSubject: { - id: 'did:example:ebfeb1f712ebc6f1c276e12ec21', - alumniOf: 'Example University' - } - } -}; - export const versionedCredentials = new Map([ - ['1.0', { + [1.0, { '@context': [ 'https://www.w3.org/2018/credentials/v1', 'https://www.w3.org/2018/credentials/examples/v1' @@ -43,7 +13,7 @@ export const versionedCredentials = new Map([ alumniOf: 'Example University' } }], - ['2.0', { + [2.0, { '@context': [ 'https://www.w3.org/ns/credentials/v2', 'https://www.w3.org/2018/credentials/examples/v1'