Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
83 commits
Select commit Hold shift + click to select a range
6fe4db2
set up initial SSZ documentation structure, and Created the initial f…
lolosaisa Jul 23, 2025
ad72dc3
labelled the types file
lolosaisa Jul 23, 2025
f644b08
updated the paths of the files ssz-quickstart.md and ssz-views-and-pr…
lolosaisa Jul 24, 2025
225793d
added the defination of SSZ in the ssz quickstart quide
lolosaisa Jul 26, 2025
45ab4bc
defined what serialization , deserialization and merklezation means …
lolosaisa Jul 26, 2025
5326854
added a simple explanation of the terms
lolosaisa Jul 26, 2025
050ca33
added the defination of the two different layers of components
lolosaisa Jul 31, 2025
30c1347
Added the typing system logic on ssz , the different kind of types an…
lolosaisa Aug 6, 2025
04437bf
docs(ssz-quickstard.md): Finished the typing system section of the co…
lolosaisa Aug 6, 2025
57d29d6
docs: Added a link to the lodestar types repository if the developers…
lolosaisa Aug 6, 2025
9600ad4
docs: Fixed the typing errors
lolosaisa Aug 6, 2025
e1e4e01
docs: fized the number of columns in a table that was causing a rende…
lolosaisa Aug 6, 2025
52fb152
docs(ssz-quickstart.md): Added the description of Fork -Specific Shem…
lolosaisa Aug 6, 2025
175078f
docs(ssz-quickstart.md): Added the default values section, defined wh…
lolosaisa Aug 7, 2025
0d2cd0b
docs: Fixing a table issue
lolosaisa Aug 7, 2025
152a05d
docs(ssz-quickstart.md): Added how to use default value section, with…
lolosaisa Aug 11, 2025
241dfc8
docs(ssz-quickstart.md): Setting Fiels in your zeroed /default object
lolosaisa Aug 11, 2025
4668cfe
docs(ssz-quickstart.md): How to modify primitive values
lolosaisa Aug 11, 2025
3f0966b
docs(ssz-quickstart.md): How Lodestar enhances Typescript Safety
lolosaisa Aug 11, 2025
71b1c00
docs(ssz-quickstart.md):Added real life examples of how typescript sa…
lolosaisa Aug 11, 2025
3b4e013
docs(ssz-quickstart.md): Added a table that explain Lodestar SSZ TYpe…
lolosaisa Aug 11, 2025
4a0eb4a
docs: Removed the fork-specific folders and the files
lolosaisa Aug 11, 2025
02077ff
docs(ssz-quickstart.md): Added the serialization and deserialization …
lolosaisa Aug 12, 2025
f84bb46
docs(ssz-quickstart.md): Changed up the defination to avoid repetition
lolosaisa Aug 12, 2025
f7084ac
docs(ssz-quickstart.md): Added the deserialization section and an exm…
lolosaisa Aug 12, 2025
0c6f950
docs(ssz-quickstart.md): Added code example of the whole process
lolosaisa Aug 12, 2025
74b311d
docs(ssz-quickstart.md): Fixing prettier issues
lolosaisa Aug 12, 2025
f997ec5
docs(ssz-quickstart.md): Finalized the seerialization and deserializa…
lolosaisa Aug 12, 2025
da4d816
Update SSZ quickstart documentation
lolosaisa Sep 19, 2025
15b857a
docs: Added the Hashing aspect on the ssz-quickstart.md with example
lolosaisa Sep 26, 2025
866d838
docs(5045): Added the merkleization part with examples
lolosaisa Sep 26, 2025
e844180
docs(5045): JSON Conversion in ssz, added how to encode data to json …
lolosaisa Sep 26, 2025
2533aff
docs(5045): Added how to decode from JSON back to ssz object
lolosaisa Sep 26, 2025
356323a
docs(5045): add detailed README mapping docs and examples for SSZ
lolosaisa Sep 26, 2025
3b46c21
docs(5045): FInished the quickstart file and now moving forward to th…
lolosaisa Oct 1, 2025
1aa39ef
docs(5045): Added the defination of views and why they are needed per…
lolosaisa Oct 1, 2025
13a7192
docs(5045): Polished the defination for views to be doc friendly
lolosaisa Oct 1, 2025
b069272
docs(5045): Added the views vs values section and the outline for the…
lolosaisa Oct 2, 2025
dcc6281
docs(5045): Added the difference between a plain object and a view in…
lolosaisa Oct 2, 2025
18e16fe
docs(5045): Added the description of Tree Views
lolosaisa Oct 3, 2025
e10ae03
docs(5045): Added the defination for tree view and code on how they o…
lolosaisa Oct 4, 2025
a655090
docs(5045): Deleted the tree backed views after mismatching the defin…
lolosaisa Oct 4, 2025
1e3cb61
docs(5045): Added the defination of tree backed views and the two mai…
lolosaisa Oct 4, 2025
cc5805d
docs(5045): Tree views as a wrapper around a tree and a type that pro…
lolosaisa Oct 4, 2025
1407820
docs(5045): Added an example of how to create an attestation view, mo…
lolosaisa Oct 4, 2025
2dfc4d5
docs(5045): added a more detailed exampls that includes the property …
lolosaisa Oct 4, 2025
764bd87
docs(5045): Added subview behaviour for views code included
lolosaisa Oct 4, 2025
4028627
docs(5045): Added the defination for TRee ViewDU
lolosaisa Oct 4, 2025
73bde8b
docs(5045): Added a code example of the treeviewDU
lolosaisa Oct 5, 2025
82aab6f
docs(5045): Added the key features of the treeviewDU
lolosaisa Oct 5, 2025
8c12202
docs(5045): Fixed the structure/prettie heading
lolosaisa Oct 5, 2025
e0f44ae
docs(5045): Added the subview behaviour of a viewDU
lolosaisa Oct 5, 2025
fd6b57e
docs(5045): Added the code example for subview difference
lolosaisa Oct 5, 2025
b8788f7
docs(5045): Added the section for common operations with views so the…
lolosaisa Oct 6, 2025
51671f7
docs(5045): Added more examples of operations
lolosaisa Oct 6, 2025
927fef5
docs(5045): Fixed some prettier typos
lolosaisa Oct 6, 2025
a0bf5ca
docs(5045): Added the defination of proofs
lolosaisa Oct 7, 2025
b9a9f2c
docs(5045): How the proofs are built
lolosaisa Oct 7, 2025
6ba7480
docs(5045): Described how the proofs works and the methods that enabl…
lolosaisa Oct 7, 2025
d26dd45
docs(5045): Added the code to show how proofs are created
lolosaisa Oct 7, 2025
85b2b76
docs(5045): Added the how to reconstruct from proofs and the anatomy …
lolosaisa Oct 7, 2025
a726ef4
docs(5045): How to verify proofs
lolosaisa Oct 7, 2025
34f0e5a
docs(5045): Added te section for tips fr the typescript developers
lolosaisa Oct 7, 2025
b2290d6
docs(5045):Added resources for more studies
lolosaisa Oct 8, 2025
904b034
docs(5045): Added the generalized incies section
lolosaisa Oct 8, 2025
ce7e739
docs(5045): how are generalized Indices used
lolosaisa Oct 8, 2025
ba88193
docs(5045): Added the common pitfals and how to debug them if it happems
lolosaisa Oct 8, 2025
e5bc948
docs(5045): Added best practices when working with ssz
lolosaisa Oct 8, 2025
06a9d5e
Fixing typos and adding missing elements in the quickstart.md
lolosaisa Oct 9, 2025
345446f
docs(5045): Added the table of content in the SSZ quickstart.md
lolosaisa Oct 9, 2025
73d3b4e
docs(5045): Restructured the ssz components upto the fork specific s…
lolosaisa Oct 9, 2025
a4c076b
docs(5045): Fixed the structure for the core ssz types made transitio…
lolosaisa Oct 9, 2025
1755b8f
docs(5045): Fixed the typos and changed the code to typescript
lolosaisa Oct 9, 2025
59317cc
docs(5045): Finished making the changes on ssz quickstart md
lolosaisa Oct 9, 2025
082606d
docs(5045): Added a table of content in the views and proofs file and…
lolosaisa Oct 10, 2025
aa6d3b5
docs(5045): Instead of having a folder with different files for examp…
lolosaisa Oct 10, 2025
1981d47
docs(5045): Added the examples for developers
lolosaisa Oct 10, 2025
ba4ffd3
docs(5045): Added all the the explanation for each code and the conce…
lolosaisa Oct 10, 2025
08e083b
docs(5045): Deleted some repetion , polished all the finals for the f…
lolosaisa Oct 10, 2025
f583522
docs: finalize SSZ documentation and examples overview
lolosaisa Oct 10, 2025
c692f6c
fixed the typos and grammar issues in the README file
lolosaisa Oct 21, 2025
7dc43ee
deleted the grammar errors and fixed typos
lolosaisa Oct 21, 2025
084242e
fixes: Fixed the typos in the ssz views and proofs
lolosaisa Oct 21, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions packages/types/ssz-docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
# Lodestar Types Documentation & Examples

