Skip to content

Commit

Permalink
feat:refactor clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
Nickqiaoo committed Dec 25, 2023
1 parent f60df29 commit 6f12177
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 61 deletions.
2 changes: 1 addition & 1 deletion src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl Block {
}

let concatenated_hashes = tx_hashes.concat();
Sha256::digest(&concatenated_hashes).to_vec()
Sha256::digest(concatenated_hashes).to_vec()
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
60 changes: 28 additions & 32 deletions src/bundle.rs
Original file line number Diff line number Diff line change
@@ -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<Action>,
actions: Vec<Action>,
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<<Authorized as Authorization>::SpendAuth>> for Action {
fn from(a: &oAction<<Authorized as Authorization>::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<Authorized, i64>> for Bundle {
fn from(b: &orchard::Bundle<Authorized, i64>) -> 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),
Expand Down Expand Up @@ -87,4 +83,4 @@ impl fmt::Display for Bundle {

Ok(())
}
}
}
22 changes: 16 additions & 6 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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()),
}
}

Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/deposit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Authorized, i64> {
pub fn deposit(address: &str, value: u64) -> Bundle<Authorized, i64> {
let wallets = Wallets::new();
let wallet = wallets.get_wallet(address).unwrap();

Expand Down Expand Up @@ -40,7 +40,7 @@ pub fn deposit(address: &String, value: u64) -> Bundle<Authorized, i64> {
shielding_bundle
}

pub fn save_note(bundle: &Bundle<Authorized, i64>, address: &String) {
pub fn save_note(bundle: &Bundle<Authorized, i64>, address: &str) {
let mut wallets = Wallets::new();
let wallet = wallets.get_wallet(address).unwrap();
let sk = wallet.sk();
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use structopt::StructOpt;

mod block;
mod blockchain;
mod bundle;
mod cli;
mod deposit;
mod iterator;
Expand All @@ -15,7 +16,6 @@ mod wallet;
mod wallets;
mod withdraw;
mod zsend;
mod bundle;

fn main() {
let mut c = cli::Cli {
Expand Down
2 changes: 1 addition & 1 deletion src/pow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -61,7 +61,7 @@ impl Transaction {
id: self.id.clone(),
vin: inputs,
vout: outputs,
bundle:Bundle::default(),
bundle: Bundle::default(),
}
}

Expand Down Expand Up @@ -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();

Expand Down
13 changes: 5 additions & 8 deletions src/withdraw.rs
Original file line number Diff line number Diff line change
@@ -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<Authorized, i64> {
pub fn withdraw(address: &str) -> Bundle<Authorized, i64> {
let wallets = Wallets::new();
let wallet = wallets.get_z_wallet(address).unwrap();

Expand Down Expand Up @@ -39,10 +39,7 @@ pub fn withdraw(address: &String) -> Bundle<Authorized, i64> {
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();
Expand All @@ -53,7 +50,7 @@ pub fn withdraw(address: &String) -> Bundle<Authorized, i64> {
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);
Expand Down
9 changes: 3 additions & 6 deletions src/zsend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Authorized, i64> {
pub fn zsend(from: &str, to: &str) -> Bundle<Authorized, i64> {
let wallets = Wallets::new();

let mut rng = OsRng;
Expand Down Expand Up @@ -47,10 +47,7 @@ pub fn zsend(from: &String, to: &String) -> Bundle<Authorized, i64> {
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(())
Expand All @@ -65,7 +62,7 @@ pub fn zsend(from: &String, to: &String) -> Bundle<Authorized, i64> {
shielded_bundle
}

pub fn save_note(bundle: &Bundle<Authorized, i64>, from: &String, to: &String) {
pub fn save_note(bundle: &Bundle<Authorized, i64>, from: &str, to: &str) {
let mut wallets = Wallets::new();
let to_wallet = wallets.get_z_wallet(to).unwrap();
let to_sk = to_wallet.sk();
Expand Down

0 comments on commit 6f12177

Please sign in to comment.