Skip to content

Commit

Permalink
Add tor masternodes (#149)
Browse files Browse the repository at this point in the history
* Add tor masternodes

* comment

* Tor v3 support

---------

Co-authored-by: JSKitty <[email protected]>
  • Loading branch information
Duddino and JSKitty authored Jul 11, 2023
1 parent e262647 commit aac4a10
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 12 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 @@ -1259,20 +1259,23 @@ export async function destroyMasternode() {

/**
* Takes an ip address and adds the port.
* If it's a tor address, ip.onion:port will be used (e.g. expyuzz4wqqyqhjn.onion:12345)
* 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
40 changes: 32 additions & 8 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,18 +75,41 @@ 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')) {
const onionBytes = base32
.decode(ip.slice(0, -6))
.split('')
.map((c) => c.charCodeAt(0));
switch (onionBytes.length) {
case 10:
bytes = [0xfd, 0x87, 0xd8, 0x7e, 0xeb, 0x43, ...onionBytes];
break;
case 35:
bytes = [0x04, 32, ...onionBytes.slice(0, 32)];
break;
default:
throw new Error('Invalid onion 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));
bytesToHex([
...new Array(Math.max(16 - bytes.length, 0)).fill(0),
...bytes,
]) + bytesToHex(Masternode._numToBytes(port, 2, false));
return res;
}

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 aac4a10

Please sign in to comment.