From 4629472d69778290a39b9ddab7cb4eee70854cea Mon Sep 17 00:00:00 2001 From: Andrew Poelstra Date: Mon, 1 Sep 2014 15:11:38 -0500 Subject: [PATCH] Block in wallet support --- src/wallet/mod.rs | 1 + src/wallet/wallet.rs | 88 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 src/wallet/wallet.rs diff --git a/src/wallet/mod.rs b/src/wallet/mod.rs index 65e9731..fb7c789 100644 --- a/src/wallet/mod.rs +++ b/src/wallet/mod.rs @@ -18,4 +18,5 @@ pub mod address; pub mod bip32; +pub mod wallet; diff --git a/src/wallet/wallet.rs b/src/wallet/wallet.rs new file mode 100644 index 0000000..9009473 --- /dev/null +++ b/src/wallet/wallet.rs @@ -0,0 +1,88 @@ +// Rust Bitcoin Library +// Written in 2014 by +// Andrew Poelstra +// 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 . +// + +//! # 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, + external_path: Vec +} + +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 +} + +impl Wallet { + /// Creates a new wallet from a seed + pub fn from_seed(network: Network, seed: &[u8]) -> Result { + 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(()) + } +} + + +