HP smart contract nodejs library. (#101)

This commit is contained in:
Ravin Perera
2020-07-20 13:38:08 +05:30
committed by GitHub
parent 09e72ef8bb
commit 311d20aba6
33 changed files with 154 additions and 528 deletions

1
examples/nodejs_client/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
node_modules

View File

@@ -0,0 +1 @@
{"publicKey":"705bf26354ee4c63c0e5d5d883c07cefc3196d049bd3825f827eb3bc23ead035","privateKey":"07ce6f7d6f0da38d5956ecc4ea1d18de77fc8bded089eb52199a46ffe2098c88705bf26354ee4c63c0e5d5d883c07cefc3196d049bd3825f827eb3bc23ead035","keyType":"ed25519"}

View File

@@ -0,0 +1,146 @@
const fs = require('fs');
const readline = require('readline');
const sodium = require('libsodium-wrappers');
const { exit } = require('process');
const { HotPocketClient, HotPocketProtocols, HotPocketEvents } = require('./hp-client-lib');
const bson = require('bson');
var path = require("path");
async function main() {
await sodium.ready;
let keys = {};
const key_file = '.hp_client_keys';
if (!fs.existsSync(key_file)) {
keys = sodium.crypto_sign_keypair();
keys.privateKey = sodium.to_hex(keys.privateKey)
keys.publicKey = sodium.to_hex(keys.publicKey)
fs.writeFileSync(key_file, JSON.stringify(keys))
} else {
keys = JSON.parse(fs.readFileSync(key_file))
keys.privateKey = Uint8Array.from(Buffer.from(keys.privateKey, 'hex'))
keys.publicKey = Uint8Array.from(Buffer.from(keys.publicKey, 'hex'))
}
const pkhex = 'ed' + Buffer.from(keys.publicKey).toString('hex');
console.log('My public key is: ' + pkhex);
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, HotPocketProtocols.BSON, keys);
// Establish HotPocket connection.
if (!await hpc.connect()) {
console.log('Connection failed.');
exit;
}
console.log('HotPocket Connected.');
// This will get fired if HP server disconnects unexpectedly.
hpc.on(HotPocketEvents.disconnect, () => {
console.log('Server diconnected');
exit;
})
// This will get fired when contract sends an output.
hpc.on(HotPocketEvents.contractOutput, (output) => {
const result = bson.deserialize(output);
if (result.type == "uploadResult") {
if (result.status == "ok")
console.log("File " + result.fileName + " uploaded successfully.");
else
console.log("File " + result.fileName + " upload failed. reason: " + result.status);
}
else if (result.type == "deleteResult") {
if (result.status == "ok")
console.log("File " + result.fileName + " deleted successfully.");
else
console.log("File " + result.fileName + " delete failed. reason: " + result.status);
}
else {
console.log("Unknown contract output.");
}
})
// This will get fired when contract sends a read response.
hpc.on(HotPocketEvents.contractReadResponse, (response) => {
const result = bson.deserialize(response);
if (result.type == "downloadResult") {
if (result.status == "ok") {
fs.writeFileSync(result.fileName, result.content.buffer);
console.log("File " + result.fileName + " downloaded to current directory.");
}
else {
console.log("File " + result.fileName + " download failed. reason: " + result.status);
}
}
else {
console.log("Unknown read request result.");
}
})
// On ctrl + c we should close HP connection gracefully.
process.once('SIGINT', function () {
console.log('SIGINT received...');
hpc.close();
});
// start listening for stdin
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
console.log("Ready to accept inputs.");
const input_pump = () => {
rl.question('', async (inp) => {
if (inp.startsWith("upload ")) {
const filePath = inp.substr(7);
const fileName = path.basename(filePath)
const fileContent = fs.readFileSync(filePath);
const sizeKB = Math.round(fileContent.length / 1024);
console.log("Uploading file " + fileName + " (" + sizeKB + " KB)");
const submissionStatus = await hpc.sendContractInput(bson.serialize({
type: "upload",
fileName: fileName,
content: fileContent
}), null, 100);
if (submissionStatus && submissionStatus != "ok")
console.log("Upload failed. reason: " + submissionStatus);
}
else if (inp.startsWith("delete ")) {
const fileName = inp.substr(7);
const submissionStatus = await hpc.sendContractInput(bson.serialize({
type: "delete",
fileName: fileName
}));
if (submissionStatus && submissionStatus != "ok")
console.log("Delete failed. reason: " + submissionStatus);
}
else if (inp.startsWith("download ")) {
const fileName = inp.substr(9);
hpc.sendContractReadRequest(bson.serialize({
type: "download",
fileName: fileName
}));
}
else {
console.log("Invalid command. [upload <local path> | delete <filename> | download <filename>] expected.")
}
input_pump();
})
}
input_pump();
}
main();

