Skip to content

Commit

Permalink
feat:add printchain
Browse files Browse the repository at this point in the history
  • Loading branch information
Nickqiaoo committed Dec 25, 2023
1 parent d66131b commit f60df29
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 15 deletions.
3 changes: 3 additions & 0 deletions src/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@ impl Blockchain {
}

pub fn verify_transaction(&self, tx: &Transaction) -> bool {
if tx.is_coinbase() {
return true
}
let mut prev_txs = HashMap::new();

for vin in tx.vin.iter() {
Expand Down
40 changes: 28 additions & 12 deletions src/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,31 @@ impl From<&orchard::Bundle<Authorized, i64>> for Bundle {
}
}

// impl fmt::Display for Bundle {
// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
// _ = writeln!(f, "{}", hex::encode(&self.id));
// for (i, v) in self.vin.iter().enumerate() {
// _ = writeln!(f, "vin{}>>>{}", i, v);
// }
// for (i, v) in self.vout.iter().enumerate() {
// _ = writeln!(f, "vout{}>>>{}", i, v);
// }
// Ok(())
// }
// }
impl fmt::Display for Action {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
writeln!(f, "Nullifier: {}", self.nullifier)?;
writeln!(f, "rk: {}", self.rk)?;
writeln!(f, "cmx: {}", self.cmx)?;
writeln!(f, "OutCiphertext: {}", self.out_ciphertext)?;
writeln!(f, "EphemeralKey: {}", self.ephemeral_key)?;
writeln!(f, "EncCiphertext: {}", self.enc_ciphertext)?;
writeln!(f, "cv: {}", self.cv)?;
writeln!(f, "SpendAuthSig: {}", self.spend_auth_sig)
}
}

impl fmt::Display for Bundle {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
_ = writeln!(f, "Flags: {}", self.flags);
_ = writeln!(f, "ValueBalance: {}", self.value_balance);
_ = writeln!(f, "Anchor: {}", self.anchor);
_ = writeln!(f, "Proof: {}", self.proof);
_ = writeln!(f, "BindingSignature: {}", self.binding_sig);

for (i, action) in self.actions.iter().enumerate() {
_ = writeln!(f, "Action {}:\n{}", i, action);
}

Ok(())
}
}
9 changes: 8 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{println, vec};

use crate::{blockchain::Blockchain, deposit, pow::ProofOfWork, transaction, verify, wallet, wallets::Wallets, withdraw, zsend};
use structopt::StructOpt;
use crate::transaction::new_coinbase_tx;
use crate::transaction::{new_coinbase_tx, Transaction};

pub struct Cli {
pub cmd: Command,
Expand Down Expand Up @@ -158,8 +158,15 @@ impl Cli {
}

fn zsend(&self, from: String, to: String) {
let mut bc = Blockchain::new(&from);

let bundle = zsend::zsend(&from, &to);
verify::verify_bundle(&bundle);

let mut tx = Transaction::default();
tx.bundle = (&bundle).into();
tx.set_id();
bc.mine_block(vec![tx]);
zsend::save_note(&bundle, &from, &to);
}
fn withdraw(&self, address: String) {
Expand Down
5 changes: 3 additions & 2 deletions src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
};
use crate::bundle::Bundle;

#[derive(Serialize, Deserialize, Clone)]
#[derive(Serialize, Deserialize, Clone, Default)]
pub struct Transaction {
pub id: Vec<u8>,
pub vin: Vec<TXInput>,
Expand All @@ -17,7 +17,7 @@ pub struct Transaction {
}

impl Transaction {
fn set_id(&mut self) {
pub fn set_id(&mut self) {
let json_str = serde_json::to_string(self).unwrap();
self.id = Sha256::digest(json_str.as_bytes()).to_vec();
}
Expand Down Expand Up @@ -141,6 +141,7 @@ impl fmt::Display for Transaction {
for (i, v) in self.vout.iter().enumerate() {
_ = writeln!(f, "vout{}>>>{}", i, v);
}
_ = writeln!(f, "{}", self.bundle);
Ok(())
}
}
Expand Down

0 comments on commit f60df29

Please sign in to comment.