Skip to content

Commit

Permalink
feat(core-kernel): ip address utils (#4195)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastijankuzner authored Nov 25, 2020
1 parent 9f9178d commit a776999
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 0 deletions.
94 changes: 94 additions & 0 deletions __tests__/unit/core-kernel/utils/ip-address.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import "jest-extended";

import { isValidAddress, isIPv6Address, normalizeAddress } from "@packages/core-kernel/src/utils/ip-address";

describe("isValidAddress", () => {
it("should return true for valid IPv6 address", () => {
expect(isValidAddress("2001:3984:3989::104")).toBeTrue();
});

it("should return true for localhost IPv6 address", () => {
expect(isValidAddress("::1")).toBeTrue();
});

it("should return true for :: IPv6 address", () => {
expect(isValidAddress("::")).toBeTrue();
});

it("should return true for valid IPv6 address in brackets", () => {
expect(isValidAddress("[2001:3984:3989::104]")).toBeTrue();
});

it("should return false for invalid IPv6 address", () => {
expect(isValidAddress("2001:3984:3989:104:1:2001:3984:3989:10")).toBeFalse(); // Too long address
});

it("should return true for valid IPv4 address", () => {
expect(isValidAddress("127.0.0.1")).toBeTrue();
});

it("should return true for invalid IPv4 address", () => {
expect(isValidAddress("127.0.0.300")).toBeFalse();
});


it("should return false for random string", () => {
expect(isValidAddress("random")).toBeFalse();
});
});

describe("isIPv6Address", () => {
it("should return true for valid IPv6 address", () => {
expect(isIPv6Address("2001:3984:3989::104")).toBeTrue();
});

it("should return true for localhost IPv6 address", () => {
expect(isIPv6Address("::1")).toBeTrue();
});

it("should return true for :: IPv6 address", () => {
expect(isIPv6Address("::")).toBeTrue();
});

it("should return true for valid IPv6 address in brackets", () => {
expect(isIPv6Address("[2001:3984:3989::104]")).toBeTrue();
});

it("should return false for invalid IPv6 address", () => {
expect(isIPv6Address("2001:3984:3989:104:1:2001:3984:3989:10")).toBeFalse(); // Too long address
});

it("should return false for valid IPv4 address", () => {
expect(isIPv6Address("127.0.0.1")).toBeFalse();
});

it("should return false for random string", () => {
expect(isIPv6Address("random")).toBeFalse();
});
});

describe("normalizeAddress", () => {
it("should return normalized IPv6 address", () => {
expect(normalizeAddress("2001:3984:3989::104")).toEqual("[2001:3984:3989::104]");
});

it("should return normalized localhost IPv6 address", () => {
expect(normalizeAddress("::1")).toEqual("[::1]");
});

it("should return normalized :: IPv6 address", () => {
expect(normalizeAddress("::")).toEqual("[::]");
});

it("should keep normalized IPv6 address in brackets", () => {
expect(normalizeAddress("[2001:3984:3989::104]")).toEqual("[2001:3984:3989::104]");
});

it("should return same IPv4 address", () => {
expect(normalizeAddress("127.0.0.1")).toEqual("127.0.0.1");
});

it("should return same random string", () => {
expect(normalizeAddress("random")).toEqual("random");
});
});
1 change: 1 addition & 0 deletions packages/core-kernel/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"fs-extra": "^8.1.0",
"import-fresh": "^3.2.1",
"inversify": "^5.0.1",
"ipaddr.js": "^2.0.0",
"log-process-errors": "^5.0.3",
"nanomatch": "^1.2.13",
"nsfw": "^2.0.0",
Expand Down
1 change: 1 addition & 0 deletions packages/core-kernel/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { isBlacklisted } from "./is-blacklisted";
import { getBlockNotChainedErrorMessage, isBlockChained } from "./is-block-chained";
import { isWhitelisted } from "./is-whitelisted";
import { calculateRound, isNewRound } from "./round-calculator";
export * as IpAddress from "./ip-address";
export * as Search from "./search";
import { calculate } from "./supply-calculator";

Expand Down
27 changes: 27 additions & 0 deletions packages/core-kernel/src/utils/ip-address.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import ipaddr from "ipaddr.js";

export const isValidAddress = (ip: string) => {
return ipaddr.isValid(clean(ip));
};

export const isIPv6Address = (ip: string) => {
try {
return ipaddr.parse(clean(ip)).kind() === "ipv6";
} catch {}

return false;
};

export const normalizeAddress = (ip: string) => {
ip = clean(ip);

if (isIPv6Address(ip)) {
return `[${ip}]`;
}

return ip;
};

const clean = (ip: string) => {
return ip.replace("[", "").replace("]", "");
};

0 comments on commit a776999

Please sign in to comment.