@dignetwork/dig-sdk 0.0.1-alpha.151 → 0.0.1-alpha.153

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.
@@ -143,9 +143,9 @@ class ServerCoin {
143
143
  // Sample server coins by epoch
144
144
  async sampleServerCoinsByEpoch(epoch, sampleSize = 5, blacklist = []) {
145
145
  // We dont want our own IP to be included
146
- const myIp = await (0, network_1.getPublicIpAddress)();
147
- if (myIp) {
148
- blacklist.push(myIp);
146
+ const host = await (0, network_1.getPublicHost)();
147
+ if (host) {
148
+ blacklist.push(host);
149
149
  }
150
150
  const serverCoinPeers = await this.getAllEpochPeers(epoch, blacklist);
151
151
  if (Environment_1.Environment.DEBUG) {
@@ -1 +1 @@
1
- {"version":3,"file":"Environment.d.ts","sourceRoot":"","sources":["../../src/utils/Environment.ts"],"names":[],"mappings":"AAGA,qBAAa,WAAW;IACtB,OAAO,CAAC,MAAM,CAAC,OAAO,CAAkB;IAGxC,OAAO,CAAC,MAAM,CAAC,SAAS;IAOxB,OAAO,CAAC,MAAM,CAAC,mBAAmB;IAQlC,OAAO,CAAC,MAAM,CAAC,WAAW;IAK1B,OAAO,CAAC,MAAM,CAAC,eAAe;IAK9B,OAAO,CAAC,MAAM,CAAC,eAAe;IAO9B,MAAM,KAAK,YAAY,IAAI,MAAM,GAAG,SAAS,CAG5C;IAGD,MAAM,KAAK,YAAY,IAAI,MAAM,GAAG,SAAS,CAG5C;IAGD,MAAM,KAAK,gBAAgB,IAAI,MAAM,GAAG,SAAS,CAGhD;IAGD,MAAM,KAAK,qBAAqB,IAAI,MAAM,GAAG,SAAS,CAGrD;IAGD,MAAM,KAAK,SAAS,IAAI,MAAM,GAAG,SAAS,CAGzC;IAGD,MAAM,KAAK,sBAAsB,IAAI,MAAM,GAAG,SAAS,CAEtD;IAGD,MAAM,KAAK,cAAc,IAAI,OAAO,GAAG,SAAS,CAG/C;IAGD,MAAM,KAAK,KAAK,IAAI,OAAO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G,SAAS,CAEzC;CACF"}
@@ -16,7 +16,8 @@ class Environment {
16
16
  static isValidHostnameOrIp(hostname) {
17
17
  // Hostname regex (simple, allows subdomains but not special characters)
18
18
  const hostnamePattern = /^(([a-zA-Z0-9](-*[a-zA-Z0-9])*)\.)*([a-zA-Z0-9](-*[a-zA-Z0-9])*)\.?$/;
19
- return this.isValidIp(hostname) || hostnamePattern.test(hostname);
19
+ const ipv6Pattern = /^(([0-9a-fA-F]{1,4}:){7}([0-9a-fA-F]{1,4}|:)|(([0-9a-fA-F]{1,4}:){1,7}|:):(([0-9a-fA-F]{1,4}:){1,6}|:):([0-9a-fA-F]{1,4}|:):([0-9a-fA-F]{1,4}|:)|::)$/;
20
+ return this.isValidIp(hostname) || ipv6Pattern.test(hostname) || hostnamePattern.test(hostname);
20
21
  }
21
22
  // Helper to validate if a number is a valid port (between 1 and 65535)
22
23
  static isValidPort(port) {
@@ -55,7 +56,7 @@ class Environment {
55
56
  // Static getter for PUBLIC_IP (valid IP)
56
57
  static get PUBLIC_IP() {
57
58
  const value = process.env["PUBLIC_IP"];
58
- return value && this.isValidIp(value) ? value : undefined;
59
+ return value && this.isValidHostnameOrIp(value) ? value : undefined;
59
60
  }
60
61
  // Static getter for DISK_SPACE_LIMIT_BYTES (number, optional)
61
62
  static get DISK_SPACE_LIMIT_BYTES() {
@@ -1 +1 @@
1
- {"version":3,"file":"PeerRanker.d.ts","sourceRoot":"","sources":["../../src/utils/PeerRanker.ts"],"names":[],"mappings":"AAUA,MAAM,WAAW,WAAW;IAC1B,EAAE,EAAE,MAAM,CAAC;IACX,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,EAAE,MAAM,CAAC;CACnB;AAED;;GAEG;AACH,qBAAa,UAAU;IACrB,OAAO,CAAC,WAAW,CAAW;IAC9B,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAS;IAChC,OAAO,CAAC,MAAM,CAAC,OAAO,CAAS;IAC/B,OAAO,CAAC,OAAO,CAAS;IACxB,OAA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IAAI,IAAI;CAG7B"}
@@ -42,7 +42,6 @@ class PeerRanker {
42
42
  async measureLatency(ip) {
43
43
  const cachedMetrics = peerCache.get(ip);
44
44
  if (cachedMetrics && cachedMetrics.latency) {
45
- console.log(`Latency for IP ${ip} retrieved from cache.`);
46
45
  return cachedMetrics.latency;
47
46
  }
48
47
  const url = `https://${ip}:4159/diagnostics/ping`;
@@ -83,7 +82,6 @@ class PeerRanker {
83
82
  return latency;
84
83
  }
85
84
  catch (error) {
86
- console.error(`Latency measurement failed for IP ${ip}:`, error.message);
87
85
  throw new Error(`Latency measurement failed for IP ${ip}`);
88
86
  }
89
87
  }
@@ -128,7 +126,6 @@ class PeerRanker {
128
126
  return bandwidth;
129
127
  }
130
128
  catch (error) {
131
- console.error(`Bandwidth measurement failed for IP ${ip}:`, error.message);
132
129
  throw new Error(`Bandwidth measurement failed for IP ${ip}`);
133
130
  }
134
131
  }
@@ -1,3 +1,3 @@
1
- export declare const getPublicIpAddress: () => Promise<string | undefined>;
1
+ export declare const getPublicHost: () => Promise<string | undefined>;
2
2
  export declare const formatHost: (host: string) => string;
3
3
  //# sourceMappingURL=network.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"network.d.ts","sourceRoot":"","sources":["../../src/utils/network.ts"],"names":[],"mappings":"AAoBA,eAAO,MAAM,kBAAkB,QAAa,OAAO,CAAC,MAAM,GAAG,SAAS,CA6CrE,CAAC;AAGF,eAAO,MAAM,UAAU,SAAU,MAAM,KAAG,MAUzC,CAAC"}
1
+ {"version":3,"file":"network.d.ts","sourceRoot":"","sources":["../../src/utils/network.ts"],"names":[],"mappings":"AAoBA,eAAO,MAAM,aAAa,QAAa,OAAO,CAAC,MAAM,GAAG,SAAS,CA+ChE,CAAC;AAIF,eAAO,MAAM,UAAU,SAAU,MAAM,KAAG,MAUzC,CAAC"}
@@ -3,29 +3,28 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.formatHost = exports.getPublicIpAddress = void 0;
7
- /*
8
- * Stopgap until better solution for finding public IPS found
9
- */
6
+ exports.formatHost = exports.getPublicHost = void 0;
10
7
  const superagent_1 = __importDefault(require("superagent"));
11
8
  const Environment_1 = require("./Environment");
12
9
  const MAX_RETRIES = 5;
13
10
  const RETRY_DELAY = 2000; // in milliseconds
14
11
  // Regular expression for validating both IPv4 and IPv6 addresses
15
12
  const ipv4Regex = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
16
- const ipv6Regex = /^(([0-9a-fA-F]{1,4}:){7}([0-9a-fA-F]{1,4}|:)|(([0-9a-fA-F]{1,4}:){1,7}|:):(([0-9a-fA-F]{1,4}:){1,6}|:):([0-9a-fA-F]{1,4}|:):([0-9a-fA-F]{1,4}|:)|::)$/;
17
- // Helper function to validate the IP address
18
- const isValidIp = (ip) => {
19
- return ipv4Regex.test(ip) || ipv6Regex.test(ip);
13
+ const ipv6Regex = /^(([0-9a-fA-F]{1,4}:){7}([0-9a-fA-F]{1,4}|:)|(([0-9a-fA-F]{1,7}|:):){1,7}([0-9a-fA-F]{1,4}|:))$/;
14
+ // Regular expression for validating hostnames
15
+ const hostnameRegex = /^(([a-zA-Z0-9](-*[a-zA-Z0-9])*)\.)*[a-zA-Z]{2,}$/;
16
+ // Helper function to validate the IP address or hostname
17
+ const isValidHost = (host) => {
18
+ return ipv4Regex.test(host) || ipv6Regex.test(host) || hostnameRegex.test(host);
20
19
  };
21
- const getPublicIpAddress = async () => {
22
- const publicIp = Environment_1.Environment.PUBLIC_IP;
23
- if (publicIp) {
24
- console.log("Public IP address from env:", publicIp);
25
- if (isValidIp(publicIp)) {
26
- return publicIp;
20
+ const getPublicHost = async () => {
21
+ const publicHost = Environment_1.Environment.PUBLIC_IP;
22
+ if (publicHost) {
23
+ console.log("Public IP/Hostname from env:", publicHost);
24
+ if (isValidHost(publicHost)) {
25
+ return publicHost;
27
26
  }
28
- console.error("Invalid public IP address in environment variable");
27
+ console.error("Invalid public IP/Hostname in environment variable");
29
28
  return undefined;
30
29
  }
31
30
  let attempt = 0;
@@ -34,24 +33,24 @@ const getPublicIpAddress = async () => {
34
33
  const response = await superagent_1.default.get("https://api.datalayer.storage/user/v1/get_user_ip");
35
34
  if (response.body && response.body.success) {
36
35
  const ipAddress = response.body.ip_address;
37
- if (isValidIp(ipAddress)) {
36
+ if (isValidHost(ipAddress)) {
38
37
  return ipAddress;
39
38
  }
40
- throw new Error("Invalid IP address format received");
39
+ throw new Error("Invalid IP address or hostname format received");
41
40
  }
42
- throw new Error("Failed to retrieve public IP address");
41
+ throw new Error("Failed to retrieve public host");
43
42
  }
44
43
  catch (error) {
45
44
  attempt++;
46
- console.error(`Error fetching public IP address (Attempt ${attempt}):`, error.message);
45
+ console.error(`Error fetching public host (Attempt ${attempt}):`, error.message);
47
46
  if (attempt >= MAX_RETRIES) {
48
- throw new Error("Could not retrieve public IP address after several attempts");
47
+ throw new Error("Could not retrieve public host after several attempts");
49
48
  }
50
49
  await new Promise((resolve) => setTimeout(resolve, RETRY_DELAY));
51
50
  }
52
51
  }
53
52
  };
54
- exports.getPublicIpAddress = getPublicIpAddress;
53
+ exports.getPublicHost = getPublicHost;
55
54
  // Helper function to wrap IPv6 addresses in brackets
56
55
  const formatHost = (host) => {
57
56
  const ipv6Pattern = /^[a-fA-F0-9:]+$/; // Simple regex to match raw IPv6 addresses (without brackets)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dignetwork/dig-sdk",
3
- "version": "0.0.1-alpha.151",
3
+ "version": "0.0.1-alpha.153",
4
4
  "description": "",
5
5
  "type": "commonjs",
6
6
  "main": "./dist/index.js",