Skip to content

Commit

Permalink
feat:refactor deposit
Browse files Browse the repository at this point in the history
  • Loading branch information
Nickqiaoo committed Dec 20, 2023
1 parent 3995e4f commit cfadcb8
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 6 deletions.
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "0.1.0"
edition = "2021"

[features]
dev-graph = ["halo2_proofs/dev-graph", "plotters", "plotters/bitmap_backend","plotters/bitmap_encoder"]
dev-graph = ["halo2_proofs/dev-graph", "plotters", "plotters/bitmap_backend", "plotters/bitmap_encoder"]

[dependencies]
hex = "0.4"
Expand All @@ -25,4 +25,5 @@ plotters = { version = "0.3.0", default-features = true, optional = true }
pasta_curves = "0.5"
orchard = "0.6.0"
bridgetree = "0.4"
lazy_static = "1.4"
lazy_static = "1.4"
zcash_note_encryption = "0.4"
17 changes: 16 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{println, vec};

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

pub struct Cli {
Expand Down Expand Up @@ -40,6 +40,13 @@ pub enum Command {
#[structopt(help = "Address")]
address: String,
},
#[structopt(name = "deposit", about = "deposit")]
Deposit{
#[structopt(help = "address")]
address: String,
#[structopt(help = "amount")]
amount: u64,
}
}

impl Cli {
Expand All @@ -53,6 +60,7 @@ impl Cli {
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),
}
}

Expand Down Expand Up @@ -119,4 +127,11 @@ impl Cli {
}
println!("Balance of '{}': {}", address, balance);
}

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

29 changes: 28 additions & 1 deletion src/deposit.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{wallets::Wallets, merkle,};
use crate::{wallets::Wallets, merkle, wallet};
use rand::rngs::OsRng;
use orchard::{
builder::Builder,
Expand All @@ -11,6 +11,7 @@ use orchard::{
value::NoteValue,
Bundle,
};
use zcash_note_encryption::try_note_decryption;


pub fn deposit(address: String, value: u64) -> Bundle<Authorized, i64> {
Expand Down Expand Up @@ -40,4 +41,30 @@ pub fn deposit(address: String, value: u64) -> Bundle<Authorized, i64> {
proven.apply_signatures(rng, sighash, &[]).unwrap()
};
shielding_bundle
}

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();
let fvk = FullViewingKey::from(&sk);
let ivk = PreparedIncomingViewingKey::new(&fvk.to_ivk(Scope::External));

let (note, _, _) = bundle
.actions()
.iter()
.find_map(|action| {
let domain = OrchardDomain::for_action(action);
try_note_decryption(&domain, &ivk, action)
})
.unwrap();
let n = wallet::Note{
value:note.value().into(),
rseed: *note.rseed().as_bytes(),
nf: *note.rho().to_bytes(),
};

let wallet = wallets.get_mut_wallet(address).unwrap();
wallet.notes.extend(n);
_ = wallets.save_to_file();
}
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ mod wallets;
mod deposit;
mod merkle;
mod transfer;
mod verify;
mod withdraw;

fn main() {
let mut c = cli::Cli {
Expand Down
3 changes: 2 additions & 1 deletion src/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ use orchard::{
circuit::VerifyingKey,
Bundle,
};
use crate::merkle;

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 Down
16 changes: 15 additions & 1 deletion src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,20 @@ pub(crate) const CHECKSUM_LENGTH: usize = 4;
pub struct Wallet {
pub private_key: String,
pub public_key: String,
pub spend_key : String,
pub spend_key : String,
pub notes:Vec<Note>,
}
#[derive(Serialize, Deserialize, Clone)]
pub struct Note {
pub value :u64,
pub rseed: [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()))?
}
}

impl Wallet {
Expand All @@ -31,6 +44,7 @@ impl 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![],
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/wallets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::fs::File;
use std::io;
use std::io::Read;
use std::path::Path;
use orchard::Address;

const WALLET_FILE: &str = "wallets.dat";

Expand Down Expand Up @@ -39,6 +40,10 @@ impl Wallets {
self.wallets.get(address)
}

pub fn get_mut_wallet(&mut self, address: &str) -> &mut Wallet {
self.wallets.get_mut(address)?
}

pub fn get_z_wallet(&self, address: &str) -> Option<&Wallet> {
self.zwallets.get(address)
}
Expand Down

0 comments on commit cfadcb8

Please sign in to comment.