Skip to content

Commit

Permalink
feat:add deposit
Browse files Browse the repository at this point in the history
  • Loading branch information
Nickqiaoo committed Dec 18, 2023
1 parent e3dfab0 commit 7bbeba1
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ halo2_gadgets = { version = "0.3.0",features = ["unstable-sha256-gadget"] }
ecies = "0.2.6"
halo2_proofs = "0.3.0"
plotters = { version = "0.3.0", default-features = true, optional = true }
pasta_curves = "0.5"
pasta_curves = "0.5"
orchard = "0.6.0"
57 changes: 57 additions & 0 deletions src/deposit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use crate::{wallets::Wallets};
use rand::rngs::OsRng;
use orchard::{
builder::Builder,
bundle::{Authorized, Flags},
circuit::{ProvingKey, VerifyingKey},
keys::{FullViewingKey, PreparedIncomingViewingKey, Scope, SpendAuthorizingKey, SpendingKey},
note::ExtractedNoteCommitment,
note_encryption::OrchardDomain,
tree::{MerkleHashOrchard, MerklePath},
value::NoteValue,
Bundle,
};


pub fn deposit(address: String, value: u64) -> Bundle<Authorized, i64> {
let wallets = Wallets::new();
let wallet = wallets.get_wallet(&address).unwrap();

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

let sk = wallet.sk();
let fvk = FullViewingKey::from(&sk);
let recipient = fvk.address_at(0u32, Scope::External);

// Create a shielding bundle.
let shielding_bundle: Bundle<_, i64> = {
// Use the empty tree.
let anchor = MerkleHashOrchard::empty_root(32.into()).into();

let mut builder = Builder::new(Flags::from_parts(false, true), anchor);
assert_eq!(
builder.add_recipient(None, recipient, NoteValue::from_raw(5000), None),
Ok(())
);
let unauthorized = builder.build(&mut rng).unwrap();
let sighash = unauthorized.commitment().into();
let proven = unauthorized.create_proof(&pk, &mut rng).unwrap();
proven.apply_signatures(rng, sighash, &[]).unwrap()
};
shielding_bundle
}

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();
let bvk = bundle.binding_validating_key();
for action in bundle.actions() {
assert_eq!(action.rk().verify(&sighash, action.authorization()), Ok(()));
}
assert_eq!(
bvk.verify(&sighash, bundle.authorization().binding_signature()),
Ok(())
);
}
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ mod transaction_input;
mod transaction_output;
mod wallet;
mod wallets;
mod deposit;
mod transfer;

fn main() {
let mut c = cli::Cli {
Expand Down
3 changes: 3 additions & 0 deletions src/transfer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub fn transfer(address: String, value: u64) -> Bundle<Authorized, i64> {

}
26 changes: 26 additions & 0 deletions src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ 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 @@ -11,16 +14,23 @@ pub(crate) const CHECKSUM_LENGTH: usize = 4;
pub struct Wallet {
pub private_key: String,
pub public_key: String,
pub spend_key : String,
}

impl Wallet {
pub fn new() -> Wallet {
let secp = Secp256k1::new();
let (private_key, public_key) = secp.generate_keypair(&mut OsRng);

let mut rng = randRng::default();
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()),
}
}

Expand All @@ -32,6 +42,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);
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();
keys::SpendingKey::from_bytes(spend_key.unwrap()).unwrap()
}

}

pub fn validate_address(address: &String) -> bool {
Expand Down
8 changes: 8 additions & 0 deletions src/wallets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const WALLET_FILE: &str = "wallets.dat";
#[derive(Serialize, Deserialize)]
pub struct Wallets {
wallets: HashMap<String, Wallet>,
zwallets:HashMap<String, Wallet>,
}

impl Wallets {
Expand All @@ -22,8 +23,10 @@ impl Wallets {
pub fn create_wallet(&mut self) -> String {
let wallet = Wallet::new();
let address = wallet.get_address();
let zaddr = wallet.get_z_address();

self.wallets.insert(address.clone(), wallet);
self.zwallets.insert(zaddr.clone(), wallet);

address
}
Expand All @@ -36,6 +39,10 @@ impl Wallets {
self.wallets.get(address)
}

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

fn load_from_file() -> io::Result<Self> {
if Path::new(WALLET_FILE).exists() {
let mut file = File::open(WALLET_FILE)?;
Expand All @@ -48,6 +55,7 @@ impl Wallets {
} else {
Ok(Wallets {
wallets: HashMap::new(),
zwallets: HashMap::new(),
})
}
}
Expand Down

0 comments on commit 7bbeba1

Please sign in to comment.