MenuStart / Getting started

Getting started

Fe Codec turns your Fe types into JSON, deterministic CBOR or a format of your own, and hashes them as EIP-712 typed data. You describe a type once, a generator writes its serializers, and the same code runs in contracts, native programs and WebAssembly.

You need the Fe toolchain, version 26.7.0. Nothing else: the generator is written in Fe too.

Install the generator

Fe 26.7.0 has no derive macros or compile-time reflection, so fe_codec_gen runs as a separate tool that generates plain Fe trait implementations beside your models. Install the prebuilt binary (Linux x86_64/aarch64 or macOS Apple silicon) into ~/.local/bin:

curl -fsSL https://github.com/emeieiron/fe-codec/releases/latest/download/installer.sh | sh

The installer verifies SHA256SUMS automatically. Pass --no-modify-path (sh -s -- --no-modify-path) in CI, or configure FE_CODEC_VERSION (for example v0.1.0) and FE_CODEC_INSTALL_DIR.

Build from source

On an unsupported host or unreleased commit, check out the repository at the revision you depend on and build the generator with fe alone:

git clone https://github.com/emeieiron/fe-codec.git && cd fe-codec && git checkout <commit>
fe build --backend native tools/generator --out-dir ~/.local/bin

Add the ingots

Add the runtime ingots to your fe.toml, pinned to the matching release commit; Fe pins a Git dependency by its full commit hash (listed on each GitHub Release page). One Git source supplies several ingots by name:

[dependencies]
fe_codec_core = { source = "https://github.com/emeieiron/fe-codec.git", rev = "<commit>", name = "fe_codec_core" }
fe_codec_json = { source = "https://github.com/emeieiron/fe-codec.git", rev = "<commit>", name = "fe_codec_json" }
fe_codec_cbor = { source = "https://github.com/emeieiron/fe-codec.git", rev = "<commit>", name = "fe_codec_cbor" }

Add fe_codec_eip712 the same way if you sign typed data.

Describe your types

Mark structs and enums #[serializable]. The types are ordinary Fe: nothing else changes about how you use them.

use fe_codec_core::Text
use std::evm::Address

#[serializable]
pub struct Order {
    pub id: u64,
    pub buyer: Address,
    pub total: u256,
    pub status: OrderStatus,
    pub note: Option<Text<32>>,
}

#[serializable]
pub enum OrderStatus {
    Open,
    Paid,
}

Generate the serializers

Write them into a module beside the models, and commit the result:

fe_codec_gen --source src/models.fe --out src/serializers.fe

Run the same command with --check in CI to catch a stale file. Pass --source once per module when your models span several files. The generated module is ordinary Fe that implements traits on your types, so you never call into it directly. The generator describes every option, and how FeMP can run it on each build instead.

Encode and decode

The annotated bindings choose the type and the buffer capacity:

fn round_trip(_ order: Order) -> CodecResult<Order> {
    let bytes: BoundedBuffer<192> = fe_codec_json::encode(order)?
    let back: Order = fe_codec_json::decode(bytes)?
    Result::Ok(back)
}

Without annotations, pass the generic arguments yourself. Fe infers any you write as _ or leave off the end, so fe_codec_json::encode<_, 192>(order) names only the capacity and fe_codec_json::decode<Order>(bytes) takes it from bytes.

Switching formats doesn’t touch the serializer. Change the ingot, or write code that is generic over Format:

let cbor = fe_codec_cbor::encode<Order, 64>(order)?

pub fn store<F: Format>(_ format: F, _ order: Order) -> CodecResult<BoundedBuffer<192>> {
    format.encode<Order, 192>(order)
}

An order with ID 7, a buyer, a total of 10¹⁸, status Paid and the note "gift" encodes as 119 bytes of JSON or 43 bytes of CBOR:

{"id":7,"buyer":"0xd8da6bf26964af9d7eed9e03e53415d37aa96045","total":1000000000000000000,"status":"Paid","note":"gift"}
a5 01 07 02 54 d8da6bf26964af9d7eed9e03e53415d37aa96045 03 1b 0de0b6b3a7640000 04 01 05 64 67696674

Next