@chainsafesystems/ssz
v1.6.0
Published
A pure JS implementation of simple serialize
Downloads
6
Readme
ChainSafe ETH2.0 Projects
Note: There is a lot of work being done that are core infrastructural pieces for Eth2.0. Contributions to any of the below repositories would be greatly appreciated. All the libraries are written in TypeScript (or in the process of being converted from pure JS to TypeScript): -- PM / Meta Repo |-- Beacon Chain |-- Simple Serialize (SSZ) |-- Fixed Size Numbers |-- BLS Singatures and Signature Aggregation
ssz-js
Simple Serialize (SSZ) in pure Javascript
Install
npm install @chainsafesystems/ssz
Usage
See API.md for comprehensive API docs.
Serialize:
Array
let array = [1, 2, 3]
let serialized = ssz.serialize(array, ['uint16'])
Boolean
let bool = true
let serialized = ssz.serialize(bool, 'bool')
Number
let num = 16
let serialized = ssz.serialize(num, 'uint32')
Bytes
let bytes = Buffer.from([1,2,3])
let serialized = ssz.serialize(bytes, 'bytes')
We can also serialize to BytesN:
// Note: N === bytes.length
let arr = new Uint8Array(1)
let bytes = Buffer.from(arr.buffer)
let serialized = ssz.serialize(bytes, 'bytes1')
Object
let obj = {
a: Buffer.from('hello'),
b: 10,
c: false
}
let types = {
'fields' : [
['a', 'bytes'],
['b', 'int16'],
['c', 'bool'],
]
}
let serialized = ssz.serialize(obj, types)
Deserialize
For deserialization we need to specify:
- data {buffer} - encoded data
- type {string | object | array} - type of data we want to decode, same as encoding types
- start {number} - optional, default: 0, start location in the data buffer from where we wish to read
// eg. deserialize a boolean at position 0
ssz.deserialize(buf, 'bool')
// eg. deserialize an object at position 32
let types = {
'fields' : [
['a', 'bool'],
['b', 'uint8']
]
}
ssz.deserialize(buf, types, 32)
Contributors
Very special thank you to Darren Langley for helping build this.