mirror of
https://github.com/EvernodeXRPL/hpcore.git
synced 2026-07-29 18:10:38 +00:00
Implemented user connection challenge handshake (#20)
Implemented user connection challenge handshake. Optimized user challenge message processing.
This commit is contained in:
2
examples/hpclient/.gitignore
vendored
Normal file
2
examples/hpclient/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
node_modules/**
|
||||
.hp_client_keys
|
||||
104
examples/hpclient/client.js
Normal file
104
examples/hpclient/client.js
Normal file
@@ -0,0 +1,104 @@
|
||||
//
|
||||
// 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.');
|
||||
});
|
||||
}
|
||||
60
examples/hpclient/package-lock.json
generated
Normal file
60
examples/hpclient/package-lock.json
generated
Normal file
@@ -0,0 +1,60 @@
|
||||
{
|
||||
"requires": true,
|
||||
"lockfileVersion": 1,
|
||||
"dependencies": {
|
||||
"async-limiter": {
|
||||
"version": "1.0.1",
|
||||
"resolved": "https://registry.npmjs.org/async-limiter/-/async-limiter-1.0.1.tgz",
|
||||
"integrity": "sha512-csOlWGAcRFJaI6m+F2WKdnMKr4HhdhFVBk0H/QbJFMCr+uO2kwohwXQPxw/9OCxp05r5ghVBFSyioixx3gfkNQ=="
|
||||
},
|
||||
"fs-extra": {
|
||||
"version": "8.1.0",
|
||||
"resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-8.1.0.tgz",
|
||||
"integrity": "sha512-yhlQgA6mnOJUKOsRUFsgJdQCvkKhcz8tlZG5HBQfReYZy46OwLcY+Zia0mtdHsOo9y/hP+CxMN0TU9QxoOtG4g==",
|
||||
"requires": {
|
||||
"graceful-fs": "^4.2.0",
|
||||
"jsonfile": "^4.0.0",
|
||||
"universalify": "^0.1.0"
|
||||
}
|
||||
},
|
||||
"graceful-fs": {
|
||||
"version": "4.2.2",
|
||||
"resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.2.2.tgz",
|
||||
"integrity": "sha512-IItsdsea19BoLC7ELy13q1iJFNmd7ofZH5+X/pJr90/nRoPEX0DJo1dHDbgtYWOhJhcCgMDTOw84RZ72q6lB+Q=="
|
||||
},
|
||||
"jsonfile": {
|
||||
"version": "4.0.0",
|
||||
"resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-4.0.0.tgz",
|
||||
"integrity": "sha1-h3Gq4HmbZAdrdmQPygWPnBDjPss=",
|
||||
"requires": {
|
||||
"graceful-fs": "^4.1.6"
|
||||
}
|
||||
},
|
||||
"libsodium": {
|
||||
"version": "0.7.5",
|
||||
"resolved": "https://registry.npmjs.org/libsodium/-/libsodium-0.7.5.tgz",
|
||||
"integrity": "sha512-0YVU2QJc5sDR5HHkGCaliYImS7pGeXi11fiOfm4DirBd96PJVZIn3LJa06ZOFjLNsWkL3UbNjYhLRUOABPL9vw=="
|
||||
},
|
||||
"libsodium-wrappers": {
|
||||
"version": "0.7.5",
|
||||
"resolved": "https://registry.npmjs.org/libsodium-wrappers/-/libsodium-wrappers-0.7.5.tgz",
|
||||
"integrity": "sha512-QE9Q+FxLLGdJRiJTuC2GB3LEHZeHX/VcbMQeNPdAixEKo86JPy6bOWND1XmMLu0tjWUu0xIY0YpJYQApxIZwbQ==",
|
||||
"requires": {
|
||||
"libsodium": "0.7.5"
|
||||
}
|
||||
},
|
||||
"universalify": {
|
||||
"version": "0.1.2",
|
||||
"resolved": "https://registry.npmjs.org/universalify/-/universalify-0.1.2.tgz",
|
||||
"integrity": "sha512-rBJeI5CXAlmy1pV+617WB9J63U6XcazHHF2f2dbJix4XzpUF0RS3Zbj0FGIOCAva5P/d/GBOYaACQ1w+0azUkg=="
|
||||
},
|
||||
"ws": {
|
||||
"version": "7.1.2",
|
||||
"resolved": "https://registry.npmjs.org/ws/-/ws-7.1.2.tgz",
|
||||
"integrity": "sha512-gftXq3XI81cJCgkUiAVixA0raD9IVmXqsylCrjRygw4+UOOGzPoxnQ6r/CnVL9i+mDncJo94tSkyrtuuQVBmrg==",
|
||||
"requires": {
|
||||
"async-limiter": "^1.0.0"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
7
examples/hpclient/package.json
Normal file
7
examples/hpclient/package.json
Normal file
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"dependencies": {
|
||||
"fs-extra": "^8.1.0",
|
||||
"libsodium-wrappers": "^0.7.5",
|
||||
"ws": "^7.1.2"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user