-
Notifications
You must be signed in to change notification settings - Fork 39
/
create_account.rs
65 lines (53 loc) · 2.13 KB
/
create_account.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
64
65
// 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(())
}