Skip to content

Commit

Permalink
Move generateCredential to helpers.
Browse files Browse the repository at this point in the history
  • Loading branch information
aljones15 committed Jul 21, 2024
1 parent 40d39c5 commit bb1fbe9
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 44 deletions.
72 changes: 28 additions & 44 deletions test/10-verify.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,12 @@
* Copyright (c) 2019-2024 Digital Bazaar, Inc. All rights reserved.
*/
import * as vc from '../lib/index.js';
import {
documentLoader,
testLoader
} from './testDocumentLoader.js';
import {assertionController} from './mocks/assertionController.js';
import chai from 'chai';
import {CredentialIssuancePurpose} from '../lib/CredentialIssuancePurpose.js';
import {DataIntegrityProof} from '@digitalbazaar/data-integrity';
import {documentLoader} from './testDocumentLoader.js';
import {generateCredential} from './helpers.js';
import {invalidContexts} from './contexts/index.js';
import jsigs from 'jsonld-signatures';
import {setupSuites} from './mocks/suites.js';
import {v4 as uuid} from 'uuid';
import {versionedCredentials} from './mocks/credential.js';
Expand All @@ -37,6 +33,11 @@ function _runSuite({
credentialFactory, cryptosuite
}) {
const title = `VC ${version} suite: ${suiteName} keyType ${keyType}`;
const generateDefaults = {
credentialFactory,
suite,
issuer
};
describe(title, async function() {
describe('verify API (credentials)', () => {
it('should verify a vc', async () => {
Expand Down Expand Up @@ -68,14 +69,14 @@ function _runSuite({
const mandatoryPointers = (version === 1.0) ?
['/issuer', '/issuanceDate'] : ['/issuer'];
// setup ecdsa-sd-2023 suite for signing selective disclosure VCs
const ecdsaSdSignSuite = new DataIntegrityProof({
const sdSignSuite = new DataIntegrityProof({
signer: keyPair.signer(), cryptosuite: createSignCryptosuite({
mandatoryPointers
})
});
ecdsaSdSignSuite.proof = {id: proofId};
sdSignSuite.proof = {id: proofId};
// setup ecdsa-sd-2023 suite for deriving selective disclosure VCs
const ecdsaSdDeriveSuite = new DataIntegrityProof({
const sdDeriveSuite = new DataIntegrityProof({
cryptosuite: createDiscloseCryptosuite({
proofId,
selectivePointers: [
Expand All @@ -84,24 +85,24 @@ function _runSuite({
})
});
// setup ecdsa-sd-2023 suite for verifying selective disclosure VCs
const ecdsaSdVerifySuite = new DataIntegrityProof({
const sdVerifySuite = new DataIntegrityProof({
cryptosuite: createVerifyCryptosuite()
});

const verifiableCredential = await vc.issue({
credential: credentialFactory(),
suite: ecdsaSdSignSuite,
suite: sdSignSuite,
documentLoader
});
const derivedCredential = await vc.derive({
verifiableCredential,
suite: ecdsaSdDeriveSuite,
suite: sdDeriveSuite,
documentLoader
});
const result = await vc.verifyCredential({
credential: derivedCredential,
controller: assertionController,
suite: ecdsaSdVerifySuite,
suite: sdVerifySuite,
documentLoader
});

Expand Down Expand Up @@ -149,11 +150,7 @@ function _runSuite({

describe('negative test', async () => {
it('fails to verify if a context resolves to null', async () => {
const {credential} = await _generateCredential({
credentialFactory,
suite,
issuer
});
const {credential} = await generateCredential(generateDefaults);
credential['@context'].push(invalidContexts.nullDoc.url);
const results = await vc.verifyCredential({
suite,
Expand All @@ -164,7 +161,7 @@ function _runSuite({
results.verified.should.be.false;
});
it('fails to verify if a context contains an invalid id', async () => {
const {credential} = await _generateCredential(credentialFactory);
const {credential} = await generateCredential(generateDefaults);
credential['@context'].push(invalidContexts.invalidId.url);
const results = await vc.verifyCredential({
suite,
Expand All @@ -175,7 +172,7 @@ function _runSuite({
results.verified.should.be.false;
});
it('fails to verify if a context has a null version', async () => {
const {credential, suite} = await _generateCredential(credentialFactory);
const {credential} = await generateCredential(generateDefaults);
credential['@context'].push(invalidContexts.nullVersion.url);
const results = await vc.verifyCredential({
suite,
Expand All @@ -186,7 +183,7 @@ function _runSuite({
results.verified.should.be.false;
});
it('fails to verify if a context has a null @id', async () => {
const {credential, suite} = await _generateCredential(credentialFactory);
const {credential} = await generateCredential(generateDefaults);
credential['@context'].push(invalidContexts.nullId.url);
const results = await vc.verifyCredential({
suite,
Expand All @@ -197,7 +194,7 @@ function _runSuite({
results.verified.should.be.false;
});
it('fails to verify if a context has a null @type', async () => {
const {credential, suite} = await _generateCredential(credentialFactory);
const {credential} = await generateCredential(generateDefaults);
credential['@context'].push(invalidContexts.nullType.url);
const results = await vc.verifyCredential({
suite,
Expand All @@ -208,7 +205,7 @@ function _runSuite({
results.verified.should.be.false;
});
it('fails to verify if a context links to a missing doc', async () => {
const {credential, suite} = await _generateCredential(credentialFactory);
const {credential} = await generateCredential(generateDefaults);
credential['@context'].push('https://fsad.digitalbazaar.com');
const results = await vc.verifyCredential({
suite,
Expand All @@ -219,7 +216,7 @@ function _runSuite({
results.verified.should.be.false;
});
it('fails to verify if a context has an invalid url', async () => {
const {credential, suite} = await _generateCredential(credentialFactory);
const {credential} = await generateCredential(generateDefaults);
credential['@context'].push('htps://fsad.digitalbazaar.');
const results = await vc.verifyCredential({
suite,
Expand Down Expand Up @@ -296,15 +293,15 @@ function _runSuite({
const mandatoryPointers = (version === 1.0) ?
['/issuer', '/issuanceDate'] : ['/issuer'];
// setup ecdsa-sd-2023 suite for signing selective disclosure VCs
const ecdsaSdSignSuite = new DataIntegrityProof({
const sdSignSuite = new DataIntegrityProof({
signer: keyPair.signer(),
cryptosuite: createSignCryptosuite({
mandatoryPointers
})
});
ecdsaSdSignSuite.proof = {id: proofId};
sdSignSuite.proof = {id: proofId};
// setup ecdsa-sd-2023 suite for deriving selective disclosure VCs
const ecdsaSdDeriveSuite = new DataIntegrityProof({
const sdDeriveSuite = new DataIntegrityProof({
cryptosuite: createDiscloseCryptosuite({
proofId,
selectivePointers: [
Expand All @@ -313,25 +310,25 @@ function _runSuite({
})
});
// setup ecdsa-sd-2023 suite for verifying selective disclosure VCs
const ecdsaSdVerifySuite = new DataIntegrityProof({
const sdVerifySuite = new DataIntegrityProof({
cryptosuite: createVerifyCryptosuite()
});

const verifiableCredential = await vc.issue({
credential: credentialFactory(),
suite: ecdsaSdSignSuite,
suite: sdSignSuite,
documentLoader
});
const derivedCredential = await vc.derive({
verifiableCredential,
suite: ecdsaSdDeriveSuite,
suite: sdDeriveSuite,
documentLoader
});
derivedCredential.credentialSubject.id = `urn:uuid:${uuid()}`;
const result = await vc.verifyCredential({
credential: derivedCredential,
controller: assertionController,
suite: ecdsaSdVerifySuite,
suite: sdVerifySuite,
documentLoader
});
result.verified.should.be.a('boolean');
Expand All @@ -343,16 +340,3 @@ function _runSuite({
});
});
}

async function _generateCredential({credentialFactory, suite, issuer}) {
const mockCredential = credentialFactory();
mockCredential.issuer = issuer;
mockCredential.id = `http://example.edu/credentials/${uuid()}`;
const credential = await jsigs.sign(mockCredential, {
compactProof: false,
documentLoader: testLoader.documentLoader.bind(testLoader),
suite,
purpose: new CredentialIssuancePurpose()
});
return {credential};
}
28 changes: 28 additions & 0 deletions test/helpers.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
/*!
* Copyright (c) 2019-2024 Digital Bazaar, Inc. All rights reserved.
*/
import {CredentialIssuancePurpose} from '../lib/CredentialIssuancePurpose.js';
import {
documentLoader as defaultLoader
} from './testDocumentLoader.js';
import jsigs from 'jsonld-signatures';
import {v4 as uuid} from 'uuid';

/**
* Creates an ISO DateTime skewed by a number of years
*
Expand All @@ -12,3 +22,21 @@ export function createSkewedTimeStamp({date = new Date(), skewYear}) {
const isoString = date.toISOString();
return `${isoString.substring(0, isoString.length - 5)}Z`;
}

export async function generateCredential({
credentialFactory,
suite,
issuer,
documentLoader = defaultLoader
}) {
const mockCredential = credentialFactory();
mockCredential.issuer = issuer;
mockCredential.id = `http://example.edu/credentials/${uuid()}`;
const credential = await jsigs.sign(mockCredential, {
compactProof: false,
documentLoader,
suite,
purpose: new CredentialIssuancePurpose()
});
return {credential};
}

0 comments on commit bb1fbe9

Please sign in to comment.