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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
use crate::{
dev_api_client::DevApiClient,
shared::{Home, Network, NetworkHome, LATEST_USERNAME, LOCALHOST_NAME},
};
use anyhow::{anyhow, Result};
use diem_crypto::PrivateKey;
use diem_infallible::duration_since_epoch;
use diem_sdk::{
client::FaucetClient,
transaction_builder::{Currency, TransactionFactory},
types::LocalAccount,
};
use diem_types::{
account_address::AccountAddress,
account_config,
chain_id::ChainId,
transaction::{authenticator::AuthenticationKey, ScriptFunction, TransactionPayload},
};
use generate_key::load_key;
use move_core_types::{
ident_str,
language_storage::{ModuleId, TypeTag},
};
use std::{
io,
path::{Path, PathBuf},
};
pub async fn handle(home: &Home, root: Option<PathBuf>, network: Network) -> Result<()> {
let network_home = home.new_network_home(&network.get_name());
network_home.generate_paths_if_nonexistent()?;
check_nodeconfig_exists_if_localhost_used(home, &network)?;
if network_home.key_path_for(LATEST_USERNAME).exists() {
match user_wants_another_key(&network_home) {
true => archive_current_files(&network_home)?,
false => return Ok(()),
}
}
let new_account = generate_new_account(&network_home)?;
let test_account = generate_test_account(&network_home)?;
match network.get_faucet_url() {
Some(_) => {
create_account_via_faucet(&network, &new_account).await?;
create_account_via_faucet(&network, &test_account).await
}
None => {
println!("Connecting to {}...", network.get_json_rpc_url());
let client = DevApiClient::new(reqwest::Client::new(), network.get_dev_api_url())?;
let factory = TransactionFactory::new(ChainId::test());
if let Some(input_root_key) = root {
network_home.copy_key_to(LATEST_USERNAME, input_root_key.as_path())?
}
let mut treasury_account =
get_treasury_account(&client, home.get_root_key_path()).await?;
create_account_via_dev_api(&mut treasury_account, &new_account, &factory, &client)
.await?;
create_account_via_dev_api(&mut treasury_account, &test_account, &factory, &client)
.await
}
}
}
fn check_nodeconfig_exists_if_localhost_used(home: &Home, network: &Network) -> Result<()> {
match network.get_name().as_str() {
LOCALHOST_NAME => match home.get_node_config_path().is_dir() {
true => Ok(()),
false => Err(anyhow!(
"A node hasn't been created yet! Run shuffle node first"
)),
},
_ => Ok(()),
}
}
fn user_wants_another_key(network_home: &NetworkHome) -> bool {
let key_path = network_home.key_path_for(LATEST_USERNAME);
let prev_public_key = generate_key::load_key(&key_path).public_key();
println!(
"Public Key already exists: {}",
::hex::encode(prev_public_key.to_bytes())
);
println!("Are you sure you want to generate a new key? [y/n]");
let user_response = ask_user_if_they_want_key(String::new());
delegate_user_response(user_response.as_str())
}
fn ask_user_if_they_want_key(mut user_response: String) -> String {
io::stdin()
.read_line(&mut user_response)
.expect("Failed to read line");
user_response.trim().to_owned()
}
fn delegate_user_response(user_response: &str) -> bool {
match user_response {
"y" => true,
"n" => false,
_ => {
println!("Please restart and enter either y or n");
false
}
}
}
fn archive_current_files(network_home: &NetworkHome) -> Result<()> {
let time = duration_since_epoch();
let archive_dir = network_home.create_archive_dir(time)?;
network_home.archive_old_key_for(LATEST_USERNAME, &archive_dir)?;
network_home.archive_old_address_for(LATEST_USERNAME, &archive_dir)
}
fn generate_new_account(network_home: &NetworkHome) -> Result<LocalAccount> {
let private_key = network_home.generate_key_file()?;
let public_key = private_key.public_key();
network_home.generate_address_file(LATEST_USERNAME, &public_key)?;
Ok(LocalAccount::new(
AuthenticationKey::ed25519(&public_key).derived_address(),
private_key,
0,
))
}
fn generate_test_account(network_home: &NetworkHome) -> Result<LocalAccount> {
let test_key = network_home.generate_testkey_file()?;
let public_test_key = test_key.public_key();
network_home.generate_testkey_address_file(&test_key.public_key())?;
Ok(LocalAccount::new(
AuthenticationKey::ed25519(&public_test_key).derived_address(),
test_key,
0,
))
}
pub async fn get_treasury_account(
client: &DevApiClient,
root_key_path: &Path,
) -> Result<LocalAccount> {
let treasury_account_key = if root_key_path.exists() {
load_key(root_key_path)
} else {
return Err(anyhow!(
"A node hasn't been created yet! Run shuffle node first"
));
};
let treasury_seq_num = client
.get_account_sequence_number(account_config::treasury_compliance_account_address())
.await?;
Ok(LocalAccount::new(
account_config::treasury_compliance_account_address(),
treasury_account_key,
treasury_seq_num,
))
}
pub async fn create_account_via_dev_api(
treasury_account: &mut LocalAccount,
new_account: &LocalAccount,
factory: &TransactionFactory,
client: &DevApiClient,
) -> Result<()> {
match client.get_account_resources(new_account.address()).await {
Ok(_) => println!("Account already exists: {}", new_account.address()),
Err(_) => {
println!("Creating a new account onchain...");
let create_new_account_txn = treasury_account.sign_with_transaction_builder(
factory.payload(encode_create_parent_vasp_account_script_function(
Currency::XUS.type_tag(),
0,
new_account.address(),
new_account.authentication_key().prefix().to_vec(),
vec![],
false,
)),
);
let bytes = bcs::to_bytes(&create_new_account_txn)?;
let json = client.post_transactions(bytes).await?;
let hash = DevApiClient::get_hash_from_post_txn(json)?;
client.check_txn_executed_from_hash(hash.as_str()).await?;
println!("Successfully created account {}", new_account.address());
println!("Public key: {}", new_account.public_key());
}
};
Ok(())
}
pub async fn create_account_via_faucet(network: &Network, account: &LocalAccount) -> Result<()> {
let faucet_account_creation_url = network.normalized_faucet_url()?.join("accounts")?;
let faucet_client = FaucetClient::new(
faucet_account_creation_url.to_string(),
network.get_json_rpc_url().to_string(),
);
let auth_key = account.authentication_key();
tokio::task::spawn_blocking(move || faucet_client.create_account(auth_key, "XUS")).await??;
println!(
"Successfully created account {} on {}",
account.address(),
network.get_name()
);
Ok(())
}
fn encode_create_parent_vasp_account_script_function(
coin_type: TypeTag,
sliding_nonce: u64,
new_account_address: AccountAddress,
auth_key_prefix: Vec<u8>,
human_name: Vec<u8>,
add_all_currencies: bool,
) -> TransactionPayload {
TransactionPayload::ScriptFunction(ScriptFunction::new(
ModuleId::new(
AccountAddress::new([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]),
ident_str!("AccountCreationScripts").to_owned(),
),
ident_str!("create_parent_vasp_account").to_owned(),
vec![coin_type],
vec![
bcs::to_bytes(&sliding_nonce).unwrap(),
bcs::to_bytes(&new_account_address).unwrap(),
bcs::to_bytes(&auth_key_prefix).unwrap(),
bcs::to_bytes(&human_name).unwrap(),
bcs::to_bytes(&add_all_currencies).unwrap(),
],
))
}
#[cfg(test)]
mod test {
use super::*;
use crate::shared;
use std::fs;
use tempfile::tempdir;
#[test]
fn test_check_nodeconfig_exists_if_localhost_used() {
let tmpdir = tempdir().unwrap();
let dir_path = tmpdir.path();
let home = Home::new(dir_path).unwrap();
assert_eq!(
check_nodeconfig_exists_if_localhost_used(&home, &shared::Network::default()).is_err(),
true
);
fs::create_dir_all(dir_path.join(".shuffle/nodeconfig")).unwrap();
assert_eq!(
check_nodeconfig_exists_if_localhost_used(&home, &shared::Network::default()).is_err(),
false
);
}
#[test]
fn test_delegate_user_response() {
assert_eq!(delegate_user_response("a"), false);
assert_eq!(delegate_user_response("n"), false);
assert_eq!(delegate_user_response("y"), true);
}
}