@wires/stbx-packet-codec
v1.0.14
Published
[![Build Status](https://travis-ci.com/statebox/js-stbx-packet-codec.svg?token=sQRsYRm6gqSjs7qsYUZp&branch=master)](https://travis-ci.com/statebox/js-stbx-packet-codec)
Downloads
41
Readme
Javascript based encoding & decoding of statebox packets
API
using Buffer
encode_transaction : transaction -> Buffer
decode_transaction : Buffer -> transaction
strings using hex
encoding
encode_transaction_as_hex
decode_hex_as_transaction
All these methods are partial and might throw is something went wrong.
See statebox.proto
for current type definitions of wiring
and tx
.
Usage example
Encoding a wiring transaction
The wiring looks like {nets, diagrams, labels}
, for example:
let wiring = {
"nets": [
{
"name": "main",
"partition": [0,1,0,1,0,2,0,5,0,3,0,5,0,3,0,3,0,4,0,3,0,5,0,2,0,5,0],
"names": [
"init",
"chooseCashIn",
"bill50",
"bill20",
"confirmDone",
"senseBill",
"senseBill2"
]
}
],
"diagrams": [
{
"name": "root",
"width": 1,
"pixels": [ 1 ]
}
],
"labels": [0]
}
// encode wiring diagram
c.encode_transaction_as_hex({wiring: wiring, previous:"feedcode"})
// => `0a02feed1a95010a84010a046d61696e1000100110001001100010021000100510001003100010051000100310001003100010041000100310001005100010021000100510001a04696e69741a0c63686f6f736543617368496e1a0662696c6c35301a0662696c6c32301a0b636f6e6669726d446f6e651a0973656e736542696c6c1a0a73656e736542696c6c32120a0a04726f6f74100118011800`
Decoding this wiring transaction
Say we obtained some hex encoded transaction and know it is a wiring transactions
let txHex = "0a001a95010a84010a046d61696e1000100110001001100010021000100510001003100010051000100310001003100010041000100310001005100010021000100510001a04696e69741a0c63686f6f736543617368496e1a0662696c6c35301a0662696c6c32301a0b636f6e6669726d446f6e651a0973656e736542696c6c1a0a73656e736542696c6c32120a0a04726f6f74100118011800"
Lets decode it into a JS object
let wiring = P.decode_transaction_as_hex(txHex)
Encoding a wiring transaction
let firing = {
previous: "deadbeef",
firing: {
execution:"cafebabe",
// message: "c0dec0dec0de", // optional message
path: [0]
}
}
P.encode_transaction_as_hex(firing)
// => '0a04deadbeef12080a04cafebabe1000'
Browser Usage
This module depends on a protocol buffer library, to use it, you must use a packer.