Crate ssz

source ·
Expand description

Provides encoding (serialization) and decoding (deserialization) in the SimpleSerialize (SSZ) format designed for use in Ethereum 2.0.

Adheres to the Ethereum 2.0 SSZ specification at v0.12.1.

§Example

use ssz_derive::{Encode, Decode};
use ssz::{Decode, Encode};

#[derive(PartialEq, Debug, Encode, Decode)]
struct Foo {
    a: u64,
    b: Vec<u16>,
}

fn ssz_encode_decode_example() {
    let foo = Foo {
        a: 42,
        b: vec![1, 3, 3, 7]
    };

    let ssz_bytes: Vec<u8> = foo.as_ssz_bytes();

    let decoded_foo = Foo::from_ssz_bytes(&ssz_bytes).unwrap();

    assert_eq!(foo, decoded_foo);
}

See examples/ for manual implementations of the Encode and Decode traits.

Modules§

  • Provides a “legacy” version of SSZ encoding for Option<T> where T: Encode + Decode.

Macros§

Structs§

Enums§

Constants§

Traits§

  • Provides SSZ decoding (de-serialization) via the from_ssz_bytes(&bytes) method.
  • Provides SSZ encoding (serialization) via the as_ssz_bytes(&self) method.
  • Partial variant of std::iter::FromIterator.

Functions§

  • Decodes bytes as if it were a list of variable-length items.
  • Encode len as a little-endian byte array of BYTES_PER_LENGTH_OFFSET length.
  • Reads a BYTES_PER_LENGTH_OFFSET-byte length from bytes, where bytes.len() >= BYTES_PER_LENGTH_OFFSET.
  • Takes bytes, assuming it is the encoding for a SSZ union, and returns the union-selector and the body (trailing bytes).
  • Convenience function to SSZ encode an object supporting ssz::Encode.