Skip to content

Commit 41b3c56

Browse files
authored
Merge pull request #38 from iden3/feature/bump-js-merkle-tree
upgrade js-merkletree
2 parents e1f4193 + f5751d6 commit 41b3c56

File tree

5 files changed

+91
-36
lines changed

5 files changed

+91
-36
lines changed

package-lock.json

+6-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@iden3/js-jsonld-merklization",
3-
"version": "1.2.0",
3+
"version": "1.1.0",
44
"description": "json ld merklization library",
55
"main": "./dist/node/cjs/index.js",
66
"module": "./dist/node/esm/index.js",
@@ -67,7 +67,7 @@
6767
},
6868
"peerDependencies": {
6969
"@iden3/js-crypto": "1.0.2",
70-
"@iden3/js-merkletree": "1.0.2"
70+
"@iden3/js-merkletree": "1.1.0"
7171
},
7272
"dependencies": {
7373
"@js-temporal/polyfill": "^0.4.4",

tests/cache.ts

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { RemoteDocument } from 'jsonld/jsonld-spec';
2+
import { getDocumentLoader } from '../src/lib/options';
3+
import { Options } from '../src/lib/types/types';
4+
import { W3C_CREDENTIAL_2018, W3C_VC_SCHEMA } from './data';
5+
import { DocumentLoader } from '../src/loaders/jsonld-loader';
6+
7+
const cache = new Map<string, RemoteDocument>();
8+
cache.set(W3C_CREDENTIAL_2018, {
9+
document: W3C_VC_SCHEMA,
10+
documentUrl: W3C_CREDENTIAL_2018
11+
});
12+
export const cacheLoader = (opts?: Options): DocumentLoader => {
13+
return async (url): Promise<RemoteDocument> => {
14+
let remoteDoc = cache.get(url);
15+
if (remoteDoc) {
16+
return remoteDoc;
17+
}
18+
remoteDoc = await getDocumentLoader(opts)(url);
19+
cache.set(url, remoteDoc);
20+
return remoteDoc;
21+
};
22+
};

tests/data.ts

+6
Original file line numberDiff line numberDiff line change
@@ -776,3 +776,9 @@ export const arr_test = `{
776776
{"code":2 , "name":"def"}
777777
]
778778
}`;
779+
780+
export const W3C_CREDENTIAL_2018 = 'https://www.w3.org/2018/credentials/v1';
781+
782+
export const W3C_VC_SCHEMA = JSON.parse(
783+
`{"@context":{"@version":1.1,"@protected":true,"id":"@id","type":"@type","VerifiableCredential":{"@id":"https://www.w3.org/2018/credentials#VerifiableCredential","@context":{"@version":1.1,"@protected":true,"id":"@id","type":"@type","cred":"https://www.w3.org/2018/credentials#","sec":"https://w3id.org/security#","xsd":"http://www.w3.org/2001/XMLSchema#","credentialSchema":{"@id":"cred:credentialSchema","@type":"@id","@context":{"@version":1.1,"@protected":true,"id":"@id","type":"@type","cred":"https://www.w3.org/2018/credentials#","JsonSchemaValidator2018":"cred:JsonSchemaValidator2018"}},"credentialStatus":{"@id":"cred:credentialStatus","@type":"@id"},"credentialSubject":{"@id":"cred:credentialSubject","@type":"@id"},"evidence":{"@id":"cred:evidence","@type":"@id"},"expirationDate":{"@id":"cred:expirationDate","@type":"xsd:dateTime"},"holder":{"@id":"cred:holder","@type":"@id"},"issued":{"@id":"cred:issued","@type":"xsd:dateTime"},"issuer":{"@id":"cred:issuer","@type":"@id"},"issuanceDate":{"@id":"cred:issuanceDate","@type":"xsd:dateTime"},"proof":{"@id":"sec:proof","@type":"@id","@container":"@graph"},"refreshService":{"@id":"cred:refreshService","@type":"@id","@context":{"@version":1.1,"@protected":true,"id":"@id","type":"@type","cred":"https://www.w3.org/2018/credentials#","ManualRefreshService2018":"cred:ManualRefreshService2018"}},"termsOfUse":{"@id":"cred:termsOfUse","@type":"@id"},"validFrom":{"@id":"cred:validFrom","@type":"xsd:dateTime"},"validUntil":{"@id":"cred:validUntil","@type":"xsd:dateTime"}}},"VerifiablePresentation":{"@id":"https://www.w3.org/2018/credentials#VerifiablePresentation","@context":{"@version":1.1,"@protected":true,"id":"@id","type":"@type","cred":"https://www.w3.org/2018/credentials#","sec":"https://w3id.org/security#","holder":{"@id":"cred:holder","@type":"@id"},"proof":{"@id":"sec:proof","@type":"@id","@container":"@graph"},"verifiableCredential":{"@id":"cred:verifiableCredential","@type":"@id","@container":"@graph"}}},"EcdsaSecp256k1Signature2019":{"@id":"https://w3id.org/security#EcdsaSecp256k1Signature2019","@context":{"@version":1.1,"@protected":true,"id":"@id","type":"@type","sec":"https://w3id.org/security#","xsd":"http://www.w3.org/2001/XMLSchema#","challenge":"sec:challenge","created":{"@id":"http://purl.org/dc/terms/created","@type":"xsd:dateTime"},"domain":"sec:domain","expires":{"@id":"sec:expiration","@type":"xsd:dateTime"},"jws":"sec:jws","nonce":"sec:nonce","proofPurpose":{"@id":"sec:proofPurpose","@type":"@vocab","@context":{"@version":1.1,"@protected":true,"id":"@id","type":"@type","sec":"https://w3id.org/security#","assertionMethod":{"@id":"sec:assertionMethod","@type":"@id","@container":"@set"},"authentication":{"@id":"sec:authenticationMethod","@type":"@id","@container":"@set"}}},"proofValue":"sec:proofValue","verificationMethod":{"@id":"sec:verificationMethod","@type":"@id"}}},"EcdsaSecp256r1Signature2019":{"@id":"https://w3id.org/security#EcdsaSecp256r1Signature2019","@context":{"@version":1.1,"@protected":true,"id":"@id","type":"@type","sec":"https://w3id.org/security#","xsd":"http://www.w3.org/2001/XMLSchema#","challenge":"sec:challenge","created":{"@id":"http://purl.org/dc/terms/created","@type":"xsd:dateTime"},"domain":"sec:domain","expires":{"@id":"sec:expiration","@type":"xsd:dateTime"},"jws":"sec:jws","nonce":"sec:nonce","proofPurpose":{"@id":"sec:proofPurpose","@type":"@vocab","@context":{"@version":1.1,"@protected":true,"id":"@id","type":"@type","sec":"https://w3id.org/security#","assertionMethod":{"@id":"sec:assertionMethod","@type":"@id","@container":"@set"},"authentication":{"@id":"sec:authenticationMethod","@type":"@id","@container":"@set"}}},"proofValue":"sec:proofValue","verificationMethod":{"@id":"sec:verificationMethod","@type":"@id"}}},"Ed25519Signature2018":{"@id":"https://w3id.org/security#Ed25519Signature2018","@context":{"@version":1.1,"@protected":true,"id":"@id","type":"@type","sec":"https://w3id.org/security#","xsd":"http://www.w3.org/2001/XMLSchema#","challenge":"sec:challenge","created":{"@id":"http://purl.org/dc/terms/created","@type":"xsd:dateTime"},"domain":"sec:domain","expires":{"@id":"sec:expiration","@type":"xsd:dateTime"},"jws":"sec:jws","nonce":"sec:nonce","proofPurpose":{"@id":"sec:proofPurpose","@type":"@vocab","@context":{"@version":1.1,"@protected":true,"id":"@id","type":"@type","sec":"https://w3id.org/security#","assertionMethod":{"@id":"sec:assertionMethod","@type":"@id","@container":"@set"},"authentication":{"@id":"sec:authenticationMethod","@type":"@id","@container":"@set"}}},"proofValue":"sec:proofValue","verificationMethod":{"@id":"sec:verificationMethod","@type":"@id"}}},"RsaSignature2018":{"@id":"https://w3id.org/security#RsaSignature2018","@context":{"@version":1.1,"@protected":true,"challenge":"sec:challenge","created":{"@id":"http://purl.org/dc/terms/created","@type":"xsd:dateTime"},"domain":"sec:domain","expires":{"@id":"sec:expiration","@type":"xsd:dateTime"},"jws":"sec:jws","nonce":"sec:nonce","proofPurpose":{"@id":"sec:proofPurpose","@type":"@vocab","@context":{"@version":1.1,"@protected":true,"id":"@id","type":"@type","sec":"https://w3id.org/security#","assertionMethod":{"@id":"sec:assertionMethod","@type":"@id","@container":"@set"},"authentication":{"@id":"sec:authenticationMethod","@type":"@id","@container":"@set"}}},"proofValue":"sec:proofValue","verificationMethod":{"@id":"sec:verificationMethod","@type":"@id"}}},"proof":{"@id":"https://w3id.org/security#proof","@type":"@id","@container":"@graph"}}}`
784+
);

tests/merklization.test.ts

+55-28
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@ import { TestHasher } from './hasher';
2828
import { poseidon } from '@iden3/js-crypto';
2929
import { normalizeIPFSNodeURL } from '../src/loaders/jsonld-loader';
3030
import customSchemaJSON from './testdata/custom-schema.json';
31+
import { cacheLoader } from './cache';
3132

3233
describe('tests merkelization', () => {
3334
it('multigraph TestEntriesFromRDF', async () => {
34-
const dataset = await RDFDataset.fromDocument(JSON.parse(multigraphDoc2));
35+
const dataset = await RDFDataset.fromDocument(JSON.parse(multigraphDoc2), cacheLoader());
3536

3637
const entries = await RDFEntry.fromDataSet(dataset, DEFAULT_HASHER);
3738

@@ -90,7 +91,7 @@ describe('tests merkelization', () => {
9091
});
9192

9293
it('TestEntriesFromRDF', async () => {
93-
const dataSet = await RDFDataset.fromDocument(JSON.parse(testDocument));
94+
const dataSet = await RDFDataset.fromDocument(JSON.parse(testDocument), cacheLoader());
9495
const entries = await RDFEntry.fromDataSet(dataSet, DEFAULT_HASHER);
9596
const wantEntries = [
9697
new RDFEntry(
@@ -371,7 +372,7 @@ describe('tests merkelization', () => {
371372
});
372373

373374
it('test proof', async () => {
374-
const dataSet = await RDFDataset.fromDocument(JSON.parse(testDocument));
375+
const dataSet = await RDFDataset.fromDocument(JSON.parse(testDocument), cacheLoader());
375376
const entries = await RDFEntry.fromDataSet(dataSet, DEFAULT_HASHER);
376377

377378
const mt = new Merkletree(new InMemoryDB(str2Bytes('')), true, 40);
@@ -401,7 +402,7 @@ describe('tests merkelization', () => {
401402
});
402403

403404
it('TestProofInteger', async () => {
404-
const dataSet = await RDFDataset.fromDocument(JSON.parse(testDocument));
405+
const dataSet = await RDFDataset.fromDocument(JSON.parse(testDocument), cacheLoader());
405406
const entries = await RDFEntry.fromDataSet(dataSet, DEFAULT_HASHER);
406407
const mt = new Merkletree(new InMemoryDB(str2Bytes('')), true, 40);
407408

@@ -446,7 +447,7 @@ describe('tests merkelization', () => {
446447

447448
describe('TestMerklizer_Proof', () => {
448449
it('test Merklizer with path as a Path', async () => {
449-
const mz = await Merklizer.merklizeJSONLD(testDocument);
450+
const mz = await Merklizer.merklizeJSONLD(testDocument, { documentLoader: cacheLoader() });
450451
const path = new Path([
451452
'https://www.w3.org/2018/credentials#credentialSubject',
452453
1,
@@ -475,8 +476,10 @@ describe('tests merkelization', () => {
475476
});
476477

477478
it('test Merklizer with path as shortcut string', async () => {
478-
const mz = await Merklizer.merklizeJSONLD(testDocument);
479-
const path = await mz.resolveDocPath('credentialSubject.1.birthCountry');
479+
const mz = await Merklizer.merklizeJSONLD(testDocument, { documentLoader: cacheLoader() });
480+
const path = await mz.resolveDocPath('credentialSubject.1.birthCountry', {
481+
documentLoader: cacheLoader()
482+
});
480483

481484
const { proof, value } = await mz.proof(path);
482485

@@ -587,8 +590,10 @@ describe('tests merkelization', () => {
587590
});
588591

589592
it('TestExistenceProof', async () => {
590-
const mz = await Merklizer.merklizeJSONLD(doc1);
591-
const path = await mz.resolveDocPath('credentialSubject.birthday');
593+
const mz = await Merklizer.merklizeJSONLD(doc1, { documentLoader: cacheLoader() });
594+
const path = await mz.resolveDocPath('credentialSubject.birthday', {
595+
documentLoader: cacheLoader()
596+
});
592597

593598
const wantPath = new Path([
594599
'https://www.w3.org/2018/credentials#credentialSubject',
@@ -605,7 +610,7 @@ describe('tests merkelization', () => {
605610
});
606611

607612
it('TestArrayMerklization', async () => {
608-
const mz = await Merklizer.merklizeJSONLD(arr_test);
613+
const mz = await Merklizer.merklizeJSONLD(arr_test, { documentLoader: cacheLoader() });
609614

610615
const expectedPath = new Path([
611616
'uuid:urn:87caf7a2-fee3-11ed-be56-0242ac120002#countries',
@@ -634,7 +639,9 @@ describe('tests merkelization', () => {
634639

635640
it('TestPathFromDocument', async () => {
636641
const inp = 'credentialSubject.1.birthDate';
637-
const result = await Path.fromDocument(null, testDocument, inp);
642+
const result = await Path.fromDocument(null, testDocument, inp, {
643+
documentLoader: cacheLoader()
644+
});
638645

639646
const want = new Path([
640647
'https://www.w3.org/2018/credentials#credentialSubject',
@@ -647,7 +654,9 @@ describe('tests merkelization', () => {
647654

648655
it('TestPathFromDocument - path to nested field', async () => {
649656
const inp = 'objectField.customNestedField';
650-
const result = await Path.fromDocument(null, nestedFieldDocument, inp);
657+
const result = await Path.fromDocument(null, nestedFieldDocument, inp, {
658+
documentLoader: cacheLoader()
659+
});
651660

652661
const want = new Path([
653662
'urn:uuid:87caf7a2-fee3-11ed-be56-0242ac120001#objectField',
@@ -658,9 +667,11 @@ describe('tests merkelization', () => {
658667
});
659668

660669
it('TestMerklizer_RawValue', async () => {
661-
const mz = await Merklizer.merklizeJSONLD(multigraphDoc);
670+
const mz = await Merklizer.merklizeJSONLD(multigraphDoc, { documentLoader: cacheLoader() });
662671

663-
const path = await Path.fromDocument(null, multigraphDoc, 'verifiableCredential.birthday');
672+
const path = await Path.fromDocument(null, multigraphDoc, 'verifiableCredential.birthday', {
673+
documentLoader: cacheLoader()
674+
});
664675

665676
const val = await mz.rawValue(path);
666677
expect(val).toEqual(19960425);
@@ -1003,9 +1014,12 @@ describe('tests merkelization', () => {
10031014
}
10041015
}`;
10051016

1006-
const mz = await Merklizer.merklizeJSONLD(testPresentationDoc, { hasher: new TestHasher() });
1017+
const mz = await Merklizer.merklizeJSONLD(testPresentationDoc, {
1018+
hasher: new TestHasher(),
1019+
documentLoader: cacheLoader()
1020+
});
10071021

1008-
const p = await mz.resolveDocPath('credentialSubject');
1022+
const p = await mz.resolveDocPath('credentialSubject', { documentLoader: cacheLoader() });
10091023

10101024
const { proof, value } = await mz.proof(p);
10111025

@@ -1036,7 +1050,9 @@ describe('tests merkelization', () => {
10361050
}
10371051
};
10381052

1039-
const mz = await Merklizer.merklizeJSONLD(JSON.stringify(testPresentationDoc));
1053+
const mz = await Merklizer.merklizeJSONLD(JSON.stringify(testPresentationDoc), {
1054+
documentLoader: cacheLoader()
1055+
});
10401056

10411057
const testPresentationDoc2 = {
10421058
id: 'uuid:presentation:12312',
@@ -1054,12 +1070,14 @@ describe('tests merkelization', () => {
10541070
}
10551071
};
10561072

1057-
const mz2 = await Merklizer.merklizeJSONLD(JSON.stringify(testPresentationDoc2));
1073+
const mz2 = await Merklizer.merklizeJSONLD(JSON.stringify(testPresentationDoc2), {
1074+
documentLoader: cacheLoader()
1075+
});
10581076
expect((await mz.root()).toString()).toEqual((await mz2.root()).toString());
10591077
});
10601078

10611079
it('xsd:dateTime', async () => {
1062-
const mz = await Merklizer.merklizeJSONLD(testDocument);
1080+
const mz = await Merklizer.merklizeJSONLD(testDocument, { documentLoader: cacheLoader() });
10631081

10641082
const path = new Path([
10651083
'https://www.w3.org/2018/credentials#credentialSubject',
@@ -1072,7 +1090,7 @@ describe('tests merkelization', () => {
10721090
});
10731091

10741092
it('empty datatype', async () => {
1075-
const mz = await Merklizer.merklizeJSONLD(testDocument);
1093+
const mz = await Merklizer.merklizeJSONLD(testDocument, { documentLoader: cacheLoader() });
10761094

10771095
const path = new Path([
10781096
'https://www.w3.org/2018/credentials#credentialSubject',
@@ -1115,7 +1133,7 @@ describe('tests merkelization', () => {
11151133
];
11161134

11171135
for (const tc of testCases) {
1118-
const result = await Merklizer.merklizeJSONLD(tc.doc);
1136+
const result = await Merklizer.merklizeJSONLD(tc.doc, { documentLoader: cacheLoader() });
11191137
const root = await result.root();
11201138
expect(root.bigInt().toString()).toEqual(tc.wantRoot.toString());
11211139
}
@@ -1135,18 +1153,25 @@ describe('merklize document with ipfs context', () => {
11351153
});
11361154

11371155
it('ipfsNodeURL is set', async () => {
1138-
const mz: Merklizer = await Merklizer.merklizeJSONLD(ipfsDocument, {
1139-
ipfsNodeURL
1140-
});
1156+
const opts = {
1157+
documentLoader: cacheLoader({
1158+
ipfsNodeURL
1159+
})
1160+
};
1161+
1162+
const mz: Merklizer = await Merklizer.merklizeJSONLD(ipfsDocument, opts);
11411163
expect((await mz.root()).bigInt().toString()).toEqual(
11421164
'19309047812100087948241250053335720576191969395309912987389452441269932261840'
11431165
);
11441166
});
11451167

11461168
it('ipfsGatewayURL is set', async () => {
1147-
const mz: Merklizer = await Merklizer.merklizeJSONLD(ipfsDocument, {
1148-
ipfsNodeURL
1149-
});
1169+
const opts = {
1170+
documentLoader: cacheLoader({
1171+
ipfsNodeURL
1172+
})
1173+
};
1174+
const mz: Merklizer = await Merklizer.merklizeJSONLD(ipfsDocument, opts);
11501175
expect((await mz.root()).bigInt().toString()).toEqual(
11511176
'19309047812100087948241250053335720576191969395309912987389452441269932261840'
11521177
);
@@ -1160,7 +1185,9 @@ describe('merklize document with ipfs context', () => {
11601185

11611186
it('TestExistenceProofIPFS', async () => {
11621187
const opts = {
1163-
ipfsGatewayURL: 'https://ipfs.io'
1188+
documentLoader: cacheLoader({
1189+
ipfsGatewayURL: 'https://ipfs.io'
1190+
})
11641191
};
11651192
const mz = await Merklizer.merklizeJSONLD(testDocumentIPFS, opts);
11661193
const path = await mz.resolveDocPath('credentialSubject.testNewTypeInt', opts);

0 commit comments

Comments
 (0)