Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

The C ABI

lez/core_ffi exposes the wallet and a sequencer over a C ABI for callers not writing Rust (Building on the wallet library is the Rust API), with a generated header at lez/core_ffi/core_ffi.h.

The crate builds three ways, declared in lez/core_ffi/Cargo.toml:

crate-typeFor
cdyliba shared library to load at runtime
staticliblinking into a binary or an iOS or Android build
rlibdepending on it from Rust, though the Rust API is better

It has two independent halves: sequencer_ffi_* runs a node in your process, and wallet_ffi_* drives a wallet.

Building and linking it

cargo build --release -p core_ffi compiles the L1 stack, risc0, the L1’s zone SDK (logos-blockchain-zone-sdk) and the sequencer, because this crate exposes a node as well as a wallet. Expect tens of minutes cold, and a 568 MB libcore_ffi.a or a 94 MB libcore_ffi.dylib. A caller that only sends transactions still builds all of it.

Linking on macOS needs extra frameworks, because the prover pulls in Metal:

cc -o app app.c target/release/libcore_ffi.a \
   -framework Security -framework CoreFoundation -framework SystemConfiguration \
   -framework Metal -framework Foundation -lc++

Without Metal and Foundation the link fails on undefined _MTLCreateSystemDefaultDevice.

Which sequencer the wallet talks to

Every wallet constructor takes a sequencer pointer that decides where transactions go (client_for, lez/core_ffi/src/wallet/lifecycle.rs):

You passThe wallet uses
a pointer from sequencer_ffi_start_sequencerthat in-process sequencer directly, with no network in between
nullan HTTP client built from the sequencer address in the wallet’s config file

Null is not an error: it points an FFI wallet at a remote node, which most callers want. A real pointer embeds a node and a wallet in one process, and must outlive every handle built from it.

The sequencer side

Seventeen functions. Lifecycle is sequencer_ffi_setup, then sequencer_ffi_start_sequencer and sequencer_ffi_stop_sequencer, mirroring the setup and start subcommands of sequencer_service, with options as JSON. sequencer_ffi_is_healthy answers like the node’s /health route, and sequencer_ffi_init_logger turns on logging.

Ten reads ask the in-process sequencer, not the indexer: account, block, block by hash, block by transaction hash, block range, last block, transaction, transactions by account (at most 100 per call, more returns InvalidArgument), events, executor status. Plus sequencer_ffi_send_transaction, and sequencer_ffi_free_query_block_id_by_transaction to free what one read returns.

For history rather than current state, use the indexer (Reading state from a client explains the split).

The wallet side

Seventy functions, in groups mostly matching the CLI:

GroupWhat it covers
Lifecyclecreate_new, open, save, destroy, restore_data, set_password, storage_is_encrypted
Accountscreate public and private, import either, list, resolve, read, balance, account view, initial key
Transferssix shapes: public, shielded, deshielded, private, shielded_owned, private_owned
Keysexport and rebuild private account keys, including from raw bytes
PDAsderive a public or private PDA account id
Pendinglist and receive
Program loadercreate and update a header, write a segment, deploy, update
Generic transactionssend an arbitrary public or private transaction, message already encoded
Feesget_fee_state, set_fee_params, set_dry_run, set_allow_revert
Syncsync_to_block, get_last_synced_block, get_current_block_height, poll_transaction_status
Labelsadd, check availability, list for an account
Shared accountscreate_new_group, create_sealing_key, create_private_shared_account, invite_shared_account, join_shared_account (which needs the wallet’s sealing key first, else Unapplicable)
Bridgewithdraw to L1, which the bridge program rejects in this version

wallet_ffi_create_new starts a fresh wallet at the chain tip, as the CLI does, falling back to genesis when the node is unreachable. On a first run nothing before the tip is scanned unless you call wallet_ffi_rescan_private_accounts, which rediscovers private accounts from genesis.

The six transfer shapes cover the public and private combinations, which take different arguments: an account id for a recipient you can name, key material for one you cannot. The _owned pair is for a private recipient this wallet owns, credited in the transaction instead of waiting in the mailbox.

With set_dry_run on, a send simulates and reports its cost instead of going out; this is how a non-Rust client prices a transaction. Accounts and transfers explains the numbers.

Two conventions that will bite you

Check the error before you dereference. Most sequencer reads and sequencer_ffi_send_transaction return a PointerResult, a value pointer plus an error field; the pointer is valid only when the error is clear. Its doc comment says “Pointer is not guaranteed” (lez/core_ffi/src/primitives/result.rs). Most wallet calls instead return an FfiOperationError and write through out pointers, which hold nothing usable unless it is Success.

Free what you are given. Twenty-one *_free_* functions are declared: eight wallet_ffi_free_* (account data, account identity, account lists, label lists, pending message lists, programs, transaction results, transfer results); one sequencer_ffi_free_*; and twelve for the shared types such as strings and keys (eleven primitives_ffi_free_* plus primitves_ffi_free_ffi_account_opt, which the header misspells and a C caller must match). Free a string with primitives_ffi_free_cstring and private account keys with primitives_ffi_free_private_account_keys, not a wallet_ffi_ call: header doc comments naming wallet_ffi_free_string or wallet_ffi_free_private_account_keys point at undeclared functions. Nothing is reference counted or freed for you.

What is verified here

The build and link recipe above, creating a wallet from C with a null sequencer pointer, and freeing its mnemonic with primitives_ffi_free_cstring are exercised. The rest is documented from the header and the Rust behind it, not from a C caller, so a signature in core_ffi.h overrides any description here.

Getting the header

core_ffi.h is generated with cbindgen and committed, so you can read it without building. It is the authoritative signature list, 99 declarations long.

The indexer has a separate header at lez/indexer/ffi/indexer_ffi.h, not covered here.