From 6f121770e3b5512c9c2ebb666ec1be80e20b6da0 Mon Sep 17 00:00:00 2001 From: Nickqiaoo Date: Mon, 25 Dec 2023 20:07:15 +0800 Subject: [PATCH] feat:refactor clippy --- src/block.rs | 2 +- src/blockchain.rs | 2 +- src/bundle.rs | 60 ++++++++++++++++++++++------------------------ src/cli.rs | 22 ++++++++++++----- src/deposit.rs | 4 ++-- src/main.rs | 2 +- src/pow.rs | 2 +- src/transaction.rs | 6 ++--- src/withdraw.rs | 13 ++++------ src/zsend.rs | 9 +++---- 10 files changed, 61 insertions(+), 61 deletions(-) diff --git a/src/block.rs b/src/block.rs index 8f81c14..430c5b5 100644 --- a/src/block.rs +++ b/src/block.rs @@ -52,7 +52,7 @@ impl Block { } let concatenated_hashes = tx_hashes.concat(); - Sha256::digest(&concatenated_hashes).to_vec() + Sha256::digest(concatenated_hashes).to_vec() } } diff --git a/src/blockchain.rs b/src/blockchain.rs index 8336011..be86e49 100644 --- a/src/blockchain.rs +++ b/src/blockchain.rs @@ -183,7 +183,7 @@ impl Blockchain { pub fn verify_transaction(&self, tx: &Transaction) -> bool { if tx.is_coinbase() { - return true + return true; } let mut prev_txs = HashMap::new(); diff --git a/src/bundle.rs b/src/bundle.rs index 02c3b30..a37f001 100644 --- a/src/bundle.rs +++ b/src/bundle.rs @@ -1,58 +1,54 @@ -use std::fmt; -use serde::{Deserialize, Serialize}; -use orchard::{ - Action as oAction, - bundle::{Authorized}, -}; use orchard::bundle::Authorization; -use crate::transaction::Transaction; +use orchard::{bundle::Authorized, Action as oAction}; +use serde::{Deserialize, Serialize}; +use std::fmt; #[derive(Clone, Serialize, Deserialize, Default)] pub struct Bundle { - actions:Vec, + actions: Vec, flags: u8, value_balance: i64, anchor: String, proof: String, - binding_sig:String, + binding_sig: String, } -#[derive(Clone,Serialize, Deserialize)] -pub struct Action{ +#[derive(Clone, Serialize, Deserialize)] +pub struct Action { nullifier: String, rk: String, - cmx: String, - out_ciphertext:String, - ephemeral_key:String, - enc_ciphertext:String, + cmx: String, + out_ciphertext: String, + ephemeral_key: String, + enc_ciphertext: String, cv: String, spend_auth_sig: String, } impl From<&oAction<::SpendAuth>> for Action { fn from(a: &oAction<::SpendAuth>) -> Self { - let rk:[u8; 32] = a.rk().into(); - let sig :[u8; 64] = a.authorization().into(); - Action{ - nullifier: hex::encode(a.nullifier().to_bytes()), - rk: hex::encode(rk), - cmx: hex::encode(a.cmx().to_bytes()), - out_ciphertext: hex::encode(a.encrypted_note().out_ciphertext), - ephemeral_key: hex::encode(a.encrypted_note().epk_bytes), - enc_ciphertext: hex::encode(a.encrypted_note().enc_ciphertext), - cv: hex::encode(a.cv_net().to_bytes()), - spend_auth_sig: hex::encode(sig), - } + let rk: [u8; 32] = a.rk().into(); + let sig: [u8; 64] = a.authorization().into(); + Action { + nullifier: hex::encode(a.nullifier().to_bytes()), + rk: hex::encode(rk), + cmx: hex::encode(a.cmx().to_bytes()), + out_ciphertext: hex::encode(a.encrypted_note().out_ciphertext), + ephemeral_key: hex::encode(a.encrypted_note().epk_bytes), + enc_ciphertext: hex::encode(a.encrypted_note().enc_ciphertext), + cv: hex::encode(a.cv_net().to_bytes()), + spend_auth_sig: hex::encode(sig), + } } } impl From<&orchard::Bundle> for Bundle { fn from(b: &orchard::Bundle) -> Self { - let sig :[u8; 64] = b.authorization().binding_signature().into(); + let sig: [u8; 64] = b.authorization().binding_signature().into(); - Bundle{ - actions: b.actions().iter().map(|action| Action::from(action)).collect(), + Bundle { + actions: b.actions().iter().map(Action::from).collect(), flags: b.flags().to_byte(), - value_balance: b.value_balance().clone(), + value_balance: *b.value_balance(), anchor: hex::encode(b.anchor().to_bytes()), proof: hex::encode(b.authorization().proof().as_ref()), binding_sig: hex::encode(sig), @@ -87,4 +83,4 @@ impl fmt::Display for Bundle { Ok(()) } -} \ No newline at end of file +} diff --git a/src/cli.rs b/src/cli.rs index 7070fc4..f8f104f 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,8 +1,11 @@ use std::{println, vec}; -use crate::{blockchain::Blockchain, deposit, pow::ProofOfWork, transaction, verify, wallet, wallets::Wallets, withdraw, zsend}; +use crate::transaction::new_coinbase_tx; +use crate::{ + blockchain::Blockchain, deposit, pow::ProofOfWork, transaction, verify, wallet, + wallets::Wallets, withdraw, zsend, +}; use structopt::StructOpt; -use crate::transaction::{new_coinbase_tx, Transaction}; pub struct Cli { pub cmd: Command, @@ -73,7 +76,7 @@ impl Cli { Command::Getbalance { address } => self.get_balance(address.clone()), Command::Deposit { address, amount } => self.deposit(address.clone(), *amount), Command::Zsend { from, to } => self.zsend(from.clone(), to.clone()), - Command::Withdraw {address} => self.withdraw(address.clone()), + Command::Withdraw { address } => self.withdraw(address.clone()), } } @@ -147,7 +150,12 @@ impl Cli { fn deposit(&self, address: String, amount: u64) { let mut bc = Blockchain::new(&address); - let mut tx = transaction::new_utxo_transaction(address.clone(), "11111111111111111111".to_string(), amount as i64, &bc); + let mut tx = transaction::new_utxo_transaction( + address.clone(), + "11111111111111111111".to_string(), + amount as i64, + &bc, + ); let bundle = deposit::deposit(&address, amount); verify::verify_bundle(&bundle); @@ -163,8 +171,10 @@ impl Cli { let bundle = zsend::zsend(&from, &to); verify::verify_bundle(&bundle); - let mut tx = Transaction::default(); - tx.bundle = (&bundle).into(); + let mut tx = transaction::Transaction { + bundle: (&bundle).into(), + ..Default::default() + }; tx.set_id(); bc.mine_block(vec![tx]); zsend::save_note(&bundle, &from, &to); diff --git a/src/deposit.rs b/src/deposit.rs index 8363c6a..a1ba47e 100644 --- a/src/deposit.rs +++ b/src/deposit.rs @@ -11,7 +11,7 @@ use orchard::{ use rand::rngs::OsRng; use zcash_note_encryption::try_note_decryption; -pub fn deposit(address: &String, value: u64) -> Bundle { +pub fn deposit(address: &str, value: u64) -> Bundle { let wallets = Wallets::new(); let wallet = wallets.get_wallet(address).unwrap(); @@ -40,7 +40,7 @@ pub fn deposit(address: &String, value: u64) -> Bundle { shielding_bundle } -pub fn save_note(bundle: &Bundle, address: &String) { +pub fn save_note(bundle: &Bundle, address: &str) { let mut wallets = Wallets::new(); let wallet = wallets.get_wallet(address).unwrap(); let sk = wallet.sk(); diff --git a/src/main.rs b/src/main.rs index f1f156f..8b45bf4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ use structopt::StructOpt; mod block; mod blockchain; +mod bundle; mod cli; mod deposit; mod iterator; @@ -15,7 +16,6 @@ mod wallet; mod wallets; mod withdraw; mod zsend; -mod bundle; fn main() { let mut c = cli::Cli { diff --git a/src/pow.rs b/src/pow.rs index 7f0ac99..d3c97fa 100644 --- a/src/pow.rs +++ b/src/pow.rs @@ -37,7 +37,7 @@ impl<'a> ProofOfWork<'a> { pub fn validate(&self) -> bool { let data = self.prepare_data(self.block.nonce); - let hash = Sha256::digest(&data); + let hash = Sha256::digest(data); let hash_int = BigUint::from_bytes_le(&hash); hash_int < self.target diff --git a/src/transaction.rs b/src/transaction.rs index 1eab689..36bb462 100644 --- a/src/transaction.rs +++ b/src/transaction.rs @@ -2,11 +2,11 @@ use serde::{Deserialize, Serialize}; use sha2::{Digest, Sha256}; use std::{collections::HashMap, fmt}; +use crate::bundle::Bundle; use crate::{ blockchain::Blockchain, transaction_input::TXInput, transaction_output::TXOutput, wallet, wallets::Wallets, }; -use crate::bundle::Bundle; #[derive(Serialize, Deserialize, Clone, Default)] pub struct Transaction { @@ -61,7 +61,7 @@ impl Transaction { id: self.id.clone(), vin: inputs, vout: outputs, - bundle:Bundle::default(), + bundle: Bundle::default(), } } @@ -158,7 +158,7 @@ pub fn new_coinbase_tx(to: &str, data: &str, value: i64) -> Transaction { id: vec![], vin: vec![txin], vout: vec![txout], - bundle:Bundle::default(), + bundle: Bundle::default(), }; tx.set_id(); diff --git a/src/withdraw.rs b/src/withdraw.rs index 12c2786..9e99327 100644 --- a/src/withdraw.rs +++ b/src/withdraw.rs @@ -1,15 +1,15 @@ +use crate::wallets::Wallets; use bridgetree::BridgeTree; use orchard::builder::Builder; -use orchard::Bundle; use orchard::bundle::{Authorized, Flags}; use orchard::circuit::ProvingKey; use orchard::keys::{FullViewingKey, SpendAuthorizingKey}; use orchard::note::ExtractedNoteCommitment; use orchard::tree::{MerkleHashOrchard, MerklePath}; +use orchard::Bundle; use rand::rngs::OsRng; -use crate::wallets::Wallets; -pub fn withdraw(address: &String) -> Bundle { +pub fn withdraw(address: &str) -> Bundle { let wallets = Wallets::new(); let wallet = wallets.get_z_wallet(address).unwrap(); @@ -39,10 +39,7 @@ pub fn withdraw(address: &String) -> Bundle { assert_eq!(anchor, merkle_path.root(cmx)); let mut builder = Builder::new(Flags::from_parts(true, false), anchor); - assert_eq!( - builder.add_spend(fvk, note, merkle_path), - Ok(()) - ); + assert_eq!(builder.add_spend(fvk, note, merkle_path), Ok(())); let unauthorized = builder.build(&mut rng).unwrap(); let sighash = unauthorized.commitment().into(); let proven = unauthorized.create_proof(&pk, &mut rng).unwrap(); @@ -53,7 +50,7 @@ pub fn withdraw(address: &String) -> Bundle { shielding_bundle } -pub fn save_note(address: &String) { +pub fn save_note(address: &str) { let mut wallets = Wallets::new(); let wallet = wallets.get_mut_z_wallet(address); wallet.notes.remove(0); diff --git a/src/zsend.rs b/src/zsend.rs index c530bec..989ab1d 100644 --- a/src/zsend.rs +++ b/src/zsend.rs @@ -14,7 +14,7 @@ use orchard::{ use rand::rngs::OsRng; use zcash_note_encryption::try_note_decryption; -pub fn zsend(from: &String, to: &String) -> Bundle { +pub fn zsend(from: &str, to: &str) -> Bundle { let wallets = Wallets::new(); let mut rng = OsRng; @@ -47,10 +47,7 @@ pub fn zsend(from: &String, to: &String) -> Bundle { assert_eq!(anchor, merkle_path.root(cmx)); let mut builder = Builder::new(Flags::from_parts(true, true), anchor); - assert_eq!( - builder.add_spend(from_fvk, note, merkle_path), - Ok(()) - ); + assert_eq!(builder.add_spend(from_fvk, note, merkle_path), Ok(())); assert_eq!( builder.add_recipient(None, recipient, NoteValue::from_raw(old_note.value), None), Ok(()) @@ -65,7 +62,7 @@ pub fn zsend(from: &String, to: &String) -> Bundle { shielded_bundle } -pub fn save_note(bundle: &Bundle, from: &String, to: &String) { +pub fn save_note(bundle: &Bundle, from: &str, to: &str) { let mut wallets = Wallets::new(); let to_wallet = wallets.get_z_wallet(to).unwrap(); let to_sk = to_wallet.sk();