Expand description
Soroban WASM smart contract decompiler.
This crate reconstructs idiomatic Rust source code from compiled Soroban WASM binaries. It combines the contract specification metadata embedded in the WASM custom sections with bytecode-level stack simulation to produce output that closely resembles the original contract source, including type definitions, function signatures, storage operations, authentication calls, and cross-contract invocations.
§Quick start
The simplest way to use the crate is through the top-level decompile
function, which runs the full pipeline and returns formatted Rust source:
use soroban_decompiler::{decompile, DecompileOptions};
let wasm = std::fs::read("contract.wasm").unwrap();
let options = DecompileOptions { signatures_only: false };
let source = decompile(&wasm, &options).unwrap();
println!("{source}");§Architecture
The decompilation pipeline runs in four stages. Each stage is also available as a standalone entry point for tools that need intermediate results.
-
Spec extraction (
spec_extract) – readscontractspecv0custom sections to recover struct definitions, enum variants, error codes, event schemas, and function signatures with fully typed and named parameters. Entry point:extract_spec. -
WASM analysis (
wasm_analysis) – parses the binary withwalrus, traces through Soroban dispatcher chains, and simulates the stack for each implementation function. The simulator tracks values through locals, memory stores, function calls, and control flow branches, resolving host function call arguments back to their origins (parameters, constants, or earlier call results). Callee memory writes are propagated to the caller so that helper functions that store through pointer parameters have their results visible in the calling function. Entry point:analyze. -
Pattern recognition (
pattern_recognizer) – maps host call sequences to high-level Soroban SDK operations. For example, asymbol_new_from_linear_memoryfollowed byget_contract_databecomesenv.storage().instance().get(symbol_short!("KEY")). This stage also resolves struct field accesses through map unpack operations, detects i128 round-trips, strips Soroban Val encoding boilerplate, and runs dead variable elimination and common subexpression elimination. The output is a typed intermediate representation defined inir. -
Code generation (
codegen) – walks the IR and emits Rust token streams usingsynandquote, then formats the result withprettyplease. Reconstructs#[contracttype]definitions,#[contracterror]error enums,#[contractimpl]function bodies, and the top-level#[contract]struct with appropriateuseimports.
Modules§
- codegen
- Rust source code generation from the decompiled IR.
- host_
functions - Soroban host function database.
- ir
- High-level intermediate representation for decompiled Soroban contracts.
- pattern_
recognizer - Pattern recognition: maps WASM host call sequences to idiomatic Soroban SDK operations.
- spec_
extract - Contract specification extraction from WASM custom sections.
- wasm_
analysis - WASM module analysis for Soroban contract decompilation.
- wasm_
imports - WASM import table resolution against the Soroban host function database.
Structs§
- Decompile
Options - Options controlling the decompilation process.
Functions§
- analyze
- Analyze WASM function bodies, resolving exports and host calls.
- decompile
- Decompile a Soroban WASM binary into formatted Rust source code.
- extract_
spec - Extract contract spec entries from a compiled Soroban WASM binary.
- resolve_
imports - Resolve all host function imports in a WASM binary.