Skip to content

Commit

Permalink
Use and support z32 (#37)
Browse files Browse the repository at this point in the history
* Use and support z32

* Test uses z32
  • Loading branch information
LuKks authored Oct 21, 2023
1 parent 7caa00e commit b5e501e
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 17 deletions.
3 changes: 2 additions & 1 deletion bin/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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 })
Expand Down
5 changes: 3 additions & 2 deletions bin/keygen.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
Expand Down
18 changes: 9 additions & 9 deletions bin/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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 })
Expand All @@ -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
}
}
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion lib/client-socket.js
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -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 })
Expand Down
3 changes: 2 additions & 1 deletion lib/get-known-peer.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -11,7 +12,7 @@ module.exports = function getKnownPeer (host) {
}
}

return Buffer.from(host, 'hex')
return HypercoreId.decode(host)
}

function readKnownPeers () {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 4 additions & 3 deletions test/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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, '# <public key>\n', { flag: 'wx' })
fs.appendFileSync(firewall, keyPair.publicKey.toString('hex') + '\n')
fs.appendFileSync(firewall, HypercoreId.encode(keyPair.publicKey) + '\n')
}

async function useTestnet (t) {
Expand Down

0 comments on commit b5e501e

Please sign in to comment.