Skip to content

Commit f4cacbf

Browse files
committed
Add parseDisclosureProofValue & Use DeriveStub.
1 parent ccccede commit f4cacbf

File tree

2 files changed

+86
-14
lines changed

2 files changed

+86
-14
lines changed

tests/suites/proxies.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
* Copyright 2024 Digital Bazaar, Inc.
33
* SPDX-License-Identifier: BSD-3-Clause
44
*/
5+
import {Token, Type} from 'cborg';
56
import crypto from 'node:crypto';
6-
import {stubDerive} from './stubs.js';
7+
import {DeriveStub} from './stubs.js';
78
/**
89
* Creates a proxy of an object with stubs.
910
*
@@ -154,7 +155,17 @@ async function _canonizeProof(proof, {
154155
}
155156

156157
export function invalidCborTagProxy(suite) {
157-
const stubs = {derive: stubDerive};
158+
const typeEncoders = {
159+
Uint8Array(uint8Array) {
160+
return [
161+
new Token(Type.tag, 2),
162+
new Token(Type.bytes, uint8Array.map(b => b + 1))
163+
];
164+
}
165+
};
166+
const stubs = {derive({...args}) {
167+
return new DeriveStub({typeEncoders}).derive({...args});
168+
}};
158169
if(suite._cryptosuite) {
159170
suite._cryptosuite = createProxy({
160171
original: suite._cryptosuite,

tests/suites/stubs.js

Lines changed: 73 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import {
1212
selectJsonLd,
1313
stripBlankNodePrefixes
1414
} from '@digitalbazaar/di-sd-primitives';
15-
import {Token, Type} from 'cborg';
1615

1716
const CBOR_PREFIX_BASE = new Uint8Array([0xd9, 0x5d, 0x00]);
1817
const CBOR_PREFIX_DERIVED = new Uint8Array([0xd9, 0x5d, 0x01]);
@@ -21,6 +20,38 @@ const CBOR_PREFIX_DERIVED = new Uint8Array([0xd9, 0x5d, 0x01]);
2120
const TAGS = [];
2221
TAGS[64] = bytes => bytes;
2322

23+
export class DeriveStub {
24+
constructor({typeEncoders}) {
25+
this.typeEncoders = typeEncoders;
26+
}
27+
async derive({
28+
cryptosuite, document, proofSet,
29+
documentLoader, dataIntegrityProof
30+
}) {
31+
// get test specific options
32+
const {typeEncoders} = this;
33+
// find matching base `proof` in `proofSet`
34+
const {options: {proofId}} = cryptosuite;
35+
const baseProof = await _findProof({proofId, proofSet, dataIntegrityProof});
36+
// generate data for disclosure
37+
const {
38+
baseSignature, publicKey, signatures,
39+
labelMap, mandatoryIndexes, revealDoc
40+
} = await _createDisclosureData(
41+
{cryptosuite, document, proof: baseProof, documentLoader});
42+
43+
// create new disclosure proof
44+
const newProof = {...baseProof};
45+
newProof.proofValue = await serializeDisclosureProofValue({
46+
baseSignature, publicKey, signatures,
47+
labelMap, mandatoryIndexes, typeEncoders
48+
});
49+
// attach proof to reveal doc w/o context
50+
delete newProof['@context'];
51+
revealDoc.proof = newProof;
52+
return revealDoc;
53+
}
54+
}
2455
// Stubs the ecdsa-sd-2023 derive function
2556
export async function stubDerive({
2657
cryptosuite, document, proofSet,
@@ -37,7 +68,7 @@ export async function stubDerive({
3768

3869
// create new disclosure proof
3970
const newProof = {...baseProof};
40-
newProof.proofValue = await invalidSerializeDisclosureProofValue(
71+
newProof.proofValue = await serializeDisclosureProofValue(
4172
{baseSignature, publicKey, signatures, labelMap, mandatoryIndexes});
4273

4374
// attach proof to reveal doc w/o context
@@ -47,17 +78,10 @@ export async function stubDerive({
4778
}
4879

4980
// ecdsa-sd-2023 method that uses invalid cbor tags
50-
function invalidSerializeDisclosureProofValue({
51-
baseSignature, publicKey, signatures, labelMap, mandatoryIndexes
81+
function serializeDisclosureProofValue({
82+
baseSignature, publicKey, signatures,
83+
labelMap, mandatoryIndexes, typeEncoders
5284
} = {}) {
53-
const typeEncoders = {
54-
Uint8Array(uint8Array) {
55-
return [
56-
new Token(Type.tag, 2),
57-
new Token(Type.bytes, uint8Array.map(b => b + 1))
58-
];
59-
}
60-
};
6185
// encode as multibase (base64url no pad) CBOR
6286
const payload = [
6387
// Uint8Array
@@ -241,3 +265,40 @@ async function _findProof({proofId, proofSet, dataIntegrityProof}) {
241265
}
242266
return proof;
243267
}
268+
269+
// ecdsa-sd-2023 proofValue
270+
export function parseDisclosureProofValue({proof} = {}) {
271+
try {
272+
// decode from base64url
273+
const proofValue = base64url.decode(proof.proofValue.slice(1));
274+
275+
const payload = proofValue.subarray(CBOR_PREFIX_DERIVED.length);
276+
const [
277+
baseSignature,
278+
publicKey,
279+
signatures,
280+
compressedLabelMap,
281+
mandatoryIndexes
282+
] = cborg.decode(payload, {useMaps: true, tags: TAGS});
283+
284+
const labelMap = _decompressLabelMap(compressedLabelMap);
285+
const params = {
286+
baseSignature, publicKey, signatures, labelMap, mandatoryIndexes
287+
};
288+
return params;
289+
} catch(e) {
290+
const err = new TypeError(
291+
'The proof does not include a valid "proofValue" property.');
292+
err.cause = e;
293+
throw err;
294+
}
295+
}
296+
// ecdsa-sd-2023 proofValue
297+
function _decompressLabelMap(compressedLabelMap) {
298+
const map = new Map();
299+
for(const [k, v] of compressedLabelMap.entries()) {
300+
map.set(`c14n${k}`, `u${base64url.encode(v)}`);
301+
}
302+
return map;
303+
}
304+

0 commit comments

Comments
 (0)