@dignetwork/dig-sdk 0.0.1-alpha.78 → 0.0.1-alpha.79

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.
@@ -8,8 +8,7 @@ export declare class DigNetwork {
8
8
  static subscribeToStore(storeId: string): Promise<void>;
9
9
  static findPeerWithStoreKey(storeId: string, rootHash: string, key?: string, intialBlackList?: string[]): Promise<DigPeer | null>;
10
10
  static unsubscribeFromStore(storeId: string): void;
11
- syncStoreFromPeers(): Promise<void>;
12
- private fetchAvailablePeers;
13
- downloadFileFromPeers(dataPath: string, filePath: string, overwrite: boolean): Promise<void>;
11
+ syncStoreFromPeers(maxRootsToProcess?: number): Promise<void>;
12
+ fetchAvailablePeers(): Promise<DigPeer[]>;
14
13
  }
15
14
  //# sourceMappingURL=DigNetwork.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"DigNetwork.d.ts","sourceRoot":"","sources":["../../src/DigNetwork/DigNetwork.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AASpC,qBAAa,UAAU;IACrB,OAAO,CAAC,SAAS,CAAY;IAC7B,OAAO,CAAC,UAAU,CAAa;IAC/B,OAAO,CAAC,QAAQ,CAAS;IACzB,OAAO,CAAC,aAAa,CAA2B;gBAEpC,OAAO,EAAE,MAAM;WAOP,gBAAgB,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;WAQhD,oBAAoB,CACtC,OAAO,EAAE,MAAM,EACf,QAAQ,EAAE,MAAM,EAChB,GAAG,CAAC,EAAE,MAAM,EACZ,eAAe,GAAE,MAAM,EAAO,GAC7B,OAAO,CAAC,OAAO,GAAG,IAAI,CAAC;WAyDZ,oBAAoB,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI;IAO5C,kBAAkB,IAAI,OAAO,CAAC,IAAI,CAAC;YA0GlC,mBAAmB;IAWpB,qBAAqB,CAChC,QAAQ,EAAE,MAAM,EAChB,QAAQ,EAAE,MAAM,EAChB,SAAS,EAAE,OAAO,GACjB,OAAO,CAAC,IAAI,CAAC;CA2CjB"}
1
+ {"version":3,"file":"DigNetwork.d.ts","sourceRoot":"","sources":["../../src/DigNetwork/DigNetwork.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AASpC,qBAAa,UAAU;IACrB,OAAO,CAAC,SAAS,CAAY;IAC7B,OAAO,CAAC,UAAU,CAAa;IAC/B,OAAO,CAAC,QAAQ,CAAS;IACzB,OAAO,CAAC,aAAa,CAA2B;gBAEpC,OAAO,EAAE,MAAM;WAOP,gBAAgB,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;WAQhD,oBAAoB,CACtC,OAAO,EAAE,MAAM,EACf,QAAQ,EAAE,MAAM,EAChB,GAAG,CAAC,EAAE,MAAM,EACZ,eAAe,GAAE,MAAM,EAAO,GAC7B,OAAO,CAAC,OAAO,GAAG,IAAI,CAAC;WA2DZ,oBAAoB,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI;IAO5C,kBAAkB,CAAC,iBAAiB,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAgG7D,mBAAmB,IAAI,OAAO,CAAC,OAAO,EAAE,CAAC;CAUvD"}
@@ -96,10 +96,9 @@ class DigNetwork {
96
96
  });
97
97
  fs.unlinkSync(path.join(config_1.DIG_FOLDER_PATH, "stores", storeId + ".json"));
98
98
  }
