-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsign_options.rs
53 lines (46 loc) · 1.78 KB
/
sign_options.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
use near_api::*;
use near_crypto::SecretKey;
use near_primitives::account::AccessKeyPermission;
use signer::generate_seed_phrase;
#[tokio::main]
async fn main() {
let network = near_workspaces::sandbox().await.unwrap();
let account = network.dev_create_account().await.unwrap();
let network = NetworkConfig::from(network);
// Current secret key from workspace
let current_secret_key: SecretKey = account.secret_key().to_string().parse().unwrap();
let (new_seed_phrase, public_key) = generate_seed_phrase().unwrap();
// Let's add new key and get the seed phrase
Account(account.id().clone())
.add_key(AccessKeyPermission::FullAccess, public_key)
.with_signer(Signer::new(Signer::from_secret_key(current_secret_key.clone())).unwrap())
.send_to(&network)
.await
.unwrap();
// Let's add ledger to the account with the new seed phrase
let ledger_pubkey = Signer::from_ledger().get_public_key().unwrap();
Account(account.id().clone())
.add_key(AccessKeyPermission::FullAccess, ledger_pubkey)
.with_signer(
Signer::new(Signer::from_seed_phrase(&new_seed_phrase, Some("smile")).unwrap())
.unwrap(),
)
.send_to(&network)
.await
.unwrap();
println!("Signing with ledger");
// Let's sign some tx with the ledger key
Account(account.id().clone())
.delete_key(current_secret_key.public_key())
.with_signer(Signer::new(Signer::from_ledger()).unwrap())
.send_to(&network)
.await
.unwrap();
let keys = Account(account.id().clone())
.list_keys()
.fetch_from(&network)
.await
.unwrap();
// Should contain 2 keys: new key from seed phrase, and ledger key
println!("{:#?}", keys);
}