More misc cleanup for rustc changes

This commit is contained in:
Andrew Poelstra 2015-03-26 14:21:48 -05:00
parent 0bf5809674
commit f1aed644c6
9 changed files with 31 additions and 28 deletions

View File

@ -8,13 +8,13 @@ authors = ["Andrew Poelstra <apoelstra@wpsoftware.net>"]
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 = "*"

View File

@ -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)
}

View File

@ -42,6 +42,7 @@
#![warn(missing_doc)]
extern crate alloc;
extern crate byteorder;
extern crate collections;
extern crate core;
extern crate rand;

View File

@ -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};

View File

@ -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::<LittleEndian>(checksum);
base58_encode_slice(data.as_slice())
}
}

View File

@ -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<DumbHasherState> 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])
}
}

View File

@ -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.

View File

@ -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::<BigEndian>(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::<BigEndian>(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::<BigEndian>(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::<BigEndian>(n + (1 << 31));
}
ChildNumber::Normal(n) => {
u64_to_be_bytes(n as u64, 4, |raw| ret.push_all(raw));
ret.write_u32::<BigEndian>(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::<BigEndian>(n + (1 << 31));
}
ChildNumber::Normal(n) => {
u64_to_be_bytes(n as u64, 4, |raw| ret.push_all(raw));
ret.write_u32::<BigEndian>(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)) };

View File

@ -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