Skip to content

Commit

Permalink
feat:add cli
Browse files Browse the repository at this point in the history
  • Loading branch information
Nickqiaoo committed Dec 25, 2023
1 parent 03e30e7 commit d66131b
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "linar"
name = "tinyzcash"
version = "0.1.0"
edition = "2021"

Expand Down
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,21 @@
# tinyzcash
# tinyzcash 0.1.0
A simple CLI application

## USAGE:
tinyzcash <SUBCOMMAND>

## FLAGS:
-h, --help Prints help information
-V, --version Prints version information

## SUBCOMMANDS:
createblockchain create a new blockchain
createwallet create a new wallet
deposit deposit funds
getbalance get the balance of a wallet
help Prints this message or the help of the given subcommand(s)
listaddress list all addresses
printchain print the entire blockchain
send send funds
withdraw withdraw funds
zsend send funds with privacy (shielded transaction)
2 changes: 1 addition & 1 deletion src/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl Blockchain {
let b = db.open_tree("blocksBucket").unwrap();

if b.is_empty() {
let genesis = Block::genesis(new_coinbase_tx(address, COINBASEDATA));
let genesis = Block::genesis(new_coinbase_tx(address, COINBASEDATA, 10));
b.insert(&genesis.hash, genesis.serialize()).unwrap();
b.insert(b"l", genesis.hash.as_slice()).unwrap();
tip = genesis.hash.to_vec();
Expand Down
74 changes: 74 additions & 0 deletions src/bundle.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
use std::fmt;
use serde::{Deserialize, Serialize};
use orchard::{
Action as oAction,
bundle::{Authorized},
};
use orchard::bundle::Authorization;
use crate::transaction::Transaction;

#[derive(Clone, Serialize, Deserialize, Default)]
pub struct Bundle {
actions:Vec<Action>,
flags: u8,
value_balance: i64,
anchor: String,
proof: String,
binding_sig:String,
}
#[derive(Clone,Serialize, Deserialize)]
pub struct Action{
nullifier: String,
rk: 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),
}
}
}

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();

Bundle{
actions: b.actions().iter().map(|action| Action::from(action)).collect(),
flags: b.flags().to_byte(),
value_balance: b.value_balance().clone(),
anchor: hex::encode(b.anchor().to_bytes()),
proof: hex::encode(b.authorization().proof().as_ref()),
binding_sig: hex::encode(sig),
}
}
}

// 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(())
// }
// }
23 changes: 19 additions & 4 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ 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;

pub struct Cli {
pub cmd: Command,
}

#[derive(StructOpt, Debug)]
#[structopt(name = "blockchain", about = "A simple CLI application")]
#[structopt(name = "tinyzcash", about = "A simple CLI application")]
pub enum Command {
#[structopt(name = "createBlockchain", about = "CreateBlockChain")]
#[structopt(name = "createblockchain", about = "createblockchain")]
CreateBlockChain {
#[structopt(help = "Address")]
address: String,
Expand All @@ -19,10 +20,10 @@ pub enum Command {
#[structopt(name = "createwallet", about = "create wallet")]
Createwallet,

#[structopt(name = "printchain", about = "Print the chain")]
#[structopt(name = "printchain", about = "print the chain")]
PrintChain,

#[structopt(name = "listaddress", about = "ListAddress")]
#[structopt(name = "listaddress", about = "listAddress")]
ListAddress,

#[structopt(name = "send", about = "send")]
Expand Down Expand Up @@ -145,8 +146,14 @@ 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 bundle = deposit::deposit(&address, amount);
verify::verify_bundle(&bundle);

tx.bundle = (&bundle).into();
bc.mine_block(vec![tx]);
deposit::save_note(&bundle, &address);
}

Expand All @@ -156,8 +163,16 @@ impl Cli {
zsend::save_note(&bundle, &from, &to);
}
fn withdraw(&self, address: String) {
let mut bc = Blockchain::new(&address);

let bundle = withdraw::withdraw(&address);
verify::verify_bundle(&bundle);

let wallets = Wallets::new();
let wallet = wallets.get_z_wallet(&address).unwrap();
let mut tx = new_coinbase_tx(&wallet.get_address(), "withdraw", *bundle.value_balance());
tx.bundle = (&bundle).into();
bc.mine_block(vec![tx]);
withdraw::save_note(&address);
}
}
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ mod wallet;
mod wallets;
mod withdraw;
mod zsend;
mod bundle;

fn main() {
let mut c = cli::Cli {
Expand Down
9 changes: 7 additions & 2 deletions src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ use crate::{
blockchain::Blockchain, transaction_input::TXInput, transaction_output::TXOutput, wallet,
wallets::Wallets,
};
use crate::bundle::Bundle;

#[derive(Serialize, Deserialize, Clone)]
pub struct Transaction {
pub id: Vec<u8>,
pub vin: Vec<TXInput>,
pub vout: Vec<TXOutput>,
pub bundle: Bundle,
}

impl Transaction {
Expand Down Expand Up @@ -59,6 +61,7 @@ impl Transaction {
id: self.id.clone(),
vin: inputs,
vout: outputs,
bundle:Bundle::default(),
}
}

Expand Down Expand Up @@ -142,18 +145,19 @@ impl fmt::Display for Transaction {
}
}

pub fn new_coinbase_tx(to: &str, data: &str) -> Transaction {
pub fn new_coinbase_tx(to: &str, data: &str, value: i64) -> Transaction {
let txin = TXInput {
txid: vec![],
vout: -1,
signature: vec![],
pub_key: data.as_bytes().to_vec(),
};
let txout = TXOutput::new(10, to);
let txout = TXOutput::new(value, to);
let mut tx = Transaction {
id: vec![],
vin: vec![txin],
vout: vec![txout],
bundle:Bundle::default(),
};
tx.set_id();

Expand Down Expand Up @@ -195,6 +199,7 @@ pub fn new_utxo_transaction(from: String, to: String, amount: i64, bc: &Blockcha
vin: inputs,
vout: outputs,
id: Vec::new(),
bundle: Bundle::default(),
};
tx.set_id();
bc.sign_transaction(&mut tx, wallet.private_key.clone());
Expand Down

0 comments on commit d66131b

Please sign in to comment.