Allow Register Ipv6 ips in the enodes (#186)

* Provide Registration method to enodes with IPv6
Co-authored-by: alberto.hernandez <albherna@gmail.com>
pull/188/head
Alberto Hernandez 4 years ago committed by GitHub
parent 07b0fcd7bc
commit f353047bea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      package.json
  2. 50
      scripts/allowlist_utils.js
  3. 3
      src/components/EnodeTab/Row.tsx
  4. 59
      src/util/enodetools.ts

@ -53,7 +53,9 @@
"ethers": "^4.0.32",
"idx": "^2.5.6",
"jest-junit": "^6.4.0",
"left-pad": "^1.3.0",
"node-sass": "^4.12.0",
"pad-ipv6": "^1.0.2",
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-router-dom": "^5.0.0",

@ -1,30 +1,36 @@
const Web3Utils = require("web3-utils");
const url = require('url');
const leftPad = require('left-pad');
const padIpv6 = require("pad-ipv6");
const enodeToParams = enodeURL => {
let enodeHigh = "";
let enodeLow = "";
let ip = "";
let port = "";
let extraParams = {};
const splitURL = enodeURL.split("//")[1];
if (splitURL) {
const [enodeId, rawIpAndPort] = splitURL.split("@");
if (enodeId && enodeId.length === 128) {
enodeHigh = "0x" + enodeId.slice(0, 64);
enodeLow = "0x" + enodeId.slice(64);
}
if (rawIpAndPort) {
const [ipAndPort] = rawIpAndPort.split("?");
if (ipAndPort) {
[ip, port] = ipAndPort.split(":");
}
try {
const node = new URL(enodeURL);
if (node.protocol === 'enode:') {
if (node.username.length === 128) {
enodeHigh = "0x" + node.username.slice(0, 64);
enodeLow = "0x" + node.username.slice(64);
}
ip = parseHostname(node.hostname)
port = node.port;
node.searchParams.forEach((value, name, searchParams) => { extraParams[name.toLowerCase()] = value; });
}
} catch (err) {}
return {
enodeHigh,
enodeLow,
ip: ip ? getHexIpv4(ip) : "",
port
ip,
port,
extraParams
};
};
@ -162,6 +168,14 @@ function getRetainAccountRulesContract() {
}
function parseHostname (stringHostname) {
if (stringHostname[0] === '[') {
const ipv6 = stringHostname.slice(1,-1);
return getHexIpv6(ipv6);
}
return getHexIpv4(stringHostname);
}
function getHexIpv4(stringIp) {
const splitIp = stringIp.split(".");
return `0x00000000000000000000ffff${toHex(splitIp[0])}${toHex(
@ -169,13 +183,19 @@ function getHexIpv4(stringIp) {
)}${toHex(splitIp[2])}${toHex(splitIp[3])}`;
}
function getHexIpv6(stringIpv6) {
const ipv6 = padIpv6(stringIpv6).split(":").join('');
return '0x' + ipv6;
}
function toHex(number) {
const num = Number(number).toString(16);
return num.length < 2 ? `0${num}` : num;
return leftPad(num, 2, '0');
}
module.exports = {
enodeToParams,
parseHostname,
isInitialAdminAccountsAvailable,
isInitialAllowlistedAccountsAvailable,
isInitialAllowlistedNodesAvailable,

@ -5,6 +5,7 @@ import PropTypes from 'prop-types';
import { Pill, Flex, Button } from 'rimble-ui';
// Util Helper
import hexToIp from '../../util/ipConverter';
import { buildEnode } from '../../util/enodetools';
// Constant
import { PENDING_ADDITION, PENDING_REMOVAL, FAIL_ADDITION, FAIL_REMOVAL } from '../../constants/transactions';
// Components
@ -38,7 +39,7 @@ const EnodeRow: React.FC<EnodeRow> = ({
<tr className={styles.row}>
<td colSpan={2}>
<Flex alignItems="center" className={styles.tooltipFix}>
<TextWithTooltip isAdmin={isAdmin} status={status} text={`${enodeHigh}${enodeLow}`} />
<TextWithTooltip isAdmin={isAdmin} status={status} text={`${buildEnode(enodeHigh, enodeLow)}`} />
</Flex>
</td>
<td>

@ -1,3 +1,6 @@
const leftPad = require('left-pad');
const padIpv6 = require('pad-ipv6');
export type Enode = {
enodeHigh: string;
enodeLow: string;
@ -10,26 +13,33 @@ export const enodeToParams = (enodeURL: string) => {
let enodeLow = '';
let ip = '';
let port = '';
let extraParams = new Map();
const splitURL = enodeURL.split('//')[1];
if (splitURL) {
const [enodeId, rawIpAndPort] = splitURL.split('@');
if (enodeId && enodeId.length === 128) {
enodeHigh = '0x' + enodeId.slice(0, 64);
enodeLow = '0x' + enodeId.slice(64);
}
if (rawIpAndPort) {
const [ipAndPort] = rawIpAndPort.split('?');
if (ipAndPort) {
[ip, port] = ipAndPort.split(':');
}
try {
const node = new URL(enodeURL);
if (node.protocol === 'enode:') {
// Change to Special protocol in order to parse the fields properly
node.protocol = 'https';
if (node.username.length === 128) {
enodeHigh = '0x' + node.username.slice(0, 64);
enodeLow = '0x' + node.username.slice(64);
}
ip = parseHostname(node.hostname);
port = node.port;
node.searchParams.forEach((value, name, searchParams) => {
extraParams.set(name.toLowerCase(), value);
});
}
} catch (err) {}
return {
enodeHigh,
enodeLow,
ip: ip ? getHexIpv4(ip) : '',
port
ip,
port,
extraParams
};
};
@ -47,16 +57,35 @@ export const paramsToIdentifier = ({
return `${enodeHigh}_${enodeLow}_${ip}_${port}`;
};
function parseHostname(stringHostname: string) {
if (stringHostname[0] === '[') {
const ipv6 = stringHostname.slice(1, -1);
return getHexIpv6(ipv6);
}
return getHexIpv4(stringHostname);
}
function getHexIpv4(stringIp: string) {
const splitIp = stringIp.split('.');
return `0x00000000000000000000ffff${toHex(splitIp[0])}${toHex(splitIp[1])}${toHex(splitIp[2])}${toHex(splitIp[3])}`;
}
function getHexIpv6(stringIpv6: string) {
const ipv6 = padIpv6(stringIpv6)
.split(':')
.join('');
return '0x' + ipv6;
}
function toHex(number: string) {
const num = Number(number).toString(16);
return num.length < 2 ? `0${num}` : num;
return leftPad(num, 2, '0');
}
export const buildEnode = (enodeHigh: string, enodeLow: string) => {
return `${enodeHigh.slice(2)}${enodeLow.slice(2)}`;
};
export const identifierToParams = (identifier: string) => {
const [enodeHigh, enodeLow, ip, port] = identifier.split('_');
return {

Loading…
Cancel
Save