forked from kadena-io/pact-lang-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
load.js
executable file
·74 lines (68 loc) · 2.12 KB
/
load.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env node
var fs = require('fs');
var nacl = require('tweetnacl');
var pact = require('./pact-lang-api.js');
var args = {};
var key = undefined;
process.argv.slice(2).forEach(function (v,i,a) {
if (key) {
args[key] = v; key = undefined;
} else {
if (v[0]='-') { key = v.slice(1); }
}
});
var usage = "\nload.js: create JSON for loading Pact code \n\n\
Arguments: [-cf codefile] [-c code] -n nonce -s sk -p pk [-df datafile] [-d data] [-t to -f from]\n\n\
codefile filepath containing pact code to load \n\
code pact code to execute \n\
datafile filepath containing JSON data to accompany pact code load \n\
data JSON string of data to accompany pact code load \n\
nonce nonce value for data payload \n\
sk secret key \n\
pk public key \n\
to Private message sender entity \n\
from Private message recipient JSON list \n\
";
function die(msg) {
console.error("ERROR: " + msg);
process.exit(1);
}
function dieu(msg) { die(msg + "\n\n" + usage); }
function read(fp,cb) {
fs.readFile(fp,'utf8',function(err,data) {
if (err) { die(err); }
cb(data);
});
}
function go(code,data) {
var kp = { publicKey: args.p, secretKey: args.s};
var addy;
if (args.t && args.f) {
addy = { "from": args.f, "to": JSON.parse(args.t) };
}
var msg = pact.simple.exec.createCommand(kp,args.n,code,JSON.parse(data),addy);
console.log(JSON.stringify(msg));
}
if (!args.cf && !args.c) { dieu("Missing code or codefile argument"); }
if (!args.n) { dieu("Missing nonce argument"); }
if (!args.s) { dieu("Missing sk argument"); }
if (!args.p) { dieu("Missing pk argument"); }
if ((args.t && !args.f) || (!args.t && args.f)) { dieu("Must provide both TO and FROM when formatting private payload"); }
function doData(code) {
if (args.df) {
read(args.df,function(data) {
go(code,data);
});
} else {
if (args.d) {
go(code,args.d);
} else {
go(code,"{}");
}
}
}
if (args.cf) {
read(args.cf,doData);
} else {
doData(args.c);
}