// Copyright 2021 IOTA Stiftung // SPDX-License-Identifier: Apache-2.0 //! In this example we will create a new wallet. //! //! Make sure there's no `STRONGHOLD_SNAPSHOT_PATH` file and no `WALLET_DB_PATH` folder yet! //! //! Rename `.env.example` to `.env` first, then run the command: //! ```sh //! cargo run --release --all-features --example create_account //! ``` use iota_sdk::{ client::{ constants::SHIMMER_COIN_TYPE, secret::{stronghold::StrongholdSecretManager, SecretManager}, }, crypto::keys::bip39::Mnemonic, wallet::{ClientOptions, Result, Wallet}, }; #[tokio::main] async fn main() -> Result<()> { // This example uses secrets in environment variables for simplicity which should not be done in production. dotenvy::dotenv().ok(); for var in [ "STRONGHOLD_PASSWORD", "STRONGHOLD_SNAPSHOT_PATH", "MNEMONIC", "NODE_URL", "WALLET_DB_PATH", ] { std::env::var(var).expect(&format!(".env variable '{var}' is undefined, see .env.example")); } // Setup Stronghold secret_manager let secret_manager = StrongholdSecretManager::builder() .password(std::env::var("STRONGHOLD_PASSWORD").unwrap()) .build(std::env::var("STRONGHOLD_SNAPSHOT_PATH").unwrap())?; // Only required the first time, can also be generated with `manager.generate_mnemonic()?` let mnemonic = Mnemonic::from(std::env::var("MNEMONIC").unwrap()); // The mnemonic only needs to be stored the first time secret_manager.store_mnemonic(mnemonic).await?; let client_options = ClientOptions::new().with_node(&std::env::var("NODE_URL").unwrap())?; // Create the wallet let wallet = Wallet::builder() .with_secret_manager(SecretManager::Stronghold(secret_manager)) .with_storage_path(&std::env::var("WALLET_DB_PATH").unwrap()) .with_client_options(client_options) .with_coin_type(SHIMMER_COIN_TYPE) .finish() .await?; // Create a new account let account = wallet.create_account().with_alias("Alice").finish().await?; println!("Generated new account: '{}'", account.alias().await); Ok(()) }