Skip to main content

Crate soroban_decompiler

Crate soroban_decompiler 

Source
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.

  1. Spec extraction (spec_extract) – reads contractspecv0 custom sections to recover struct definitions, enum variants, error codes, event schemas, and function signatures with fully typed and named parameters. Entry point: extract_spec.

  2. WASM analysis (wasm_analysis) – parses the binary with walrus, 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.

  3. Pattern recognition (pattern_recognizer) – maps host call sequences to high-level Soroban SDK operations. For example, a symbol_new_from_linear_memory followed by get_contract_data becomes env.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 in ir.

  4. Code generation (codegen) – walks the IR and emits Rust token streams using syn and quote, then formats the result with prettyplease. Reconstructs #[contracttype] definitions, #[contracterror] error enums, #[contractimpl] function bodies, and the top-level #[contract] struct with appropriate use imports.

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§

DecompileOptions
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.