Skip to content

Commit

Permalink
feat:add zsend
Browse files Browse the repository at this point in the history
  • Loading branch information
Nickqiaoo committed Dec 21, 2023
1 parent cfadcb8 commit d361bc4
Show file tree
Hide file tree
Showing 9 changed files with 194 additions and 135 deletions.
44 changes: 31 additions & 13 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::{println, vec};

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

pub struct Cli {
Expand Down Expand Up @@ -41,26 +44,32 @@ pub enum Command {
address: String,
},
#[structopt(name = "deposit", about = "deposit")]
Deposit{
Deposit {
#[structopt(help = "address")]
address: String,
#[structopt(help = "amount")]
amount: u64,
}
},
#[structopt(name = "zsend", about = "zsend")]
Zsend {
#[structopt(help = "from")]
from: String,
#[structopt(help = "to")]
to: String,
},
}

impl Cli {
pub fn run(&mut self) {
match &self.cmd {
Command::CreateBlockChain { address } => self.create_blockchain(address.to_string()),
Command::CreateBlockChain { address } => self.create_blockchain(address.clone()),
Command::Createwallet => self.create_wallet(),
Command::PrintChain => self.print_chain(),
Command::ListAddress => self.list_address(),
Command::Send { from, to, amount } => {
self.send(from.to_string(), to.to_string(), *amount)
}
Command::Getbalance { address } => self.get_balance(address.to_string()),
Command::Deposit {address, amount} => self.deposit(address.to_string(), *amount),
Command::Send { from, to, amount } => self.send(from.clone(), to.clone(), *amount),
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()),
}
}

Expand All @@ -79,7 +88,11 @@ impl Cli {
let w = Wallets::new();
let address = w.get_addresses();
for a in &address {
println!("{:}", a);
println!("addr:{:}", a);
}
let address = w.get_z_addresses();
for a in &address {
println!("zaddr:{:}", a);
}
}

Expand Down Expand Up @@ -128,10 +141,15 @@ impl Cli {
println!("Balance of '{}': {}", address, balance);
}

fn deposit(&self, address:String, amount:u64){
let bundle = deposit::deposit(address, amount);
fn deposit(&self, address: String, amount: u64) {
let bundle = deposit::deposit(&address, amount);
verify::verify_bundle(&bundle);
deposit::save_note(&bundle, &address);
}
}

fn zsend(&self, from: String, to: String) {
let bundle = zsend::zsend(&from, &to);
verify::verify_bundle(&bundle);
zsend::save_note(&bundle, &from, &to);
}
}
31 changes: 14 additions & 17 deletions src/deposit.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
use crate::{wallets::Wallets, merkle, wallet};
use rand::rngs::OsRng;
use crate::{merkle, wallet, wallets::Wallets};
use orchard::circuit::ProvingKey;
use orchard::{
builder::Builder,
bundle::{Authorized, Flags},
circuit::{ProvingKey, VerifyingKey},
keys::{FullViewingKey, PreparedIncomingViewingKey, Scope, SpendAuthorizingKey, SpendingKey},
note::ExtractedNoteCommitment,
keys::{FullViewingKey, PreparedIncomingViewingKey, Scope},
note_encryption::OrchardDomain,
tree::{MerkleHashOrchard, MerklePath},
value::NoteValue,
Bundle,
};
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: &String, value: u64) -> Bundle<Authorized, i64> {
let wallets = Wallets::new();
let wallet = wallets.get_wallet(&address).unwrap();
let wallet = wallets.get_wallet(address).unwrap();

let mut rng = OsRng;
let pk = ProvingKey::build();

Expand All @@ -43,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: &String) {
let mut wallets = Wallets::new();
let wallet = wallets.get_wallet(address).unwrap();
let sk = wallet.sk();
Expand All @@ -58,13 +55,13 @@ pub fn save_note(bundle: &Bundle<Authorized, i64>, address:&String){
try_note_decryption(&domain, &ivk, action)
})
.unwrap();
let n = wallet::Note{
value:note.value().into(),
let n = wallet::Note {
value: note.value().inner(),
rseed: *note.rseed().as_bytes(),
nf: *note.rho().to_bytes(),
nf: note.rho().to_bytes(),
};

let wallet = wallets.get_mut_wallet(address).unwrap();
wallet.notes.extend(n);
let wallet = wallets.get_mut_wallet(address);
wallet.notes.push(n);
_ = wallets.save_to_file();
}
}
8 changes: 4 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ use structopt::StructOpt;
mod block;
mod blockchain;
mod cli;
mod deposit;
mod iterator;
mod merkle;
mod pow;
mod transaction;
mod transaction_input;
mod transaction_output;
mod verify;
mod wallet;
mod wallets;
mod deposit;
mod merkle;
mod transfer;
mod verify;
mod withdraw;
mod zsend;