99
- async syncStoreFromPeers() {
99
+ async syncStoreFromPeers(maxRootsToProcess) {
100
100
  console.log("Starting file download process...");
101
101
  let peerBlackList = [];
102
- let selectedPeer = null;
103
102
  try {
104
103
  const rootHistory = await this.dataStore.getRootHistory();
105
104
  if (!rootHistory.length) {
@@ -114,50 +113,47 @@ class DigNetwork {
114
113
  console.log("All root hashes already exist locally. No need for download.");
115
114
  return;
116
115
  }
117
- // Process filtered rootHistory sequentially
118
- for (const rootInfo of rootHistoryFiltered) {
116
+ // If maxRootsToProcess is specified, limit the number of roots processed
117
+ const rootsToProcess = maxRootsToProcess
118
+ ? rootHistoryFiltered.slice(0, maxRootsToProcess)
119
+ : rootHistoryFiltered;
120
+ // Process the root hashes sequentially
121
+ for (const rootInfo of rootsToProcess) {
122
+ let selectedPeer = null;
119
123
  while (true) {
120
124
  try {
121
125
  // Find a peer with the store and root hash
122
126
  selectedPeer = await DigNetwork.findPeerWithStoreKey(this.dataStore.StoreId, rootInfo.root_hash, undefined, peerBlackList);
123
127
  if (!selectedPeer) {
124
- console.error(`No peer found with root hash ${rootInfo.root_hash}. Abort download.`);
125
- throw new Error("No peer found with root hash.");
128
+ console.error(`No peer found with root hash ${rootInfo.root_hash}. Moving to next root.`);
129
+ break; // Exit the while loop to proceed to the next rootInfo
126
130
  }
127
- // Ensure the selected peer has the store by checking with a HEAD request
131
+ // Check if the selected peer has the store and root hash
128
132
  const { storeExists, rootHashExists } = await selectedPeer.propagationServer.checkStoreExists(rootInfo.root_hash);
129
- if (!storeExists) {
130
- console.warn(`Peer ${selectedPeer.IpAddress} does not have the store. Trying another peer...`);
131
- peerBlackList.push(selectedPeer.IpAddress); // Add peer to blacklist and try again
133
+ if (!storeExists || !rootHashExists) {
134
+ console.warn(`Peer ${selectedPeer.IpAddress} does not have the required store or root hash. Trying another peer...`);
135
+ peerBlackList.push(selectedPeer.IpAddress); // Blacklist and retry
132
136
  continue;
133
137
  }
134
- if (!rootHashExists) {
135
- console.warn(`Peer ${selectedPeer.IpAddress} does not have the root hash. Trying another peer...`);
136
- peerBlackList.push(selectedPeer.IpAddress); // Add peer to blacklist and try again
137
- continue;
138
- }
139
- // Download the store root and all associated data from the selected peer
138
+ // Download the store root and associated data
140
139
  await selectedPeer.downloadStoreRoot(rootInfo.root_hash);
141
- peerBlackList = []; // Clear the blacklist upon successful download
142
- // Break out of the retry loop if the download succeeds
140
+ // Clear the blacklist upon successful download
141
+ peerBlackList = [];
142
+ // Break after successful download to proceed to next root hash
143
143
  break;
144
144
  }
145
145
  catch (error) {
146
- console.error(`Error downloading from peer. Retrying with another peer.`, error);
146
+ console.error(`Error downloading from peer ${selectedPeer?.IpAddress}. Retrying with another peer.`, error);
147
147
  if (selectedPeer) {
148
- peerBlackList.push(selectedPeer.IpAddress); // Add peer to blacklist and try again
148
+ peerBlackList.push(selectedPeer.IpAddress); // Blacklist and retry
149
149
  }
150
150
  }
151
151
  }
152
- // Process the latest root hash first, breaking after each success to handle new incoming roots
153
- break;
154
152
  }
155
153
  console.log("Syncing store complete.");
156
154
  }
157
155
  catch (error) {
158
- if (selectedPeer) {
159
- peerBlackList.push(selectedPeer.IpAddress);
160
- }
156
+ console.error("Error during syncing store from peers:", error);
161
157
  throw error;
162
158
  }
163
159
  }
@@ -168,37 +164,5 @@ class DigNetwork {
168
164
  const peers = await this.serverCoin.sampleCurrentEpoch(10, Array.from(this.peerBlacklist.keys()));
169
165
  return peers.map((ip) => new DigPeer_1.DigPeer(ip, this.dataStore.StoreId));
170
166
  }
171
- async downloadFileFromPeers(dataPath, filePath, overwrite) {
172
- let digPeers = await this.fetchAvailablePeers();
173
- const tempFilePath = `${filePath}.tmp`;
174
- while (true) {
175
- if (!overwrite && fs.existsSync(filePath))
176
- return;
177
- const blacklist = this.peerBlacklist.get(dataPath) || new Set();
178
- for (const digPeer of digPeers) {
179
- try {
180
- if (blacklist.has(digPeer.IpAddress))
181
- continue;
182
- // Ensure the selected peer has the store by checking with a HEAD request
183
- const { storeExists } = await digPeer.propagationServer.checkStoreExists();
184
- if (!storeExists) {
185
- console.warn(`Peer ${digPeer.IpAddress} does not have the store. Trying another peer...`);
186
- blacklist.add(digPeer.IpAddress); // Add peer to blacklist and try again
187
- continue;
188
- }
189
- await digPeer.downloadData(dataPath);
190
- return; // Exit the method if download succeeds
191
- }
192
- catch (error) {
193
- console.warn(`Failed to download ${dataPath} from ${digPeer.IpAddress}, blacklisting peer and trying next...`);
194
- blacklist.add(digPeer.IpAddress);
195
- // Clean up the temp file in case of failure
196
- if (fs.existsSync(tempFilePath)) {
197
- await unlink(tempFilePath);
198
- }
199
- }
200
- }
201
- }
202
- }
203
167
  }
204
168
  exports.DigNetwork = DigNetwork;
@@ -20,6 +20,5 @@ export declare class DigPeer {
20
20
  syncStore(): Promise<void>;
21
21
  pushStoreRoot(storeId: string, rootHash: string): Promise<void>;
22
22
  downloadStoreRoot(rootHash: string): Promise<void>;
23
- downloadData(dataPath: string): Promise<void>;
24
23
  }
25
24
  //# sourceMappingURL=DigPeer.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"DigPeer.d.ts","sourceRoot":"","sources":["../../src/DigNetwork/DigPeer.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAChD,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AACxD,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAKpD,OAAO,EAKL,MAAM,EACP,MAAM,8BAA8B,CAAC;AAKtC,qBAAa,OAAO;IAClB,OAAO,CAAC,SAAS,CAAS;IAC1B,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,cAAc,CAAgB;IACtC,OAAO,CAAC,kBAAkB,CAAoB;IAC9C,OAAO,CAAC,gBAAgB,CAAkB;gBAE9B,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM;IAS9C,IAAW,aAAa,IAAI,aAAa,CAExC;IAGD,IAAW,iBAAiB,IAAI,iBAAiB,CAEhD;IAGD,IAAW,eAAe,IAAI,eAAe,CAE5C;IAED,IAAW,SAAS,IAAI,MAAM,CAE7B;WAEa,qBAAqB,CACjC,UAAU,EAAE,MAAM,EAClB,SAAS,EAAE,MAAM,EAAE,EACnB,WAAW,EAAE,MAAM,EACnB,KAAK,EAAE,MAAM,EAAE,GACd,OAAO,CAAC,IAAI,CAAC;WAuBI,gBAAgB,CAClC,UAAU,EAAE,MAAM,EAClB,OAAO,EAAE,M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B,WAAW,EAAE,MAAM,EACnB,KAAK,EAAE,MAAM,EAAE,GACd,OAAO,CAAC,IAAI,CAAC;WAuBI,gBAAgB,CAClC,UAAU,EAAE,MAAM,EAClB,OAAO,EAAE,MAAM,EAAE,GAChB,OAAO,CAAC,IAAI,CAAC;IAwCH,WAAW,CACtB,UAAU,EAAE,MAAM,EAClB,MAAM,EAAE,MAAM,EACd,KAAK,GAAE,MAAM,EAAO,GACnB,OAAO,CAAC,IAAI,CAAC;WAYF,iBAAiB,CAAC,OAAO,EAAE,MAAM;IAqBlC,SAAS,IAAI,OAAO,CAAC,IAAI,CAAC;IAc1B,aAAa,CAAC,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAI/D,iBAAiB,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;CAOhE"}
@@ -111,8 +111,5 @@ class DigPeer {
111
111
  async downloadStoreRoot(rootHash) {
112
112
  await PropagationServer_1.PropagationServer.downloadStore(this.storeId, rootHash, this.IpAddress);
113
113
  }
114
- async downloadData(dataPath) {
115
- await this.propagationServer.downloadFile(dataPath, dataPath);
116
- }
117
114
  }
118
115
  exports.DigPeer = DigPeer;
@@ -66,7 +66,7 @@ export declare class PropagationServer {
66
66
  * Download a file from the server by sending a GET request.
67
67
  * Logs progress using a local cli-progress bar.
68
68
  */
69
- downloadFile(label: string, dataPath: string): Promise<void>;
69
+ downloadFile(label: string, dataPath: string, rootHash: string, baseDir: string): Promise<void>;
70
70
  /**
71
71
  * Static function to handle downloading multiple files from a DataStore based on file paths.
72
72
  */
@@ -1 +1 @@
1
- {"version":3,"file":"PropagationServer.d.ts","sourceRoot":"","sources":["../../src/DigNetwork/PropagationServer.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,MAAM,OAAO,CAAC;AAqB1B,qBAAa,iBAAiB;IAC5B,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,EAAE,MAAM,CAAC;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U,WAAW,CACtB,OAAO,EAAE,MAAM,EACf,QAAQ,EAAE,MAAM,EAChB,SAAS,EAAE,MAAM;IA2DnB;;;OAGG;IACG,SAAS,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IA0ElD;;OAEG;IACG,cAAc,CAClB,QAAQ,EAAE,MAAM,EAChB,QAAQ,EAAE,MAAM,GACf,OAAO,CAAC;QAAE,MAAM,EAAE,OAAO,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,CAAC;IA0B7C;;;OAGG;IACG,YAAY,CAChB,KAAK,EAAE,MAAM,EACb,QAAQ,EAAE,MAAM,EAChB,QAAQ,EAAE,MAAM,EAChB,OAAO,EAAE,MAAM;IA+FjB;;OAEG;WACU,aAAa,CACxB,OAAO,EAAE,MAAM,EACf,QAAQ,EAAE,MAAM,EAChB,SAAS,EAAE,MAAM;CA+EpB"}
@@ -1,27 +1,4 @@
1
1
  "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
2
  var __importDefault = (this && this.__importDefault) || function (mod) {
26
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
27
4
  };
@@ -43,6 +20,10 @@ const hashUtils_1 = require("../utils/hashUtils");
43
20
  const nanospinner_1 = require("nanospinner");
44
21
  const progress_stream_1 = __importDefault(require("progress-stream"));
45
22
  const stream_1 = require("stream");
23
+ const asyncPool_1 = require("../utils/asyncPool");
24
+ const fs_extra_1 = __importDefault(require("fs-extra"));
25
+ const os_1 = __importDefault(require("os"));
26
+ const merkle_1 = require("../utils/merkle");
46
27
  // Helper function to trim long filenames with ellipsis and ensure consistent padding
47
28
  function formatFilename(filename, maxLength = 30) {
48
29
  if (!filename) {
@@ -318,9 +299,7 @@ class PropagationServer {
318
299
  }));
319
300
  // Limit the number of concurrent uploads
320
301
  const concurrencyLimit = 10; // Adjust this number as needed
321
- // Import asyncPool from your utilities
322
- const { asyncPool } = await Promise.resolve().then(() => __importStar(require("../utils/asyncPool")));
323
- await asyncPool(concurrencyLimit, uploadTasks, async (task) => {
302
+ await (0, asyncPool_1.asyncPool)(concurrencyLimit, uploadTasks, async (task) => {
324
303
  await propagationServer.uploadFile(task.label, task.dataPath);
325
304
  });
326
305
  // Commit the session after all files have been uploaded
@@ -416,9 +395,9 @@ class PropagationServer {
416
395
  * Download a file from the server by sending a GET request.
417
396
  * Logs progress using a local cli-progress bar.
418
397
  */
419
- async downloadFile(label, dataPath) {
398
+ async downloadFile(label, dataPath, rootHash, baseDir) {
420
399
  const url = `https://${this.ipAddress}:${PropagationServer.port}/fetch/${this.storeId}/${dataPath}`;
421
- const downloadPath = path_1.default.join(config_1.STORE_PATH, this.storeId, dataPath);
400
+ let downloadPath = path_1.default.join(baseDir, dataPath);
422
401
  // Ensure that the directory for the file exists
423
402
  fs_1.default.mkdirSync(path_1.default.dirname(downloadPath), { recursive: true });
424
403
  const config = {
@@ -469,6 +448,13 @@ class PropagationServer {
469
448
  progressStream.on("error", reject);
470
449
  }),
471
450
  ]);
451
+ if (dataPath.includes("/data")) {
452
+ const integrity = await (0, merkle_1.merkleIntegrityCheck)(path_1.default.join(baseDir, `${rootHash}.dat`), baseDir, dataPath, rootHash);
453
+ if (!integrity) {
454
+ throw new Error(`Integrity check failed for file: ${dataPath}`);
455
+ }
456
+ console.log("integrity check");
457
+ }
472
458
  }
473
459
  catch (error) {
474
460
  throw error;
@@ -503,16 +489,37 @@ class PropagationServer {
503
489
  }
504
490
  // Limit the number of concurrent downloads
505
491
  const concurrencyLimit = 10; // Adjust this number as needed
506
- // Import asyncPool from your utilities
507
- const { asyncPool } = await Promise.resolve().then(() => __importStar(require("../utils/asyncPool")));
508
- await asyncPool(concurrencyLimit, downloadTasks, async (task) => {
509
- await propagationServer.downloadFile(task.label, task.dataPath);
510
- });
511
- // Save the rootHash.dat file
512
- fs_1.default.writeFileSync(path_1.default.join(config_1.STORE_PATH, storeId, `${rootHash}.dat`), datFileContent);
513
- const dataStore = blockchain_1.DataStore.from(storeId);
514
- await dataStore.generateManifestFile();
515
- console.log((0, colorette_1.green)(`✔ All files have been downloaded to ${storeId}.`));
492
+ // Create a temporary directory
493
+ const tempDir = fs_1.default.mkdtempSync(path_1.default.join(os_1.default.tmpdir(), "downloadStore-"));
494
+ try {
495
+ // Download files to the temporary directory
496
+ await (0, asyncPool_1.asyncPool)(concurrencyLimit, downloadTasks, async (task) => {
497
+ await propagationServer.downloadFile(task.label, task.dataPath, rootHash, path_1.default.join(tempDir, storeId));
498
+ });
499
+ // Save the rootHash.dat file to the temporary directory
500
+ fs_1.default.writeFileSync(path_1.default.join(tempDir, storeId, `${rootHash}.dat`), datFileContent);
501
+ // Integrity check for the downloaded files was done during the download
502
+ // Here we want to make sure we got all the files or we reject the download session
503
+ for (const [fileKey, fileData] of Object.entries(root.files)) {
504
+ const dataPath = (0, hashUtils_1.getFilePathFromSha256)(fileData.sha256, "data");
505
+ const downloadPath = path_1.default.join(tempDir, storeId, dataPath);
506
+ if (!fs_1.default.existsSync(path_1.default.join(downloadPath, dataPath))) {
507
+ throw new Error(`Missing file: ${fileKey}, aborting session.`);
508
+ }
509
+ }
510
+ // After all downloads are complete, copy from temp directory to the main directory
511
+ const destinationDir = path_1.default.join(config_1.STORE_PATH, storeId);
512
+ fs_extra_1.default.copySync(path_1.default.join(tempDir, storeId), destinationDir);
513
+ // Generate the manifest file in the main directory
514
+ const dataStore = blockchain_1.DataStore.from(storeId);
515
+ await dataStore.cacheStoreCreationHeight();
516
+ await dataStore.generateManifestFile();
517
+ console.log((0, colorette_1.green)(`✔ All files have been downloaded to ${storeId}.`));
518
+ }
519
+ finally {
520
+ // Clean up the temporary directory
521
+ fs_extra_1.default.removeSync(tempDir);
522
+ }
516
523
  }
517
524
  }
518
525
  exports.PropagationServer = PropagationServer;
@@ -1 +1 @@
1
- {"version":3,"file":"ServerCoin.d.ts","sourceRoot":"","sources":["../../src/blockchain/ServerCoin.ts"],"names":[],"mappings":"AACA,OAAO,EAML,UAAU,IAAI,gBAAgB,EAE/B,MAAM,8BAA8B,CAAC;AAItC,OAAO,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAC;AAWrD,qBAAa,UAAU;IACrB,OAAO,CAAC,OAAO,CAAS;IACxB,gBAAuB,iBAAiB,eAEtC;gBAEU,OAAO,EAAE,MAAM;IAKd,cAAc,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,gBAAgB,CAAC;IA2DzD,kBAAkB,CAC7B,UAAU,EAAE,gBAAgB,EAC5B,KAAK,EAAE,MAAM,EACb,MAAM,EAAE,MAAM,GACb,OAAO,CAAC,IAAI,CAAC;IAqBH,IAAI,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;YAoDjD,oBAAoB;IAerB,gBAAgB,CAAC,KAAK,EAAE,MAAM,EAAE,SAAS,GAAE,MAAM,EAAO,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;IA4C5E,mBAAmB,CAC9B,SAAS,GAAE,MAAM,EAAO,GACvB,OAAO,CAAC,MAAM,E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KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;YAoDjD,oBAAoB;IAerB,gBAAgB,CAAC,KAAK,EAAE,MAAM,EAAE,SAAS,GAAE,MAAM,EAAO,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;IA0C5E,mBAAmB,CAC9B,SAAS,GAAE,MAAM,EAAO,GACvB,OAAO,CAAC,MAAM,EAAE,CAAC;IAMP,kBAAkB,CAC7B,UAAU,GAAE,MAAU,EACtB,SAAS,GAAE,MAAM,EAAO,GACvB,OAAO,CAAC,MAAM,EAAE,CAAC;IAMP,wBAAwB,CACnC,KAAK,EAAE,MAAM,EACb,UAAU,GAAE,MAAU,EACtB,SAAS,GAAE,MAAM,EAAO,GACvB,OAAO,CAAC,MAAM,EAAE,CAAC;WAeN,eAAe,IAAI;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE;IAKpD,sBAAsB,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAiDrD,kBAAkB,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;WA+B1C,uBAAuB;YAkE7B,sBAAsB;IAWvB,uBAAuB,CAClC,YAAY,EAAE,MAAM,EACpB,MAAM,EAAE,MAAM,GACb,OAAO,CAAC,OAAO,CAAC;WAuBL,sBAAsB,CAAC,mBAAmB,EAAE,IAAI,GAAG;QAC/D,KAAK,EAAE,MAAM,CAAC;QACd,KAAK,EAAE,MAAM,CAAC;KACf;CA2BF"}
@@ -103,7 +103,6 @@ class ServerCoin {
103
103
  // Check if the result is already cached
104
104
  const cachedPeers = serverCoinPeersCache.get(cacheKey);
105
105
  if (cachedPeers) {
106
- console.log('!');
107
106
  return cachedPeers;
108
107
  }
109
108
  const epochBasedHint = (0, datalayer_driver_1.morphLauncherId)(Buffer.from(this.storeId, "hex"), BigInt(epoch));
@@ -126,7 +125,6 @@ class ServerCoin {
126
125
  const peerList = Array.from(serverCoinPeers);
127
126
  // Cache the result
128
127
  serverCoinPeersCache.set(cacheKey, peerList);
129
- console.log(peerList);
130
128
  return peerList;
131
129
  }
132
130
  async getActiveEpochPeers(blacklist = []) {
@@ -0,0 +1,2 @@
1
+ export declare function merkleIntegrityCheck(treePath: string, tmpDir: string, dataPath: string, roothash: string): Promise<boolean>;
2
+ //# sourceMappingURL=merkle.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"merkle.d.ts","sourceRoot":"","sources":["../../src/utils/merkle.ts"],"names":[],"mappings":"AAIA,wBAAsB,oBAAoB,CACtC,QAAQ,EAAE,MAAM,EAChB,MAAM,EAAE,MAAM,EACd,QAAQ,EAAE,MAAM,EAChB,QAAQ,EAAE,MAAM,GACf,OAAO,CAAC,OAAO,CAAC,CA6BlB"}
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.merkleIntegrityCheck = merkleIntegrityCheck;
7
+ const DataIntegrityTree_1 = require("../DataIntegrityTree");
8
+ const fs_1 = __importDefault(require("fs"));
9
+ const path_1 = __importDefault(require("path"));
10
+ async function merkleIntegrityCheck(treePath, tmpDir, dataPath, roothash) {
11
+ const rootHashContent = fs_1.default.readFileSync(treePath, "utf-8");
12
+ const tree = JSON.parse(rootHashContent);
13
+ // Extract expected sha256 from dataPath
14
+ const expectedSha256 = dataPath.replace("data", "").replace(/\//g, "");
15
+ console.log("expectedSha256", expectedSha256);
16
+ // Find the hexKey in the tree based on matching sha256
17
+ const hexKey = Object.keys(tree.files).find((key) => {
18
+ const fileData = tree.files[key]; // Inline type definition
19
+ return fileData.sha256 === expectedSha256;
20
+ });
21
+ if (!hexKey) {
22
+ throw new Error(`No matching file found with sha256: ${expectedSha256}`);
23
+ }
24
+ // Validate the integrity with the foreign tree
25
+ const integrity = await DataIntegrityTree_1.DataIntegrityTree.validateKeyIntegrityWithForeignTree(hexKey, expectedSha256, tree, roothash, path_1.default.join(tmpDir, "data"));
26
+ console.log("Integrity check result:", integrity);
27
+ return integrity;
28
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dignetwork/dig-sdk",
3
- "version": "0.0.1-alpha.78",
3
+ "version": "0.0.1-alpha.79",
4
4
  "description": "",
5
5
  "type": "commonjs",
6
6
  "main": "./dist/index.js",