@dignetwork/dig-sdk 0.0.1-alpha.5 → 0.0.1-alpha.51

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.
Files changed (53) hide show
  1. package/dist/DataIntegrityTree/DataIntegrityTree.d.ts +2 -2
  2. package/dist/DataIntegrityTree/DataIntegrityTree.d.ts.map +1 -1
  3. package/dist/DataIntegrityTree/DataIntegrityTree.js +83 -29
  4. package/dist/DigNetwork/ContentServer.d.ts +6 -1
  5. package/dist/DigNetwork/ContentServer.d.ts.map +1 -1
  6. package/dist/DigNetwork/ContentServer.js +40 -16
  7. package/dist/DigNetwork/DigNetwork.d.ts +2 -5
  8. package/dist/DigNetwork/DigNetwork.d.ts.map +1 -1
  9. package/dist/DigNetwork/DigNetwork.js +119 -155
  10. package/dist/DigNetwork/DigPeer.d.ts +8 -3
  11. package/dist/DigNetwork/DigPeer.d.ts.map +1 -1
  12. package/dist/DigNetwork/DigPeer.js +116 -114
  13. package/dist/DigNetwork/PropagationServer.d.ts +8 -2
  14. package/dist/DigNetwork/PropagationServer.d.ts.map +1 -1
  15. package/dist/DigNetwork/PropagationServer.js +105 -73
  16. package/dist/blockchain/DataStore.d.ts +1 -4
  17. package/dist/blockchain/DataStore.d.ts.map +1 -1
  18. package/dist/blockchain/DataStore.js +5 -83
  19. package/dist/blockchain/DataStoreSerializer.d.ts +1 -1
  20. package/dist/blockchain/DataStoreSerializer.d.ts.map +1 -1
  21. package/dist/blockchain/FullNodePeer.d.ts +9 -1
  22. package/dist/blockchain/FullNodePeer.d.ts.map +1 -1
  23. package/dist/blockchain/FullNodePeer.js +60 -14
  24. package/dist/blockchain/ServerCoin.d.ts +11 -3
  25. package/dist/blockchain/ServerCoin.d.ts.map +1 -1
  26. package/dist/blockchain/ServerCoin.js +45 -17
  27. package/dist/blockchain/Wallet.d.ts +1 -1
  28. package/dist/blockchain/Wallet.d.ts.map +1 -1
  29. package/dist/blockchain/Wallet.js +1 -1
  30. package/dist/blockchain/coins.d.ts +1 -1
  31. package/dist/blockchain/coins.d.ts.map +1 -1
  32. package/dist/blockchain/coins.js +1 -1
  33. package/dist/types.d.ts +1 -0
  34. package/dist/types.d.ts.map +1 -1
  35. package/dist/utils/FileTransfer.d.ts +47 -0
  36. package/dist/utils/FileTransfer.d.ts.map +1 -0
  37. package/dist/utils/FileTransfer.js +209 -0
  38. package/dist/utils/StoreArchiveManager.d.ts +45 -0
  39. package/dist/utils/StoreArchiveManager.d.ts.map +1 -0
  40. package/dist/utils/StoreArchiveManager.js +153 -0
  41. package/dist/utils/config.d.ts +0 -1
  42. package/dist/utils/config.d.ts.map +1 -1
  43. package/dist/utils/config.js +5 -12
  44. package/dist/utils/directoryUtils.d.ts +0 -6
  45. package/dist/utils/directoryUtils.d.ts.map +1 -1
  46. package/dist/utils/directoryUtils.js +30 -11
  47. package/dist/utils/index.d.ts +0 -1
  48. package/dist/utils/index.d.ts.map +1 -1
  49. package/dist/utils/index.js +0 -1
  50. package/package.json +9 -3
  51. package/dist/utils/deltaUtils.d.ts +0 -2
  52. package/dist/utils/deltaUtils.d.ts.map +0 -1
  53. package/dist/utils/deltaUtils.js +0 -83
