mirror of
https://github.com/EvernodeXRPL/hpcore.git
synced 2026-04-29 15:37:59 +00:00
Contract and client library improvements. (#184)
* Added tty check to contract libs. * Javascript browser-native client. * Removed hex encoding in user json outputs. * Updated file contract for new contract library.
This commit is contained in:
3
examples/nodejs_client/.gitignore
vendored
3
examples/nodejs_client/.gitignore
vendored
@@ -1 +1,2 @@
|
||||
node_modules
|
||||
node_modules
|
||||
dist
|
||||
@@ -3,7 +3,7 @@ const readline = require('readline');
|
||||
const { exit } = require('process');
|
||||
const bson = require('bson');
|
||||
var path = require("path");
|
||||
const HotPocket = require('./hp-client-lib');
|
||||
const HotPocket = require('./hp-node-client-lib');
|
||||
|
||||
async function main() {
|
||||
|
||||
|
||||
@@ -103,8 +103,7 @@ function HotPocketClient(contractId, server, keys, protocol = protocols.json) {
|
||||
|
||||
if (m.type == 'handshake_challenge') {
|
||||
// Check whether contract id is matching if specified.
|
||||
if (contractId && m.contract_id != contractId)
|
||||
{
|
||||
if (contractId && m.contract_id != contractId) {
|
||||
console.error("Contract id mismatch.")
|
||||
ws.close();
|
||||
}
|
||||
@@ -121,8 +120,7 @@ function HotPocketClient(contractId, server, keys, protocol = protocols.json) {
|
||||
}, 100);
|
||||
}
|
||||
else if (m.type == 'contract_read_response') {
|
||||
const decoded = msgHelper.binaryDecode(m.content);
|
||||
emitter.emit(events.contractReadResponse, decoded);
|
||||
emitter.emit(events.contractReadResponse, msgHelper.deserializeOutput(m.content));
|
||||
}
|
||||
else if (m.type == 'contract_input_status') {
|
||||
const sigKey = (typeof m.input_sig === "string") ? m.input_sig : m.input_sig.toString("hex");
|
||||
@@ -136,8 +134,7 @@ function HotPocketClient(contractId, server, keys, protocol = protocols.json) {
|
||||
}
|
||||
}
|
||||
else if (m.type == 'contract_output') {
|
||||
const decoded = msgHelper.binaryDecode(m.content);
|
||||
emitter.emit(events.contractOutput, decoded);
|
||||
emitter.emit(events.contractOutput, msgHelper.deserializeOutput(m.content));
|
||||
}
|
||||
else if (m.type == "stat_response") {
|
||||
statResponseResolvers.forEach(resolver => {
|
||||
@@ -224,10 +221,6 @@ function MessageHelper(keys, protocol) {
|
||||
return protocol == protocols.json ? buffer.toString("hex") : buffer;
|
||||
}
|
||||
|
||||
this.binaryDecode = function (content) {
|
||||
return (protocol == protocols.json) ? Buffer.from(content, "hex") : content.buffer;
|
||||
}
|
||||
|
||||
this.serializeObject = function (obj) {
|
||||
return protocol == protocols.json ? JSON.stringify(obj) : bson.serialize(obj);
|
||||
}
|
||||
@@ -242,6 +235,10 @@ function MessageHelper(keys, protocol) {
|
||||
Buffer.isBuffer(input) ? input : Buffer.from(input);
|
||||
}
|
||||
|
||||
this.deserializeOutput = function (content) {
|
||||
return (protocol == protocols.json) ? content : content.buffer;
|
||||
}
|
||||
|
||||
this.createHandshakeResponse = function (challenge) {
|
||||
// For handshake response encoding Hot Pocket always uses json.
|
||||
// Handshake response will specify the protocol to use for subsequent messages.
|
||||
@@ -1,4 +1,4 @@
|
||||
const { HotPocketClient, HotPocketKeyGenerator, HotPocketEvents } = require('./hp-client-lib');
|
||||
const { HotPocketClient, HotPocketKeyGenerator, HotPocketEvents } = require('./hp-node-client-lib');
|
||||
|
||||
async function main() {
|
||||
|
||||
|
||||
2469
examples/nodejs_client/package-lock.json
generated
2469
examples/nodejs_client/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -1,4 +1,8 @@
|
||||
{
|
||||
"scripts": {
|
||||
"buildnode": "browserify --node -p tinyify hp-node-client-lib.js -o dist/hp-node-client-lib.js",
|
||||
"buildbrowser": "browserify -p tinyify hp-node-client-lib.js -o dist/hp-browsercompat-client-lib.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"libsodium-wrappers": "0.7.6",
|
||||
"ws": "7.1.2",
|
||||
@@ -6,5 +10,9 @@
|
||||
"bson": "4.0.4",
|
||||
"utf-8-validate": "5.0.2",
|
||||
"bufferutil": "4.0.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"browserify": "16.5.2",
|
||||
"tinyify": "3.0.0"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
const readline = require('readline');
|
||||
const { exit } = require('process');
|
||||
const HotPocket = require('./hp-client-lib');
|
||||
const HotPocket = require('./hp-node-client-lib');
|
||||
|
||||
async function main() {
|
||||
const keys = await HotPocket.KeyGenerator.generate();
|
||||
@@ -28,12 +28,12 @@ async function main() {
|
||||
|
||||
// This will get fired when contract sends an output.
|
||||
hpc.on(HotPocket.events.contractOutput, (output) => {
|
||||
console.log("Contract output>> " + Buffer.from(output, "hex"));
|
||||
console.log("Contract output>> " + output);
|
||||
})
|
||||
|
||||
// This will get fired when contract sends a read response.
|
||||
hpc.on(HotPocket.events.contractReadResponse, (response) => {
|
||||
console.log("Contract read response>> " + Buffer.from(response, "hex"));
|
||||
console.log("Contract read response>> " + response);
|
||||
})
|
||||
|
||||
// On ctrl + c we should close HP connection gracefully.
|
||||
|
||||
Reference in New Issue
Block a user