mirror of
https://github.com/EvernodeXRPL/hpcore.git
synced 2026-04-29 15:37:59 +00:00
HP smart contract nodejs library. (#101)
This commit is contained in:
1
examples/nodejs_contract/.gitignore
vendored
Normal file
1
examples/nodejs_contract/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
node_modules
|
||||
30
examples/nodejs_contract/echo_contract.js
Normal file
30
examples/nodejs_contract/echo_contract.js
Normal file
@@ -0,0 +1,30 @@
|
||||
const { HotPocketContract } = require("./hp-contract-lib");
|
||||
const fs = require('fs');
|
||||
|
||||
const hpc = new HotPocketContract();
|
||||
|
||||
//console.log("===Echo contract started===");
|
||||
|
||||
// We just save execution timestamp as an example state file change.
|
||||
if (!hpc.readonly)
|
||||
fs.appendFileSync("exects.txt", "ts:" + hpc.timestamp + "\n");
|
||||
|
||||
Object.keys(hpc.users).forEach(function (key) {
|
||||
|
||||
const user = hpc.users[key];
|
||||
const inputBuf = user.readInput();
|
||||
if (inputBuf) {
|
||||
const userInput = inputBuf.toString("utf8");
|
||||
|
||||
// Append user input to a state file if not in read only mode.
|
||||
if (!hpc.readonly)
|
||||
fs.appendFileSync("userinputs.txt", userInput + "\n");
|
||||
|
||||
if (userInput == "ts")
|
||||
user.sendOutput(fs.readFileSync("exects.txt"));
|
||||
else
|
||||
user.sendOutput("Echoing: " + userInput);
|
||||
}
|
||||
});
|
||||
|
||||
//console.log("===Echo contract ended===");
|
||||
82
examples/nodejs_contract/file_contract.js
Normal file
82
examples/nodejs_contract/file_contract.js
Normal file
@@ -0,0 +1,82 @@
|
||||
const fs = require('fs');
|
||||
const bson = require('bson');
|
||||
|
||||
//console.log("===File contract started===");
|
||||
|
||||
const hpargs = JSON.parse(fs.readFileSync(0, 'utf8'));
|
||||
//console.log("Contract args received from hp: " + hpargs);
|
||||
|
||||
Object.keys(hpargs.usrfd).forEach(function (key) {
|
||||
const userfds = hpargs.usrfd[key];
|
||||
|
||||
if (userfds[0] != -1) {
|
||||
|
||||
const input = fs.readFileSync(userfds[0]);
|
||||
const msg = bson.deserialize(input);
|
||||
|
||||
if (msg.type == "upload") {
|
||||
if (fs.existsSync(msg.fileName)) {
|
||||
fs.writeSync(userfds[1], bson.serialize({
|
||||
type: "uploadResult",
|
||||
status: "already_exists",
|
||||
fileName: msg.fileName
|
||||
}));
|
||||
}
|
||||
else if (msg.content.length > 10 * 1024 * 1024) { // 10MB
|
||||
fs.writeSync(userfds[1], bson.serialize({
|
||||
type: "uploadResult",
|
||||
status: "too_large",
|
||||
fileName: msg.fileName
|
||||
}));
|
||||
}
|
||||
else {
|
||||
|
||||
// Save the file.
|
||||
fs.writeFileSync(msg.fileName, msg.content.buffer);
|
||||
|
||||
fs.writeSync(userfds[1], bson.serialize({
|
||||
type: "uploadResult",
|
||||
status: "ok",
|
||||
fileName: msg.fileName
|
||||
}));
|
||||
}
|
||||
}
|
||||
else if (msg.type == "delete") {
|
||||
if (fs.existsSync(msg.fileName)) {
|
||||
fs.unlinkSync(msg.fileName);
|
||||
fs.writeSync(userfds[1], bson.serialize({
|
||||
type: "deleteResult",
|
||||
status: "ok",
|
||||
fileName: msg.fileName
|
||||
}));
|
||||
}
|
||||
else {
|
||||
fs.writeSync(userfds[1], bson.serialize({
|
||||
type: "deleteResult",
|
||||
status: "not_found",
|
||||
fileName: msg.fileName
|
||||
}));
|
||||
}
|
||||
}
|
||||
else if (msg.type == "download") {
|
||||
if (fs.existsSync(msg.fileName)) {
|
||||
const fileContent = fs.readFileSync(msg.fileName);
|
||||
fs.writeSync(userfds[1], bson.serialize({
|
||||
type: "downloadResult",
|
||||
status: "ok",
|
||||
fileName: msg.fileName,
|
||||
content: fileContent
|
||||
}));
|
||||
}
|
||||
else {
|
||||
fs.writeSync(userfds[1], bson.serialize({
|
||||
type: "downloadResult",
|
||||
status: "not_found",
|
||||
fileName: msg.fileName
|
||||
}));
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
//console.log("===File contract ended===");
|
||||
27
examples/nodejs_contract/hp-contract-lib.js
Normal file
27
examples/nodejs_contract/hp-contract-lib.js
Normal file
@@ -0,0 +1,27 @@
|
||||
const fs = require('fs');
|
||||
|
||||
function HotPocketContract() {
|
||||
const hpargs = JSON.parse(fs.readFileSync(0, 'utf8'));
|
||||
this.readonly = hpargs.readonly;
|
||||
this.timestamp = hpargs.ts;
|
||||
this.users = {};
|
||||
|
||||
Object.keys(hpargs.usrfd).forEach((userPubKey) => {
|
||||
const userfds = hpargs.usrfd[userPubKey];
|
||||
this.users[userPubKey] = new HotPocketChannel(userfds[0], userfds[1]);
|
||||
});
|
||||
}
|
||||
|
||||
function HotPocketChannel(infd, outfd) {
|
||||
this.readInput = function () {
|
||||
return infd == -1 ? null : fs.readFileSync(infd);
|
||||
}
|
||||
|
||||
this.sendOutput = function (output) {
|
||||
fs.writeFileSync(outfd, output);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
HotPocketContract
|
||||
}
|
||||
39
examples/nodejs_contract/package-lock.json
generated
Normal file
39
examples/nodejs_contract/package-lock.json
generated
Normal file
@@ -0,0 +1,39 @@
|
||||
{
|
||||
"requires": true,
|
||||
"lockfileVersion": 1,
|
||||
"dependencies": {
|
||||
"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=="
|
||||
},
|
||||
"long": {
|
||||
"version": "4.0.0",
|
||||
"resolved": "https://registry.npmjs.org/long/-/long-4.0.0.tgz",
|
||||
"integrity": "sha512-XsP+KhQif4bjX1kbuSiySJFNAehNxgLb6hPRGJ9QsUr8ajHkuXGdrHmFUTUUXhDwVX2R5bY4JNZEwbUiMhV+MA=="
|
||||
}
|
||||
}
|
||||
}
|
||||
5
examples/nodejs_contract/package.json
Normal file
5
examples/nodejs_contract/package.json
Normal file
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"dependencies": {
|
||||
"bson": "4.0.4"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user