Skip to content

Commit

Permalink
Add tor masternodes
Browse files Browse the repository at this point in the history
  • Loading branch information
Duddino committed Jul 4, 2023
1 parent f9f80d5 commit cdd6ed8
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 10 deletions.
23 changes: 23 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"@noble/hashes": "^1.1.5",
"@noble/secp256k1": "^1.7.0",
"@popperjs/core": "^2.11.6",
"base32": "^0.0.7",
"bech32": "^2.0.0",
"biginteger": "^1.0.3",
"bip39": "^3.0.4",
Expand Down
11 changes: 7 additions & 4 deletions scripts/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -1213,20 +1213,23 @@ export async function destroyMasternode() {

/**
* Takes an ip address and adds the port.
* If it's a tor address, ip.onion will be used (e.g. expyuzz4wqqyqhjn.onion)
* If it's an IPv4 address, ip:port will be used, (e.g. 127.0.0.1:12345)
* If it's an IPv6 address, [ip]:port will be used, (e.g. [::1]:12345)
* @param {String} ip - Ip address with or without port
* @returns {String}
*/
function parseIpAddress(ip) {
// IPv4 without port
if (ip.match(/\d+\.\d+\.\d+\.\d+/)) {
// IPv4 or tor without port
if (ip.match(/\d+\.\d+\.\d+\.\d+/) || ip.match(/\w+\.onion/)) {
return `${ip}:${cChainParams.current.MASTERNODE_PORT}`;
}
// IPv4 with port
if (ip.match(/\d+\.\d+\.\d+\.\d+:\d+/)) {

// IPv4 or tor with port
if (ip.match(/\d+\.\d+\.\d+\.\d+:\d+/) || ip.match(/\w+\.onion:\d+/)) {
return ip;
}

// IPv6 without port
if (Address6.isValid(ip)) {
return `[${ip}]:${cChainParams.current.MASTERNODE_PORT}`;
Expand Down
36 changes: 30 additions & 6 deletions scripts/masternode.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { Address6 } from 'ip-address';
import * as nobleSecp256k1 from '@noble/secp256k1';
import { OP } from './script.js';
import bs58 from 'bs58';
import base32 from 'base32';

/**
* Construct a Masternode
Expand Down Expand Up @@ -74,15 +75,38 @@ export default class Masternode {
}

/**
* @param {String} ip
* @param {Number} port
* @param {string} ip
* @param {number} port
* @returns {string} hex representation of the IP + port pair
*/
static _decodeIpAddress(ip, port) {
const address = ip.includes('.')
? Address6.fromAddress4(ip)
: new Address6(ip);
const bytes = address.toUnsignedByteArray();
/**
* @type {Array<Number>?}
*/
let bytes;
if (ip.endsWith('.onion')) {
bytes = [
0xfd,
0x87,
0xd8,
0x7e,
0xeb,
0x43,
...base32
.decode(ip.slice(0, -6))
.split('')
.map((c) => c.charCodeAt(0)),
];
console.log(bytes);
if (bytes.length !== 16) {
throw new Error('Invalid tor address');
}
} else {
const address = ip.includes('.')
? Address6.fromAddress4(ip)
: new Address6(ip);
bytes = address.toUnsignedByteArray();
}
const res =
bytesToHex([...new Array(16 - bytes.length).fill(0), ...bytes]) +
bytesToHex(Masternode._numToBytes(port, 2, false));
Expand Down
3 changes: 3 additions & 0 deletions webpack.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ module.exports = {
alias: {
'bn.js': path.join(__dirname, 'node_modules/bn.js/lib/bn.js'),
},
fallback: {
fs: false,
},
},
plugins: [
new HtmlWebpackPlugin({
Expand Down

0 comments on commit cdd6ed8

Please sign in to comment.