@@ -106,7 +106,7 @@ declare class DataIntegrityTree {
106
106
  * @param rootHash - The root hash of the tree. Defaults to the latest root hash.
107
107
  * @returns The readable stream for the file.
108
108
  */
109
- getValueStream(hexKey: string, rootHash?: string | null): Readable;
109
+ getValueStream(hexKey: string, rootHash?: string | null, byteOffset?: number | null, length?: number | null): Readable;
110
110
  /**
111
111
  * Delete all leaves from the Merkle tree.
112
112
  */
@@ -154,7 +154,7 @@ declare class DataIntegrityTree {
154
154
  * @param expectedRootHash - The expected root hash of the Merkle tree.
155
155
  * @returns A boolean indicating if the SHA-256 is present in the foreign tree and the root hash matches.
156
156
  */
157
- static validateKeyIntegrityWithForeignTree(key: string, sha256: string, serializedTree: object, expectedRootHash: string): boolean;
157
+ static validateKeyIntegrityWithForeignTree(hexkey: string, sha256: string, serializedTree: object, expectedRootHash: string, dataDir: string): Promise<boolean>;
158
158
  }
159
159
  export { DataIntegrityTree };
160
160
  //# 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,OAAO,CAAC,QAAQ,CAAS;IACzB,OAAO,CAAC,OAAO,CAAS;IACjB,KAAK,EAAE,GAAG,CAAC,MAAM,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;IAC5D,OAAO,CAAC,IAAI,CAAa;gBAEb,OAAO,EAAE,MAAM,EAAE,OAAO,GAAE,wBAA6B;WAiDrD,IAAI,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,wBAAwB,GAAG,iBAAiB;IAIzF;;;OAGG;IACH,OAAO,CAAC,aAAa;IAQrB;;;OAGG;IACH,OAAO,CAAC,eAAe;IAUvB;;;;OAIG;IACH,OAAO,CAAC,kBAAkB;IAa1B;;;;OAIG;YACW,WAAW;IAazB;;;;;OAKG;IACG,SAAS,CAAC,UAAU,EAAE,QAAQ,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IA+EjE;;;OAGG;IACH,SAAS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MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO;IA4B5D;;;;;OAKG;IACH,WAAW,CACT,SAAS,EAAE,MAAM,EACjB,SAAS,EAAE,MAAM,GAChB;QAAE,KAAK,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;QAAC,OAAO,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;KAAE;IA+B/D;;;;;OAKG;IACG,kBAAkB,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IA0D5E;;;;;;;;;OASG;WACU,mCAAmC,CAC9C,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,EACd,cAAc,EAAE,MAAM,EACtB,gBAAgB,EAAE,MAAM,EACxB,OAAO,EAAE,MAAM,GACd,OAAO,CAAC,OAAO,CAAC;CAyFpB;AAED,OAAO,EAAE,iBAAiB,EAAE,CAAC"}
@@ -111,7 +111,10 @@ class DataIntegrityTree {
111
111
  }
112
112
  }
113
113
  static from(storeId, options) {
114
- return new DataIntegrityTree(storeId, { ...options, disableInitialize: true });
114
+ return new DataIntegrityTree(storeId, {
115
+ ...options,
116
+ disableInitialize: true,
117
+ });
115
118
  }
116
119
  /**
117
120
  * Load the manifest file.
@@ -220,14 +223,19 @@ class DataIntegrityTree {
220
223
  hash: combinedHash,
221
224
  sha256: sha256,
222
225
  });
226
+ await new Promise((resolve) => setTimeout(resolve, 100));
223
227
  this._rebuildTree();
228
+ await new Promise((resolve) => setTimeout(resolve, 100));
224
229
  resolve();
225
230
  }
226
231
  catch (err) {
227
232
  reject(err);
228
233
  }
234
+ tempWriteStream.end();
235
+ finalWriteStream.end();
229
236
  });
230
237
  tempWriteStream.on("error", (err) => {
238
+ tempWriteStream.end();
231
239
  reject(err);
232
240
  });
233
241
  readStream.on("error", (err) => {
@@ -337,7 +345,9 @@ class DataIntegrityTree {
337
345
  ? fs.readFileSync(manifestPath, "utf-8").trim().split("\n")
338
346
  : [];
339
347
  // Check if the last entry is the same as the rootHash to avoid duplicates
340
- const latestRootHash = manifestContent.length > 0 ? manifestContent[manifestContent.length - 1] : null;
348
+ const latestRootHash = manifestContent.length > 0
349
+ ? manifestContent[manifestContent.length - 1]
350
+ : null;
341
351
  if (latestRootHash !== rootHash) {
342
352
  // Append the new rootHash if it is not the same as the last one
343
353
  fs.appendFileSync(manifestPath, `${rootHash}\n`);
@@ -413,7 +423,7 @@ class DataIntegrityTree {
413
423
  * @param rootHash - The root hash of the tree. Defaults to the latest root hash.
414
424
  * @returns The readable stream for the file.
415
425
  */
