Compile a contract
This page is about compiling smart contracts and what verifiable builds are. Verifiable builds are particularly important for smart contracts as it is necessary to reproduce every single step in order to verify a contract’s compiled bytecode against the supplied source code or project to ensure that they are exactly the same.
Compile a Contract
Make sure you added the following line in the [lib]
section of your Cargo.toml
:
[lib]crate-type = ["cdylib", "rlib"]
To compile #![no_std]
Rust code to WASM we need to follow the following steps:
1. Install wasm-pack
To build the WASM contracts, wasm-pack
is required:
cargo install wasm-pack
2. Set the right rust version
Make sure that you use version 1.75.0-nightly
of rustc. This can be achieved by having a rust-toolchain.toml
file in your project or have set the rustc compiler to the following:
[toolchain]channel = "nightly-2023-11-10" # 1.75.0-nightlytargets = ["wasm32-unknown-unknown"]components = ["rust-src", "rustfmt", "cargo", "clippy"]
This ensures compatibility with the dusk-core
dependency in your contract.
3. Add the wasm target
If you have not yet added the wasm32 target to Rust, you can add it with the following command:
rustup target add wasm32-unknown-unknown
4. Specify the target explicitly
You can pass a target flag to the cargo build command, to explicitly set the target you want to compile to. This command compiles the Rust project to WebAssembly in release mode, optimizing the output for performance.
cargo build --target wasm32-unknown-unknown --release -Znext-lockfile-bump
Once the contract has been compiled successfully, you can execute in your cli:
find . -name "*.wasm"
By doing so, you should see the build file named: your_contract_project_name.wasm
.
Now that you have compiled the contract, you can deploy it to the Dusk blockchain.
Verifiable Builds
For deterministic and verifiable builds, use our Dockerized build environment, which guarantees identical outputs. This is the easiest way to ensure your smart contract builds can be reproduced by others.
Pull the Docker Image
Use the prebuilt Docker image:
docker pull dusknode/dusk-verifiable-builds:0.2.0
Run the Build Command
Replace <path-to-contract-code>
and <path-to-output-folder>
with the appropriate paths to your contract source code and output directory:
docker run --rm \ -v <path-to-contract-code>:/source \ -v <path-to-output-folder>:/target \ dusknode/dusk-verifiable-builds:0.2.0
After running this command, the compiled .wasm
files will be available, depending on your build target, in:
/target/final-output/wasm32
/target/final-output/wasm64
Verify the Output
To ensure your contract has compiled correctly, locate the .wasm
files:
find <path-to-output-folder> -name "*.wasm"
Reproduce the Build
Share the Docker image and your source code with auditors or collaborators to enable them to reproduce the same build.