View File

@@ -0,0 +1,243 @@
const ws_api = require('ws');
const sodium = require('libsodium-wrappers');
const EventEmitter = require('events');
const bson = require('bson');
const protocols = {
JSON: "json",
BSON: "bson"
}
Object.freeze(protocols);
const events = {
disconnect: "disconnect",
contractOutput: "contractOutput",
contractReadResponse: "contractReadResponse"
}
Object.freeze(events);
function HotPocketClient(server, protocol, keys) {
if (protocol != protocols.JSON && protocol != protocols.BSON)
throw new Error("Protocol: 'json' or 'bson' expected.");
let ws = null;
const msgHelper = new MessageHelper(keys, protocol);
const emitter = new EventEmitter();
let handshakeResolver = null;
let statResponseResolver = null;
let contractInputResolvers = {};
this.connect = function () {
return new Promise(resolve => {
handshakeResolver = resolve;
ws = new ws_api(server, {
rejectUnauthorized: false
})
ws.on('close', () => {
// If there are any ongoing resolvers resolve them with error output.
handshakeResolver && handshakeResolver(false);
handshakeResolver = null;
statResponseResolver && statResponseResolver(null);
statResponseResolver = null;
Object.values(contractInputResolvers).forEach(resolver => resolver(null));
contractInputResolvers = {};
emitter.emit(events.disconnect);
});
ws.on('message', (msg) => {
try {
// Use JSON if we are still in handshake phase.
m = handshakeResolver ? JSON.parse(msg) : msgHelper.deserializeMessage(msg);
} catch (e) {
console.log("Exception deserializing: " + received_msg);
return;
}
if (m.type == 'handshake_challenge') {
// 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)
handshakeResolver && handshakeResolver(true);
handshakeResolver = null;
}, 100);
}
else if (m.type == 'contract_read_response') {
const decoded = msgHelper.binaryDecode(m.content);
emitter.emit(events.contractReadResponse, decoded);
}
else if (m.type == 'contract_input_status') {
const sigKey = (typeof m.input_sig === "string") ? m.input_sig : m.input_sig.toString("hex");
const resolver = contractInputResolvers[sigKey];
if (resolver) {
if (m.status == "accepted")
resolver("ok");
else
resolver(m.reason);
delete contractInputResolvers[sigKey];
}
}
else if (m.type == 'contract_output') {
const decoded = msgHelper.binaryDecode(m.content);
emitter.emit(events.contractOutput, decoded);
}
else if (m.type == "stat_response") {
statResponseResolver && statResponseResolver({
lcl: m.lcl,
lclSeqNo: m.lcl_seqno
});
statResponseResolver = null;
}
else {
console.log("Received unrecognized message: type:" + m.type);
}
});
});
};
this.on = function (event, listener) {
emitter.on(event, listener);
}
this.close = function () {
Promise.resolve().then
return new Promise(resolve => {
try {
ws.removeAllListeners("close");
ws.on("close", resolve);
ws.close();
} catch (error) {
resolve();
}
})
}
this.getStatus = function () {
const msg = msgHelper.createStatusRequest();
const p = new Promise(resolve => {
statResponseResolver = resolve;
});
ws.send(msgHelper.serializeObject(msg));
return p;
}
this.sendContractInput = async function (input, nonce = null, maxLclOffset = null) {
if (!maxLclOffset)
maxLclOffset = 10;
if (!nonce)
nonce = (new Date()).getTime().toString();
// Acquire the current lcl and add the specified offset.
const stat = await this.getStatus();
if (!stat)
return new Promise(resolve => resolve(null));
const maxLclSeqNo = stat.lclSeqNo + maxLclOffset;
const msg = msgHelper.createContractInput(input, nonce, maxLclSeqNo);
const sigKey = (typeof msg.sig === "string") ? msg.sig : msg.sig.toString("hex");
const p = new Promise(resolve => {
contractInputResolvers[sigKey] = resolve;
});
ws.send(msgHelper.serializeObject(msg));
return p;
}
this.sendContractReadRequest = function (request) {
const msg = msgHelper.createReadRequest(request);
ws.send(msgHelper.serializeObject(msg));
}
}
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;
}
this.binaryDecode = function (content) {
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);
}
this.deserializeMessage = function (m) {
return protocol == protocols.JSON ? JSON.parse(m) : bson.deserialize(m);
}
this.createHandshakeResponse = function (challenge) {
// For handshake response encoding we Hot Pocket always use json.
// Handshake response will specify the protocol to use for subsequent messages.
const sigBytes = sodium.crypto_sign_detached(challenge, keys.privateKey);
return {
type: "handshake_response",
challenge: challenge,
sig: Buffer.from(sigBytes).toString("hex"),
pubkey: "ed" + Buffer.from(keys.publicKey).toString("hex"),
protocol: protocol
}
}
this.createContractInput = function (input, nonce, maxLclSeqNo) {
if (input.length == 0)
return null;
const inpContainer = {
input: this.binaryEncode(input),
nonce: nonce,
max_lcl_seqno: maxLclSeqNo
}
const inpContainerBytes = this.serializeObject(inpContainer);
const sigBytes = sodium.crypto_sign_detached(Buffer.from(inpContainerBytes), keys.privateKey);
const signedInpContainer = {
type: "contract_input",
input_container: this.binaryEncode(inpContainerBytes),
sig: this.binaryEncode(sigBytes)
}
return signedInpContainer;
}
this.createReadRequest = function (request) {
if (request.length == 0)
return null;
return {
type: "contract_read_request",
content: this.binaryEncode(request)
}
}
this.createStatusRequest = function () {
return { type: 'stat' };
}
}
module.exports = {
HotPocketClient,
HotPocketProtocols: protocols,
HotPocketEvents: events
};

