This commit is contained in:
Aditya Kulkarni
2019-09-08 15:03:24 -07:00
parent 5bd8b754df
commit 972558620c
4 changed files with 47 additions and 8 deletions

View File

@@ -22,6 +22,7 @@ protobuf = "2"
rustyline = "5.0.2" rustyline = "5.0.2"
byteorder = "1" byteorder = "1"
rand = "0.5.6" rand = "0.5.6"
json = "0.12.0"
[dependencies.bellman] [dependencies.bellman]
path = "../../librustzcash/bellman" path = "../../librustzcash/bellman"

View File

@@ -72,7 +72,8 @@ impl Command for AddressCommand {
} }
fn exec(&self, _args: &[String], lightclient: &mut LightClient) { fn exec(&self, _args: &[String], lightclient: &mut LightClient) {
lightclient.do_address(); let res = lightclient.do_address();
println!("{}", res.pretty(2));
} }
} }

View File

@@ -11,8 +11,13 @@ use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
use std::error::Error; use std::error::Error;
use json::{object, JsonValue};
use zcash_primitives::transaction::{TxId, Transaction}; use zcash_primitives::transaction::{TxId, Transaction};
use zcash_primitives::note_encryption::Memo; use zcash_primitives::note_encryption::Memo;
use zcash_client_backend::{
constants::testnet::HRP_SAPLING_PAYMENT_ADDRESS, encoding::encode_payment_address,
};
use futures::Future; use futures::Future;
use hyper::client::connect::{Destination, HttpConnector}; use hyper::client::connect::{Destination, HttpConnector};
@@ -67,9 +72,21 @@ impl LightClient {
self.wallet.last_scanned_height() as u64 self.wallet.last_scanned_height() as u64
} }
pub fn do_address(&self) { pub fn do_address(&self) -> json::JsonValue{
println!("Address: {}", self.wallet.address(0)); // TODO: This is showing only the default address let addresses = self.wallet.address.iter().map( |ad| {
println!("Balance: {}", self.wallet.balance()); let address = encode_payment_address(HRP_SAPLING_PAYMENT_ADDRESS, &ad);
object!{
"address" => address.clone(),
"balance" => self.wallet.balance(Some(address.clone())),
"verified_balance" => self.wallet.verified_balance(Some(address)),
}
}).collect::<Vec<JsonValue>>();
object!{
"balance" => self.wallet.balance(None),
"verified_balance" => self.wallet.verified_balance(None),
"addresses" => addresses
}
} }
pub fn do_read(&mut self) { pub fn do_read(&mut self) {
@@ -223,7 +240,7 @@ impl LightClient {
}; };
print!("Syncing {}/{}, Balance = {} \r", print!("Syncing {}/{}, Balance = {} \r",
last_scanned_height, last_block, self.wallet.balance()); last_scanned_height, last_block, self.wallet.balance(None));
self.fetch_blocks(last_scanned_height, end_height, simple_callback); self.fetch_blocks(last_scanned_height, end_height, simple_callback);

View File

@@ -356,7 +356,7 @@ pub struct LightWallet {
// a private key // a private key
extsks: Vec<ExtendedSpendingKey>, extsks: Vec<ExtendedSpendingKey>,
extfvks: Vec<ExtendedFullViewingKey>, extfvks: Vec<ExtendedFullViewingKey>,
address: Vec<PaymentAddress<Bls12>>, pub address: Vec<PaymentAddress<Bls12>>,
blocks: Arc<RwLock<Vec<BlockData>>>, blocks: Arc<RwLock<Vec<BlockData>>>,
pub txs: Arc<RwLock<HashMap<TxId, WalletTx>>>, pub txs: Arc<RwLock<HashMap<TxId, WalletTx>>>,
@@ -539,7 +539,7 @@ impl LightWallet {
encode_payment_address(HRP_SAPLING_PAYMENT_ADDRESS, &self.address[account]) encode_payment_address(HRP_SAPLING_PAYMENT_ADDRESS, &self.address[account])
} }
pub fn balance(&self) -> u64 { pub fn balance(&self, addr: Option<String>) -> u64 {
self.txs self.txs
.read() .read()
.unwrap() .unwrap()
@@ -547,13 +547,23 @@ impl LightWallet {
.map(|tx| { .map(|tx| {
tx.notes tx.notes
.iter() .iter()
.filter(|nd| { // TODO, this whole section is shared with verified_balance. Refactor it.
match addr.clone() {
Some(a) => a == encode_payment_address(
HRP_SAPLING_PAYMENT_ADDRESS,
&nd.extfvk.fvk.vk
.into_payment_address(nd.diversifier, &JUBJUB).unwrap()
),
None => true
}
})
.map(|nd| if nd.spent.is_none() { nd.note.value } else { 0 }) .map(|nd| if nd.spent.is_none() { nd.note.value } else { 0 })
.sum::<u64>() .sum::<u64>()
}) })
.sum::<u64>() .sum::<u64>()
} }
pub fn verified_balance(&self) -> u64 { pub fn verified_balance(&self, addr: Option<String>) -> u64 {
let anchor_height = match self.get_target_height_and_anchor_offset() { let anchor_height = match self.get_target_height_and_anchor_offset() {
Some((height, anchor_offset)) => height - anchor_offset as u32, Some((height, anchor_offset)) => height - anchor_offset as u32,
None => return 0, None => return 0,
@@ -567,6 +577,16 @@ impl LightWallet {
if tx.block as u32 <= anchor_height { if tx.block as u32 <= anchor_height {
tx.notes tx.notes
.iter() .iter()
.filter(|nd| { // TODO, this whole section is shared with verified_balance. Refactor it.
match addr.clone() {
Some(a) => a == encode_payment_address(
HRP_SAPLING_PAYMENT_ADDRESS,
&nd.extfvk.fvk.vk
.into_payment_address(nd.diversifier, &JUBJUB).unwrap()
),
None => true
}
})
.map(|nd| if nd.spent.is_none() { nd.note.value } else { 0 }) .map(|nd| if nd.spent.is_none() { nd.note.value } else { 0 })
.sum::<u64>() .sum::<u64>()
} else { } else {