fn main() {
let mut c = cli::Cli {
Expand Down
5 changes: 3 additions & 2 deletions src/merkle.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use bridgetree::BridgeTree;
use orchard::tree::{MerkleHashOrchard, MerklePath};
use lazy_static::lazy_static;
use orchard::tree::MerkleHashOrchard;

lazy_static! {
pub static ref MERKLE:BridgeTree::<MerkleHashOrchard, u32, 32> = BridgeTree::<MerkleHashOrchard, u32, 32>::new(100);
pub static ref MERKLE: BridgeTree::<MerkleHashOrchard, u32, 32> =
BridgeTree::<MerkleHashOrchard, u32, 32>::new(100);
}
66 changes: 0 additions & 66 deletions src/transfer.rs

This file was deleted.

11 changes: 3 additions & 8 deletions src/verify.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
use orchard::{
bundle::Authorized,
circuit::VerifyingKey,
Bundle,
};
use crate::merkle;
use orchard::{bundle::Authorized, circuit::VerifyingKey, Bundle};

pub fn verify_bundle(bundle: &Bundle<Authorized, i64>){
pub fn verify_bundle(bundle: &Bundle<Authorized, i64>) {
let vk = VerifyingKey::build();
assert!(matches!(bundle.verify_proof(&vk), Ok(())));
let sighash: [u8; 32] = bundle.commitment().into();
Expand All @@ -17,4 +12,4 @@ pub fn verify_bundle(bundle: &Bundle<Authorized, i64>){
bvk.verify(&sighash, bundle.authorization().binding_signature()),
Ok(())
);
}
}
48 changes: 29 additions & 19 deletions src/wallet.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
use orchard::keys::FullViewingKey;
use orchard::note::{Nullifier, RandomSeed};
use orchard::value::NoteValue;
use orchard::{keys, Address};
use rand::rngs::OsRng as randRng;
use rand::RngCore;
use ripemd::{Digest as RipemdDigest, Ripemd160};
use secp256k1::rand::rngs::OsRng;
use secp256k1::Secp256k1;
use serde::{Deserialize, Serialize};
use sha2::Sha256;
use rand::RngCore;
use rand::rngs::OsRng as randRng;
use orchard::keys;

const VERSION: u8 = 0x00;
pub(crate) const CHECKSUM_LENGTH: usize = 4;
Expand All @@ -14,19 +17,26 @@ pub(crate) const CHECKSUM_LENGTH: usize = 4;
pub struct Wallet {
pub private_key: String,
pub public_key: String,
pub spend_key : String,
pub notes:Vec<Note>,
pub spend_key: String,
pub notes: Vec<Note>,
}
#[derive(Serialize, Deserialize, Clone)]
pub struct Note {
pub value :u64,
pub value: u64,
pub rseed: [u8; 32],
pub nf:[u8; 32],
pub nf: [u8; 32],
}

impl Note {
pub fn to_note(&self, addr : orchard::Address) -> orchard::note {
Option::from(orchard::Note::from_parts(addr, self.value.into(), self.nf.into(), self.rseed.into()))?
pub fn to_note(&self, addr: orchard::Address) -> orchard::Note {
let old_nf = Nullifier::from_bytes(&self.nf).unwrap();
orchard::Note::from_parts(
addr,
NoteValue::from_raw(self.value),
old_nf,
RandomSeed::from_bytes(self.rseed, &old_nf).unwrap(),
)
.unwrap()
}
}

Expand All @@ -36,15 +46,15 @@ impl Wallet {
let (private_key, public_key) = secp.generate_keypair(&mut OsRng);

let mut rng = randRng::default();
let mut random_bytes = [0u8; 32];
let mut random_bytes = [0u8; 32];
rng.fill_bytes(&mut random_bytes);
let spend_key = keys::SpendingKey::from_zip32_seed(&random_bytes, 0, 0).unwrap();

Wallet {
private_key: hex::encode(private_key.secret_bytes()),
public_key: public_key.to_string(),
spend_key: hex::encode(spend_key.to_bytes()),
notes :vec![],
notes: vec![],
}
}

Expand All @@ -56,22 +66,22 @@ impl Wallet {
versioned_payload.extend_from_slice(&checksum);
bs58::encode(&versioned_payload).into_string()
}

pub fn get_z_address(&self) -> String {
let spend_key = hex::decode(&self.spend_key).unwrap();
let spend_key : Result<[u8; 32], _> = spend_key.try_into();
let spend_key = keys::SpendingKey::from_bytes(spend_key.unwrap()).unwrap();
let fvk: keys::FullViewingKey = (&spend_key).into();
let addr = fvk.address_at(0u32, keys::Scope::External);
let addr = self.z_address();
hex::encode(addr.to_raw_address_bytes())
}

pub fn sk(&self) -> keys::SpendingKey {
let spend_key = hex::decode(&self.spend_key).unwrap();
let spend_key : Result<[u8; 32], _> = spend_key.try_into();
let spend_key: Result<[u8; 32], _> = spend_key.try_into();
keys::SpendingKey::from_bytes(spend_key.unwrap()).unwrap()
}


pub fn z_address(&self) -> Address {
let fvk = FullViewingKey::from(&self.sk());
fvk.address_at(0u32, keys::Scope::External)
}
}

pub fn validate_address(address: &String) -> bool {
Expand Down
Loading

0 comments on commit d361bc4

Please sign in to comment.