-
Notifications
You must be signed in to change notification settings - Fork 61
/
ledger_nano.rs
79 lines (63 loc) · 2.56 KB
/
ledger_nano.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright 2022 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0
//! cargo run --example ledger_nano --release --features=ledger_nano
use std::{env, time::Instant};
use dotenv::dotenv;
use iota_wallet::{
account_manager::AccountManager,
iota_client::constants::SHIMMER_COIN_TYPE,
secret::{ledger_nano::LedgerSecretManager, SecretManager},
AddressWithAmount, ClientOptions, Result,
};
// In this example we will create addresses with a ledger nano hardware wallet
// To use the ledger nano simulator clone https://github.com/iotaledger/ledger-shimmer-app, run `git submodule init && git submodule update --recursive`,
// then `./build.sh -m nanos|nanox|nanosplus -s` and use `true` in `LedgerSecretManager::new(true)`.
#[tokio::main]
async fn main() -> Result<()> {
// This example uses dotenv, which is not safe for use in production
dotenv().ok();
let client_options = ClientOptions::new()
.with_node(&env::var("NODE_URL").unwrap())?
.with_node_sync_disabled();
let secret_manager = LedgerSecretManager::new(true);
let manager = AccountManager::builder()
.with_secret_manager(SecretManager::LedgerNano(secret_manager))
.with_storage_path("ledger_nano_walletdb")
.with_client_options(client_options)
.with_coin_type(SHIMMER_COIN_TYPE)
.finish()
.await?;
println!("{:?}", manager.get_ledger_nano_status().await?);
// Get account or create a new one
let account_alias = "ledger";
let account = match manager.get_account(account_alias).await {
Ok(account) => account,
_ => {
// first we'll create an example account and store it
manager
.create_account()
.with_alias(account_alias.to_string())
.finish()
.await?
}
};
let address = account.generate_addresses(1, None).await?;
println!("{:?}", address);
let now = Instant::now();
let balance = account.sync(None).await?;
println!("Syncing took: {:.2?}", now.elapsed());
println!("Balance: {:?}", balance);
// send transaction
let outputs = vec![AddressWithAmount {
address: "rms1qpszqzadsym6wpppd6z037dvlejmjuke7s24hm95s9fg9vpua7vluaw60xu".to_string(),
amount: 1_000_000,
}];
let transaction = account.send_amount(outputs, None).await?;
println!(
"Transaction: {} Block sent: {}/api/core/v2/blocks/{}",
transaction.transaction_id,
&env::var("NODE_URL").unwrap(),
transaction.block_id.expect("No block created yet")
);
Ok(())
}