Skip to content

Commit 0bc7e5f

Browse files
committed
add data model testing
Signed-off-by: PatStLouis <[email protected]>
1 parent 9b1afc4 commit 0bc7e5f

File tree

5 files changed

+80
-40
lines changed

5 files changed

+80
-40
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
"chai": "^4.3.7",
5858
"chai-string": "^1.5.0",
5959
"data-integrity-test-suite-assertion": "github:w3c-ccg/data-integrity-test-suite-assertion",
60-
"jsonld-document-loader": "^2.0.0",
60+
"jsonld-document-loader": "^2.2.0",
6161
"json-canon": "^1.0.1",
6262
"klona": "^2.0.6",
6363
"multibase": "^4.0.6",

tests/70-data-model.js

Lines changed: 46 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,19 @@
33
* SPDX-License-Identifier: BSD-3-Clause
44
*/
55
import {
6-
assertIssuedVc,
7-
createInitialVc,
8-
createValidCredential,
9-
getProofs,
6+
generateCredential,
7+
multikeyFromVerificationMethod,
8+
proofExists,
9+
secureCredential,
1010
setupReportableTestSuite,
1111
setupRow
1212
} from './helpers.js';
13+
import {
14+
assertDataIntegrityProof
15+
} from './assertions.js';
1316
import chai from 'chai';
1417
import {endpoints} from 'vc-test-suite-implementations';
18+
import {expect} from 'chai';
1519

1620
const should = chai.should();
1721

