From b5e501e812751f9f1924cd56938a9398056fec45 Mon Sep 17 00:00:00 2001 From: Lucas Barrena Date: Sat, 21 Oct 2023 14:28:40 -0300 Subject: [PATCH] Use and support z32 (#37) * Use and support z32 * Test uses z32 --- bin/client.js | 3 ++- bin/keygen.js | 5 +++-- bin/server.js | 18 +++++++++--------- lib/client-socket.js | 3 ++- lib/get-known-peer.js | 3 ++- package.json | 1 + test/helpers/index.js | 7 ++++--- 7 files changed, 23 insertions(+), 17 deletions(-) diff --git a/bin/client.js b/bin/client.js index 8d90786..7d5aefe 100755 --- a/bin/client.js +++ b/bin/client.js @@ -6,6 +6,7 @@ const { Command } = require('commander') const Protomux = require('protomux') const DHT = require('hyperdht') const goodbye = require('graceful-goodbye') +const HypercoreId = require('hypercore-id-encoding') const { SHELLDIR } = require('../constants.js') const { ClientSocket } = require('../lib/client-socket.js') const { ShellClient } = require('../lib/shell.js') @@ -37,7 +38,7 @@ async function cmd (serverPublicKey, options = {}) { serverPublicKey = getKnownPeer(serverPublicKey) - const seed = Buffer.from(fs.readFileSync(keyfile, 'utf8'), 'hex') + const seed = HypercoreId.encode(fs.readFileSync(keyfile, 'utf8').trim()) const keyPair = DHT.keyPair(seed) const node = new DHT({ bootstrap: options.testnet ? [{ host: '127.0.0.1', port: 40838 }] : undefined }) diff --git a/bin/keygen.js b/bin/keygen.js index 6d809f3..0b47638 100755 --- a/bin/keygen.js +++ b/bin/keygen.js @@ -6,6 +6,7 @@ const readline = require('readline') const { Command } = require('commander') const Keychain = require('keypear') const DHT = require('hyperdht') +const HypercoreId = require('hypercore-id-encoding') const { SHELLDIR } = require('../constants.js') const isModule = require.main !== module @@ -52,11 +53,11 @@ async function cmd (options = {}) { const seed = Keychain.seed() fs.mkdirSync(path.dirname(keyfile), { recursive: true }) - fs.writeFileSync(keyfile, seed.toString('hex') + comment + '\n', { flag: 'wx', mode: '600' }) + fs.writeFileSync(keyfile, HypercoreId.encode(seed) + comment + '\n', { flag: 'wx', mode: '600' }) console.log('Your key has been saved in', keyfile) console.log('The public key is:') - console.log(DHT.keyPair(seed).publicKey.toString('hex')) + console.log(HypercoreId.encode(DHT.keyPair(seed).publicKey)) if (isModule) console.log() } diff --git a/bin/server.js b/bin/server.js index a4376a4..a3c80b8 100755 --- a/bin/server.js +++ b/bin/server.js @@ -7,6 +7,7 @@ const DHT = require('hyperdht') const goodbye = require('graceful-goodbye') const Protomux = require('protomux') const readFile = require('read-file-live') +const HypercoreId = require('hypercore-id-encoding') const { SHELLDIR } = require('../constants.js') const { waitForSocketTermination } = require('../lib/client-socket.js') const { ShellServer } = require('../lib/shell.js') @@ -50,8 +51,7 @@ async function cmd (options = {}) { }) goodbye(() => unwatchFirewall(), 3) } - - const seed = Buffer.from(fs.readFileSync(keyfile, 'utf8'), 'hex') + const seed = HypercoreId.decode(fs.readFileSync(keyfile, 'utf8').trim()) const keyPair = DHT.keyPair(seed) const node = new DHT({ bootstrap: options.testnet ? [{ host: '127.0.0.1', port: 40838 }] : undefined }) @@ -66,27 +66,27 @@ async function cmd (options = {}) { if (protocols === PROTOCOLS) { console.log('To connect to this shell, on another computer run:') - console.log('hypershell ' + keyPair.publicKey.toString('hex')) + console.log('hypershell ' + HypercoreId.encode(keyPair.publicKey)) } else { console.log('Running server with restricted protocols') - console.log('Server key: ' + keyPair.publicKey.toString('hex')) + console.log('Server key: ' + HypercoreId.encode(keyPair.publicKey)) } console.log() function onFirewall (remotePublicKey, remoteHandshakePayload) { if (allowed === true) { - console.log('Firewall allowed:', remotePublicKey.toString('hex')) + console.log('Firewall allowed:', HypercoreId.encode(remotePublicKey)) return false } for (const publicKey of allowed) { if (remotePublicKey.equals(publicKey)) { - console.log('Firewall allowed:', remotePublicKey.toString('hex')) + console.log('Firewall allowed:', HypercoreId.encode(remotePublicKey)) return false } } - console.log('Firewall denied:', remotePublicKey.toString('hex')) + console.log('Firewall denied:', HypercoreId.encode(remotePublicKey)) return true } } @@ -95,7 +95,7 @@ function onconnection ({ protocols, options }, socket) { const node = this.dht socket.on('end', () => socket.end()) - socket.on('close', () => console.log('Connection closed', socket.remotePublicKey.toString('hex'))) + socket.on('close', () => console.log('Connection closed', HypercoreId.encode(socket.remotePublicKey))) socket.on('error', function (error) { if (error.code === 'ECONNRESET' || error.code === 'ETIMEDOUT') return console.error(error.code, error) @@ -154,7 +154,7 @@ function readAuthorizedPeers (filename) { try { const list = typeof filename === 'string' ? fs.readFileSync(filename, 'utf8') : filename return configs.parse(list) - .map(v => Buffer.from(v, 'hex')) + .map(v => HypercoreId.decode(v)) } catch (error) { if (error.code === 'ENOENT') return [] throw error diff --git a/lib/client-socket.js b/lib/client-socket.js index fa8a3e0..fce7fbd 100644 --- a/lib/client-socket.js +++ b/lib/client-socket.js @@ -1,6 +1,7 @@ const fs = require('fs') const DHT = require('hyperdht') const goodbye = require('graceful-goodbye') +const HypercoreId = require('hypercore-id-encoding') const getKnownPeer = require('./get-known-peer.js') module.exports = { @@ -11,7 +12,7 @@ module.exports = { function ClientSocket ({ keyfile, serverPublicKey, reusableSocket = false, testnet = false }) { serverPublicKey = getKnownPeer(serverPublicKey) - const seed = Buffer.from(fs.readFileSync(keyfile, 'utf8'), 'hex') + const seed = HypercoreId.decode(fs.readFileSync(keyfile, 'utf8').trim()) const keyPair = DHT.keyPair(seed) const node = new DHT({ bootstrap: testnet ? [{ host: '127.0.0.1', port: 40838 }] : undefined }) diff --git a/lib/get-known-peer.js b/lib/get-known-peer.js index 07fff68..7db1829 100644 --- a/lib/get-known-peer.js +++ b/lib/get-known-peer.js @@ -1,6 +1,7 @@ const fs = require('fs') const path = require('path') const configs = require('tiny-configs') +const HypercoreId = require('hypercore-id-encoding') const { SHELLDIR } = require('../constants.js') module.exports = function getKnownPeer (host) { @@ -11,7 +12,7 @@ module.exports = function getKnownPeer (host) { } } - return Buffer.from(host, 'hex') + return HypercoreId.decode(host) } function readKnownPeers () { diff --git a/package.json b/package.json index f90a21f..755b8a9 100644 --- a/package.json +++ b/package.json @@ -28,6 +28,7 @@ "commander": "^9.4.1", "compact-encoding": "^2.11.0", "graceful-goodbye": "^1.1.0", + "hypercore-id-encoding": "^1.2.0", "hyperdht": "^6.6.0", "keypear": "^1.1.1", "protomux": "^3.4.0", diff --git a/test/helpers/index.js b/test/helpers/index.js index a9ba45f..2a061f1 100644 --- a/test/helpers/index.js +++ b/test/helpers/index.js @@ -7,6 +7,7 @@ const { spawn } = require('child_process') const createTestnet = require('hyperdht/testnet') const DHT = require('hyperdht') const Keychain = require('keypear') +const HypercoreId = require('hypercore-id-encoding') const BIN_KEYGEN = path.join(__dirname, '..', '..', 'bin/keygen.js') const BIN_SERVER = path.join(__dirname, '..', '..', 'bin/server.js') @@ -109,15 +110,15 @@ function spawnCopy (t, source, target, { clientkey }) { function keygen (keyfile) { const seed = Keychain.seed() fs.mkdirSync(path.dirname(keyfile), { recursive: true }) - fs.writeFileSync(keyfile, seed.toString('hex') + '\n', { flag: 'wx' }) + fs.writeFileSync(keyfile, HypercoreId.encode(seed) + '\n', { flag: 'wx' }) return DHT.keyPair(seed) } function addAuthorizedPeer (firewall, keyfile) { - const seed = Buffer.from(fs.readFileSync(keyfile, 'utf8'), 'hex') + const seed = HypercoreId.decode(fs.readFileSync(keyfile, 'utf8').trim()) const keyPair = DHT.keyPair(seed) if (!fs.existsSync(firewall)) fs.writeFileSync(firewall, '# \n', { flag: 'wx' }) - fs.appendFileSync(firewall, keyPair.publicKey.toString('hex') + '\n') + fs.appendFileSync(firewall, HypercoreId.encode(keyPair.publicKey) + '\n') } async function useTestnet (t) {