-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstbx-cli-codec.js
executable file
·73 lines (62 loc) · 2.16 KB
/
stbx-cli-codec.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
#!/usr/bin/env node
const {program, exit_error, exit_success, get_input_json, get_input, put_output } = require("./util.js")
let help = () => console.log(`
Examples:
# Encoding a transition firing:
echo '{"firing":{"execution":"aabbeeff","path":[0]},"previous":"00aaff22"}' > tx.json
cat tx.json | stbx codec -e -o tx.hex
echo '{"wiring":{"nets":[{"name":"foo","names":["a","b"],"partition":[0,1,0,1,0,0]}],"diagrams":[{"name":"z","width":1,"pixels":[1],"names":["z"]}],"labels":[0]},"previous":"0a0a0a0a"}' > tx2.json
cat tx.json | stbx codec -e -o tx2.hex
# Decoding a hexadecimally encoded transaction back to JSON:
stbx codec -d -i tx.hex
`)
program
.on('--help', help)
.option('-e, --encode', 'Encode message')
.option('-d, --decode', 'Decode message')
.parse(process.argv);
function encode_or_fail(obj) {
try {
const Stbx = require("@statebox/stbx-js")
if (!obj.wiring && !obj.firing && !obj.root) {
exit_error('tx must have .firing, .wiring or .root property')
} else {
let e = Stbx.encode(obj)
if (e.trim() === "") {
exit_error('something went wrong encoding, returned empty string!')
} else {
return Buffer.from(e, 'hex')
}
}
} catch(e) {
exit_error(`failed to encode, ${e.message}`)
}
}
function decode_or_fail(buf) {
try {
const Stbx = require("@statebox/stbx-js")
return Stbx.decode(buf.toString('hex'))
} catch(e) {
exit_error(`failed to decode, ${e.message}`)
}
}
async function main () {
if(program.encode && program.decode) {
exit_error('must pass either --encode or --decode, not both')
}
// run encoding
if(program.encode) {
let parsed = await get_input_json()
let output = encode_or_fail(parsed)
put_output(output.toString('hex'))
exit_success()
}
if(program.decode) {
let input = await get_input()
let buf = Buffer.from(input.toString('utf8'), 'hex')
let obj = decode_or_fail(buf)
put_output(JSON.stringify(obj))
exit_success()
}
}
main()