Skip to content

Commit be58ca6

Browse files
committed
Move proxies to separate file.
1 parent 3c13c49 commit be58ca6

File tree

2 files changed

+140
-110
lines changed

2 files changed

+140
-110
lines changed

tests/suites/algorithms.js

Lines changed: 1 addition & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
generators,
88
issueCloned
99
} from 'data-integrity-test-suite-assertion';
10-
import crypto from 'node:crypto';
10+
import {invalidHashProxy, unsafeProxy} from './proxies.js';
1111
import {getMultiKey} from '../vc-generator/key-gen.js';
1212
import {getSuites} from './helpers.js';
1313

@@ -273,35 +273,6 @@ function _generateNoTypeCryptosuite({
273273
return invalidCryptosuite({...noType, cryptosuiteName: ''});
274274
}
275275

276-
function unsafeProxy(suite) {
277-
if(typeof suite !== 'object') {
278-
return suite;
279-
}
280-
// if the suite has a cryptosuite object proxy it
281-
if(suite._cryptosuite) {
282-
suite._cryptosuite = new Proxy(suite._cryptosuite, {
283-
get(target, prop) {
284-
if(prop === 'canonize') {
285-
return function(doc, options) {
286-
return target.canonize(doc, {...options, safe: false});
287-
};
288-
}
289-
return Reflect.get(...arguments);
290-
}
291-
});
292-
}
293-
return new Proxy(suite, {
294-
get(target, prop) {
295-
if(prop === 'canonize') {
296-
return function(doc, options) {
297-
return target.canonize(doc, {...options, safe: false});
298-
};
299-
}
300-
return Reflect.get(...arguments);
301-
}
302-
});
303-
}
304-
305276
async function _commonSetup({
306277
credential,
307278
mandatoryPointers,
@@ -331,83 +302,3 @@ async function _commonSetup({
331302
}));
332303
return credentials;
333304
}
334-
335-
function invalidHashProxy({
336-
suiteName,
337-
keyType,
338-
suite,
339-
}) {
340-
if(typeof suite !== 'object') {
341-
return suite;
342-
}
343-
if(suite._cryptosuite) {
344-
if(suiteName !== 'ecdsa-rdfc-2019') {
345-
throw new Error(`Unsupported suite ${suiteName}`);
346-
}
347-
suite._cryptosuite = new Proxy(suite._cryptosuite, {
348-
get(target, prop) {
349-
if(prop === 'createVerifyData') {
350-
return async function({
351-
cryptosuite, document, proof,
352-
documentLoader, dataIntegrityProof
353-
} = {}) {
354-
// this switch the hash to the wrong hash for that keyType
355-
const algorithm = (keyType === 'P-256') ? 'sha384' : 'sha256';
356-
const c14nOptions = {
357-
documentLoader,
358-
safe: true,
359-
base: null,
360-
skipExpansion: false,
361-
messageDigestAlgorithm: algorithm
362-
};
363-
364-
// await both c14n proof hash and c14n document hash
365-
const [proofHash, docHash] = await Promise.all([
366-
// canonize and hash proof
367-
_canonizeProof(proof, {
368-
document, cryptosuite, dataIntegrityProof, c14nOptions
369-
}).then(c14nProofOptions => sha({
370-
algorithm,
371-
string: c14nProofOptions
372-
})),
373-
// canonize and hash document
374-
cryptosuite.canonize(document, c14nOptions).then(
375-
c14nDocument => sha({algorithm, string: c14nDocument}))
376-
]);
377-
// concatenate hash of c14n proof options and hash of c14n document
378-
return _concat(proofHash, docHash);
379-
};
380-
}
381-
return Reflect.get(...arguments);
382-
}
383-
});
384-
}
385-
return suite;
386-
}
387-
388-
function _concat(b1, b2) {
389-
const rval = new Uint8Array(b1.length + b2.length);
390-
rval.set(b1, 0);
391-
rval.set(b2, b1.length);
392-
return rval;
393-
}
394-
395-
export async function sha({algorithm, string}) {
396-
return new Uint8Array(crypto.createHash(algorithm).update(string).digest());
397-
}
398-
399-
async function _canonizeProof(proof, {
400-
document, cryptosuite, dataIntegrityProof, c14nOptions
401-
}) {
402-
// `proofValue` must not be included in the proof options
403-
proof = {
404-
'@context': document['@context'],
405-
...proof
406-
};
407-
dataIntegrityProof.ensureSuiteContext({
408-
document: proof, addSuiteContext: true
409-
});
410-
delete proof.proofValue;
411-
return cryptosuite.canonize(proof, c14nOptions);
412-
}
413-

tests/suites/proxies.js

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/*!
2+
* Copyright 2024 Digital Bazaar, Inc.
3+
* SPDX-License-Identifier: BSD-3-Clause
4+
*/
5+
import crypto from 'node:crypto';
6+
7+
export function createProxy({original, stubs}) {
8+
if(typeof original === 'object') {
9+
throw new Error(`Expected parameter original to be an object received ` +
10+
`${typeof original}`);
11+
}
12+
return new Proxy(original, {
13+
get(target, prop) {
14+
if(stubs[prop]) {
15+
return stubs[prop];
16+
}
17+
return Reflect.get(...arguments);
18+
}
19+
});
20+
}
21+
22+
/**
23+
* The major jsonld api suites use is canonize.
24+
* This function intercepts calls on canonize and
25+
* pass safe: false allowing for invalid jsonld to
26+
* be issued.
27+
*
28+
* @param {object} suite - A DataIntegrityProof.
29+
*
30+
* @returns {Proxy<object>} Returns a proxy of the proof.
31+
*/
32+
export function unsafeProxy(suite) {
33+
if(typeof suite !== 'object') {
34+
return suite;
35+
}
36+
// if the suite has a cryptosuite object proxy it
37+
if(suite._cryptosuite) {
38+
suite._cryptosuite = new Proxy(suite._cryptosuite, {
39+
get(target, prop) {
40+
if(prop === 'canonize') {
41+
return function(doc, options) {
42+
return target.canonize(doc, {...options, safe: false});
43+
};
44+
}
45+
return Reflect.get(...arguments);
46+
}
47+
});
48+
}
49+
return new Proxy(suite, {
50+
get(target, prop) {
51+
if(prop === 'canonize') {
52+
return function(doc, options) {
53+
return target.canonize(doc, {...options, safe: false});
54+
};
55+
}
56+
return Reflect.get(...arguments);
57+
}
58+
});
59+
}
60+
61+
export function invalidHashProxy({
62+
suiteName,
63+
keyType,
64+
suite,
65+
}) {
66+
if(typeof suite !== 'object') {
67+
return suite;
68+
}
69+
if(suite._cryptosuite) {
70+
if(suiteName !== 'ecdsa-rdfc-2019') {
71+
throw new Error(`Unsupported suite ${suiteName}`);
72+
}
73+
suite._cryptosuite = new Proxy(suite._cryptosuite, {
74+
get(target, prop) {
75+
if(prop === 'createVerifyData') {
76+
return async function({
77+
cryptosuite, document, proof,
78+
documentLoader, dataIntegrityProof
79+
} = {}) {
80+
// this switch the hash to the wrong hash for that keyType
81+
const algorithm = (keyType === 'P-256') ? 'sha384' : 'sha256';
82+
const c14nOptions = {
83+
documentLoader,
84+
safe: true,
85+
base: null,
86+
skipExpansion: false,
87+
messageDigestAlgorithm: algorithm
88+
};
89+
90+
// await both c14n proof hash and c14n document hash
91+
const [proofHash, docHash] = await Promise.all([
92+
// canonize and hash proof
93+
_canonizeProof(proof, {
94+
document, cryptosuite, dataIntegrityProof, c14nOptions
95+
}).then(c14nProofOptions => sha({
96+
algorithm,
97+
string: c14nProofOptions
98+
})),
99+
// canonize and hash document
100+
cryptosuite.canonize(document, c14nOptions).then(
101+
c14nDocument => sha({algorithm, string: c14nDocument}))
102+
]);
103+
// concatenate hash of c14n proof options and hash of c14n document
104+
return _concat(proofHash, docHash);
105+
};
106+
}
107+
return Reflect.get(...arguments);
108+
}
109+
});
110+
}
111+
return suite;
112+
}
113+
114+
function _concat(b1, b2) {
115+
const rval = new Uint8Array(b1.length + b2.length);
116+
rval.set(b1, 0);
117+
rval.set(b2, b1.length);
118+
return rval;
119+
}
120+
121+
export async function sha({algorithm, string}) {
122+
return new Uint8Array(crypto.createHash(algorithm).update(string).digest());
123+
}
124+
125+
async function _canonizeProof(proof, {
126+
document, cryptosuite, dataIntegrityProof, c14nOptions
127+
}) {
128+
// `proofValue` must not be included in the proof options
129+
proof = {
130+
'@context': document['@context'],
131+
...proof
132+
};
133+
dataIntegrityProof.ensureSuiteContext({
134+
document: proof, addSuiteContext: true
135+
});
136+
delete proof.proofValue;
137+
return cryptosuite.canonize(proof, c14nOptions);
138+
}
139+

0 commit comments

Comments
 (0)