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
266
267
268
269
270
271
272
273
#![feature(async_closure)]
pub mod endpoints;
pub use endpoints::account::link_account::*;
pub use endpoints::entity::check_kyc::*;
pub use endpoints::entity::register::*;
pub use endpoints::entity::request_kyc::*;
pub use endpoints::entity::update::address::*;
pub use endpoints::entity::update::email::*;
pub use endpoints::entity::update::identity::*;
pub use endpoints::entity::update::phone::*;
pub use endpoints::entity::*;
pub use endpoints::transaction::cancel_transaction::*;
pub use endpoints::transaction::get_transactions::*;
pub use endpoints::transaction::issue_sila::*;
pub use endpoints::transaction::redeem_sila::*;
pub use endpoints::transaction::transfer_sila::*;
pub use endpoints::wallet::get_sila_balance::*;
use std::str::FromStr;
use eth_checksum;
use lazy_static::lazy_static;
use secp256k1::{Secp256k1, SecretKey};
use serde::{Deserialize, Serialize};
use sha3::{Digest, Keccak256};
use std::convert::TryInto;
use std::env;
use std::future::Future;
use std::time::SystemTime;
use uuid::Uuid;
use web3::{types::H160, types::H256};
#[derive(Clone)]
pub struct SilaParams {
pub gateway: String,
pub app_handle: String,
pub app_address: String,
pub app_private_key: Option<String>,
}
lazy_static! {
static ref SILA_PARAMS: SilaParams = {
SilaParams {
gateway: env::var("SILA_GATEWAY").expect("SILA_GATEWAY must be set"),
app_handle: env::var("SILA_APP_HANDLE").expect("SILA_APP_HANDLE must be set"),
app_address: env::var("SILA_APP_ADDRESS").expect("SILA_APP_ADDRESS must be set"),
app_private_key: Option::from(
env::var("SILA_APP_KEY").expect("SILA_APP_KEY must be set"),
),
}
};
}
#[derive(Deserialize, Serialize, PartialEq)]
pub enum Status {
SUCCESS,
FAILURE,
}
impl std::fmt::Display for Status {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match *self {
Status::SUCCESS => write!(f, "SUCCESS"),
Status::FAILURE => write!(f, "FAILURE"),
}
}
}
#[derive(Clone)]
pub struct SignedMessageParams {
pub sila_handle: Option<String>,
pub message: String,
pub usersignature: Option<String>,
pub authsignature: String,
}
#[derive(Deserialize, Serialize, Clone)]
pub struct Header {
pub reference: String,
pub created: u64,
pub user_handle: Option<String>,
pub auth_handle: String,
pub version: String,
pub crypto: String,
}
impl Default for Header {
fn default() -> Self {
Header {
reference: Uuid::new_v4().to_string(),
created: SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.expect("could not calculate current time")
.as_secs(),
user_handle: Option::None,
auth_handle: String::new(),
version: "0.2".to_string(),
crypto: "ETH".to_string(),
}
}
}
#[derive(Deserialize, Serialize, Clone)]
pub struct HeaderMessage {
pub header: Header,
pub message: String,
}
fn hash_message(message: String) -> [u8; 32] {
let mut hasher = Keccak256::new();
hasher.update(&message);
hasher
.finalize()
.as_slice()
.try_into()
.expect("Wrong length")
}
pub fn checksum(address: &str) -> String {
eth_checksum::checksum(address)
}
#[derive(Deserialize, Serialize, Clone)]
pub struct KeyParams {
pub address: String,
pub private_key: Option<String>,
}
#[derive(Deserialize, Serialize)]
pub struct SignDataParams {
pub message: String,
pub user_params: Option<KeyParams>,
pub app_params: KeyParams,
}
#[derive(Copy, Clone)]
pub struct SignData {
pub address: [u8; 20],
pub message_hash: [u8; 32],
pub private_key: Option<[u8; 32]>,
}
#[derive(Copy, Clone)]
pub struct SignDataPair {
pub user: Option<SignData>,
pub app: SignData,
}
impl From<SignDataParams> for SignDataPair {
fn from(params: SignDataParams) -> Self {
let hash = hash_message(params.message);
let mut user = Option::None;
if params.user_params.is_some() {
user = Option::from(SignData {
address: *H160::from_str(¶ms.user_params.clone().unwrap().address)
.unwrap()
.as_fixed_bytes(),
message_hash: hash,
private_key: Option::from(
*H256::from_str(¶ms.user_params.unwrap().private_key.unwrap())
.unwrap()
.as_fixed_bytes(),
),
})
};
SignDataPair {
user,
app: SignData {
address: *H160::from_str(¶ms.app_params.address)
.unwrap()
.as_fixed_bytes(),
message_hash: hash,
private_key: Option::from(
*H256::from_str(¶ms.app_params.private_key.unwrap())
.unwrap()
.as_fixed_bytes(),
),
},
}
}
}
#[derive(Deserialize, Serialize)]
pub struct Signatures {
pub usersignature: Option<String>,
pub authsignature: String,
}
#[derive(Clone)]
pub struct Signature {
pub data: String,
}
#[derive(Clone)]
pub struct Signer<F, Fut>
where
F: Fn(SignData) -> Fut,
Fut: Future<Output = Signature>,
{
pub sign_func: F,
}
impl<F, Fut> Signer<F, Fut>
where
F: Fn(SignData) -> Fut,
Fut: Future<Output = Signature>,
{
pub fn new(signer: F) -> Signer<F, Fut> {
Signer { sign_func: signer }
}
pub fn sign(self, sign_data: SignData) -> Fut {
(self.sign_func)(sign_data)
}
}
pub async fn default_sign(user_data: Option<SignData>, app_data: SignData) -> Signatures {
let closure = async move |x: SignData| {
let message = secp256k1::Message::from_slice(&x.message_hash).unwrap();
let secret_key = SecretKey::from_slice(&x.private_key.unwrap()).unwrap();
let secp = Secp256k1::new();
let signature = secp.sign_ecdsa_recoverable(&message, &secret_key);
let (id, bytes) = signature.serialize_compact();
let recovery_id = match id.to_i32() {
1 => 0x1c,
_ => 0x1b,
};
let mut eth_array = [0; 65];
eth_array[0..64].copy_from_slice(&bytes[0..64]);
eth_array[64] = recovery_id;
Signature {
data: hex::encode(eth_array),
}
};
let user_signer = Signer::new(closure);
let app_signer = Signer::new(closure);
match user_data {
Some(x) => Signatures {
usersignature: Option::from(user_signer.sign(x).await.data),
authsignature: app_signer.sign(app_data).await.data,
},
None => Signatures {
usersignature: Option::None,
authsignature: app_signer.sign(app_data).await.data,
},
}
}
pub fn header_message() -> HeaderMessage {
HeaderMessage {
header: Header {
..Default::default()
},
message: "header_msg".to_string(),
}
}
pub struct SignParams {
pub message_hash: [u8; 32],
pub address: String,
pub private_key: String,
}