Skip to main content

Getting Started with Rust

Safe Password Storage

In a production setup, do not store passwords in the host's environment variables or in the source code. See our backup and security recommendations for production setups.

Prerequisites

info

We recommend you update Rust to the latest stable version rustup update stable.

The library uses cmake and openssl. You may need install additional build tools on your system to run the build process successfully using Cargo.

Windows

You can install all needed prerequisites on Windows with Chocolatey. If you have any of the tools already installed just remove them from the command

Install Chocolatey with

Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))

and install the needed packages with

choco install visualstudio2022buildtools visualstudio2022-workload-vctools rust-ms msys2 llvm

macOS

You can install cmake and openssl with Homebrew by running the following commands:

brew install cmake
brew install [email protected]
# you may want to add this to your .zshrc or .bashrc since you'll need it to compile the crate
OPENSSL_ROOT_DIR=$(brew --prefix [email protected])

Linux

You can install cmake and openssl with your distro's package manager or download from their websites. On Debian and Ubuntu you will also need the build-essential package.

Include the Library in Your Cargo.toml

The only thing you need to do to start using the library is to add it as dependency in your Cargo.toml file:

[dependencies]
iota-wallet = { git = "https://github.com/iotaledger/wallet.rs", branch = "develop" }
tokio = "1.21.2"

Use the Library

In order to use the library, you need to create an AccountManager:

use std::path::PathBuf;

use iota_wallet::{
account_manager::AccountManager,
iota_client::constants::SHIMMER_COIN_TYPE,
secret::{stronghold::StrongholdSecretManager, SecretManager},
ClientOptions, Result,
};

#[tokio::main]
async fn main() -> Result<()> {
// Shouldn't be hardcoded in production
// mnemonic can be generated with `manager.generate_mnemonic()?` and will be the only way to recover your funds if
// you loose the stronghold file/password, so be sure to save it securely
let nonsecure_use_of_development_mnemonic = "endorse answer radar about source reunion marriage tag sausage weekend frost daring base attack because joke dream slender leisure group reason prepare broken river".to_string();
let stronghold_password = "some_hopefully_secure_password";

// Setup Stronghold secret_manager
let mut secret_manager = StrongholdSecretManager::builder()
.password(&stronghold_password)
.build(PathBuf::from("wallet.stronghold"))?;

// The mnemonic only needs to be stored the first time
secret_manager
.store_mnemonic(nonsecure_use_of_development_mnemonic)
.await?;

// Create the accounts_and_addresses manager with the secret_manager and client options
let client_options = ClientOptions::new().with_node("http://localhost:14265")?;

let manager = AccountManager::builder()
.with_secret_manager(SecretManager::Stronghold(secret_manager))
.with_client_options(client_options)
.with_coin_type(SHIMMER_COIN_TYPE)
.finish()
.await?;

// Create a new accounts_and_addresses, this will automatically generate an address
let account = manager
.create_account()
.with_alias("Alice".to_string())
.finish()
.await?;

println!(
"Generated a new accounts_and_addresses with addresses {:?}",
account.addresses().await?
);

Ok(())
}

What's Next?

Now that you are up and running, you can get acquainted with the library using its how-to guides and the repository's code examples.