Add wallet birthday
This commit is contained in:
@@ -283,7 +283,7 @@ impl Command for SeedCommand {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn exec(&self, _args: &[&str], lightclient: &LightClient) -> String {
|
fn exec(&self, _args: &[&str], lightclient: &LightClient) -> String {
|
||||||
lightclient.do_seed_phrase()
|
format!("{}", lightclient.do_seed_phrase().pretty(2))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -183,7 +183,7 @@ impl LightClient {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new(seed_phrase: Option<String>, config: &LightClientConfig) -> io::Result<Self> {
|
pub fn new(seed_phrase: Option<String>, config: &LightClientConfig, latest_block: u64) -> io::Result<Self> {
|
||||||
let mut lc = if config.get_wallet_path().exists() {
|
let mut lc = if config.get_wallet_path().exists() {
|
||||||
// Make sure that if a wallet exists, there is no seed phrase being attempted
|
// Make sure that if a wallet exists, there is no seed phrase being attempted
|
||||||
if !seed_phrase.is_none() {
|
if !seed_phrase.is_none() {
|
||||||
@@ -202,7 +202,7 @@ impl LightClient {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
let l = LightClient {
|
let l = LightClient {
|
||||||
wallet : Arc::new(LightWallet::new(seed_phrase, config)?),
|
wallet : Arc::new(LightWallet::new(seed_phrase, config, latest_block)?),
|
||||||
config : config.clone(),
|
config : config.clone(),
|
||||||
sapling_output : vec![],
|
sapling_output : vec![],
|
||||||
sapling_spend : vec![]
|
sapling_spend : vec![]
|
||||||
@@ -213,6 +213,7 @@ impl LightClient {
|
|||||||
l
|
l
|
||||||
};
|
};
|
||||||
|
|
||||||
|
info!("Read wallet with birthday {}", lc.wallet.get_first_tx_block());
|
||||||
|
|
||||||
// Read Sapling Params
|
// Read Sapling Params
|
||||||
let mut f = match File::open(config.get_params_path("sapling-output.params")) {
|
let mut f = match File::open(config.get_params_path("sapling-output.params")) {
|
||||||
@@ -368,8 +369,11 @@ impl LightClient {
|
|||||||
format!("{:?}", LightClient::get_info(uri))
|
format!("{:?}", LightClient::get_info(uri))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn do_seed_phrase(&self) -> String {
|
pub fn do_seed_phrase(&self) -> JsonValue {
|
||||||
self.wallet.get_seed_phrase()
|
object!{
|
||||||
|
"seed" => self.wallet.get_seed_phrase(),
|
||||||
|
"birthday" => self.wallet.get_birthday()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return a list of all notes, spent and unspent
|
// Return a list of all notes, spent and unspent
|
||||||
|
|||||||
@@ -121,6 +121,10 @@ pub struct LightWallet {
|
|||||||
blocks: Arc<RwLock<Vec<BlockData>>>,
|
blocks: Arc<RwLock<Vec<BlockData>>>,
|
||||||
pub txs: Arc<RwLock<HashMap<TxId, WalletTx>>>,
|
pub txs: Arc<RwLock<HashMap<TxId, WalletTx>>>,
|
||||||
|
|
||||||
|
// The block at which this wallet was born. Rescans
|
||||||
|
// will start from here.
|
||||||
|
birthday: u64,
|
||||||
|
|
||||||
// Non-serialized fields
|
// Non-serialized fields
|
||||||
config: LightClientConfig,
|
config: LightClientConfig,
|
||||||
}
|
}
|
||||||
@@ -146,7 +150,7 @@ impl LightWallet {
|
|||||||
(extsk, extfvk, address)
|
(extsk, extfvk, address)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new(seed_phrase: Option<String>, config: &LightClientConfig) -> io::Result<Self> {
|
pub fn new(seed_phrase: Option<String>, config: &LightClientConfig, latest_block: u64) -> io::Result<Self> {
|
||||||
use rand::{FromEntropy, ChaChaRng, Rng};
|
use rand::{FromEntropy, ChaChaRng, Rng};
|
||||||
|
|
||||||
// This is the source entropy that corresponds to the 24-word seed phrase
|
// This is the source entropy that corresponds to the 24-word seed phrase
|
||||||
@@ -181,14 +185,15 @@ impl LightWallet {
|
|||||||
let (extsk, extfvk, address) = LightWallet::get_pk_from_bip39seed(&bip39_seed.as_bytes());
|
let (extsk, extfvk, address) = LightWallet::get_pk_from_bip39seed(&bip39_seed.as_bytes());
|
||||||
|
|
||||||
Ok(LightWallet {
|
Ok(LightWallet {
|
||||||
seed: seed_bytes,
|
seed: seed_bytes,
|
||||||
extsks: vec![extsk],
|
extsks: vec![extsk],
|
||||||
extfvks: vec![extfvk],
|
extfvks: vec![extfvk],
|
||||||
address: vec![address],
|
address: vec![address],
|
||||||
tkeys: vec![tpk],
|
tkeys: vec![tpk],
|
||||||
blocks: Arc::new(RwLock::new(vec![])),
|
blocks: Arc::new(RwLock::new(vec![])),
|
||||||
txs: Arc::new(RwLock::new(HashMap::new())),
|
txs: Arc::new(RwLock::new(HashMap::new())),
|
||||||
config: config.clone(),
|
config: config.clone(),
|
||||||
|
birthday: latest_block,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -236,15 +241,22 @@ impl LightWallet {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let birthday = if version >= 2 {
|
||||||
|
reader.read_u64::<LittleEndian>()?
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
};
|
||||||
|
|
||||||
Ok(LightWallet{
|
Ok(LightWallet{
|
||||||
seed: seed_bytes,
|
seed: seed_bytes,
|
||||||
extsks: extsks,
|
extsks,
|
||||||
extfvks: extfvks,
|
extfvks,
|
||||||
address: addresses,
|
address: addresses,
|
||||||
tkeys: vec![tpk],
|
tkeys: vec![tpk],
|
||||||
blocks: Arc::new(RwLock::new(blocks)),
|
blocks: Arc::new(RwLock::new(blocks)),
|
||||||
txs: Arc::new(RwLock::new(txs)),
|
txs: Arc::new(RwLock::new(txs)),
|
||||||
config: config.clone(),
|
config: config.clone(),
|
||||||
|
birthday,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -274,6 +286,10 @@ impl LightWallet {
|
|||||||
})?;
|
})?;
|
||||||
utils::write_string(&mut writer, &self.config.chain_name)?;
|
utils::write_string(&mut writer, &self.config.chain_name)?;
|
||||||
|
|
||||||
|
// While writing the birthday, be sure that we're right, and that we don't
|
||||||
|
// have a tx that is before the current birthday
|
||||||
|
writer.write_u64::<LittleEndian>(self.get_birthday())?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -284,6 +300,24 @@ impl LightWallet {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get_birthday(&self) -> u64 {
|
||||||
|
cmp::min(self.get_first_tx_block(), self.birthday)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the first block that this wallet has a tx in. This is often used as the wallet's "birthday"
|
||||||
|
// If there are no Txns, then the actual birthday (which is recorder at wallet creation) is returned
|
||||||
|
// If no birthday was recorded, return the sapling activation height
|
||||||
|
pub fn get_first_tx_block(&self) -> u64 {
|
||||||
|
// Find the first transaction
|
||||||
|
let mut blocks = self.txs.read().unwrap().values()
|
||||||
|
.map(|wtx| wtx.block as u64)
|
||||||
|
.collect::<Vec<u64>>();
|
||||||
|
blocks.sort();
|
||||||
|
|
||||||
|
*blocks.first() // Returns optional
|
||||||
|
.unwrap_or(&cmp::max(self.birthday, self.config.sapling_activation_height))
|
||||||
|
}
|
||||||
|
|
||||||
// Get all z-address private keys. Returns a Vector of (address, privatekey)
|
// Get all z-address private keys. Returns a Vector of (address, privatekey)
|
||||||
pub fn get_z_private_keys(&self) -> Vec<(String, String)> {
|
pub fn get_z_private_keys(&self) -> Vec<(String, String)> {
|
||||||
self.extsks.iter().map(|sk| {
|
self.extsks.iter().map(|sk| {
|
||||||
|
|||||||
13
src/main.rs
13
src/main.rs
@@ -58,12 +58,7 @@ pub fn main() {
|
|||||||
consensus_branch_id : info.consensus_branch_id,
|
consensus_branch_id : info.consensus_branch_id,
|
||||||
};
|
};
|
||||||
|
|
||||||
let lightclient = match LightClient::new(seed, &config) {
|
// Configure logging first.
|
||||||
Ok(lc) => Arc::new(lc),
|
|
||||||
Err(e) => { eprintln!("Failed to start wallet. Error was:\n{}", e); return; }
|
|
||||||
};
|
|
||||||
|
|
||||||
// Configure logging first.
|
|
||||||
let logfile = FileAppender::builder()
|
let logfile = FileAppender::builder()
|
||||||
.encoder(Box::new(PatternEncoder::new("{l} -{d(%Y-%m-%d %H:%M:%S)}- {m}\n")))
|
.encoder(Box::new(PatternEncoder::new("{l} -{d(%Y-%m-%d %H:%M:%S)}- {m}\n")))
|
||||||
.build(config.get_log_path()).unwrap();
|
.build(config.get_log_path()).unwrap();
|
||||||
@@ -80,6 +75,12 @@ pub fn main() {
|
|||||||
info!("Starting ZecLite CLI");
|
info!("Starting ZecLite CLI");
|
||||||
info!("Light Client config {:?}", config);
|
info!("Light Client config {:?}", config);
|
||||||
|
|
||||||
|
let lightclient = match LightClient::new(seed, &config, info.block_height) {
|
||||||
|
Ok(lc) => Arc::new(lc),
|
||||||
|
Err(e) => { eprintln!("Failed to start wallet. Error was:\n{}", e); return; }
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
// At startup, run a sync
|
// At startup, run a sync
|
||||||
let sync_update = lightclient.do_sync(true);
|
let sync_update = lightclient.do_sync(true);
|
||||||
println!("{}", sync_update);
|
println!("{}", sync_update);
|
||||||
|
|||||||
Reference in New Issue
Block a user