Fix wallet serialization to not use "" as a key anywhere

Turns out TOML does not support tables named "", so we instead encode
the accounts list as an array rather than a name-keyed hashmap. This
is fine since the account name is in the account structure itself
anyway.
This commit is contained in:
Andrew Poelstra 2014-09-05 11:55:25 -05:00
parent bc7077fac4
commit 9f56b9c57f
1 changed files with 37 additions and 1 deletions

View File

@ -20,6 +20,7 @@ use std::collections::HashMap;
use std::default::Default;
use std::io::extensions::u64_from_be_bytes;
use collections::hash::sip::hash_with_keys;
use serialize::{Decoder, Decodable, Encoder, Encodable};
use blockdata::transaction::{PayToPubkeyHash, TxOut};
use network::constants::Network;
@ -52,12 +53,47 @@ impl Default for Account {
}
/// A wallet
#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Show)]
#[deriving(Clone, PartialEq, Eq, Show)]
pub struct Wallet {
master: ExtendedPrivKey,
accounts: HashMap<String, Account>
}
impl<S: Encoder<E>, E> Encodable<S, E> for Wallet {
fn encode(&self, s: &mut S) -> Result<(), E> {
s.emit_struct("wallet", 2, |s| {
try!(s.emit_struct_field("master", 0, |s| self.master.encode(s)));
s.emit_struct_field("accounts", 1,
|s| s.emit_seq(self.accounts.len(), |s| {
for (_, account) in self.accounts.iter() {
try!(account.encode(s));
}
Ok(())
}))
})
}
}
impl<D: Decoder<E>, E> Decodable<D, E> for Wallet {
fn decode(d: &mut D) -> Result<Wallet, E> {
d.read_struct("wallet", 2, |d| {
Ok(Wallet {
master: try!(d.read_struct_field("master", 0, Decodable::decode)),
accounts: try!(d.read_struct_field("accounts", 1, |d| {
d.read_seq(|d, len| {
let mut ret = HashMap::new();
for i in range(0, len) {
let accnt: Account = try!(d.read_seq_elt(i, Decodable::decode));
ret.insert(accnt.name.clone(), accnt);
}
Ok(ret)
})
}))
})
})
}
}
impl Wallet {
/// Creates a new wallet from a BIP32 seed
#[inline]