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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
use crate::{
client_proxy::ClientProxy,
commands::{blocking_cmd, report_error, subcommand_execute, Command},
};
pub struct AccountCommand {}
impl Command for AccountCommand {
fn get_aliases(&self) -> Vec<&'static str> {
vec!["account", "a"]
}
fn get_description(&self) -> &'static str {
"Account operations"
}
fn execute(&self, client: &mut ClientProxy, params: &[&str]) {
let commands: Vec<Box<dyn Command>> = vec![
Box::new(AccountCommandCreateLocal {}),
Box::new(AccountCommandListAccounts {}),
Box::new(AccountCommandRecoverWallet {}),
Box::new(AccountCommandWriteRecovery {}),
Box::new(AccountCommandMint {}),
Box::new(AccountCommandAddCurrency {}),
];
subcommand_execute(params[0], commands, client, ¶ms[1..]);
}
}
pub struct AccountCommandCreateLocal {}
impl Command for AccountCommandCreateLocal {
fn get_aliases(&self) -> Vec<&'static str> {
vec!["create", "c"]
}
fn get_description(&self) -> &'static str {
"Create a local account--no on-chain effect. Returns reference ID to use in other operations"
}
fn execute(&self, client: &mut ClientProxy, _params: &[&str]) {
println!(">> Creating/retrieving next local account from wallet");
match client.create_next_account(true) {
Ok(account_data) => println!(
"Created/retrieved local account #{} address {}",
account_data.index,
hex::encode(account_data.address)
),
Err(e) => report_error("Error creating local account", e),
}
}
}
pub struct AccountCommandRecoverWallet {}
impl Command for AccountCommandRecoverWallet {
fn get_aliases(&self) -> Vec<&'static str> {
vec!["recover", "r"]
}
fn get_params_help(&self) -> &'static str {
"<file_path>"
}
fn get_description(&self) -> &'static str {
"Recover Diem wallet from the file path"
}
fn execute(&self, client: &mut ClientProxy, params: &[&str]) {
println!(">> Recovering Wallet");
match client.recover_wallet_accounts(params) {
Ok(account_data) => {
println!(
"Wallet recovered and the first {} child accounts were derived",
account_data.len()
);
for data in account_data {
println!("#{} address {}", data.index, hex::encode(data.address));
}
}
Err(e) => report_error("Error recovering Diem wallet", e),
}
}
}
pub struct AccountCommandWriteRecovery {}
impl Command for AccountCommandWriteRecovery {
fn get_aliases(&self) -> Vec<&'static str> {
vec!["write", "w"]
}
fn get_params_help(&self) -> &'static str {
"<file_path>"
}
fn get_description(&self) -> &'static str {
"Save Diem wallet mnemonic recovery seed to disk"
}
fn execute(&self, client: &mut ClientProxy, params: &[&str]) {
println!(">> Saving Diem wallet mnemonic recovery seed to disk");
match client.write_recovery(params) {
Ok(_) => println!("Saved mnemonic seed to disk"),
Err(e) => report_error("Error writing mnemonic recovery seed to file", e),
}
}
}
pub struct AccountCommandListAccounts {}
impl Command for AccountCommandListAccounts {
fn get_aliases(&self) -> Vec<&'static str> {
vec!["list", "la"]
}
fn get_description(&self) -> &'static str {
"Print all accounts that were created or loaded"
}
fn execute(&self, client: &mut ClientProxy, _params: &[&str]) {
client.print_all_accounts();
}
}
pub struct AccountCommandMint {}
impl Command for AccountCommandMint {
fn get_aliases(&self) -> Vec<&'static str> {
vec!["mint", "mintb", "m", "mb"]
}
fn get_params_help(&self) -> &'static str {
"<receiver_account_ref_id>|<receiver_account_address> <number_of_coins> <currency_code> [use_base_units (default=false)]"
}
fn get_description(&self) -> &'static str {
"Send currency of the given type from the faucet address to the given recipient address. Creates an account at the recipient address if one does not already exist."
}
fn execute(&self, client: &mut ClientProxy, params: &[&str]) {
if params.len() < 4 || params.len() > 5 {
println!("Invalid number of arguments for mint");
return;
}
match client.mint_coins(params, true) {
Ok(_) => {
println!("Finished sending coins from faucet!");
}
Err(e) => report_error("Error transferring coins from faucet", e),
}
}
}
pub struct AccountCommandAddCurrency {}
impl Command for AccountCommandAddCurrency {
fn get_aliases(&self) -> Vec<&'static str> {
vec!["addc", "addcb", "ac", "acb"]
}
fn get_params_help(&self) -> &'static str {
"<account_address> <currency_code>"
}
fn get_description(&self) -> &'static str {
"Add specified currency to the account. Suffix 'b' is for blocking"
}
fn execute(&self, client: &mut ClientProxy, params: &[&str]) {
if params.len() < 3 {
println!("Invalid number of arguments for adding currency to account");
return;
}
println!(">> Adding zero balance in currency to account");
let is_blocking = blocking_cmd(params[0]);
match client.add_currency(params, is_blocking) {
Ok(_) => {
if is_blocking {
println!("Finished adding currency to account!");
} else {
println!("Currency addition request submitted");
}
}
Err(e) => report_error("Error adding zero balance in currency to account", e),
}
}
}