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
use state::Storage;
use std::sync::RwLock;
use prost::Message;
use super::rpc::Rpc;
mod proto { include!("../../../libqaul/src/rpc/protobuf_generated/rust/qaul.rpc.user_accounts.rs"); }
static USERACCOUNTS: Storage<RwLock<UserAccounts>> = Storage::new();
pub enum MyUserAccountInitialiation {
Uninitialized,
NoDefaultAccount,
Initialized,
}
pub struct UserAccounts {
initialiation: MyUserAccountInitialiation,
my_user_account: Option<proto::MyUserAccount>,
}
impl UserAccounts {
pub fn init() {
let user_accounts = UserAccounts {
initialiation: MyUserAccountInitialiation::Uninitialized,
my_user_account: None,
};
USERACCOUNTS.set(RwLock::new(user_accounts));
Self::request_default_account();
}
pub fn get_user_id() -> Option<Vec<u8>> {
let user_accounts = USERACCOUNTS.get().read().unwrap();
if let Some(my_user_account) = &user_accounts.my_user_account {
return Some(my_user_account.id.clone())
}
None
}
pub fn cli(command: &str) {
match command {
cmd if cmd.starts_with("default") => {
Self::request_default_account();
},
cmd if cmd.starts_with("create ") => {
Self::create_user_account(cmd.strip_prefix("create ").unwrap().to_string());
},
_ => log::error!("unknown account command"),
}
}
fn create_user_account(user_name: String) {
let proto_message = proto::UserAccounts {
message: Some(proto::user_accounts::Message::CreateUserAccount(
proto::CreateUserAccount {
name: user_name,
}
)),
};
let mut buf = Vec::with_capacity(proto_message.encoded_len());
proto_message.encode(&mut buf).expect("Vec<u8> provides capacity as needed");
Rpc::send_message(buf, super::rpc::proto::Modules::Useraccounts.into(), "".to_string());
}
fn request_default_account() {
let proto_message = proto::UserAccounts {
message: Some(proto::user_accounts::Message::GetDefaultUserAccount(true)),
};
let mut buf = Vec::with_capacity(proto_message.encoded_len());
proto_message.encode(&mut buf).expect("Vec<u8> provides capacity as needed");
Rpc::send_message(buf, super::rpc::proto::Modules::Useraccounts.into(), "".to_string());
}
pub fn rpc(data: Vec<u8>) {
match proto::UserAccounts::decode(&data[..]) {
Ok(user_accounts) => {
match user_accounts.message {
Some(proto::user_accounts::Message::DefaultUserAccount(proto_defaultuseraccount)) => {
let mut user_accounts = USERACCOUNTS.get().write().unwrap();
if proto_defaultuseraccount.user_account_exists {
if let Some(my_user_account) = proto_defaultuseraccount.my_user_account {
println!("Your user account is:");
println!("{}, ID[{}]",my_user_account.name, my_user_account.id_base58);
println!(" public key: {}", my_user_account.key_base58);
user_accounts.my_user_account = Some(my_user_account);
user_accounts.initialiation = MyUserAccountInitialiation::Initialized;
}
else {
log::error!("unexpected message configuration");
}
}
else {
println!("No user account created yet");
println!("Please create a user account:");
println!("");
println!(" account create {{Your User Name}}");
println!("");
user_accounts.initialiation = MyUserAccountInitialiation::NoDefaultAccount;
}
},
Some(proto::user_accounts::Message::MyUserAccount(proto_myuseraccount)) => {
let mut user_accounts = USERACCOUNTS.get().write().unwrap();
println!("New user account created:");
println!("{}, ID[{}]",proto_myuseraccount.name, proto_myuseraccount.id_base58);
println!(" public key: {}", proto_myuseraccount.key_base58);
user_accounts.my_user_account = Some(proto_myuseraccount);
user_accounts.initialiation = MyUserAccountInitialiation::Initialized;
},
_ => {
log::error!("unprocessable RPC user accounts message");
},
}
},
Err(error) => {
log::error!("{:?}", error);
},
}
}
}