65
examples/nodejs_client/package-lock.json generated Normal file
View File

@@ -0,0 +1,65 @@
{
"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=="
},
"base64-js": {
"version": "1.3.1",
"resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.3.1.tgz",
"integrity": "sha512-mLQ4i2QO1ytvGWFWmcngKO//JXAQueZvwEKtjgQFM4jIK0kU+ytMfplL8j+n5mspOfjHwoAg+9yhb7BwAHm36g=="
},
"bson": {
"version": "4.0.4",
"resolved": "https://registry.npmjs.org/bson/-/bson-4.0.4.tgz",
"integrity": "sha512-Ioi3TD0/1V3aI8+hPfC56TetYmzfq2H07jJa9A1lKTxWsFtHtYdLMGMXjtGEg9v0f72NSM07diRQEUNYhLupIA==",
"requires": {
"buffer": "^5.1.0",
"long": "^4.0.0"
}
},
"buffer": {
"version": "5.6.0",
"resolved": "https://registry.npmjs.org/buffer/-/buffer-5.6.0.tgz",
"integrity": "sha512-/gDYp/UtU0eA1ys8bOs9J6a+E/KWIY+DZ+Q2WESNUA0jFRsJOc0SNUO6xJ5SGA1xueg3NL65W6s+NY5l9cunuw==",
"requires": {
"base64-js": "^1.0.2",
"ieee754": "^1.1.4"
}
},
"ieee754": {
"version": "1.1.13",
"resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.1.13.tgz",
"integrity": "sha512-4vf7I2LYV/HaWerSo3XmlMkp5eZ83i+/CDluXi/IGTs/O1sejBNhTtnxzmRZfvOUqj7lZjqHkeTvpgSFDlWZTg=="
},
"libsodium": {
"version": "0.7.6",
"resolved": "https://registry.npmjs.org/libsodium/-/libsodium-0.7.6.tgz",
"integrity": "sha512-hPb/04sEuLcTRdWDtd+xH3RXBihpmbPCsKW/Jtf4PsvdyKh+D6z2D2gvp/5BfoxseP+0FCOg66kE+0oGUE/loQ=="
},
"libsodium-wrappers": {
"version": "0.7.6",
"resolved": "https://registry.npmjs.org/libsodium-wrappers/-/libsodium-wrappers-0.7.6.tgz",
"integrity": "sha512-OUO2CWW5bHdLr6hkKLHIKI4raEkZrf3QHkhXsJ1yCh6MZ3JDA7jFD3kCATNquuGSG6MjjPHQIQms0y0gBDzjQg==",
"requires": {
"libsodium": "0.7.6"
}
},
"long": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/long/-/long-4.0.0.tgz",
"integrity": "sha512-XsP+KhQif4bjX1kbuSiySJFNAehNxgLb6hPRGJ9QsUr8ajHkuXGdrHmFUTUUXhDwVX2R5bY4JNZEwbUiMhV+MA=="
},
"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"
}
}
}
}