416
- getValueStream(hexKey, rootHash = null) {
426
+ getValueStream(hexKey, rootHash = null, byteOffset = null, length = null) {
417
427
  if (!isHexString(hexKey)) {
418
428
  throw new Error("key must be a valid hex string");
419
429
  }
@@ -436,8 +446,22 @@ class DataIntegrityTree {
436
446
  if (!fs.existsSync(filePath)) {
437
447
  throw new Error(`File at path ${filePath} does not exist`);
438
448
  }
439
- // Create a read stream and pipe it through a decompression stream using the same algorithm (gzip)
440
- const readStream = fs.createReadStream(filePath);
449
+ const fileSize = fs.statSync(filePath).size;
450
+ // Validate offset and length
451
+ if (byteOffset !== null && length !== null) {
452
+ if (byteOffset + length > fileSize) {
453
+ throw new Error(`Offset (${byteOffset}) and length (${length}) exceed the file size (${fileSize}).`);
454
+ }
455
+ }
456
+ // Create the read stream with optional byte range
457
+ const options = {};
458
+ if (byteOffset !== null) {
459
+ options.start = byteOffset;
460
+ }
461
+ if (length !== null && byteOffset !== null) {
462
+ options.end = byteOffset + length - 1; // `end` is inclusive, hence `byteOffset + length - 1`
463
+ }
464
+ const readStream = fs.createReadStream(filePath, options);
441
465
  const decompressStream = zlib.createGunzip();
442
466
  // Return the combined stream as a generic Readable stream
443
467
  return readStream.pipe(decompressStream);
@@ -610,8 +634,8 @@ class DataIntegrityTree {
610
634
  * @param expectedRootHash - The expected root hash of the Merkle tree.
611
635
  * @returns A boolean indicating if the SHA-256 is present in the foreign tree and the root hash matches.
612
636
  */
613
- static validateKeyIntegrityWithForeignTree(key, sha256, serializedTree, expectedRootHash) {
614
- if (!isHexString(key)) {
637
+ static async validateKeyIntegrityWithForeignTree(hexkey, sha256, serializedTree, expectedRootHash, dataDir) {
638
+ if (!isHexString(hexkey)) {
615
639
  throw new Error("key must be a valid hex string");
616
640
  }
617
641
  if (!isHexString(sha256)) {
@@ -620,29 +644,59 @@ class DataIntegrityTree {
620
644
  if (!isHexString(expectedRootHash)) {
621
645
  throw new Error("expectedRootHash must be a valid hex string");
622
646
  }
623
- // Deserialize the foreign tree
624
- const leaves = serializedTree.leaves.map((leaf) => Buffer.from(leaf, "hex"));
625
- const tree = new merkletreejs_1.MerkleTree(leaves, crypto_js_1.SHA256, { sortPairs: true });
626
- // Verify that the deserialized tree's root matches the expected root hash
627
- const treeRootHash = tree.getRoot().toString("hex");
628
- if (treeRootHash !== expectedRootHash) {
629
- console.warn(`Expected root hash ${expectedRootHash}, but got ${treeRootHash}`);
630
- return false;
647
+ // File path based on sha256
648
+ const filePath = path.join(dataDir, sha256.match(/.{1,2}/g).join("/"));
649
+ // Check if the file exists
650
+ if (!fs.existsSync(filePath)) {
651
+ throw new Error(`File at path ${filePath} does not exist`);
631
652
  }
632
- // Rebuild the files map from the serialized tree
633
- // @ts-ignore
634
- tree.files = new Map(Object.entries(serializedTree.files).map(([key, value]) => [
635
- key,
636
- { hash: value.hash, sha256: value.sha256 },
637
- ]));
638
- // Check if the SHA-256 exists in the foreign tree's files
639
- const combinedHash = crypto
640
- .createHash("sha256")
641
- .update(`${toHex(key)}/${sha256}`)
642
- .digest("hex");
643
- const leaf = Buffer.from(combinedHash, "hex");
644
- const isInTree = tree.getLeafIndex(leaf) !== -1;
645
- return isInTree;
653
+ const compressedReadStream = fs.createReadStream(filePath);
654
+ const decompressStream = zlib.createGunzip();
655
+ const hash = crypto.createHash("sha256");
656
+ // Process file decompression and hash comparison
657
+ return new Promise((resolve, reject) => {
658
+ compressedReadStream.pipe(decompressStream);
659
+ decompressStream.on("data", (chunk) => {
660
+ hash.update(chunk);
661
+ });
662
+ decompressStream.on("end", () => {
663
+ const uncompressedSha256 = hash.digest("hex");
664
+ console.log(`SHA-256 of uncompressed file: ${uncompressedSha256}`);
665
+ if (uncompressedSha256 !== sha256) {
666
+ console.warn(`File hash mismatch. Expected: ${sha256}, got: ${uncompressedSha256}`);
667
+ return resolve(false);
668
+ }
669
+ // Deserialize the foreign tree
670
+ const leaves = serializedTree.leaves.map((leaf) => Buffer.from(leaf, "hex"));
671
+ const tree = new merkletreejs_1.MerkleTree(leaves, crypto_js_1.SHA256, { sortPairs: true });
672
+ // Verify that the deserialized tree's root matches the expected root hash
673
+ const treeRootHash = tree.getRoot().toString("hex");
674
+ if (treeRootHash !== expectedRootHash) {
675
+ console.warn(`Expected root hash ${expectedRootHash}, but got ${treeRootHash}`);
676
+ return resolve(false);
677
+ }
678
+ // Rebuild the files map from the serialized tree
679
+ // @ts-ignore
680
+ tree.files = new Map(Object.entries(serializedTree.files).map(([key, value]) => [
681
+ key,
682
+ { hash: value.hash, sha256: value.sha256 },
683
+ ]));
684
+ // Check if the SHA-256 exists in the foreign tree's files
685
+ const combinedHash = crypto
686
+ .createHash("sha256")
687
+ .update(`${hexkey}/${sha256}`)
688
+ .digest("hex");
689
+ const leaf = Buffer.from(combinedHash, "hex");
690
+ const isInTree = tree.getLeafIndex(leaf) !== -1;
691
+ resolve(isInTree);
692
+ });
693
+ decompressStream.on("error", (err) => {
694
+ reject(err);
695
+ });
696
+ compressedReadStream.on("error", (err) => {
697
+ reject(err);
698
+ });
699
+ });
646
700
  }
647
701
  }
648
702
  exports.DataIntegrityTree = DataIntegrityTree;
@@ -3,6 +3,8 @@ import { Readable } from "stream";
3
3
  export declare class ContentServer {
4
4
  private ipAddress;
5
5
  private storeId;
6
+ private static certPath;
7
+ private static keyPath;
6
8
  private static readonly port;
7
9
  constructor(ipAddress: string, storeId: string);
8
10
  getKey(key: string, rootHash: string, challengeHex?: string): Promise<string>;
@@ -15,10 +17,13 @@ export declare class ContentServer {
15
17
  success: boolean;
16
18
  headers?: http.IncomingHttpHeaders;
17
19
  }>;
18
- headStore(): Promise<{
20
+ headStore(options?: {
21
+ hasRootHash: string;
22
+ }): Promise<{
19
23
  success: boolean;
20
24
  headers?: http.IncomingHttpHeaders;
21
25
  }>;
26
+ hasRootHash(rootHash: string): Promise<boolean>;
22
27
  streamKey(key: string): Promise<Readable>;
23
28
  private head;
24
29
  private fetchJson;
@@ -1 +1 @@
1
- {"version":3,"file":"ContentServer.d.ts","sourceRoot":"","sources":["../../src/DigNetwork/ContentServer.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,MAAM,CAAC;AAExB,OAAO,EAAE,QAAQ,EAAE,MAAM,QAAQ,CAAC;AAGlC,qBAAa,aAAa;IACxB,OAAO,CAAC,SAAS,CAAS;IAC1B,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAM;gBAEtB,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM;IAMjC,MAAM,CAAC,GAAG,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,YAAY,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAa7E,iBAAiB,IAAI,OAAO,CAAC,MAAM,CAAC;IAepC,YAAY,IAAI,OAAO,CAAC,GAAG,CAAC;IAM5B,cAAc,IAAI,OAAO,CAAC,GAAG,CAAC;IAM9B,cAAc,IAAI,OAAO,CAAC,GAAG,CAAC;IAM9B,YAAY,IAAI,OAAO,CAAC,GAAG,CAAC;IAM5B,OAAO,CAClB,GAAG,EAAE,MAAM,GACV,OAAO,CAAC;QAAE,OAAO,EAAE,OAAO,CAAC;QAAC,OAAO,CAAC,EAAE,IAAI,CAAC,mBAAmB,CAAA;KAAE,CAAC;IAMvD,SAAS,IAAI,OAAO,CAAC;QAChC,OAAO,EAAE,OAAO,CAAC;Q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E,OAAO,CAAC;QACjB,OAAO,CAAC,EAAE,IAAI,CAAC,mBAAmB,CAAC;KACpC,CAAC;IAUW,WAAW,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAUrD,SAAS,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,QAAQ,CAAC;YA0ClC,IAAI;YA0EJ,SAAS;YAMT,gBAAgB;YAiChB,KAAK;CA0DpB"}
@@ -4,17 +4,24 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.ContentServer = void 0;
7
+ const fs_1 = __importDefault(require("fs"));
7
8
  const http_1 = __importDefault(require("http"));
8
9
  const url_1 = require("url");
10
+ const ssl_1 = require("../utils/ssl");
9
11
  class ContentServer {
10
12
  constructor(ipAddress, storeId) {
11
13
  this.ipAddress = ipAddress;
12
14
  this.storeId = storeId;
15
+ if (!ContentServer.certPath || !ContentServer.keyPath) {
16
+ const { certPath, keyPath } = (0, ssl_1.getOrCreateSSLCerts)();
17
+ ContentServer.certPath = certPath;
18
+ ContentServer.keyPath = keyPath;
19
+ }
13
20
  }
14
21
  // Method to get the content of a specified key from the peer, with optional challenge query
15
22
  async getKey(key, rootHash, challengeHex) {
16
23
  // Construct the base URL
17
- let url = `http://${this.ipAddress}/chia.${this.storeId}.${rootHash}/${key}`;
24
+ let url = `https://${this.ipAddress}:${ContentServer.port}/chia.${this.storeId}.${rootHash}/${key}`;
18
25
  // If a challenge is provided, append it as a query parameter
19
26
  if (challengeHex) {
20
27
  url += `?challenge=${challengeHex}`;
@@ -35,38 +42,49 @@ class ContentServer {
35
42
  }
36
43
  // Method to get the .well-known information
37
44
  async getWellKnown() {
38
- const url = `http://${this.ipAddress}/.well-known`;
45
+ const url = `https://${this.ipAddress}:${ContentServer.port}/.well-known`;
39
46
  return this.fetchJson(url);
40
47
  }
41
48
  // Method to get the list of known stores
42
49
  async getKnownStores() {
43
- const url = `http://${this.ipAddress}/.well-known/stores`;
50
+ const url = `https://${this.ipAddress}:${ContentServer.port}/.well-known/stores`;
44
51
  return this.fetchJson(url);
45
52
  }
46
53
  // Method to get the index of all stores
47
54
  async getStoresIndex() {
48
- const url = `http://${this.ipAddress}/`;
55
+ const url = `https://${this.ipAddress}:${ContentServer.port}/`;
49
56
  return this.fetchJson(url);
50
57
  }
51
58
  // Method to get the index of keys in a store
52
59
  async getKeysIndex() {
53
- const url = `http://${this.ipAddress}/${this.storeId}`;
60
+ const url = `https://${this.ipAddress}:${ContentServer.port}/${this.storeId}`;
54
61
  return this.fetchJson(url);
55
62
  }
56
63
  // Method to check if a specific key exists (HEAD request)
57
64
  async headKey(key) {
58
- const url = `http://${this.ipAddress}/${this.storeId}/${key}`;
59
- return this.head(url); // Return the object from head method
65
+ const url = `https://${this.ipAddress}:${ContentServer.port}/${this.storeId}/${key}`;
66
+ return this.head(url);
60
67
  }
61
68
  // Method to check if a specific store exists (HEAD request)
62
- async headStore() {
63
- const url = `http://${this.ipAddress}/${this.storeId}`;
64
- console.log({ url });
65
- return this.head(url); // Return the object from head method
69
+ async headStore(options) {
70
+ let url = `https://${this.ipAddress}:${ContentServer.port}/${this.storeId}`;
71
+ if (options?.hasRootHash) {
72
+ url += `?hasRootHash=${options.hasRootHash}`;
73
+ }
74
+ return this.head(url);
75
+ }
76
+ async hasRootHash(rootHash) {
77
+ const { success, headers } = await this.headStore({
78
+ hasRootHash: rootHash,
79
+ });
80
+ if (success) {
81
+ return headers?.["x-has-root-hash"] === "true";
82
+ }
83
+ return false;
66
84
  }
67
85
  streamKey(key) {
68
86
  return new Promise((resolve, reject) => {
69
- const url = `http://${this.ipAddress}/${this.storeId}/${key}`;
87
+ const url = `https://${this.ipAddress}:${ContentServer.port}/${this.storeId}/${key}`;
70
88
  const urlObj = new url_1.URL(url);
71
89
  const requestOptions = {
72
90
  hostname: urlObj.hostname,
@@ -93,7 +111,7 @@ class ContentServer {
93
111
  }
94
112
  });
95
113
  request.on("error", (error) => {
96
- console.error(`Request error for ${url}:`, error);
114
+ console.error(`GET Request error for ${url}:`, error);
97
115
  reject(error);
98
116
  });
99
117
  request.end();
@@ -111,6 +129,9 @@ class ContentServer {
111
129
  (urlObj.protocol === "http:" ? 80 : ContentServer.port),
112
130
  path: urlObj.pathname + urlObj.search,
113
131
  method: "HEAD",
132
+ key: fs_1.default.readFileSync(ContentServer.keyPath),
133
+ cert: fs_1.default.readFileSync(ContentServer.certPath),
134
+ rejectUnauthorized: false,
114
135
  };
115
136
  const request = http_1.default.request(requestOptions, (response) => {
116
137
  const { statusCode, headers } = response;
@@ -149,7 +170,7 @@ class ContentServer {
149
170
  }
150
171
  });
151
172
  request.on("error", (error) => {
152
- console.error(`Request error for ${url}:`, error);
173
+ console.error(`HEAD ${url}:`, error.message);
153
174
  reject({ success: false });
154
175
  });
155
176
  request.end();
@@ -201,6 +222,9 @@ class ContentServer {
201
222
  port: urlObj.port || ContentServer.port,
202
223
  path: urlObj.pathname + urlObj.search, // Include query params
203
224
  method: "GET",
225
+ key: fs_1.default.readFileSync(ContentServer.keyPath),
226
+ cert: fs_1.default.readFileSync(ContentServer.certPath),
227
+ rejectUnauthorized: false,
204
228
  };
205
229
  const request = http_1.default.request(requestOptions, (response) => {
206
230
  let data = "";
@@ -232,7 +256,7 @@ class ContentServer {
232
256
  }
233
257
  });
234
258
  request.on("error", (error) => {
235
- console.error(`Request error for ${url}:`, error);
259
+ console.error(`GET ${url}:`, error.message);
236
260
  reject(error);
237
261
  });
238
262
  request.end();
@@ -240,4 +264,4 @@ class ContentServer {
240
264
  }
241
265
  }
242
266
  exports.ContentServer = ContentServer;
243
- ContentServer.port = 80;
267
+ ContentServer.port = 4161;
@@ -5,15 +5,12 @@ export declare class DigNetwork {
5
5
  private storeDir;
6
6
  private peerBlacklist;
7
7
  constructor(storeId: string);
8
- private uploadPreflight;
9
- uploadStoreHead(digPeer: DigPeer): Promise<void>;
10
- uploadStore(digPeer: DigPeer): Promise<void>;
11
8
  static subscribeToStore(storeId: string): Promise<void>;
9
+ static findPeerWithStoreKey(storeId: string, rootHash: string, key?: string, intialBlackList?: string[]): Promise<DigPeer | null>;
12
10
  static unsubscribeFromStore(storeId: string): void;
13
- downloadFiles(forceDownload?: boolean, renderProgressBar?: boolean, skipData?: boolean): Promise<void>;
11
+ downloadFiles(skipData?: boolean): Promise<void>;
14
12
  private fetchAvailablePeers;
15
13
  private downloadHeightFile;
16
- private downloadManifestFile;
17
14
  private downloadFileFromPeers;
18
15
  private runProgressBar;
19
16
  }
@@ -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;WA+BrC,gBAAgB,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;WAQtD,oBAAoB,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI;IAQ5C,aAAa,CACxB,aAAa,GAAE,OAAe,EAC9B,iBAAiB,GAAE,OAAc,EACjC,QAAQ,GAAE,OAAe,GACxB,OAAO,CAAC,IAAI,CAAC;YAqFF,mBAAmB;YAWnB,kBAAkB;YASlB,oBAAoB;YASpB,qBAAqB;YAuErB,cAAc;CAuB7B"}
1
+ {"version":3,"file":"DigNetwork.d.ts","sourceRoot":"","sources":["../../src/DigNetwork/DigNetwork.ts"],"names":[],"mappings":"AAGA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