-
Notifications
You must be signed in to change notification settings - Fork 41
/
getting_started.rs
63 lines (52 loc) · 2.17 KB
/
getting_started.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Copyright 2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0
//! In this example we will create a new wallet, a mnemonic, and an initial account. Then, we'll print the first address
//! of that account.
//!
//! Rename `.env.example` to `.env` first, then run the command:
//! ```sh
//! cargo run --release --all-features --example wallet_getting_started
//! ```
use iota_sdk::{
client::{
constants::SHIMMER_COIN_TYPE,
secret::{stronghold::StrongholdSecretManager, SecretManager},
},
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 ["NODE_URL"] {
std::env::var(var).expect(&format!(".env variable '{var}' is undefined, see .env.example"));
}
// Setup Stronghold secret manager.
// WARNING: Never hardcode passwords in production code.
let secret_manager = StrongholdSecretManager::builder()
.password("password".to_owned()) // A password to encrypt the stored data.
.build("vault.stronghold")?; // The path to store the account snapshot.
let client_options = ClientOptions::new().with_node(&std::env::var("NODE_URL").unwrap())?;
// Set up and store the wallet.
let wallet = Wallet::builder()
.with_secret_manager(SecretManager::Stronghold(secret_manager))
.with_client_options(client_options)
.with_coin_type(SHIMMER_COIN_TYPE)
.with_storage_path("getting-started-db")
.finish()
.await?;
// Generate a mnemonic and store its seed in the Stronghold vault.
// INFO: It is best practice to back up the mnemonic somewhere secure.
let mnemonic = wallet.generate_mnemonic()?;
println!("Mnemonic: {}", mnemonic.as_ref());
wallet.store_mnemonic(mnemonic).await?;
// Create an account.
let account = wallet
.create_account()
.with_alias("Alice") // A name to associate with the created account.
.finish()
.await?;
let first_address = &account.addresses().await?[0];
println!("{}", first_address.address());
Ok(())
}