This repository has been archived by the owner on Oct 27, 2021. It is now read-only.
forked from darkrenaissance/darkfi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.rs
544 lines (440 loc) · 16.9 KB
/
client.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
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
use async_executor::Executor;
use async_std::sync::{Arc, Mutex};
use bellman::groth16;
use bls12_381::Bls12;
use log::{debug, info, warn};
use url::Url;
use crate::{
blockchain::{rocks::columns, Rocks, RocksColumn, Slab},
crypto::{
merkle::{CommitmentTree, IncrementalWitness},
merkle_node::MerkleNode,
note::{EncryptedNote, Note},
nullifier::Nullifier,
OwnCoin,
},
serial::{serialize, Decodable, Encodable},
service::{GatewayClient, GatewaySlabsSubscriber},
state::{state_transition, ProgramState, StateUpdate},
tx,
wallet::{walletdb::Balances, CashierDbPtr, Keypair, WalletPtr},
Result,
};
#[derive(Debug)]
pub enum ClientFailed {
NotEnoughValue(u64),
InvalidAddress(String),
InvalidAmount(u64),
UnableToGetDepositAddress,
UnableToGetWithdrawAddress,
DoesNotHaveCashierPublicKey,
DoesNotHaveKeypair,
EmptyPassword,
WalletInitialized,
KeyExists,
ClientError(String),
}
pub struct Client {
mint_params: bellman::groth16::Parameters<Bls12>,
spend_params: bellman::groth16::Parameters<Bls12>,
gateway: GatewayClient,
wallet: WalletPtr,
pub main_keypair: Keypair,
}
impl Client {
pub async fn new(
rocks: Arc<Rocks>,
gateway_addrs: (Url, Url),
wallet: WalletPtr,
mint_params: bellman::groth16::Parameters<Bls12>,
spend_params: bellman::groth16::Parameters<Bls12>,
) -> Result<Self> {
wallet.init_db().await?;
if wallet.get_keypairs()?.is_empty() {
wallet.key_gen()?;
}
let main_keypair = wallet.get_keypairs()?[0].clone();
info!(
target: "CLIENT", "Main Keypair: {}",
bs58::encode(&serialize(&main_keypair.public)).into_string()
);
let slabstore = RocksColumn::<columns::Slabs>::new(rocks.clone());
// create gateway client
debug!(target: "CLIENT", "Creating GatewayClient");
let gateway = GatewayClient::new(gateway_addrs.0, gateway_addrs.1, slabstore)?;
Ok(Self {
mint_params,
spend_params,
wallet,
gateway,
main_keypair,
})
}
pub async fn start(&mut self) -> Result<()> {
self.gateway.start().await?;
Ok(())
}
pub async fn transfer(
&mut self,
token_id: jubjub::Fr,
pub_key: jubjub::SubgroupPoint,
amount: u64,
) -> ClientResult<()> {
debug!(target: "CLIENT", "Start transfer {}", amount);
let token_id_exists = self.wallet.token_id_exists(&token_id)?;
if token_id_exists {
self.send(pub_key, amount, token_id, false).await?;
} else {
return Err(ClientFailed::NotEnoughValue(amount));
}
debug!(target: "CLIENT", "End transfer {}", amount);
Ok(())
}
pub async fn send(
&mut self,
pub_key: jubjub::SubgroupPoint,
amount: u64,
token_id: jubjub::Fr,
clear_input: bool,
) -> ClientResult<()> {
debug!(target: "CLIENT", "Start send {}", amount);
if amount == 0 {
return Err(ClientFailed::InvalidAmount(amount as u64));
}
let slab = self
.build_slab_from_tx(pub_key, amount, token_id, clear_input)
.await?;
self.gateway.put_slab(slab).await?;
debug!(target: "CLIENT", "End send {}", amount);
Ok(())
}
async fn build_slab_from_tx(
&self,
pub_key: jubjub::SubgroupPoint,
value: u64,
token_id: jubjub::Fr,
clear_input: bool,
) -> Result<Slab> {
debug!(target: "CLIENT", "Start build slab from tx");
let mut clear_inputs: Vec<tx::TransactionBuilderClearInputInfo> = vec![];
let mut inputs: Vec<tx::TransactionBuilderInputInfo> = vec![];
let mut outputs: Vec<tx::TransactionBuilderOutputInfo> = vec![];
if clear_input {
let signature_secret = self.main_keypair.private;
let input = tx::TransactionBuilderClearInputInfo {
value,
token_id,
signature_secret,
};
clear_inputs.push(input);
} else {
inputs = self.build_inputs(value, token_id, &mut outputs).await?;
}
outputs.push(tx::TransactionBuilderOutputInfo {
value,
token_id,
public: pub_key,
});
let builder = tx::TransactionBuilder {
clear_inputs,
inputs,
outputs,
};
let mut tx_data = vec![];
{
let tx = builder.build(&self.mint_params, &self.spend_params);
tx.encode(&mut tx_data).expect("encode tx");
}
let slab = Slab::new(tx_data);
debug!(target: "CLIENT", "End build slab from tx");
Ok(slab)
}
async fn build_inputs(
&self,
amount: u64,
token_id: jubjub::Fr,
outputs: &mut Vec<tx::TransactionBuilderOutputInfo>,
) -> Result<Vec<tx::TransactionBuilderInputInfo>> {
debug!(target: "CLIENT", "Start build inputs");
let mut inputs: Vec<tx::TransactionBuilderInputInfo> = vec![];
let mut inputs_value: u64 = 0;
let own_coins = self.wallet.get_own_coins()?;
for own_coin in own_coins.iter() {
if inputs_value >= amount {
break;
}
self.wallet.confirm_spend_coin(&own_coin.coin)?;
let witness = &own_coin.witness;
let merkle_path = witness.path().unwrap();
inputs_value += own_coin.note.value;
let input = tx::TransactionBuilderInputInfo {
merkle_path,
secret: own_coin.secret,
note: own_coin.note.clone(),
};
inputs.push(input);
}
if inputs_value < amount {
return Err(ClientFailed::NotEnoughValue(inputs_value).into());
}
if inputs_value > amount {
let return_value: u64 = inputs_value - amount;
outputs.push(tx::TransactionBuilderOutputInfo {
value: return_value,
token_id,
public: self.main_keypair.public,
});
}
debug!(target: "CLIENT", "End build inputs");
Ok(inputs)
}
pub async fn connect_to_subscriber_from_cashier(
&self,
state: Arc<Mutex<State>>,
cashier_wallet: CashierDbPtr,
notify: async_channel::Sender<(jubjub::SubgroupPoint, u64)>,
executor: Arc<Executor<'_>>,
) -> Result<()> {
// start subscribing
debug!(target: "CLIENT", "Start subscriber for cashier");
let gateway_slabs_sub: GatewaySlabsSubscriber =
self.gateway.start_subscriber(executor.clone()).await?;
let secret_key = self.main_keypair.private;
let wallet = self.wallet.clone();
let task: smol::Task<Result<()>> = executor.spawn(async move {
loop {
let slab = gateway_slabs_sub.recv().await?;
debug!(target: "CLIENT", "Received new slab");
debug!(target: "CLIENT", "Starting build tx from slab");
let tx = tx::Transaction::decode(&slab.get_payload()[..]);
if let Err(e) = tx {
warn!("TX: {}", e.to_string());
continue;
}
let mut state = state.lock().await;
let update = state_transition(&state, tx?);
if let Err(e) = update {
warn!("state transition: {}", e.to_string());
continue;
}
let mut secret_keys: Vec<jubjub::Fr> = vec![secret_key];
let mut withdraw_keys = cashier_wallet.get_withdraw_private_keys()?;
secret_keys.append(&mut withdraw_keys);
let state_apply = state
.apply(
update?,
secret_keys.clone(),
Some(notify.clone()),
wallet.clone(),
)
.await;
if let Err(e) = state_apply {
warn!("apply state: {}", e.to_string());
continue;
}
}
});
task.detach();
Ok(())
}
pub async fn connect_to_subscriber(
&self,
state: Arc<Mutex<State>>,
executor: Arc<Executor<'_>>,
) -> Result<()> {
// start subscribing
debug!(target: "CLIENT", "Start subscriber");
let gateway_slabs_sub: GatewaySlabsSubscriber =
self.gateway.start_subscriber(executor.clone()).await?;
let secret_key = self.main_keypair.private;
let wallet = self.wallet.clone();
let task: smol::Task<Result<()>> = executor.spawn(async move {
loop {
let slab = gateway_slabs_sub.recv().await?;
debug!(target: "CLIENT", "Received new slab");
debug!(target: "CLIENT", "Starting build tx from slab");
let tx = tx::Transaction::decode(&slab.get_payload()[..]);
if let Err(e) = tx {
warn!("TX: {}", e.to_string());
continue;
}
let mut state = state.lock().await;
let update = state_transition(&state, tx?);
if let Err(e) = update {
warn!("state transition: {}", e.to_string());
continue;
}
let secret_keys: Vec<jubjub::Fr> = vec![secret_key];
let state_apply = state
.apply(update?, secret_keys.clone(), None, wallet.clone())
.await;
if let Err(e) = state_apply {
warn!("apply state: {}", e.to_string());
continue;
}
}
});
task.detach();
Ok(())
}
pub async fn init_db(&self) -> Result<()> {
self.wallet.init_db().await
}
pub async fn key_gen(&self) -> Result<()> {
self.wallet.key_gen()
}
pub async fn get_balances(&self) -> Result<Balances> {
self.wallet.get_balances()
}
pub async fn token_id_exists(&self, token_id: &jubjub::Fr) -> Result<bool> {
self.wallet.token_id_exists(token_id)
}
pub async fn get_token_id(&self) -> Result<Vec<jubjub::Fr>> {
self.wallet.get_token_id()
}
}
pub struct State {
// The entire merkle tree state
pub tree: CommitmentTree<MerkleNode>,
// List of all previous and the current merkle roots
// This is the hashed value of all the children.
pub merkle_roots: RocksColumn<columns::MerkleRoots>,
// Nullifiers prevent double spending
pub nullifiers: RocksColumn<columns::Nullifiers>,
// Mint verifying key used by ZK
pub mint_pvk: groth16::PreparedVerifyingKey<Bls12>,
// Spend verifying key used by ZK
pub spend_pvk: groth16::PreparedVerifyingKey<Bls12>,
// List of cashier public keys
pub public_keys: Vec<jubjub::SubgroupPoint>,
}
impl ProgramState for State {
fn is_valid_cashier_public_key(&self, public: &jubjub::SubgroupPoint) -> bool {
debug!(target: "CLIENT STATE", "Check if it is valid cashier public key");
self.public_keys.contains(public)
}
fn is_valid_merkle(&self, merkle_root: &MerkleNode) -> bool {
debug!(target: "CLIENT STATE", "Check if it is valid merkle");
if let Ok(mr) = self.merkle_roots.key_exist(*merkle_root) {
return mr;
}
false
}
fn nullifier_exists(&self, nullifier: &Nullifier) -> bool {
debug!(target: "CLIENT STATE", "Check if nullifier exists");
if let Ok(nl) = self.nullifiers.key_exist(nullifier.repr) {
return nl;
}
false
}
// load from disk
fn mint_pvk(&self) -> &groth16::PreparedVerifyingKey<Bls12> {
&self.mint_pvk
}
fn spend_pvk(&self) -> &groth16::PreparedVerifyingKey<Bls12> {
&self.spend_pvk
}
}
impl State {
pub async fn apply(
&mut self,
update: StateUpdate,
secret_keys: Vec<jubjub::Fr>,
notify: Option<async_channel::Sender<(jubjub::SubgroupPoint, u64)>>,
wallet: WalletPtr,
) -> Result<()> {
// Extend our list of nullifiers with the ones from the update
debug!(target: "CLIENT STATE", "Extend nullifiers");
for nullifier in update.nullifiers {
self.nullifiers.put(nullifier, vec![] as Vec<u8>)?;
}
debug!(target: "CLIENT STATE", "Update merkle tree and witness ");
// Update merkle tree and witnesses
for (coin, enc_note) in update.coins.into_iter().zip(update.enc_notes.iter()) {
// Add the new coins to the merkle tree
let node = MerkleNode::from_coin(&coin);
self.tree.append(node).expect("Append to merkle tree");
debug!(target: "CLIENT STATE", "Keep track of all merkle roots");
// Keep track of all merkle roots that have existed
self.merkle_roots.put(self.tree.root(), vec![] as Vec<u8>)?;
debug!(target: "CLIENT STATE", "Update witness");
// Also update all the coin witnesses
for (coin, witness) in wallet.get_witnesses()?.iter_mut() {
witness.append(node).expect("Append to witness");
wallet.update_witness(&coin, witness.clone())?;
}
debug!(target: "CLIENT STATE", "iterate over secret_keys to decrypt note");
for secret in secret_keys.iter() {
if let Some(note) = Self::try_decrypt_note(enc_note, *secret) {
// We need to keep track of the witness for this coin.
// This allows us to prove inclusion of the coin in the merkle tree with ZK.
// Just as we update the merkle tree with every new coin, so we do the same with
// the witness.
// Derive the current witness from the current tree.
// This is done right after we add our coin to the tree (but before any other
// coins are added)
// Make a new witness for this coin
let witness = IncrementalWitness::from_tree(&self.tree);
let own_coin = OwnCoin {
coin: coin.clone(),
note: note.clone(),
secret: *secret,
witness: witness.clone(),
};
wallet.put_own_coins(own_coin)?;
let pub_key = zcash_primitives::constants::SPENDING_KEY_GENERATOR * secret;
debug!(target: "CLIENT STATE", "Received a coin: amount {} ", note.value);
debug!(target: "CLIENT STATE", "Send a notification");
if let Some(ch) = notify.clone() {
ch.send((pub_key, note.value)).await?
}
}
}
}
Ok(())
}
fn try_decrypt_note(ciphertext: &EncryptedNote, secret: jubjub::Fr) -> Option<Note> {
match ciphertext.decrypt(&secret) {
// ... and return the decrypted note for this coin.
Ok(note) => Some(note),
// We weren't able to decrypt the note with our key.
Err(_) => None,
}
}
}
impl std::error::Error for ClientFailed {}
impl std::fmt::Display for ClientFailed {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
ClientFailed::NotEnoughValue(i) => {
write!(f, "There is not enough value {}", i)
}
ClientFailed::InvalidAddress(i) => {
write!(f, "Invalid Address {}", i)
}
ClientFailed::InvalidAmount(i) => {
write!(f, "Invalid Amount {}", i)
}
ClientFailed::UnableToGetDepositAddress => f.write_str("Unable to get deposit address"),
ClientFailed::UnableToGetWithdrawAddress => {
f.write_str("Unable to get withdraw address")
}
ClientFailed::DoesNotHaveCashierPublicKey => {
f.write_str("Does not have cashier public key")
}
ClientFailed::DoesNotHaveKeypair => f.write_str("Does not have keypair"),
ClientFailed::EmptyPassword => f.write_str("Password is empty. Cannot create database"),
ClientFailed::WalletInitialized => f.write_str("Wallet already initalized"),
ClientFailed::KeyExists => f.write_str("Keypair already exists"),
ClientFailed::ClientError(i) => {
write!(f, "ClientError: {}", i)
}
}
}
}
impl From<super::error::Error> for ClientFailed {
fn from(err: super::error::Error) -> ClientFailed {
ClientFailed::ClientError(err.to_string())
}
}
pub type ClientResult<T> = std::result::Result<T, ClientFailed>;