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

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,7 +143,7 @@ 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)();
146
+ const myIp = await (0, network_1.getPublicHost)();
147
147
  if (myIp) {
148
148
  blacklist.push(myIp);
149
149
  }
@@ -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,OAAO,CAAC,cAAc,CAAS;IAG/B,OAAO,CAAC,WAAW,CAAqB;IACxC,OAAO,CAAC,YAAY,CAAa;IAEjC;;;OAGG;gBACS,WAAW,EAAE,MAAM,EAAE,EAAE,OAAO,GAAE,MAAa,EAAE,cAAc,GAAE,MAAoB;IAW/F;;;;;;OAMG;YACW,cAAc;IAoD5B;;;;;OAKG;YACW,gBAAgB;IA6C9B;;;;;OAKG;IACU,SAAS,CAAC,QAAQ,GAAE,MAAY,GAAG,OAAO,CAAC,WAAW,EAAE,CAAC;IAmCtE;;;;OAIG;IACI,eAAe,IAAI,WAAW,GAAG,IAAI;IAW5C;;OAEG;IACI,aAAa,IAAI,IAAI;CAG7B"}
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,OAAO,CAAC,cAAc,CAAS;IAG/B,OAAO,CAAC,WAAW,CAAqB;IACxC,OAAO,CAAC,YAAY,CAAa;IAEjC;;;OAGG;gBACS,WAAW,EAAE,MAAM,EAAE,EAAE,OAAO,GAAE,MAAa,EAAE,cAAc,GAAE,MAAoB;IAW/F;;;;;;OAMG;YACW,cAAc;IAkD5B;;;;;OAKG;YACW,gBAAgB;IA4C9B;;;;;OAKG;IACU,SAAS,CAAC,QAAQ,GAAE,MAAY,GAAG,OAAO,CAAC,WAAW,EAAE,CAAC;IAmCtE;;;;OAIG;IACI,eAAe,IAAI,WAAW,GAAG,IAAI;IAW5C;;OAEG;IACI,aAAa,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.152",
4
4
  "description": "",
5
5
  "type": "commonjs",
6
6
  "main": "./dist/index.js",