@dignetwork/dig-sdk 0.0.1-alpha.30 → 0.0.1-alpha.33

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.
@@ -69,7 +69,7 @@ class ContentServer {
69
69
  async headStore(options) {
70
70
  let url = `https://${this.ipAddress}/${this.storeId}`;
71
71
  if (options?.hasRootHash) {
72
- url += `hasRootHash=${options.hasRootHash}`;
72
+ url += `?hasRootHash=${options.hasRootHash}`;
73
73
  }
74
74
  return this.head(url); // Return the object from head method
75
75
  }
@@ -9,7 +9,7 @@ export declare class DigNetwork {
9
9
  uploadStoreHead(digPeer: DigPeer): Promise<void>;
10
10
  uploadStore(digPeer: DigPeer): Promise<void>;
11
11
  static subscribeToStore(storeId: string): Promise<void>;
12
- static findPeerWithStoreKey(storeId: string, rootHash: string, key?: string): Promise<string | null>;
12
+ static findPeerWithStoreKey(storeId: string, rootHash: string, key?: string, intialBlackList?: string[]): Promise<string | null>;
13
13
  static unsubscribeFromStore(storeId: string): void;
14
14
  downloadFiles(forceDownload?: boolean, renderProgressBar?: boolean, skipData?: boolean): Promise<void>;
15
15
  private fetchAvailablePeers;
@@ -1 +1 @@
1
- {"version":3,"file":"DigNetwork.d.ts","sourceRoot":"","sources":["../../src/DigNetwork/DigNetwork.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAMpC,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;YAOb,eAAe;IA8ChB,eAAe,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC;IAsDhD,WAAW,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC;WA6BrC,gBAAgB,CAAC,OAAO,EAAE,MA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OAAc,EACjC,QAAQ,GAAE,OAAe,GACxB,OAAO,CAAC,IAAI,CAAC;YAiEF,mBAAmB;YAWnB,kBAAkB;YASlB,oBAAoB;YASpB,qBAAqB;YAuErB,cAAc;CAoB7B"}
@@ -122,8 +122,8 @@ class DigNetwork {
122
122
  const digNetwork = new DigNetwork(storeId);
123
123
  await digNetwork.downloadFiles(true);
124
124
  }
125
- static async findPeerWithStoreKey(storeId, rootHash, key) {
126
- const peerBlackList = [];
125
+ static async findPeerWithStoreKey(storeId, rootHash, key, intialBlackList = []) {
126
+ const peerBlackList = intialBlackList;
127
127
  const serverCoin = new blockchain_1.ServerCoin(storeId);
128
128
  while (true) {
129
129
  try {
@@ -135,6 +135,7 @@ class DigNetwork {
135
135
  const storeResponse = await digPeer.contentServer.headStore({
136
136
  hasRootHash: rootHash,
137
137
  });
138
+ console.log(peerIp, storeResponse.headers);
138
139
  if (storeResponse.success &&
139
140
  storeResponse.headers?.["x-has-rootHash"] === "true") {
140
141
  if (!key)
@@ -167,44 +168,30 @@ class DigNetwork {
167
168
  if (!rootHistory.length)
168
169
  throw new Error("No roots found in rootHistory. Cannot proceed with file download.");
169
170
  await this.downloadHeightFile(forceDownload);
170
- const localManifestPath = path.join(this.storeDir, "manifest.dat");
171
- const localManifestHashes = fs.existsSync(localManifestPath)
172
- ? fs.readFileSync(localManifestPath, "utf-8").trim().split("\n")
173
- : [];
174
- const progressBar = renderProgressBar
175
- ? new cli_progress_1.MultiBar({
176
- clearOnComplete: false,
177
- hideCursor: true,
178
- format: "Syncing Store | {bar} | {percentage}%",
179
- noTTYOutput: true,
180
- }, cli_progress_1.Presets.shades_classic)
181
- : null;
182
- const progress = progressBar
183
- ? progressBar.create(rootHistory.length, 0)
184
- : null;
185
- const newRootHashes = [];
186
- for (let i = 0; i < rootHistory.length; i++) {
187
- const { root_hash: rootHash } = rootHistory[i];
188
- const datFilePath = path.join(this.storeDir, `${rootHash}.dat`);
189
- await this.downloadFileFromPeers(`${rootHash}.dat`, datFilePath, forceDownload);
190
- const datFileContent = JSON.parse(fs.readFileSync(datFilePath, "utf-8"));
191
- if (datFileContent.root !== rootHash)
192
- throw new Error("Root hash mismatch");
171
+ const rootHistorySorted = rootHistory
172
+ .filter((item) => item.timestamp !== undefined)
173
+ .sort((a, b) => b.timestamp - a.timestamp);
174
+ // Process rootHistory sequentially
175
+ for (const rootInfo of rootHistorySorted) {
176
+ const peerIp = await DigNetwork.findPeerWithStoreKey(this.dataStore.StoreId, rootInfo.root_hash);
177
+ if (!peerIp) {
178
+ console.error(`No peer found with root hash ${rootInfo.root_hash}. Skipping download.`);
179
+ continue; // Skip to the next rootInfo
180
+ }
181
+ const digPeer = new DigPeer_1.DigPeer(peerIp, this.dataStore.StoreId);
182
+ const rootResponse = await digPeer.propagationServer.getStoreData(`${rootInfo.root_hash}.dat`);
183
+ const root = JSON.parse(rootResponse);
193
184
  if (!skipData) {
194
- for (const file of Object.keys(datFileContent.files)) {
195
- const filePath = (0, hashUtils_1.getFilePathFromSha256)(datFileContent.files[file].sha256, path.join(this.storeDir, "data"));
196
- const isInDataDir = filePath.startsWith(path.join(this.storeDir, "data"));
197
- await this.downloadFileFromPeers((0, hashUtils_1.getFilePathFromSha256)(datFileContent.files[file].sha256, "data"), filePath, forceDownload || !isInDataDir);
198
- }
185
+ await Promise.all(root.files.map(async (file) => {
186
+ const filePath = (0, hashUtils_1.getFilePathFromSha256)(file.sha256, this.storeDir);
187
+ if (!fs.existsSync(filePath) || forceDownload) {
188
+ console.log(`Downloading ${file.sha256}...`);
189
+ await this.downloadFileFromPeers(file.sha256.match(/.{1,2}/g).join("/"), filePath, forceDownload);
190
+ }
191
+ }));
199
192
  }
200
- if (localManifestHashes[i] !== rootHash)
201
- newRootHashes.push(rootHash);
202
- progress?.increment();
203
193
  }
204
- if (newRootHashes.length)
205
- fs.appendFileSync(localManifestPath, newRootHashes.join("\n") + "\n");
206
- await this.downloadManifestFile(forceDownload);
207
- progressBar?.stop();
194
+ await this.downloadManifestFile(true);
208
195
  console.log("Syncing store complete.");
209
196
  }
210
197
  catch (error) {
@@ -185,7 +185,7 @@ class DigPeer {
185
185
  throw new Error("Invalid input. Must be a 32-byte buffer.");
186
186
  }
187
187
  // Define the seed
188
- const seed = "dig";
188
+ const seed = "digpayment";
189
189
  // Combine the seed and the original buffer
190
190
  const combinedBuffer = Buffer.concat([Buffer.from(seed), storeId]);
191
191
  // Apply SHA-256 hash to the combined buffer
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dignetwork/dig-sdk",
3
- "version": "0.0.1-alpha.30",
3
+ "version": "0.0.1-alpha.33",
4
4
  "description": "",
5
5
  "type": "commonjs",
6
6
  "main": "./dist/index.js",