@@ -29,39 +33,41 @@ const {match: issuers} = endpoints.filterByTag({
2933
describe('Data Model - Verification Methods (Multikey)', function() {
3034
setupReportableTestSuite(this);
3135
this.implemented = [...issuers.keys()];
32-
let validCredential;
33-
before(async function() {
34-
validCredential = await createValidCredential();
35-
});
3636
for(const [columnId, {endpoints}] of issuers) {
3737
describe(columnId, function() {
3838
const [issuer] = endpoints;
39-
let issuedVc;
40-
let proofs;
41-
let ecdsaProofs = [];
39+
let securedCredential;
4240
before(async function() {
43-
issuedVc = await createInitialVc({issuer, vc: validCredential});
44-
proofs = getProofs(issuedVc);
45-
if(proofs?.length) {
46-
ecdsaProofs = proofs.filter(
47-
proof => cryptosuites.includes(proof?.cryptosuite));
48-
}
41+
securedCredential = await secureCredential(
42+
{issuer, vc: generateCredential()});
4943
});
5044
beforeEach(setupRow);
5145
it('The publicKeyMultibase value of the verification method ' +
5246
'MUST start with the base-58-btc prefix (z), as defined in ' +
5347
'the Multibase section of Controller Documents 1.0.',
5448
async function() {
5549
this.test.link = 'https://www.w3.org/TR/vc-di-ecdsa/#dataintegrityproof';
56-
assertIssuedVc(issuedVc, proofs, ecdsaProofs);
50+
const proof = proofExists(securedCredential);
51+
const verificationMethod = proof.verificationMethod;
52+
// Only did key is supported
53+
const keyType = issuer.settings.supportedEcdsaKeyTypes[0];
54+
const multikey =
55+
await multikeyFromVerificationMethod(verificationMethod, keyType);
56+
expect(multikey.startsWith('z')).to.be.true;
5757
});
5858
it('A Multibase-encoded ECDSA 256-bit public key value or an ' +
5959
'ECDSA 384-bit public key value follows, as defined in the Multikey ' +
6060
'section of Controller Documents 1.0. Any other encoding ' +
6161
'MUST NOT be allowed.',
6262
async function() {
6363
this.test.link = 'https://www.w3.org/TR/vc-di-ecdsa/#dataintegrityproof';
64-
assertIssuedVc(issuedVc, proofs, ecdsaProofs);
64+
const proof = proofExists(securedCredential);
65+
const verificationMethod = proof.verificationMethod;
66+
// Only did key is supported
67+
const keyType = issuer.settings.supportedEcdsaKeyTypes[0];
68+
const multikey =
69+
await multikeyFromVerificationMethod(verificationMethod, keyType);
70+
expect(multikey).to.be.exist;
6571
});
6672
});
6773
}
@@ -70,35 +76,40 @@ describe('Data Model - Verification Methods (Multikey)', function() {
7076
describe('Data Model - Proof Representations', function() {
7177
setupReportableTestSuite(this);
7278
this.implemented = [...issuers.keys()];
73-
let validCredential;
74-
before(async function() {
75-
validCredential = await createValidCredential();
76-
});
7779
for(const [columnId, {endpoints}] of issuers) {
7880
describe(columnId, function() {
7981
const [issuer] = endpoints;
80-
let issuedVc;
81-
let proofs;
82-
let ecdsaProofs = [];
82+
let securedCredential;
8383
before(async function() {
84-
issuedVc = await createInitialVc({issuer, vc: validCredential});
85-
proofs = getProofs(issuedVc);
86-
if(proofs?.length) {
87-
ecdsaProofs = proofs.filter(
88-
proof => cryptosuites.includes(proof?.cryptosuite));
89-
}
84+
securedCredential = await secureCredential(
85+
{issuer, vc: generateCredential()});
9086
});
9187
beforeEach(setupRow);
88+
it('A proof contains the attributes specified in the ' +
89+
'Proofs section of [VC-DATA-INTEGRITY].',
90+
async function() {
91+
this.test.link = 'https://www.w3.org/TR/vc-di-ecdsa/#dataintegrityproof';
92+
const proof = proofExists(securedCredential);
93+
assertDataIntegrityProof(proof);
94+
});
9295
it('The type property MUST be DataIntegrityProof.',
9396
async function() {
9497
this.test.link = 'https://www.w3.org/TR/vc-di-ecdsa/#dataintegrityproof';
95-
assertIssuedVc(issuedVc, proofs, ecdsaProofs);
98+
const proof = proofExists(securedCredential);
99+
should.exist(proof.type,
100+
'Expected a type on the proof.');
101+
proof.type.should.equal('DataIntegrityProof',
102+
'Expected DataIntegrityProof type.');
96103
});
97104
it('The cryptosuite property MUST be ecdsa-rdfc-2019, ' +
98-
'ecdsa-jcs-2019, or ecdsa-sd-2023.',
105+
'ecdsa-jcs-2019, or ecdsa-sd-2023.',
99106
async function() {
100107
this.test.link = 'https://www.w3.org/TR/vc-di-ecdsa/#dataintegrityproof';
101-
assertIssuedVc(issuedVc, proofs, ecdsaProofs);
108+
const proof = proofExists(securedCredential);
109+
should.exist(proof.cryptosuite,
110+
'Expected a cryptosuite identifier on the proof.');
111+
proof.cryptosuite.should.be.oneOf(cryptosuites,
112+
`Expected cryptosuite for be one of ${cryptosuites}.`);
102113
});
103114
});
104115
}

tests/90-algorithms-jcs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ describe('Algorithms - Create Proof (ecdsa-jcs-2019)', function() {
5454
async function() {
5555
this.test.link = 'https://www.w3.org/TR/vc-di-ecdsa/#create-proof-ecdsa-jcs-2019';
5656
const proof = proofExists(securedCredential);
57-
assertDataIntegrityProof(proof, 'ecdsa-jcs-2019');
57+
assertDataIntegrityProof(proof);
5858
// Since we are not sending proof options, we only do a positive test
5959
});
6060
it('If unsecuredDocument.@context is present, ' +

tests/assertions.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ export function assertAllUtf8(proof) {
205205
}
206206
}
207207

208-
export function assertDataIntegrityProof(proof, cryptosuite) {
208+
export function assertDataIntegrityProof(proof) {
209209
if(proof?.id) {
210210
}
211211
should.exist(proof.type,
@@ -230,8 +230,6 @@ export function assertDataIntegrityProof(proof, cryptosuite) {
230230
}
231231
should.exist(proof.cryptosuite,
232232
'Expected a cryptosuite identifier on the proof.');
233-
proof.cryptosuite.should.equal(cryptosuite,
234-
`Expected {cryptosuite} cryptosuite.`);
235233
isValidUtf8(proof.cryptosuite).should.equal(
236234
true,
237235
'Expected cryptosuite value to be a valid UTF-8 encoded string.'

tests/helpers.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@
44
*/
55
import * as bs58 from 'base58-universal';
66
import * as bs64 from 'base64url-universal';
7+
import * as didKey from '@digitalbazaar/did-method-key';
8+
import * as EcdsaMultikey from '@digitalbazaar/ecdsa-multikey';
9+
import {CachedResolver} from '@digitalbazaar/did-io';
710
import chai from 'chai';
811
import {createRequire} from 'node:module';
12+
import {contexts as credContexts} from '@digitalbazaar/credentials-context';
913
import {isUtf8} from 'node:buffer';
14+
import {JsonLdDocumentLoader} from 'jsonld-document-loader';
1015
import {klona} from 'klona';
1116
import {readFileSync} from 'fs';
1217
import {v4 as uuidv4} from 'uuid';
@@ -313,3 +318,29 @@ export async function verifyError(verifier, securedCredential) {
313318
const response = await verifier.post({json: body});
314319
should.exist(response.error, 'Expected an error from verifier.');
315320
}
321+
322+
export async function multikeyFromVerificationMethod(
323+
verificationMethod, keyType) {
324+
const cachedResolver = new CachedResolver();
325+
const jdl = new JsonLdDocumentLoader();
326+
jdl.addDocuments({documents: credContexts});
327+
jdl.setDidResolver(cachedResolver);
328+
const didKeyDriverMultikey = didKey.driver();
329+
const prefixes = {
330+
Ed25519: 'z6Mk',
331+
'P-256': 'zDna',
332+
'P-384': 'z82L'
333+
};
334+
try {
335+
didKeyDriverMultikey.use({
336+
multibaseMultikeyHeader: prefixes[keyType],
337+
fromMultibase: EcdsaMultikey.from
338+
});
339+
cachedResolver.use(didKeyDriverMultikey);
340+
const response = await jdl.documentLoader(verificationMethod);
341+
return response.document.publicKeyMultibase;
342+
} catch(error) {
343+
// Do nothing on error
344+
}
345+
return null;
346+
}

0 commit comments

Comments
 (0)