@dignetwork/dig-sdk 0.0.1-alpha.103 → 0.0.1-alpha.106

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -155,9 +155,14 @@ declare class DataIntegrityTree {
155
155
  * @param sha256 - The SHA-256 hash of the file.
156
156
  * @param serializedTree - The foreign serialized Merkle tree.
157
157
  * @param expectedRootHash - The expected root hash of the Merkle tree.
158
+ * @param dataDir - The directory where the data is stored.
159
+ * @param verifiedSha256 - A boolean indicating if the SHA-256 hash has already been verified.
158
160
  * @returns A boolean indicating if the SHA-256 is present in the foreign tree and the root hash matches.
159
161
  */
160
- static validateKeyIntegrityWithForeignTree(hexkey: string, sha256: string, serializedTree: object, expectedRootHash: string, dataDir: string): Promise<boolean>;
162
+ static validateKeyIntegrityWithForeignTree(hexkey: string, sha256: string, serializedTree: object, expectedRootHash: string, dataDir: string, verifiedSha256?: boolean): Promise<boolean>;
163
+ private static verifyFileHash;
164
+ private static deserializeAndVerifyTree;
165
+ private static checkHashInTree;
161
166
  }
162
167
  export { DataIntegrityTree };
163
168
  //# sourceMappingURL=DataIntegrityTree.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"DataIntegrityTree.d.ts","sourceRoot":"","sources":["../../src/DataIntegrityTree/DataIntegrityTree.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAC1C,OAAO,EAAE,QAAQ,EAAE,MAAM,QAAQ,CAAC;AAuClC,MAAM,WAAW,wBAAwB;IACvC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,WAAW,CAAC,EAAE,OAAO,GAAG,SAAS,CAAC;IAClC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAGlB,iBAAiB,CAAC,EAAE,OAAO,CAAC;CAC7B;AAED;;GAEG;AACH,cAAM,iBAAiB;IACrB,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,YAAY,CAAS;IAC7B,OA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QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAyD5E;;;;;;;;;;;OAWG;WACU,mCAAmC,CAC9C,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,EACd,cAAc,EAAE,MAAM,EACtB,gBAAgB,EAAE,MAAM,EACxB,OAAO,EAAE,MAAM,EACf,cAAc,GAAE,OAAe,GAC9B,OAAO,CAAC,OAAO,CAAC;mBAgCE,cAAc;IA8CnC,OAAO,CAAC,MAAM,CAAC,wBAAwB;IAkCvC,OAAO,CAAC,MAAM,CAAC,eAAe;CAgB/B;AAED,OAAO,EAAE,iBAAiB,EAAE,CAAC"}
