Implemented contract read requests. (#98)

This commit is contained in:
Ravin Perera
2020-06-19 21:52:03 +05:30
committed by GitHub
parent 04e230c82e
commit 817ccd6a88
17 changed files with 760 additions and 587 deletions

View File

@@ -4,36 +4,45 @@ process.on('uncaughtException', (err) => {
const fs = require('fs')
//console.log("===Sample contract started===");
//console.log("Contract args received from hp: " + input);
let hpargs = JSON.parse(fs.readFileSync(0, 'utf8'));
//console.log(hpargs);
// We just save execution args as an example state file change.
fs.appendFileSync("exects.txt", "ts:" + hpargs.ts + "\n");
if (!hpargs.readonly)
fs.appendFileSync("exects.txt", "ts:" + hpargs.ts + "\n");
Object.keys(hpargs.usrfd).forEach(function (key, index) {
let userfds = hpargs.usrfd[key];
if (userfds[0] != -1) {
let userinput = fs.readFileSync(userfds[0], 'utf8');
// Append user input to a state file.
fs.appendFileSync("userinputs.txt", userinput + "\n");
fs.writeSync(userfds[1], "Echoing: " + userinput);
// Append user input to a state file if not in read only mode.
if (!hpargs.readonly)
fs.appendFileSync("userinputs.txt", userinput + "\n");
if (userinput == "ts")
fs.writeSync(userfds[1], fs.readFileSync("exects.txt"));
else
fs.writeSync(userfds[1], "Echoing: " + userinput);
}
});
if (hpargs.nplfd[0] != -1) {
let nplinput = fs.readFileSync(hpargs.nplfd[0], 'utf8');
console.log("Input received from peers:");
console.log(nplinput);
fs.writeSync(hpargs.nplfd[1], "Echoing: " + nplinput);
}
if (!hpargs.readonly) {
if (hpargs.hpfd[0] != -1) {
let hpinput = fs.readFileSync(hpargs.hpfd[0], 'utf8');
console.log("Input received from hp:");
console.log(hpinput);
fs.writeSync(hpargs.hpfd[1], "Echoing: " + hpinput);
if (hpargs.nplfd[0] != -1) {
let nplinput = fs.readFileSync(hpargs.nplfd[0], 'utf8');
console.log("Input received from peers:");
console.log(nplinput);
fs.writeSync(hpargs.nplfd[1], "Echoing: " + nplinput);
}
if (hpargs.hpfd[0] != -1) {
let hpinput = fs.readFileSync(hpargs.hpfd[0], 'utf8');
console.log("Input received from hp:");
console.log(hpinput);
fs.writeSync(hpargs.hpfd[1], "Echoing: " + hpinput);
}
}
//console.log("===Sample contract ended===");

View File

@@ -1,141 +0,0 @@
//
// 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 = '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]
var ws = new ws_api(server, {
rejectUnauthorized: false
})
/* anatomy of a public challenge
{
version: '0.1',
type: 'public_challenge',
challenge: '<hex string>'
}
*/
// if the console ctrl + c's us we should close ws gracefully
process.once('SIGINT', function (code) {
console.log('SIGINT received...');
ws.close()
});
function create_input_container(inp) {
let inp_container = {
nonce: (new Date()).getTime().toString(),
input: Buffer.from(inp).toString('hex'),
max_ledger_seqno: 9999999
}
let inp_container_bytes = JSON.stringify(inp_container);
let sig_bytes = sodium.crypto_sign_detached(inp_container_bytes, keys.privateKey);
let signed_inp_container = {
type: "contract_input",
content: inp_container_bytes.toString('hex'),
sig: Buffer.from(sig_bytes).toString('hex')
}
return JSON.stringify(signed_inp_container);
}
function create_status_request() {
let statreq = { type: 'stat' }
return JSON.stringify(statreq);
}
ws.on('message', (m) => {
console.log("-----Received raw message-----")
console.log(m.toString())
console.log("------------------------------")
try {
m = JSON.parse(m)
} catch (e) {
return
}
if (m.type != 'public_challenge') return
console.log("Received challenge message")
console.log(m)
let pkhex = 'ed' + Buffer.from(keys.publicKey).toString('hex');
console.log('My public key is: ' + pkhex);
// sign the challenge and send back the response
var sigbytes = sodium.crypto_sign_detached(m.challenge, keys.privateKey);
var response = {
type: 'challenge_resp',
challenge: m.challenge,
sig: Buffer.from(sigbytes).toString('hex'),
pubkey: pkhex
}
console.log('Sending challenge response.');
ws.send(JSON.stringify(response))
// start listening for stdin
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
// Capture user input from the console.
var input_pump = () => {
rl.question('\nProvide an input: ', (inp) => {
let msgtosend = "";
if (inp == "stat")
msgtosend = create_status_request();
else
msgtosend = create_input_container(inp);
console.log("Sending message: " + msgtosend);
ws.send(msgtosend)
input_pump()
})
}
input_pump()
});
ws.on('close', () => {
console.log('Server disconnected.');
});
}

View File

@@ -72,6 +72,19 @@ function main() {
return JSON.stringify(signed_inp_container);
}
function create_read_request_container(inp) {
if (inp.length == 0)
return "";
let container = {
type: "contract_read_request",
content: Buffer.from(inp).toString('hex'),
}
return JSON.stringify(container);
}
function create_status_request() {
let statreq = { type: 'stat' }
return JSON.stringify(statreq);
@@ -108,12 +121,15 @@ function main() {
if (inp == "stat")
msgtosend = create_status_request();
else if (inp.startsWith("read "))
msgtosend = create_read_request_container(inp.substr(5));
else
msgtosend = create_input_container(inp);
ws.send(msgtosend)
if (msgtosend.length > 0)
ws.send(msgtosend)
input_pump()
input_pump();
})
}
input_pump();
@@ -131,7 +147,7 @@ function main() {
if (m.type == 'public_challenge') {
handle_public_challange(m);
}
else if (m.type == 'contract_output') {
else if (m.type == 'contract_output' || m.type == 'contract_read_response') {
console.log(Buffer.from(m.content, 'hex').toString());
}
else if (m.type == 'request_status_result') {