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:
Ravin Perera
2020-12-05 09:08:24 +05:30
committed by GitHub
parent a421f13d91
commit b2fd8ae4b5
16 changed files with 2967 additions and 125 deletions

View File

@@ -1 +1,2 @@
node_modules
node_modules
dist

View File

@@ -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() {

View File

@@ -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.

View File

@@ -1,4 +1,4 @@
const { HotPocketClient, HotPocketKeyGenerator, HotPocketEvents } = require('./hp-client-lib');
const { HotPocketClient, HotPocketKeyGenerator, HotPocketEvents } = require('./hp-node-client-lib');
async function main() {

File diff suppressed because it is too large Load Diff

View File

@@ -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"
}
}

View File

@@ -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.