Block in wallet support

This commit is contained in:
Andrew Poelstra 2014-09-01 15:11:38 -05:00
parent f66b4ff6b3
commit 4629472d69
2 changed files with 89 additions and 0 deletions

View File

@ -18,4 +18,5 @@
pub mod address; pub mod address;
pub mod bip32; pub mod bip32;
pub mod wallet;

88
src/wallet/wallet.rs Normal file
View File

@ -0,0 +1,88 @@
// Rust Bitcoin Library
// Written in 2014 by
// Andrew Poelstra <apoelstra@wpsoftware.net>
// To the extent possible under law, the author(s) have dedicated all
// copyright and related and neighboring rights to this software to
// the public domain worldwide. This software is distributed without
// any warranty.
//
// You should have received a copy of the CC0 Public Domain Dedication
// along with this software.
// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
//
//! # Wallet
//!
//! Everything to do with the wallet
//!
use std::collections::HashMap;
use std::default::Default;
use network::constants::Network;
use wallet::bip32::{mod, ChildNumber, ExtendedPrivKey, Normal, Hardened};
/// A Wallet error
pub enum Error {
/// Tried to lookup an account by name, but none was found
AccountNotFound,
/// Tried to add an account when one already exists with that name
DuplicateAccount
}
/// An account
#[deriving(Clone, PartialEq, Eq, Show)]
pub struct Account {
name: String,
internal_path: Vec<ChildNumber>,
external_path: Vec<ChildNumber>
}
impl Default for Account {
fn default() -> Account {
Account {
name: String::new(),
internal_path: vec![Hardened(0), Normal(1)],
external_path: vec![Hardened(0), Normal(0)]
}
}
}
/// A wallet
#[deriving(Clone, PartialEq, Eq, Show)]
pub struct Wallet {
master: ExtendedPrivKey,
accounts: HashMap<String, Account>
}
impl Wallet {
/// Creates a new wallet from a seed
pub fn from_seed(network: Network, seed: &[u8]) -> Result<Wallet, bip32::Error> {
let mut accounts = HashMap::new();
accounts.insert(String::new(), Default::default());
Ok(Wallet {
master: try!(ExtendedPrivKey::new_master(network, seed)),
accounts: accounts
})
}
/// Adds an account to a wallet
pub fn add_account(&mut self, name: String)
-> Result<(), Error> {
if self.accounts.find(&name).is_some() {
return Err(DuplicateAccount);
}
let idx = self.accounts.len() as u32;
self.accounts.insert(name.clone(), Account {
name: name,
internal_path: vec![Hardened(idx), Normal(1)],
external_path: vec![Hardened(idx), Normal(0)]
});
Ok(())
}
}