Skip to content

Commit 74c5031

Browse files
committed
Add ecdsa-sd-2023 serialize disclosure function into proxies.
1 parent be58ca6 commit 74c5031

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

tests/suites/proxies.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,22 @@
22
* Copyright 2024 Digital Bazaar, Inc.
33
* SPDX-License-Identifier: BSD-3-Clause
44
*/
5+
import * as base64url from 'base64url-universal';
6+
import * as cborg from 'cborg';
57
import crypto from 'node:crypto';
68

9+
/**
10+
* Creates a proxy of an object with stubs.
11+
*
12+
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy
13+
*
14+
* @param {object} options - Options to use.
15+
* @param {object} options.original - The original object.
16+
* @param {object} options.stubs - Stubs to replace the original objects
17+
* properties and methods.
18+
*
19+
* @returns {Proxy<object>} Returns a Proxy.
20+
*/
721
export function createProxy({original, stubs}) {
822
if(typeof original === 'object') {
923
throw new Error(`Expected parameter original to be an object received ` +
@@ -111,17 +125,20 @@ export function invalidHashProxy({
111125
return suite;
112126
}
113127

128+
// concat 2 unit8Arrays together
114129
function _concat(b1, b2) {
115130
const rval = new Uint8Array(b1.length + b2.length);
116131
rval.set(b1, 0);
117132
rval.set(b2, b1.length);
118133
return rval;
119134
}
120135

136+
// sha hashing function
121137
export async function sha({algorithm, string}) {
122138
return new Uint8Array(crypto.createHash(algorithm).update(string).digest());
123139
}
124140

141+
// ecdsa-rdfc-2019 _canonizeProof method
125142
async function _canonizeProof(proof, {
126143
document, cryptosuite, dataIntegrityProof, c14nOptions
127144
}) {
@@ -137,3 +154,44 @@ async function _canonizeProof(proof, {
137154
return cryptosuite.canonize(proof, c14nOptions);
138155
}
139156

157+
// ecdsa-sd-2023 method that uses invalid cbor tags
158+
export function serializeDisclosureProofValue({
159+
baseSignature, publicKey, signatures, labelMap, mandatoryIndexes
160+
} = {}) {
161+
const CBOR_PREFIX_DERIVED = new Uint8Array([0xd9, 0x5d, 0x01]);
162+
// encode as multibase (base64url no pad) CBOR
163+
const payload = [
164+
// Uint8Array
165+
baseSignature,
166+
// Uint8Array
167+
publicKey,
168+
// array of Uint8Arrays
169+
signatures,
170+
// Map of strings => strings compressed to ints => Uint8Arrays
171+
_compressLabelMap(labelMap),
172+
// array of numbers
173+
mandatoryIndexes
174+
];
175+
const cbor = _concatBuffers([
176+
CBOR_PREFIX_DERIVED, cborg.encode(payload, {useMaps: true})
177+
]);
178+
return `u${base64url.encode(cbor)}`;
179+
}
180+
181+
function _concatBuffers(buffers) {
182+
const bytes = new Uint8Array(buffers.reduce((acc, b) => acc + b.length, 0));
183+
let offset = 0;
184+
for(const b of buffers) {
185+
bytes.set(b, offset);
186+
offset += b.length;
187+
}
188+
return bytes;
189+
}
190+
191+
function _compressLabelMap(labelMap) {
192+
const map = new Map();
193+
for(const [k, v] of labelMap.entries()) {
194+
map.set(parseInt(k.slice(4), 10), base64url.decode(v.slice(1)));
195+
}
196+
return map;
197+
}

0 commit comments

Comments
 (0)