Skip to content

Commit f414cb2

Browse files
committed
improve proof handling when asserting a secured credential
Signed-off-by: PatStLouis <[email protected]>
1 parent 56bccf3 commit f414cb2

File tree

3 files changed

+115
-60
lines changed

3 files changed

+115
-60
lines changed

tests/90-algorithms-jcs.js

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
generateCredential,
1212
getProofs,
1313
isValidDatetime,
14+
proofExists,
1415
secureCredential,
1516
setupReportableTestSuite,
1617
setupRow,
@@ -46,17 +47,18 @@ describe('Algorithms - Create Proof (ecdsa-jcs-2019)', function() {
4647
describe(columnId, function() {
4748
const [issuer] = endpoints;
4849
let securedCredential;
49-
let proof;
50+
let proofs;
5051
before(async function() {
5152
securedCredential = await secureCredential(
5253
{issuer, vc: generateCredential()});
53-
proof = getProofs(securedCredential)[0];
54+
proofs = getProofs(securedCredential);
5455
});
5556
beforeEach(setupRow);
5657
it('A data integrity proof (map), or an error, is produced as output.',
5758
async function() {
5859
this.test.link = 'https://www.w3.org/TR/vc-di-ecdsa/#create-proof-ecdsa-jcs-2019';
5960
assertSecuredCredential(securedCredential);
61+
const proof = proofExists(proofs);
6062
assertDataIntegrityProof(proof, 'ecdsa-jcs-2019');
6163
// Since we are not sending proof options, we only do a positive test
6264
});
@@ -67,6 +69,7 @@ describe('Algorithms - Create Proof (ecdsa-jcs-2019)', function() {
6769
// NOTE, for backwards compatibility reason, this step is not mandatory
6870
// This feature is designed to be used with proof sets/chains,
6971
// when adding new context in subsequent proofs
72+
const proof = proofExists(proofs);
7073
should.exist(proof['@context'],
7174
'Expected proof to have context.');
7275
canonicalize(proof['@context']).should.equal(
@@ -80,6 +83,7 @@ describe('Algorithms - Create Proof (ecdsa-jcs-2019)', function() {
8083
this.test.link = 'https://www.w3.org/TR/vc-di-ecdsa/#create-proof-ecdsa-jcs-2019';
8184
// Shallow multibase test
8285
// TODO try decoding
86+
const proof = proofExists(proofs);
8387
should.exist(proof.proofValue,
8488
'Expected proof to have proofValue.');
8589
expect(proof.proofValue.startsWith('z')).to.be.true;
@@ -124,18 +128,19 @@ describe('Algorithms - Transformation', function() {
124128
describe(columnId, function() {
125129
const [issuer] = endpoints;
126130
let securedCredential;
127-
let proof;
131+
let proofs;
128132
before(async function() {
129133
securedCredential = await secureCredential(
130134
{issuer, vc: generateCredential()});
131-
proof = getProofs(securedCredential)[0];
135+
proofs = getProofs(securedCredential);
132136
});
133137
beforeEach(setupRow);
134138
it('The proof options MUST contain a type identifier for the ' +
135139
'cryptographic suite (type) and MAY contain a cryptosuite ' +
136140
'identifier (cryptosuite).',
137141
async function() {
138142
this.test.link = 'https://www.w3.org/TR/vc-di-ecdsa/#proof-serialization-ecdsa-jcs-2019';
143+
const proof = proofExists(proofs);
139144
should.exist(proof.type,
140145
'Expected a type identifier on the proof.');
141146
});
@@ -144,6 +149,7 @@ describe('Algorithms - Transformation', function() {
144149
'(cryptosuite).',
145150
async function() {
146151
this.test.link = 'https://www.w3.org/TR/vc-di-ecdsa/#transformation-ecdsa-jcs-2019';
152+
const proof = proofExists(proofs);
147153
should.exist(proof.type,
148154
'Expected a type identifier on the proof.');
149155
should.exist(proof.cryptosuite,
@@ -153,6 +159,7 @@ describe('Algorithms - Transformation', function() {
153159
'it MUST use UTF-8 encoding.',
154160
async function() {
155161
this.test.link = 'https://www.w3.org/TR/vc-di-ecdsa/#transformation-ecdsa-jcs-2019';
162+
const proof = proofExists(proofs);
156163
assertAllUtf8(proof);
157164
});
158165
it('If options.type is not set to the string DataIntegrityProof or ' +
@@ -161,6 +168,7 @@ describe('Algorithms - Transformation', function() {
161168
'of PROOF_TRANSFORMATION_ERROR.',
162169
async function() {
163170
this.test.link = 'https://www.w3.org/TR/vc-di-ecdsa/#transformation-ecdsa-jcs-2019';
171+
const proof = proofExists(proofs);
164172
should.exist(proof.type,
165173
'Expected a type identifier on the proof.');
166174
should.exist(proof.cryptosuite,
@@ -181,18 +189,19 @@ describe('ecdsa-jcs-2019 - Algorithms - Proof Configuration', function() {
181189
describe(columnId, function() {
182190
const [issuer] = endpoints;
183191
let securedCredential;
184-
let proof;
192+
let proofs;
185193
before(async function() {
186194
securedCredential = await secureCredential(
187195
{issuer, vc: generateCredential()});
188-
proof = getProofs(securedCredential)[0];
196+
proofs = getProofs(securedCredential);
189197
});
190198
beforeEach(setupRow);
191199
it('The proof options MUST contain a type identifier for the ' +
192200
'cryptographic suite (type) and MUST contain a cryptosuite ' +
193201
'identifier (cryptosuite).',
194202
async function() {
195203
this.test.link = 'https://www.w3.org/TR/vc-di-ecdsa/#proof-configuration-ecdsa-jcs-2019';
204+
const proof = proofExists(proofs);
196205
should.exist(proof.type,
197206
'Expected a type identifier on the proof.');
198207
should.exist(proof.cryptosuite,
@@ -204,6 +213,7 @@ describe('ecdsa-jcs-2019 - Algorithms - Proof Configuration', function() {
204213
'of PROOF_GENERATION_ERROR.',
205214
async function() {
206215
this.test.link = 'https://www.w3.org/TR/vc-di-ecdsa/#proof-configuration-ecdsa-jcs-2019';
216+
const proof = proofExists(proofs);
207217
should.exist(proof.type,
208218
'Expected a type identifier on the proof.');
209219
should.exist(proof.cryptosuite,
@@ -218,6 +228,7 @@ describe('ecdsa-jcs-2019 - Algorithms - Proof Configuration', function() {
218228
'SHOULD convey an error type of PROOF_GENERATION_ERROR.',
219229
async function() {
220230
this.test.link = 'https://www.w3.org/TR/vc-di-ecdsa/#proof-configuration-ecdsa-jcs-2019';
231+
const proof = proofExists(proofs);
221232
if(proof?.created) {
222233
isValidDatetime(proof.created).should.equal(
223234
true,
@@ -236,18 +247,19 @@ describe('ecdsa-jcs-2019 - Algorithms - Proof Serialization', function() {
236247
describe(columnId, function() {
237248
const [issuer] = endpoints;
238249
let securedCredential;
239-
let proof;
250+
let proofs;
240251
before(async function() {
241252
securedCredential = await secureCredential(
242253
{issuer, vc: generateCredential()});
243-
proof = getProofs(securedCredential)[0];
254+
proofs = getProofs(securedCredential);
244255
});
245256
beforeEach(setupRow);
246257
it('The proof options MUST contain a type identifier for the ' +
247258
'cryptographic suite (type) and MAY contain a cryptosuite identifier ' +
248259
'(cryptosuite).',
249260
async function() {
250261
this.test.link = 'https://www.w3.org/TR/vc-di-ecdsa/#proof-serialization-ecdsa-jcs-2019';
262+
const proof = proofExists(proofs);
251263
should.exist(proof.type,
252264
'Expected a type identifier on the proof.');
253265
});

tests/90-algorithms-rdfc.js

Lines changed: 86 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import {
88
createValidCredential,
99
getProofs,
1010
isValidDatetime,
11-
isValidUtf8,
11+
proofExists,
12+
secureCredential,
1213
setupReportableTestSuite,
1314
setupRow
1415
} from './helpers.js';
@@ -36,16 +37,12 @@ describe('ecdsa-rdfc-2019 - Algorithms - Transformation', function() {
3637
for(const [columnId, {endpoints}] of issuers) {
3738
describe(columnId, function() {
3839
const [issuer] = endpoints;
39-
let issuedVc;
40+
let securedCredential;
4041
let proofs;
41-
let rdfc2019Proofs = [];
4242
before(async function() {
43-
issuedVc = await createInitialVc({issuer, vc: validCredential});
44-
proofs = getProofs(issuedVc);
45-
if(proofs?.length) {
46-
rdfc2019Proofs = proofs.filter(
47-
proof => proof?.cryptosuite === cryptosuite);
48-
}
43+
securedCredential = await secureCredential(
44+
{issuer, vc: generateCredential()});
45+
proofs = getProofs(securedCredential);
4946
});
5047
beforeEach(setupRow);
5148
const assertBefore = () => {
@@ -60,44 +57,91 @@ describe('ecdsa-rdfc-2019 - Algorithms - Transformation', function() {
6057
'(cryptosuite).',
6158
async function() {
6259
this.test.link = 'https://www.w3.org/TR/vc-di-ecdsa/#transformation-ecdsa-rdfc-2019';
63-
assertBefore();
64-
for(const proof of rdfc2019Proofs) {
65-
should.exist(proof.type, 'Expected a type identifier on ' +
66-
'the proof.');
67-
should.exist(proof.cryptosuite,
68-
'Expected a cryptosuite identifier on the proof.');
69-
}
60+
const proof = proofExists(proofs);
61+
should.exist(proof.type,
62+
'Expected a type identifier on the proof.');
63+
should.exist(proof.cryptosuite,
64+
'Expected a cryptosuite identifier on the proof.');
7065
});
7166
it('Whenever this algorithm encodes strings, ' +
7267
'it MUST use UTF-8 encoding.',
7368
async function() {
7469
this.test.link = 'https://www.w3.org/TR/vc-di-ecdsa/#transformation-ecdsa-rdfc-2019';
75-
assertBefore();
76-
for(const proof of rdfc2019Proofs) {
77-
should.exist(proof?.proofValue,
78-
'Expected proofValue to exist.');
79-
isValidUtf8(proof.proofValue).should.equal(
80-
true,
81-
'Expected proofValue value to be a valid UTF-8 encoded string.'
82-
);
83-
}
70+
const proof = proofExists(proofs);
71+
assertAllUtf8(proof);
8472
});
8573
it('If options.type is not set to the string DataIntegrityProof or ' +
8674
'options.cryptosuite is not set to the string ecdsa-rdfc-2019, ' +
8775
'an error MUST be raised and SHOULD convey an error type ' +
8876
'of PROOF_TRANSFORMATION_ERROR.',
8977
async function() {
9078
this.test.link = 'https://www.w3.org/TR/vc-di-ecdsa/#transformation-ecdsa-rdfc-2019';
91-
assertBefore();
92-
for(const proof of rdfc2019Proofs) {
93-
should.exist(proof.type,
94-
'Expected a type identifier on the proof.');
95-
should.exist(proof.cryptosuite,
96-
'Expected a cryptosuite identifier on the proof.');
97-
proof.type.should.equal('DataIntegrityProof',
98-
'Expected DataIntegrityProof type.');
99-
proof.cryptosuite.should.equal('ecdsa-rdfc-2019',
100-
'Expected ecdsa-rdfc-2019 cryptosuite.');
79+
const proof = proofExists(proofs);
80+
should.exist(proof.type,
81+
'Expected a type identifier on the proof.');
82+
should.exist(proof.cryptosuite,
83+
'Expected a cryptosuite identifier on the proof.');
84+
proof.type.should.equal('DataIntegrityProof',
85+
'Expected DataIntegrityProof type.');
86+
proof.cryptosuite.should.equal('ecdsa-rdfc-2019',
87+
'Expected ecdsa-rdfc-2019 cryptosuite.');
88+
});
89+
});
90+
}
91+
});
92+
93+
describe('Algorithms - Proof Configuration (ecdsa-rdfc-2019)', function() {
94+
setupReportableTestSuite(this);
95+
this.implemented = [...issuers.keys()];
96+
for(const [columnId, {endpoints}] of issuers) {
97+
describe(columnId, function() {
98+
const [issuer] = endpoints;
99+
let securedCredential;
100+
let proofs;
101+
before(async function() {
102+
securedCredential = await secureCredential(
103+
{issuer, vc: generateCredential()});
104+
proofs = getProofs(securedCredential);
105+
});
106+
beforeEach(setupRow);
107+
it('The proof options MUST contain a type identifier for the ' +
108+
'cryptographic suite (type) and MUST contain a cryptosuite ' +
109+
'identifier (cryptosuite).',
110+
async function() {
111+
this.test.link = 'https://www.w3.org/TR/vc-di-ecdsa/#proof-configuration-ecdsa-rdfc-2019';
112+
const proof = proofExists(proofs);
113+
should.exist(proof.type,
114+
'Expected a type identifier on the proof.');
115+
should.exist(proof.cryptosuite,
116+
'Expected a cryptosuite identifier on the proof.');
117+
});
118+
it('If proofConfig.type is not set to DataIntegrityProof ' +
119+
'and/or proofConfig.cryptosuite is not set to ecdsa-rdfc-2019, ' +
120+
'an error MUST be raised and SHOULD convey an error type ' +
121+
'of PROOF_GENERATION_ERROR.',
122+
async function() {
123+
this.test.link = 'https://www.w3.org/TR/vc-di-ecdsa/#proof-configuration-ecdsa-rdfc-2019';
124+
const proof = proofExists(proofs);
125+
should.exist(proof.type,
126+
'Expected a type identifier on the proof.');
127+
should.exist(proof.cryptosuite,
128+
'Expected a cryptosuite identifier on the proof.');
129+
proof.type.should.equal('DataIntegrityProof',
130+
'Expected DataIntegrityProof type.');
131+
proof.cryptosuite.should.equal('ecdsa-rdfc-2019',
132+
'Expected ecdsa-rdfc-2019 cryptosuite.');
133+
});
134+
it('If proofConfig.created is set and if the value is not a ' +
135+
'valid [XMLSCHEMA11-2] datetime, an error MUST be raised and ' +
136+
'SHOULD convey an error type of PROOF_GENERATION_ERROR.',
137+
async function() {
138+
this.test.link = 'https://www.w3.org/TR/vc-di-ecdsa/#proof-configuration-ecdsa-rdfc-2019';
139+
const proof = proofExists(proofs);
140+
if(proof?.created) {
141+
isValidDatetime(proof.created).should.equal(
142+
true,
143+
'Expected created value to be a valid datetime string.'
144+
);
101145
}
102146
});
103147
});
@@ -114,16 +158,12 @@ describe('ecdsa-rdfc-2019 - Algorithms - Proof Configuration', function() {
114158
for(const [columnId, {endpoints}] of issuers) {
115159
describe(columnId, function() {
116160
const [issuer] = endpoints;
117-
let issuedVc;
161+
let securedCredential;
118162
let proofs;
119-
let rdfc2019Proofs = [];
120163
before(async function() {
121-
issuedVc = await createInitialVc({issuer, vc: validCredential});
122-
proofs = getProofs(issuedVc);
123-
if(proofs?.length) {
124-
rdfc2019Proofs = proofs.filter(
125-
proof => proof?.cryptosuite === cryptosuite);
126-
}
164+
securedCredential = await secureCredential(
165+
{issuer, vc: generateCredential()});
166+
proofs = getProofs(securedCredential);
127167
});
128168
beforeEach(setupRow);
129169
const assertBefore = () => {
@@ -216,11 +256,9 @@ describe('ecdsa-rdfc-2019 - Algorithms - Proof Serialization', function() {
216256
'(cryptosuite).',
217257
async function() {
218258
this.test.link = 'https://www.w3.org/TR/vc-di-ecdsa/#proof-serialization-ecdsa-rdfc-2019';
219-
assertBefore();
220-
for(const proof of rdfc2019Proofs) {
221-
should.exist(proof.type,
222-
'Expected a type identifier on the proof.');
223-
}
259+
const proof = proofExists(proofs);
260+
should.exist(proof.type,
261+
'Expected a type identifier on the proof.');
224262
});
225263
});
226264
}

tests/helpers.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -288,14 +288,19 @@ export function setupRow() {
288288
};
289289
}
290290

291+
export function proofExists(proofs) {
292+
should.exist(proofs,
293+
'Expected credential to have a proof.');
294+
proofs.length.should.be.gte(1,
295+
'Expected credential to have at least one proof.');
296+
return proofs[0];
297+
}
298+
291299
export function assertSecuredCredential(securedCredential) {
292300
should.exist(securedCredential,
293301
'Expected issuer to have issued a credential.');
294302
const proofs = getProofs(securedCredential);
295-
should.exist(proofs,
296-
'Expected credential to have a proof.');
297-
proofs.length.should.equal(1,
298-
'Expected credential to have a single proof.');
303+
proofExists(proofs);
299304
}
300305

301306
export async function verifySuccess(verifier, securedCredential) {

0 commit comments

Comments
 (0)