mirror of
https://github.com/EvernodeXRPL/hpcore.git
synced 2026-04-29 15:37:59 +00:00
User input json/bson format improvements. (#174)
* Removed hex encoding in json input container. * Refactored client lib exports.
This commit is contained in:
@@ -3,11 +3,11 @@ const readline = require('readline');
|
||||
const { exit } = require('process');
|
||||
const bson = require('bson');
|
||||
var path = require("path");
|
||||
const { HotPocketClient, HotPocketKeyGenerator, HotPocketEvents } = require('./hp-client-lib');
|
||||
const HotPocket = require('./hp-client-lib');
|
||||
|
||||
async function main() {
|
||||
|
||||
const keys = await HotPocketKeyGenerator.generate();
|
||||
const keys = await HotPocket.KeyGenerator.generate();
|
||||
|
||||
const pkhex = Buffer.from(keys.publicKey).toString('hex');
|
||||
console.log('My public key is: ' + pkhex);
|
||||
@@ -15,7 +15,7 @@ async function main() {
|
||||
let server = 'wss://localhost:8080'
|
||||
if (process.argv.length == 3) server = 'wss://localhost:' + process.argv[2]
|
||||
if (process.argv.length == 4) server = 'wss://' + process.argv[2] + ':' + process.argv[3]
|
||||
const hpc = new HotPocketClient(server, keys);
|
||||
const hpc = new HotPocket.Client(server, keys, HotPocket.protocols.bson);
|
||||
|
||||
// Establish HotPocket connection.
|
||||
if (!await hpc.connect()) {
|
||||
@@ -25,13 +25,13 @@ async function main() {
|
||||
console.log('HotPocket Connected.');
|
||||
|
||||
// This will get fired if HP server disconnects unexpectedly.
|
||||
hpc.on(HotPocketEvents.disconnect, () => {
|
||||
hpc.on(HotPocket.events.disconnect, () => {
|
||||
console.log('Server diconnected');
|
||||
exit();
|
||||
})
|
||||
|
||||
// This will get fired when contract sends an output.
|
||||
hpc.on(HotPocketEvents.contractOutput, (output) => {
|
||||
hpc.on(HotPocket.events.contractOutput, (output) => {
|
||||
const result = bson.deserialize(output);
|
||||
if (result.type == "uploadResult") {
|
||||
if (result.status == "ok")
|
||||
@@ -51,7 +51,7 @@ async function main() {
|
||||
})
|
||||
|
||||
// This will get fired when contract sends a read response.
|
||||
hpc.on(HotPocketEvents.contractReadResponse, (response) => {
|
||||
hpc.on(HotPocket.events.contractReadResponse, (response) => {
|
||||
const result = bson.deserialize(response);
|
||||
if (result.type == "downloadResult") {
|
||||
if (result.status == "ok") {
|
||||
|
||||
@@ -7,8 +7,8 @@ const bson = require('bson');
|
||||
const isNodeJS = (typeof window === 'undefined');
|
||||
|
||||
const protocols = {
|
||||
JSON: "json",
|
||||
BSON: "bson"
|
||||
json: "json",
|
||||
bson: "bson"
|
||||
}
|
||||
Object.freeze(protocols);
|
||||
|
||||
@@ -40,7 +40,7 @@ const HotPocketKeyGenerator = {
|
||||
},
|
||||
}
|
||||
|
||||
function HotPocketClient(server, keys, protocol = protocols.BSON) {
|
||||
function HotPocketClient(server, keys, protocol = protocols.json) {
|
||||
|
||||
let ws = null;
|
||||
const msgHelper = new MessageHelper(keys, protocol);
|
||||
@@ -86,7 +86,7 @@ function HotPocketClient(server, keys, protocol = protocols.BSON) {
|
||||
msg = rcvd.data;
|
||||
}
|
||||
else {
|
||||
msg = (handshakeResolver || protocol == protocols.JSON) ?
|
||||
msg = (handshakeResolver || protocol == protocols.json) ?
|
||||
await rcvd.data.text() :
|
||||
Buffer.from(await rcvd.data.arrayBuffer());
|
||||
}
|
||||
@@ -105,7 +105,7 @@ function HotPocketClient(server, keys, protocol = protocols.BSON) {
|
||||
// sign the challenge and send back the response
|
||||
const response = msgHelper.createHandshakeResponse(m.challenge);
|
||||
ws.send(JSON.stringify(response));
|
||||
|
||||
|
||||
setTimeout(() => {
|
||||
// If we are still connected, report handshaking as successful.
|
||||
// (If websocket disconnects, handshakeResolver will be null)
|
||||
@@ -214,19 +214,25 @@ function MessageHelper(keys, protocol) {
|
||||
|
||||
this.binaryEncode = function (data) {
|
||||
const buffer = Buffer.isBuffer(data) ? data : Buffer.from(data);
|
||||
return protocol == protocols.JSON ? buffer.toString("hex") : buffer;
|
||||
return protocol == protocols.json ? buffer.toString("hex") : buffer;
|
||||
}
|
||||
|
||||
this.binaryDecode = function (content) {
|
||||
return (protocol == protocols.JSON) ? Buffer.from(content, "hex") : content.buffer;
|
||||
return (protocol == protocols.json) ? Buffer.from(content, "hex") : content.buffer;
|
||||
}
|
||||
|
||||
this.serializeObject = function (obj) {
|
||||
return protocol == protocols.JSON ? Buffer.from(JSON.stringify(obj)) : bson.serialize(obj);
|
||||
return protocol == protocols.json ? JSON.stringify(obj) : bson.serialize(obj);
|
||||
}
|
||||
|
||||
this.deserializeMessage = function (m) {
|
||||
return protocol == protocols.JSON ? JSON.parse(m) : bson.deserialize(m);
|
||||
return protocol == protocols.json ? JSON.parse(m) : bson.deserialize(m);
|
||||
}
|
||||
|
||||
this.serializeInput = function (input) {
|
||||
return protocol == protocols.json ?
|
||||
input.toString() :
|
||||
Buffer.isBuffer(input) ? input : Buffer.from(input);
|
||||
}
|
||||
|
||||
this.createHandshakeResponse = function (challenge) {
|
||||
@@ -248,17 +254,17 @@ function MessageHelper(keys, protocol) {
|
||||
return null;
|
||||
|
||||
const inpContainer = {
|
||||
input: this.binaryEncode(input),
|
||||
input: this.serializeInput(input),
|
||||
nonce: nonce,
|
||||
max_lcl_seqno: maxLclSeqNo
|
||||
}
|
||||
|
||||
const inpContainerBytes = this.serializeObject(inpContainer);
|
||||
const sigBytes = sodium.crypto_sign_detached(Buffer.from(inpContainerBytes), keys.privateKey);
|
||||
const serlializedInpContainer = this.serializeObject(inpContainer);
|
||||
const sigBytes = sodium.crypto_sign_detached(Buffer.from(serlializedInpContainer), keys.privateKey);
|
||||
|
||||
const signedInpContainer = {
|
||||
type: "contract_input",
|
||||
input_container: this.binaryEncode(inpContainerBytes),
|
||||
input_container: serlializedInpContainer,
|
||||
sig: this.binaryEncode(sigBytes)
|
||||
}
|
||||
|
||||
@@ -272,7 +278,7 @@ function MessageHelper(keys, protocol) {
|
||||
|
||||
return {
|
||||
type: "contract_read_request",
|
||||
content: this.binaryEncode(request)
|
||||
content: this.serializeInput(request)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -281,17 +287,16 @@ function MessageHelper(keys, protocol) {
|
||||
}
|
||||
}
|
||||
|
||||
const exportObj = {
|
||||
KeyGenerator: HotPocketKeyGenerator,
|
||||
Client: HotPocketClient,
|
||||
events: events,
|
||||
protocols: protocols
|
||||
};
|
||||
|
||||
if (isNodeJS) {
|
||||
module.exports = {
|
||||
HotPocketKeyGenerator,
|
||||
HotPocketClient,
|
||||
HotPocketEvents: events
|
||||
};
|
||||
module.exports = exportObj;
|
||||
}
|
||||
else {
|
||||
window.HotPocket = {
|
||||
KeyGenerator: HotPocketKeyGenerator,
|
||||
Client: HotPocketClient,
|
||||
Events: events
|
||||
}
|
||||
window.HotPocket = exportObj;
|
||||
}
|
||||
@@ -1,10 +1,9 @@
|
||||
const readline = require('readline');
|
||||
const { exit } = require('process');
|
||||
const { HotPocketClient, HotPocketKeyGenerator, HotPocketEvents } = require('./hp-client-lib');
|
||||
const HotPocket = require('./hp-client-lib');
|
||||
|
||||
async function main() {
|
||||
|
||||
const keys = await HotPocketKeyGenerator.generate();
|
||||
const keys = await HotPocket.KeyGenerator.generate();
|
||||
|
||||
const pkhex = Buffer.from(keys.publicKey).toString('hex');
|
||||
console.log('My public key is: ' + pkhex);
|
||||
@@ -12,7 +11,7 @@ async function main() {
|
||||
let server = 'wss://localhost:8080'
|
||||
if (process.argv.length == 3) server = 'wss://localhost:' + process.argv[2]
|
||||
if (process.argv.length == 4) server = 'wss://' + process.argv[2] + ':' + process.argv[3]
|
||||
const hpc = new HotPocketClient(server, keys);
|
||||
const hpc = new HotPocket.Client(server, keys, HotPocket.protocols.json);
|
||||
|
||||
// Establish HotPocket connection.
|
||||
if (!await hpc.connect()) {
|
||||
@@ -22,18 +21,18 @@ async function main() {
|
||||
console.log('HotPocket Connected.');
|
||||
|
||||
// This will get fired if HP server disconnects unexpectedly.
|
||||
hpc.on(HotPocketEvents.disconnect, () => {
|
||||
hpc.on(HotPocket.events.disconnect, () => {
|
||||
console.log('Server disconnected');
|
||||
exit();
|
||||
})
|
||||
|
||||
// This will get fired when contract sends an output.
|
||||
hpc.on(HotPocketEvents.contractOutput, (output) => {
|
||||
hpc.on(HotPocket.events.contractOutput, (output) => {
|
||||
console.log("Contract output>> " + Buffer.from(output, "hex"));
|
||||
})
|
||||
|
||||
// This will get fired when contract sends a read response.
|
||||
hpc.on(HotPocketEvents.contractReadResponse, (response) => {
|
||||
hpc.on(HotPocket.events.contractReadResponse, (response) => {
|
||||
console.log("Contract read response>> " + Buffer.from(response, "hex"));
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user