@@ -638,9 +638,11 @@ class DataIntegrityTree {
638
638
  * @param sha256 - The SHA-256 hash of the file.
639
639
  * @param serializedTree - The foreign serialized Merkle tree.
640
640
  * @param expectedRootHash - The expected root hash of the Merkle tree.
641
+ * @param dataDir - The directory where the data is stored.
642
+ * @param verifiedSha256 - A boolean indicating if the SHA-256 hash has already been verified.
641
643
  * @returns A boolean indicating if the SHA-256 is present in the foreign tree and the root hash matches.
642
644
  */
643
- static async validateKeyIntegrityWithForeignTree(hexkey, sha256, serializedTree, expectedRootHash, dataDir) {
645
+ static async validateKeyIntegrityWithForeignTree(hexkey, sha256, serializedTree, expectedRootHash, dataDir, verifiedSha256 = false) {
644
646
  if (!isHexString(hexkey)) {
645
647
  throw new Error("key must be a valid hex string");
646
648
  }
@@ -650,7 +652,24 @@ class DataIntegrityTree {
650
652
  if (!isHexString(expectedRootHash)) {
651
653
  throw new Error("expectedRootHash must be a valid hex string");
652
654
  }
653
- // File path based on sha256
655
+ if (!verifiedSha256) {
656
+ // Verify the file's SHA-256 hash
657
+ const isFileValid = await this.verifyFileHash(sha256, dataDir);
658
+ if (!isFileValid) {
659
+ return false;
660
+ }
661
+ }
662
+ // Deserialize and verify the tree
663
+ const tree = this.deserializeAndVerifyTree(serializedTree, expectedRootHash);
664
+ if (!tree) {
665
+ return false;
666
+ }
667
+ // Check if the combined hash exists in the foreign tree's leaves
668
+ const isInTree = this.checkHashInTree(tree, hexkey, sha256);
669
+ return isInTree;
670
+ }
671
+ // Function to verify the file's SHA-256 hash
672
+ static async verifyFileHash(sha256, dataDir) {
654
673
  const filePath = path.join(dataDir, sha256.match(/.{1,2}/g).join("/"));
655
674
  // Check if the file exists
656
675
  if (!fs.existsSync(filePath)) {
@@ -671,29 +690,7 @@ class DataIntegrityTree {
671
690
  console.warn(`File hash mismatch. Expected: ${sha256}, got: ${uncompressedSha256}`);
672
691
  return resolve(false);
673
692
  }
674
- // Deserialize the foreign tree
675
- const leaves = serializedTree.leaves.map((leaf) => Buffer.from(leaf, "hex"));
676
- const tree = new merkletreejs_1.MerkleTree(leaves, crypto_js_1.SHA256, { sortPairs: true });
677
- // Verify that the deserialized tree's root matches the expected root hash
678
- const treeRootHash = tree.getRoot().toString("hex");
679
- if (treeRootHash !== expectedRootHash) {
680
- console.warn(`Expected root hash ${expectedRootHash}, but got ${treeRootHash}`);
681
- return resolve(false);
682
- }
683
- // Rebuild the files map from the serialized tree
684
- // @ts-ignore
685
- tree.files = new Map(Object.entries(serializedTree.files).map(([key, value]) => [
686
- key,
687
- { hash: value.hash, sha256: value.sha256 },
688
- ]));
689
- // Check if the SHA-256 exists in the foreign tree's files
690
- const combinedHash = crypto
691
- .createHash("sha256")
692
- .update(`${hexkey}/${sha256}`)
693
- .digest("hex");
694
- const leaf = Buffer.from(combinedHash, "hex");
695
- const isInTree = tree.getLeafIndex(leaf) !== -1;
696
- resolve(isInTree);
693
+ resolve(true);
697
694
  });
698
695
  decompressStream.on("error", (err) => {
699
696
  reject(err);
@@ -703,5 +700,34 @@ class DataIntegrityTree {
703
700
  });
704
701
  });
705
702
  }
703
+ // Function to deserialize and verify the tree
704
+ static deserializeAndVerifyTree(serializedTree, expectedRootHash) {
705
+ // Deserialize the foreign tree
706
+ const leaves = serializedTree.leaves.map((leaf) => Buffer.from(leaf, "hex"));
707
+ const tree = new merkletreejs_1.MerkleTree(leaves, crypto_js_1.SHA256, { sortPairs: true });
708
+ // Verify that the deserialized tree's root matches the expected root hash
709
+ const treeRootHash = tree.getRoot().toString("hex");
710
+ if (treeRootHash !== expectedRootHash) {
711
+ console.warn(`Expected root hash ${expectedRootHash}, but got ${treeRootHash}`);
712
+ return null;
713
+ }
714
+ // Rebuild the files map from the serialized tree
715
+ // @ts-ignore
716
+ tree.files = new Map(Object.entries(serializedTree.files).map(([key, value]) => [
717
+ key,
718
+ { hash: value.hash, sha256: value.sha256 },
719
+ ]));
720
+ return tree;
721
+ }
722
+ // Function to check if the combined hash exists in the tree
723
+ static checkHashInTree(tree, hexkey, sha256) {
724
+ const combinedHash = crypto
725
+ .createHash("sha256")
726
+ .update(`${hexkey}/${sha256}`)
727
+ .digest("hex");
728
+ const leaf = Buffer.from(combinedHash, "hex");
729
+ const isInTree = tree.getLeafIndex(leaf) !== -1;
730
+ return isInTree;
731
+ }
706
732
  }
707
733
  exports.DataIntegrityTree = DataIntegrityTree;
@@ -11,4 +11,5 @@ export * from './spinnerUtils';
11
11
  export * from './ssl';
12
12
  export * from './validationUtils';
13
13
  export * from './Environment';
14
+ export * from './asyncPool';
14
15
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,mBAAmB,CAAC;AAClC,cAAc,oBAAoB,CAAC;AACnC,cAAc,kBAAkB,CAAC;AACjC,cAAc,cAAc,CAAC;AAC7B,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,WAAW,CAAC;AAC1B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,OAAO,CAAC;AACtB,cAAc,mBAAmB,CAAC;AAClC,cAAc,eAAe,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,mBAAmB,CAAC;AAClC,cAAc,oBAAoB,CAAC;AACnC,cAAc,kBAAkB,CAAC;AACjC,cAAc,cAAc,CAAC;AAC7B,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,WAAW,CAAC;AAC1B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,OAAO,CAAC;AACtB,cAAc,mBAAmB,CAAC;AAClC,cAAc,eAAe,CAAC;AAC9B,cAAc,aAAa,CAAC"}
@@ -27,3 +27,4 @@ __exportStar(require("./spinnerUtils"), exports);
27
27
  __exportStar(require("./ssl"), exports);
28
28
  __exportStar(require("./validationUtils"), exports);
29
29
  __exportStar(require("./Environment"), exports);
30
+ __exportStar(require("./asyncPool"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dignetwork/dig-sdk",
3
- "version": "0.0.1-alpha.103",
3
+ "version": "0.0.1-alpha.106",
4
4
  "description": "",
5
5
  "type": "commonjs",
6
6
  "main": "./dist/index.js",