Browse Source

Write chain name to wallet

checkpoints
Aditya Kulkarni 5 years ago
parent
commit
b3ca226ff4
  1. 22
      src/lightwallet/mod.rs
  2. 1
      src/main.rs
  3. 29
      src/utils.rs

22
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<u8>, Vec<u8>), Error> {
@ -250,6 +248,16 @@ impl LightWallet {
})?;
let txs = txs_tuples.into_iter().collect::<HashMap<TxId, WalletTx>>();
// 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();

1
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;

29
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<R: Read>(mut reader: R) -> io::Result<String> {
// Strings are written as <littleendian> len + bytes
let str_len = reader.read_u64::<LittleEndian>()?;
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<W: Write>(mut writer: W, s: &String) -> io::Result<()> {
// Strings are written as len + utf8
writer.write_u64::<LittleEndian>(s.as_bytes().len() as u64)?;
writer.write_all(s.as_bytes())
}
Loading…
Cancel
Save