arb_stylus/
lib.rs

1//! Stylus WASM smart contract runtime.
2//!
3//! Provides the execution pipeline for Stylus programs: WASM compilation
4//! and caching, ink metering, host I/O functions, and EVM interop.
5
6pub mod cache;
7pub mod config;
8pub mod env;
9pub mod error;
10pub mod evm_api;
11pub mod evm_api_impl;
12#[allow(unused_mut)]
13pub mod host;
14pub mod ink;
15pub mod meter;
16pub mod middleware;
17pub mod native;
18pub mod pages;
19pub mod pricing;
20pub mod run;
21
22pub use cache::InitCache;
23pub use config::{CompileConfig, StylusConfig};
24pub use evm_api::EvmApi;
25pub use evm_api_impl::StylusEvmApi;
26pub use ink::{Gas, Ink};
27pub use meter::{MachineMeter, MeteredMachine, STYLUS_ENTRY_POINT};
28pub use native::NativeInstance;
29pub use run::RunProgram;
30
31/// Prefix bytes that identify a Stylus WASM program in contract bytecode.
32///
33/// The discriminant is `[0xEF, 0xF0, 0x00]`. The `0xEF` byte conflicts with
34/// EIP-3541, so EIP-3541 must be disabled for Stylus-era blocks to allow
35/// deployment. The third byte `0x00` is the EOF version marker.
36pub const STYLUS_DISCRIMINANT: [u8; 3] = [0xEF, 0xF0, 0x00];
37
38/// Returns `true` if the bytecode is a Stylus WASM program.
39///
40/// Checks for the 3-byte discriminant prefix `[0xEF, 0xF0, 0x00]`.
41pub fn is_stylus_program(bytecode: &[u8]) -> bool {
42    bytecode.len() >= 4 && bytecode[..3] == STYLUS_DISCRIMINANT
43}
44
45/// Strips the 4-byte Stylus prefix from contract bytecode.
46///
47/// Returns `(stripped_bytecode, version_byte)` or an error if the bytecode
48/// is too short or doesn't have the Stylus discriminant.
49pub fn strip_stylus_prefix(bytecode: &[u8]) -> Result<(&[u8], u8), &'static str> {
50    if bytecode.len() < 4 {
51        return Err("bytecode too short for Stylus prefix");
52    }
53    if bytecode[..3] != STYLUS_DISCRIMINANT {
54        return Err("bytecode does not have Stylus discriminant");
55    }
56    let version = bytecode[3];
57    Ok((&bytecode[4..], version))
58}