mirror of
https://github.com/EvernodeXRPL/hpcore.git
synced 2026-04-29 15:37:59 +00:00
Implemented user connection challenge handshake. Optimized user challenge message processing.
105 lines
2.9 KiB
JavaScript
105 lines
2.9 KiB
JavaScript
//
|
|
// HotPocket client example code adopted from:
|
|
// https://github.com/codetsunami/hotpocket/blob/master/hp_client.js
|
|
//
|
|
|
|
const fs = require('fs')
|
|
const ws_api = require('ws');
|
|
const sodium = require('libsodium-wrappers')
|
|
const readline = require('readline')
|
|
|
|
// sodium has a trigger when it's ready, we will wait and execute from there
|
|
sodium.ready.then(main).catch((e) => { console.log(e) })
|
|
|
|
|
|
function main() {
|
|
|
|
var keys = sodium.crypto_sign_keypair()
|
|
|
|
|
|
// check for client keys
|
|
if (!fs.existsSync('.hp_client_keys')) {
|
|
keys.privateKey = sodium.to_hex(keys.privateKey)
|
|
keys.publicKey = sodium.to_hex(keys.publicKey)
|
|
fs.writeFileSync('.hp_client_keys', JSON.stringify(keys))
|
|
} else {
|
|
keys = JSON.parse(fs.readFileSync('.hp_client_keys'))
|
|
keys.privateKey = Uint8Array.from(Buffer.from(keys.privateKey, 'hex'))
|
|
keys.publicKey = Uint8Array.from(Buffer.from(keys.publicKey, 'hex'))
|
|
}
|
|
|
|
|
|
var server = 'ws://localhost:8080'
|
|
|
|
if (process.argv.length == 3) server = 'ws://localhost:' + process.argv[2]
|
|
|
|
if (process.argv.length == 4) server = 'ws://' + process.argv[2] + ':' + process.argv[3]
|
|
|
|
var ws = new ws_api(server)
|
|
|
|
/* anatomy of a public challenge
|
|
{
|
|
hotpocket: 0.1,
|
|
type: 'public_challenge',
|
|
challenge: '<base64 string>'
|
|
}
|
|
*/
|
|
|
|
|
|
// if the console ctrl + c's us we should close ws gracefully
|
|
process.once('SIGINT', function (code) {
|
|
console.log('SIGINT received...');
|
|
ws.close()
|
|
});
|
|
|
|
ws.on('message', (m) => {
|
|
console.log("-----Received raw message-----")
|
|
console.log(m)
|
|
console.log("------------------------------")
|
|
|
|
try {
|
|
m = JSON.parse(m)
|
|
} catch (e) {
|
|
return
|
|
}
|
|
|
|
if (m.type != 'public_challenge') return
|
|
|
|
console.log("Received challenge message")
|
|
console.log(m)
|
|
|
|
console.log('My public key is: ' + Buffer.from(keys.publicKey).toString('base64'));
|
|
|
|
// sign the challenge and send back the response
|
|
var sigbytes = sodium.crypto_sign_detached(m.challenge, keys.privateKey);
|
|
var response = {
|
|
type: 'challenge_response',
|
|
challenge: m.challenge,
|
|
sig: Buffer.from(sigbytes).toString('base64'),
|
|
pubkey: Buffer.from(keys.publicKey).toString('base64')
|
|
}
|
|
|
|
console.log('Sending challenge response...');
|
|
ws.send(JSON.stringify(response))
|
|
|
|
// start listening for stdin
|
|
const rl = readline.createInterface({
|
|
input: process.stdin,
|
|
output: process.stdout
|
|
});
|
|
|
|
var input_pump = () => {
|
|
rl.question('', (answer) => {
|
|
ws.send(answer + "\n")
|
|
input_pump()
|
|
})
|
|
}
|
|
input_pump()
|
|
|
|
});
|
|
|
|
ws.on('close', () => {
|
|
console.log('Server disconnected.');
|
|
});
|
|
}
|