diff --git a/src/lightwallet/mod.rs b/src/lightwallet/mod.rs index 34e3739..62a2177 100644 --- a/src/lightwallet/mod.rs +++ b/src/lightwallet/mod.rs @@ -38,9 +38,7 @@ use zcash_primitives::{ use data::{BlockData, WalletTx, Utxo, SaplingNoteData, SpendableNote, OutgoingTxMetadata}; -use crate::address; -use crate::prover; -use crate::LightClientConfig; +use crate::{address, prover, LightClientConfig, utils}; use sha2::{Sha256, Digest}; @@ -130,7 +128,7 @@ pub struct LightWallet { impl LightWallet { pub fn serialized_version() -> u64 { - return 1; + return 2; } fn get_sapling_params(config: &LightClientConfig) -> Result<(Vec, Vec), Error> { @@ -250,6 +248,16 @@ impl LightWallet { })?; let txs = txs_tuples.into_iter().collect::>(); + // chain_name was added in v2 + if version >= 2 { + let chain_name = utils::read_string(&mut reader)?; + + if chain_name != config.chain_name { + return Err(Error::new(ErrorKind::InvalidData, + format!("Wallet chain name {} doesn't match expected {}", chain_name, config.chain_name))); + } + } + Ok(LightWallet{ seed: seed_bytes, extsks: extsks, @@ -286,6 +294,8 @@ impl LightWallet { w.write_all(&k.0)?; v.write(w) })?; + utils::write_string(&mut writer, &self.config.chain_name)?; + Ok(()) } @@ -1839,12 +1849,12 @@ pub mod tests { let branch_id = u32::from_str_radix("2bb40e60", 16).unwrap(); let (ss, so) = LightWallet::get_sapling_params(&config).unwrap(); - + let taddr = wallet.address_from_sk(&SecretKey::from_slice(&[1u8; 32]).unwrap()); const AMOUNT_SENT: u64 = 30; let fee: u64 = DEFAULT_FEE.try_into().unwrap(); - let raw_tx = wallet.send_to_address(branch_id, &ss, &so, + let raw_tx = wallet.send_to_address(branch_id, &ss, &so, &taddr, AMOUNT_SENT, None).unwrap(); let sent_tx = Transaction::read(&raw_tx[..]).unwrap(); let sent_txid = sent_tx.txid(); diff --git a/src/main.rs b/src/main.rs index 417ee28..209d15a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,7 @@ mod lightwallet; mod address; mod prover; mod commands; +mod utils; use std::sync::Arc; use std::time::Duration; diff --git a/src/utils.rs b/src/utils.rs index b1d7929..1ece11e 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,10 +1,21 @@ -pub fn set_panic_hook() { - // When the `console_error_panic_hook` feature is enabled, we can call the - // `set_panic_hook` function at least once during initialization, and then - // we will get better error messages if our code ever panics. - // - // For more details see - // https://github.com/rustwasm/console_error_panic_hook#readme - #[cfg(feature = "console_error_panic_hook")] - console_error_panic_hook::set_once(); +use std::io::{self, Read, Write}; +use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; + +pub fn read_string(mut reader: R) -> io::Result { + // Strings are written as len + bytes + let str_len = reader.read_u64::()?; + let mut str_bytes = vec![0; str_len as usize]; + reader.read_exact(&mut str_bytes)?; + + let str = String::from_utf8(str_bytes).map_err(|e| { + io::Error::new(io::ErrorKind::InvalidData, e.to_string()) + })?; + + Ok(str) } + +pub fn write_string(mut writer: W, s: &String) -> io::Result<()> { + // Strings are written as len + utf8 + writer.write_u64::(s.as_bytes().len() as u64)?; + writer.write_all(s.as_bytes()) +} \ No newline at end of file