From f1aed644c6e408717a0990bf6eeaea35f8831b27 Mon Sep 17 00:00:00 2001 From: Andrew Poelstra Date: Thu, 26 Mar 2015 14:21:48 -0500 Subject: [PATCH] More misc cleanup for rustc changes --- Cargo.toml | 6 +++--- src/internal_macros.rs | 4 ++-- src/lib.rs | 1 + src/network/socket.rs | 3 +-- src/util/base58.rs | 9 +++++---- src/util/hash.rs | 4 ++-- src/wallet/address_index.rs | 6 ++++-- src/wallet/bip32.rs | 20 ++++++++++---------- src/wallet/wallet.rs | 6 +++--- 9 files changed, 31 insertions(+), 28 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1c32f24b..55fb2440 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,13 +8,13 @@ authors = ["Andrew Poelstra "] name = "bitcoin" path = "src/lib.rs" -[dependencies.rust-crypto] -git = "https://github.com/DaGenix/rust-crypto.git" - [dependencies.secp256k1] git = "https://github.com/apoelstra/bitcoin-secp256k1-rs.git" [dependencies] +byteorder = "*" +rand = "*" +rust-crypto = "*" rustc-serialize = "*" time = "*" diff --git a/src/internal_macros.rs b/src/internal_macros.rs index 75f47689..c4c923fb 100644 --- a/src/internal_macros.rs +++ b/src/internal_macros.rs @@ -57,9 +57,9 @@ macro_rules! impl_json { ($thing:ident, $($field:ident),+) => ( impl ::serialize::json::ToJson for $thing { fn to_json(&self) -> ::serialize::json::Json { - use std::collections::TreeMap; + use std::collections::BTreeMap; use serialize::json::{ToJson, Object}; - let mut ret = TreeMap::new(); + let mut ret = BTreeMap::new(); $( ret.insert(stringify!($field).to_string(), self.$field.to_json()); )+ Object(ret) } diff --git a/src/lib.rs b/src/lib.rs index d938b922..68c36990 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -42,6 +42,7 @@ #![warn(missing_doc)] extern crate alloc; +extern crate byteorder; extern crate collections; extern crate core; extern crate rand; diff --git a/src/network/socket.rs b/src/network/socket.rs index 8ad7f64b..edf6ac36 100644 --- a/src/network/socket.rs +++ b/src/network/socket.rs @@ -18,8 +18,7 @@ //! use time::now; -use std::rand::task_rng; -use rand::Rng; +use rand::{thread_rng, Rng}; use std::io::{BufferedReader, BufferedWriter}; use std::io::{Error, Result, ErrorKind}; use std::io::net::{ip, tcp}; diff --git a/src/util/base58.rs b/src/util/base58.rs index 00f827d8..03f53a81 100644 --- a/src/util/base58.rs +++ b/src/util/base58.rs @@ -14,7 +14,8 @@ //! # Base58 encoder and decoder -use std::io::extensions::{u64_to_le_bytes, u64_from_be_bytes}; +use byteorder::{ByteOrder, LittleEndian}; + use std::string; use util::thinvec::ThinVec; @@ -103,7 +104,7 @@ pub trait FromBase58 { } let ck_start = ret.len() - 4; let expected = Sha256dHash::from_data(ret.slice_to(ck_start)).into_le().low_u32(); - let actual = Int::from_be(u64_from_be_bytes(ret.as_slice(), ck_start, 4) as u32); + let actual = LittleEndian::read_u32(&ret[ck_start..(ck_start + 4)]); if expected != actual { return Err(BadChecksum(expected, actual)); } @@ -157,8 +158,8 @@ pub trait ToBase58 { fn to_base58check(&self) -> String { let mut data = self.base58_layout(); let checksum = Sha256dHash::from_data(data.as_slice()).into_le().low_u32(); - u64_to_le_bytes(checksum as u64, 4, - |ck| { data.push_all(ck); base58_encode_slice(data.as_slice()) }) + data.write_u32::(checksum); + base58_encode_slice(data.as_slice()) } } diff --git a/src/util/hash.rs b/src/util/hash.rs index 371e2547..9f04c139 100644 --- a/src/util/hash.rs +++ b/src/util/hash.rs @@ -19,12 +19,12 @@ use core::char::from_digit; use core::cmp::min; use std::default::Default; use std::fmt; -use std::io::extensions::u64_from_be_bytes; use std::io::MemWriter; use std::mem::transmute; use std::hash; use serialize::json::{self, ToJson}; +use byteorder::{ByteOrder, LittleEndian}; use crypto::digest::Digest; use crypto::sha2::Sha256; use crypto::ripemd160::Ripemd160; @@ -87,7 +87,7 @@ impl hash::Hasher for DumbHasher { let mut ret = DumbHasherState([0; 8]); value.hash(&mut ret); let DumbHasherState(res) = ret; - u64_from_be_bytes(res.as_slice(), 0, 8) + LittleEndian::read_u64(&res[0..8]) } } diff --git a/src/wallet/address_index.rs b/src/wallet/address_index.rs index 569872c0..3c093e58 100644 --- a/src/wallet/address_index.rs +++ b/src/wallet/address_index.rs @@ -19,7 +19,7 @@ //! use std::collections::HashMap; -use collections::hash::sip::hash_with_keys; +use std::hash::{hash, Hash, SipHasher}; use secp256k1::key::SecretKey; @@ -105,7 +105,9 @@ impl AddressIndex { /// A filtering function used for creating a small address index. #[inline] pub fn admissible_address(&self, addr: &Address) -> bool { - hash_with_keys(self.k1, self.k2, &addr.as_slice()) & 0xFF == 0 + let mut hasher = SipHasher::new_with_keys(self.k1, self.k2); + addr.hash(&mut hasher); + hasher.finish() & 0xFF == 0 } /// A filtering function used for creating a small address index. diff --git a/src/wallet/bip32.rs b/src/wallet/bip32.rs index be921634..e27e6818 100644 --- a/src/wallet/bip32.rs +++ b/src/wallet/bip32.rs @@ -17,9 +17,9 @@ //! at https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki use std::default::Default; -use std::io::extensions::{u64_to_be_bytes, u64_from_be_bytes}; use serialize::{Decoder, Decodable, Encoder, Encodable}; +use byteorder::{ByteOrder, BigEndian}; use crypto::digest::Digest; use crypto::hmac::Hmac; use crypto::mac::Mac; @@ -165,14 +165,14 @@ impl ExtendedPrivKey { secp256k1::init(); // Note the unwrap: this is fine, we checked the SK when we created it hmac.input(PublicKey::from_secret_key(&self.secret_key, true).as_slice()); - u64_to_be_bytes(n as u64, 4, |raw| hmac.input(raw)); + hmac.write_u32::(n); } ChildNumber::Hardened(n) => { if n >= (1 << 31) { return Err(InvalidChildNumber(i)) } // Hardened key: use only secret data to prevent public derivation hmac.input([0]); hmac.input(self.secret_key.as_slice()); - u64_to_be_bytes(n as u64 + (1 << 31), 4, |raw| hmac.input(raw)); + hmac.write_u32::(n + (1 << 31)); } } hmac.raw_result(result.as_mut_slice()); @@ -240,7 +240,7 @@ impl ExtendedPubKey { ChildNumber::Normal(n) => { let mut hmac = Hmac::new(Sha512::new(), self.chain_code.as_slice()); hmac.input(self.public_key.as_slice()); - u64_to_be_bytes(n as u64, 4, |raw| hmac.input(raw)); + hmac.write_u32::(n); let mut result = [0; 64]; hmac.raw_result(result.as_mut_slice()); @@ -294,10 +294,10 @@ impl ToBase58 for ExtendedPrivKey { ret.push_all(self.parent_fingerprint.as_slice()); match self.child_number { ChildNumber::Hardened(n) => { - u64_to_be_bytes(n as u64 + (1 << 31), 4, |raw| ret.push_all(raw)); + ret.write_u32::(n + (1 << 31)); } ChildNumber::Normal(n) => { - u64_to_be_bytes(n as u64, 4, |raw| ret.push_all(raw)); + ret.write_u32::(n); } } ret.push_all(self.chain_code.as_slice()); @@ -313,7 +313,7 @@ impl FromBase58 for ExtendedPrivKey { return Err(InvalidLength(data.len())); } - let cn_int = u64_from_be_bytes(data.as_slice(), 9, 4) as u32; + let cn_int = BigEndian::read_u32(&data[9..13]); let child_number = if cn_int < (1 << 31) { ChildNumber::Normal(cn_int) } else { ChildNumber::Hardened(cn_int - (1 << 31)) }; @@ -346,10 +346,10 @@ impl ToBase58 for ExtendedPubKey { ret.push_all(self.parent_fingerprint.as_slice()); match self.child_number { ChildNumber::Hardened(n) => { - u64_to_be_bytes(n as u64 + (1 << 31), 4, |raw| ret.push_all(raw)); + ret.write_u32::(n + (1 << 31)); } ChildNumber::Normal(n) => { - u64_to_be_bytes(n as u64, 4, |raw| ret.push_all(raw)); + ret.write_u32::(n); } } ret.push_all(self.chain_code.as_slice()); @@ -364,7 +364,7 @@ impl FromBase58 for ExtendedPubKey { return Err(InvalidLength(data.len())); } - let cn_int = u64_from_be_bytes(data.as_slice(), 9, 4) as u32; + let cn_int = BigEndian::read_u32(&data[9..13]); let child_number = if cn_int < (1 << 31) { ChildNumber::Normal(cn_int) } else { ChildNumber::Hardened(cn_int - (1 << 31)) }; diff --git a/src/wallet/wallet.rs b/src/wallet/wallet.rs index a93fa349..7b877b0d 100644 --- a/src/wallet/wallet.rs +++ b/src/wallet/wallet.rs @@ -18,11 +18,11 @@ use std::collections::HashMap; use std::default::Default; -use std::io::extensions::u64_from_be_bytes; use serialize::{Decoder, Decodable, Encoder, Encodable}; use secp256k1::key::PublicKey; +use byteorder::{ByteOrder, LittleEndian}; use blockdata::utxoset::UtxoSet; use network::constants::Network; use wallet::bip32::{self, ChildNumber, ExtendedPrivKey, ExtendedPubKey}; @@ -241,8 +241,8 @@ impl Wallet { #[inline] pub fn siphash_key(&self) -> (u64, u64) { let ck_slice = self.master.chain_code.as_slice(); - (u64_from_be_bytes(ck_slice, 0, 8), - u64_from_be_bytes(ck_slice, 8, 8)) + (LittleEndian::read_u64(&ret[0..8]), + LittleEndian::read_u64(&ret[8..16])) } /// Total balance