Skip to content

Commit d69e5a8

Browse files
Merge pull request #137 from DIG-Network/release/v0.0.1-alpha.152
Release/v0.0.1 alpha.152
2 parents 7c8e5da + 7fa4af5 commit d69e5a8

File tree

6 files changed

+34
-27
lines changed

6 files changed

+34
-27
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
44

5+
### [0.0.1-alpha.152](https://github.com/DIG-Network/dig-chia-sdk/compare/v0.0.1-alpha.151...v0.0.1-alpha.152) (2024-10-06)
6+
7+
8+
### Features
9+
10+
* support hostnames in publicip env ([96a721b](https://github.com/DIG-Network/dig-chia-sdk/commit/96a721ba1aeba4e1abdfb86874fc340b568b6526))
11+
512
### [0.0.1-alpha.151](https://github.com/DIG-Network/dig-chia-sdk/compare/v0.0.1-alpha.150...v0.0.1-alpha.151) (2024-10-06)
613

714

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@dignetwork/dig-sdk",
3-
"version": "0.0.1-alpha.151",
3+
"version": "0.0.1-alpha.152",
44
"description": "",
55
"type": "commonjs",
66
"main": "./dist/index.js",

src/blockchain/ServerCoin.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { NconfManager } from "../utils/NconfManager";
1414
import { CoinData, ServerCoinData } from "../types";
1515
import { DataStore } from "./DataStore";
1616
import NodeCache from "node-cache";
17-
import { getPublicIpAddress } from "../utils/network";
17+
import { getPublicHost } from "../utils/network";
1818
import { Environment } from "../utils/Environment";
1919

2020
const serverCoinCollateral = 300_000_000;
@@ -265,7 +265,7 @@ export class ServerCoin {
265265
blacklist: string[] = []
266266
): Promise<string[]> {
267267
// We dont want our own IP to be included
268-
const myIp = await getPublicIpAddress();
268+
const myIp = await getPublicHost();
269269
if (myIp) {
270270
blacklist.push(myIp);
271271
}

src/utils/PeerRanker.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ export class PeerRanker {
5353
private async measureLatency(ip: string): Promise<number> {
5454
const cachedMetrics = peerCache.get<PeerMetrics>(ip);
5555
if (cachedMetrics && cachedMetrics.latency) {
56-
console.log(`Latency for IP ${ip} retrieved from cache.`);
5756
return cachedMetrics.latency;
5857
}
5958

@@ -97,7 +96,6 @@ export class PeerRanker {
9796

9897
return latency;
9998
} catch (error: any) {
100-
console.error(`Latency measurement failed for IP ${ip}:`, error.message);
10199
throw new Error(`Latency measurement failed for IP ${ip}`);
102100
}
103101
}
@@ -148,7 +146,6 @@ export class PeerRanker {
148146

149147
return bandwidth;
150148
} catch (error: any) {
151-
console.error(`Bandwidth measurement failed for IP ${ip}:`, error.message);
152149
throw new Error(`Bandwidth measurement failed for IP ${ip}`);
153150
}
154151
}

src/utils/network.ts

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
/*
2-
* Stopgap until better solution for finding public IPS found
3-
*/
41
import superagent from "superagent";
52
import { Environment } from "./Environment";
63

@@ -11,22 +8,27 @@ const RETRY_DELAY = 2000; // in milliseconds
118
const ipv4Regex =
129
/^(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]?)$/;
1310
const ipv6Regex =
14-
/^(([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}|:)|::)$/;
11+
/^(([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}|:))$/;
1512

16-
// Helper function to validate the IP address
17-
const isValidIp = (ip: string): boolean => {
18-
return ipv4Regex.test(ip) || ipv6Regex.test(ip);
13+
// Regular expression for validating hostnames
14+
const hostnameRegex = /^(([a-zA-Z0-9](-*[a-zA-Z0-9])*)\.)*[a-zA-Z]{2,}$/;
15+
16+
// Helper function to validate the IP address or hostname
17+
const isValidHost = (host: string): boolean => {
18+
return ipv4Regex.test(host) || ipv6Regex.test(host) || hostnameRegex.test(host);
1919
};
2020

21-
export const getPublicIpAddress = async (): Promise<string | undefined> => {
22-
const publicIp = Environment.PUBLIC_IP;
21+
export const getPublicHost = async (): Promise<string | undefined> => {
22+
const publicHost = Environment.PUBLIC_IP;
23+
24+
if (publicHost) {
25+
console.log("Public IP/Hostname from env:", publicHost);
2326

24-
if (publicIp) {
25-
console.log("Public IP address from env:", publicIp);
26-
if (isValidIp(publicIp)) {
27-
return publicIp;
27+
if (isValidHost(publicHost)) {
28+
return publicHost;
2829
}
29-
console.error("Invalid public IP address in environment variable");
30+
31+
console.error("Invalid public IP/Hostname in environment variable");
3032
return undefined;
3133
}
3234

@@ -41,22 +43,22 @@ export const getPublicIpAddress = async (): Promise<string | undefined> => {
4143
if (response.body && response.body.success) {
4244
const ipAddress = response.body.ip_address;
4345

44-
if (isValidIp(ipAddress)) {
46+
if (isValidHost(ipAddress)) {
4547
return ipAddress;
4648
}
47-
throw new Error("Invalid IP address format received");
49+
throw new Error("Invalid IP address or hostname format received");
4850
}
49-
throw new Error("Failed to retrieve public IP address");
51+
throw new Error("Failed to retrieve public host");
5052
} catch (error: any) {
5153
attempt++;
5254
console.error(
53-
`Error fetching public IP address (Attempt ${attempt}):`,
55+
`Error fetching public host (Attempt ${attempt}):`,
5456
error.message
5557
);
5658

5759
if (attempt >= MAX_RETRIES) {
5860
throw new Error(
59-
"Could not retrieve public IP address after several attempts"
61+
"Could not retrieve public host after several attempts"
6062
);
6163
}
6264

@@ -65,6 +67,7 @@ export const getPublicIpAddress = async (): Promise<string | undefined> => {
6567
}
6668
};
6769

70+
6871
// Helper function to wrap IPv6 addresses in brackets
6972
export const formatHost = (host: string): string => {
7073
const ipv6Pattern = /^[a-fA-F0-9:]+$/; // Simple regex to match raw IPv6 addresses (without brackets)

0 commit comments

Comments
 (0)