View File

@@ -0,0 +1,7 @@
{
"dependencies": {
"libsodium-wrappers": "0.7.6",
"ws": "7.1.2",
"bson": "4.0.4"
}
}

View File

@@ -0,0 +1,85 @@
const fs = require('fs');
const readline = require('readline');
const sodium = require('libsodium-wrappers');
const { exit } = require('process');
const { HotPocketClient, HotPocketProtocols, HotPocketEvents } = require('./hp-client-lib');
async function main() {
await sodium.ready;
let keys = {};
const key_file = '.hp_client_keys';
if (!fs.existsSync(key_file)) {
keys = sodium.crypto_sign_keypair();
keys.privateKey = sodium.to_hex(keys.privateKey)
keys.publicKey = sodium.to_hex(keys.publicKey)
fs.writeFileSync(key_file, JSON.stringify(keys))
} else {
keys = JSON.parse(fs.readFileSync(key_file))
keys.privateKey = Uint8Array.from(Buffer.from(keys.privateKey, 'hex'))
keys.publicKey = Uint8Array.from(Buffer.from(keys.publicKey, 'hex'))
}
const pkhex = 'ed' + Buffer.from(keys.publicKey).toString('hex');
console.log('My public key is: ' + pkhex);
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, HotPocketProtocols.JSON, keys);
// Establish HotPocket connection.
if (!await hpc.connect()) {
console.log('Connection failed.');
exit;
}
console.log('HotPocket Connected.');
// This will get fired if HP server disconnects unexpectedly.
hpc.on(HotPocketEvents.disconnect, () => {
console.log('Server diconnected');
exit;
})
// This will get fired when contract sends an output.
hpc.on(HotPocketEvents.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) => {
console.log("Contract read response>> " + Buffer.from(response, "hex"));
})
// On ctrl + c we should close HP connection gracefully.
process.once('SIGINT', function () {
console.log('SIGINT received...');
hpc.close();
});
// start listening for stdin
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
console.log("Ready to accept inputs.");
const input_pump = () => {
rl.question('', async (inp) => {
if (inp.startsWith("read "))
hpc.sendContractReadRequest(inp.substr(5))
else {
const submissionStatus = await hpc.sendContractInput(inp);
if (submissionStatus && submissionStatus != "ok")
console.log("Input submission failed. reason: " + submissionStatus);
}
input_pump();
})
}
input_pump();
}
main();