Skip to content

Commit

Permalink
Use version number for key in credentials map & remove old cred mocks.
Browse files Browse the repository at this point in the history
  • Loading branch information
aljones15 committed Nov 24, 2023
1 parent e93e0dd commit e6b2c1d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 48 deletions.
49 changes: 33 additions & 16 deletions test/10-verify.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()', () => {
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -528,7 +538,8 @@ for(const [version, mockCredential] of versionedCredentials) {
await _generatePresentation({
challenge,
credentialsCount: count,
mockCredential
mockCredential,
version
});
const result = await vc.verify({
documentLoader,
Expand Down Expand Up @@ -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';
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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,
Expand Down
34 changes: 2 additions & 32 deletions test/mocks/credential.js
Original file line number Diff line number Diff line change
@@ -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: '<span lang="en">Example University</span>'
}
},
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: '<span lang="en">Example University</span>'
}
}
};

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'
Expand All @@ -43,7 +13,7 @@ export const versionedCredentials = new Map([
alumniOf: '<span lang="en">Example University</span>'
}
}],
['2.0', {
[2.0, {
'@context': [
'https://www.w3.org/ns/credentials/v2',
'https://www.w3.org/2018/credentials/examples/v1'
Expand Down

0 comments on commit e6b2c1d

Please sign in to comment.