This repository provides **learning resources and runnable examples** for working with [SSZ (Simple Serialize)](https://github.com/ethereum/consensus-specs/tree/dev/ssz) in TypeScript.
It is designed for Ethereum developers, researchers, and contributors working with the Lodestar consensus client, but also useful for anyone learning SSZ.

---

## 📁 Structure

```

ssz-docs/
├─ ssz-quickstart.md
├─ ssz-views-and-proofs.md
├─ ssz-examples.md
└─ ...
```

---

### Overview

- **`ssz-quickstart.md`** — Beginner-friendly guide to SSZ. Learn serialization, deserialization, and `hashTreeRoot`.
- **`ssz-views-and-proofs.md`** — Advanced guide on SSZ views, Merkle proofs, and efficient data access.
- **`ssz-examples.md`** — Runnable **TypeScript demos** that complement the docs.

This documentation serves as a **quick reference and tutorial set** for Lodestar contributors, client developers, and researchers exploring the SSZ type system.

---

## 📘 Documentation

### **1. `ssz-quickstart.md`**

- Covers primitive types (`uint64`), containers, lists, and vectors.
- Explains **serialization**, **deserialization**, and **hashing** (`hashTreeRoot`).
- Includes **JSON conversion** (`toJson`, `fromJson`) for API use.
- Ideal starting point for newcomers.

### **2. `ssz-views-and-proofs.md`**

- Explains **tree-backed views** for efficient, mutable access.
- Demonstrates how to **generate and verify Merkle proofs**.
- Covers **light client use cases**, and **nested data access**.
- Recommended after completing the quickstart.

### **3. `ssz-examples.md`**

- Runnable **TypeScript demos** that complement the docs.
- Ideal for beginners who want to test out different concepts in the SSZ.

---

## 💻 Running the Examples

The `examples` are in the ssz-examples.md, contains runnable **TypeScript demos** that match the topics covered in the docs.
Each example in `ssz-examples.md` is self-contained and can be run directly with `ts-node`.

### **Setup**

```bash
npm install
# or, directly:
npm install @chainsafe/ssz @lodestar/types typescript ts-node
```

Run an Example:
Pick an example from the file and place it in its own file, e.g. Container.ts, then run the file with the following command.

```
npx ts-node examples/simple-container.ts

```

Examples Explained

1. simple-container

Defines a basic SSZ container with primitive fields (e.g. uint64).

Demonstrates:
`serialize()` — convert container to SSZ bytes
`deserialize()` — parse bytes back into structured data
Useful for: learning container patterns and core SSZ workflow.

2. nested-container

Shows how containers can contain other containers.
Demonstrates:
Serialization and deserialization of nested structures
Useful for: building real-world Ethereum objects (validators, blocks, etc.).

3. lists-vectors.ts
Introduces SSZ lists (variable-length) and vectors (fixed-length).

Demonstrates:
Creating dynamic lists
Enforcing fixed-size vectors
Serializing both forms
Useful for: handling validator lists, committees, or signatures in consensus types.

Find more examples in the `ssz-examples.md` file.

## 📘 Learn More

### Resources

[Further Reading](https://ethereum.org/developers/docs/data-structures-and-encoding/ssz/)
[Building blocks ssz](https://eth2book.info/altair/part2/building_blocks/ssz/)
[merkle multiproofs](https://github.com/ethereum/consensus-specs/blob/dev/ssz/merkle-proofs.md#merkle-multiproofs)
[simple serialize](https://github.com/ethereum/consensus-specs/blob/dev/ssz/simple-serialize.md)
[Lodestar types](https://github.com/ChainSafe/lodestar/tree/unstable/packages/types)
219 changes: 219 additions & 0 deletions packages/types/ssz-docs/ssz-examples.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
# SSZ Examples

This file contains runnable TypeScript examples that demonstrate SSZ operations — from encoding to hashing, Merkleization, JSON conversion, and proof generation.

All examples assume you’ve installed `@chainsafe/ssz`.

```bash
npm install @chainsafe/ssz
```
## 1. Simple Container
What it does:
Demonstrates how to define and use a simple ContainerType with basic integer fields.
This is the most fundamental SSZ structure — equivalent to a struct in other languages.

```ts
import { ContainerType, UintNumberType } from "@chainsafe/ssz";

const SimpleContainer = new ContainerType({
a: new UintNumberType(8),
b: new UintNumberType(16),
});

const value = { a: 1, b: 500 };
const serialized = SimpleContainer.serialize(value);
console.log("Serialized:", serialized);
console.log("Deserialized:", SimpleContainer.deserialize(serialized));
```
Concepts:

Creating a container type
Serializing & deserializing objects
Type safety with field definitions



## 2. Nested Container#
What it does:
Shows how containers can contain other containers or lists, creating hierarchical data structures.

```ts
import { ContainerType, ListType, UintNumberType } from "@chainsafe/ssz";

const Inner = new ContainerType({ x: new UintNumberType(8) });
const Outer = new ContainerType({
items: new ListType(Inner, 4),
});

const value = { items: [{ x: 1 }, { x: 2 }, { x: 3 }] };
console.log("Root:", Outer.hashTreeRoot(value));
```
Concepts:

Composition with nested containers
Merkle tree root generation from nested structures

## 3. Lists & Vectors
What it does:
Illustrates the difference between lists (variable-length) and vectors (fixed-length) collections in SSZ.

```ts
import { ListType, VectorType, UintNumberType } from "@chainsafe/ssz";

const ListU8 = new ListType(new UintNumberType(8), 4);
const VectorU8 = new VectorType(new UintNumberType(8), 4);

console.log("List root:", ListU8.hashTreeRoot([1, 2, 3]));
console.log("Vector root:", VectorU8.hashTreeRoot([1, 2, 3, 4]));
```

Concepts:

`ListType` allows up to N items
`VectorType` requires exactly N items
Merkle roots for collection types
## 4. Hashing & Merkleization

What it does:
Shows how SSZ performs hashing (Merkleization) to compute the hashTreeRoot — a cryptographic fingerprint of structured data.

```ts
import { ContainerType, UintNumberType } from "@chainsafe/ssz";

const MyType = new ContainerType({
balance: new UintNumberType(64),
});

const user = { balance: 1200n };
console.log("HashTreeRoot:", MyType.hashTreeRoot(user));

```

Concepts:
Computing Merkle roots
Deterministic state representation
Used for consensus and proofs in Ethereum
## 5. JSON Conversion

What it does:
Demonstrates converting between SSZ values and JSON, useful when working with APIs or external systems.

```ts
import { ContainerType, UintNumberType } from "@chainsafe/ssz";

const Account = new ContainerType({
id: new UintNumberType(8),
balance: new UintNumberType(64),
});

const obj = { id: 1, balance: 3000n };
const json = Account.toJson(obj);
const fromJson = Account.fromJson(json);

console.log("To JSON:", json);
console.log("From JSON:", fromJson);
```

Concepts:

`toJson()` for converting SSZ → JSON
`fromJson()` for JSON → SSZ object
Great for API data serialization
## 6. Proofs
What it does:
Shows how to generate a Merkle proof for verifying specific fields in an SSZ structure.
Proofs allow you to prove that a piece of data belongs to a larger structure without revealing everything.

```ts
import { ProofType, ContainerType, UintNumberType } from "@chainsafe/ssz";

const Balance = new ContainerType({
amount: new UintNumberType(64),
nonce: new UintNumberType(32),
});

const data = { amount: 1000n, nonce: 42 };
const proof = ProofType.createProof(Balance, data, ["amount"]);

console.log("Proof:", proof);
```
Concepts:

Partial Merkle proofs
Data verification with minimal exposure
Useful for light clients and on-chain verification
## 7. Common View Operations

What it does:
Demonstrates how SSZ “views” work — allowing tree-backed state mutations without rebuilding the entire structure.

```ts
import { ContainerType, ListType, UintNumberType } from "@chainsafe/ssz";

const Container = new ContainerType({
a: new ListType(new UintNumberType(8), 4),
});

const c1 = Container.toView({ a: [1, 2] });
const c2 = c1.clone();

// Mutate both views
c1.a.set(0, 99);
console.log(c1.a.getAll(), c2.a.getAll());
```

Concepts:

Tree-backed views for efficient state changes
Shared memory behavior across clones
Useful for blockchain state updates


## 8. Advanced Example: Tree-Backed Views
What it does:
Demonstrates tree-backed views more deeply by mutating fields and observing how the root hash changes efficiently.

```ts
import { ContainerType, UintNumberType } from "@chainsafe/ssz";

const Complex = new ContainerType({
x: new UintNumberType(32),
y: new UintNumberType(32),
});

const view = Complex.toView({ x: 10, y: 20 });
console.log("Root before:", view.hashTreeRoot());
view.x.set(100);
console.log("Root after mutation:", view.hashTreeRoot());
```
Concepts:

Persistent data structures
Efficient partial updates
State versioning in SSZ

## To Run Examples:

You can copy any code block and run it directly in a TypeScript file:

```
ts-node ssz-examples.ts
```

## Tip
If you prefer organized files, you can still split these into:
In your own enviroment/
```
examples/
├─ simple-container.ts
├─ nested-container.ts
├─ lists-vectors.ts
├─ hashing.ts
├─ simple-container-json.ts
├─ nested-container-json.ts
├─ list-json.ts
└─ hashing-json.ts

```

I hope this docs help you understand `SSZ` better.
Loading
Loading