-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy path01_generate_addresses.rs
65 lines (50 loc) · 2.21 KB
/
01_generate_addresses.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
//! cargo run --example generate_addresses --release -- [NODE URL]
use iota_client::{api::GetAddressesBuilder, secret::SecretManager, Client, Result};
#[tokio::main]
async fn main() -> Result<()> {
// This example uses dotenv, which is not safe for use in production
dotenv::dotenv().ok();
// Take the node URL from command line argument or use one from env as default.
let node_url = std::env::args()
.nth(1)
.unwrap_or_else(|| std::env::var("NODE_URL").unwrap());
// Create a client instance
let client = Client::builder()
.with_node(&node_url)? // Insert your node URL here
.finish()?;
let secret_manager =
SecretManager::try_from_mnemonic(&std::env::var("NON_SECURE_USE_OF_DEVELOPMENT_MNEMONIC_1").unwrap())?;
// Generate addresses with default account index and range
let addresses = client.get_addresses(&secret_manager).finish().await?;
println!("List of generated public addresses:\n{addresses:#?}\n");
// Generate addresses with custom account index and range
let addresses = client
.get_addresses(&secret_manager)
.with_account_index(0)
.with_range(0..4)
.finish()
.await?;
println!("List of generated public addresses:\n{addresses:#?}\n");
// Generate internal addresses with custom account index and range
let addresses = client
.get_addresses(&secret_manager)
.with_account_index(0)
.with_range(0..4)
.with_internal_addresses(true)
.finish()
.await?;
println!("List of generated internal addresses:\n{addresses:#?}\n");
// Generating addresses with `client.get_addresses(&secret_manager)`, will by default get the bech32_hrp (Bech32
// human readable part) from the node info, generating it "offline" requires setting it with
// `with_bech32_hrp(bech32_hrp)`
let addresses = GetAddressesBuilder::new(&secret_manager)
.with_bech32_hrp(client.get_bech32_hrp().await?)
.with_account_index(0)
.with_range(0..4)
.finish()
.await?;
println!("List of offline generated public addresses:\n{addresses:#?}\n");
Ok(())
}