moved hash to primitives

This commit is contained in:
debris 2016-09-19 14:56:45 +02:00
parent ebed1b0b71
commit 2a43085bda
13 changed files with 78 additions and 66 deletions

View File

@ -3,7 +3,7 @@
//! https://en.bitcoin.it/wiki/Protocol_documentation#tx
use hex::FromHex;
use primitives::Bytes;
use bytes::Bytes;
use ser::{
Deserializable, Reader, Error as ReaderError, deserialize,
Serializable, Stream, serialize

View File

@ -27,7 +27,7 @@ pub mod script;
pub mod ser;
pub mod crypto;
pub mod hash;
pub mod network;
pub use rustc_serialize::hex;
pub use primitives::{hash, bytes};

View File

@ -1,6 +1,5 @@
use std::{fmt, ops, cmp, str};
use hex::{ToHex, FromHex, FromHexError};
use ser::{Stream, Serializable, Reader, Deserializable, Error as ReaderError};
macro_rules! impl_hash {
($name: ident, $size: expr) => {
@ -97,23 +96,6 @@ macro_rules! impl_hash {
}
}
// TODO: move to ser module
impl Serializable for $name {
fn serialize(&self, stream: &mut Stream) {
stream.append_slice(&self.0);
}
}
impl Deserializable for $name {
fn deserialize(reader: &mut Reader) -> Result<Self, ReaderError> where Self: Sized {
let slice = try!(reader.read_slice($size));
let mut result = Self::default();
result.copy_from_slice(slice);
Ok(result)
}
}
impl $name {
pub fn reversed(&self) -> Self {
let mut result = self.clone();

View File

@ -1,3 +1,2 @@
mod bytes;
pub use self::bytes::Bytes;
pub mod bytes;
pub mod hash;

View File

@ -1,4 +1,4 @@
use primitives::Bytes;
use bytes::Bytes;
use script::{Opcode, Script, Num};
#[derive(Default)]

View File

@ -1,5 +1,5 @@
use std::{cmp, mem};
use primitives::Bytes;
use bytes::Bytes;
use keys::{Signature, Public};
use chain::SEQUENCE_LOCKTIME_DISABLE_FLAG;
use crypto::{sha1, sha256, dhash160, dhash256, ripemd160};
@ -891,7 +891,7 @@ pub fn eval_script(
#[cfg(test)]
mod tests {
use hex::FromHex;
use primitives::Bytes;
use bytes::Bytes;
use chain::Transaction;
use script::{
Opcode, Script, VerificationFlags, Builder, Error, Num, TransactionInputSigner,

View File

@ -1,6 +1,6 @@
//! Script numeric.
use std::ops;
use primitives::Bytes;
use bytes::Bytes;
use script::Error;
/// Numeric opcodes (OP_1ADD, etc) are restricted to operating on 4-byte integers.

View File

@ -2,7 +2,7 @@
use std::{fmt, ops};
use hex::ToHex;
use primitives::Bytes;
use bytes::Bytes;
use script::{Opcode, Error};
/// Maximum number of bytes pushable to the stack

View File

@ -1,5 +1,5 @@
use script::{Script, Builder};
use primitives::Bytes;
use bytes::Bytes;
use keys::KeyPair;
use crypto::dhash256;
use hash::H256;
@ -224,7 +224,7 @@ impl TransactionInputSigner {
#[cfg(test)]
mod tests {
use hex::FromHex;
use primitives::Bytes;
use bytes::Bytes;
use hash::H256;
use keys::{KeyPair, Private, Address};
use chain::{OutPoint, TransactionOutput, Transaction};

64
src/ser/impls.rs Normal file
View File

@ -0,0 +1,64 @@
use bytes::Bytes;
use hash::{H160, H256, H264, H512, H520};
use ser::{Serializable, Stream, Deserializable, Reader, Error};
use ser::compact_integer::CompactInteger;
macro_rules! impl_ser_for_hash {
($name: ident, $size: expr) => {
impl Serializable for $name {
fn serialize(&self, stream: &mut Stream) {
stream.append_slice(&**self);
}
}
impl Deserializable for $name {
fn deserialize(reader: &mut Reader) -> Result<Self, Error> where Self: Sized {
let slice = try!(reader.read_slice($size));
let mut result = Self::default();
result.copy_from_slice(slice);
Ok(result)
}
}
}
}
impl_ser_for_hash!(H160, 20);
impl_ser_for_hash!(H256, 32);
impl_ser_for_hash!(H264, 33);
impl_ser_for_hash!(H512, 64);
impl_ser_for_hash!(H520, 65);
impl Serializable for Bytes {
fn serialize(&self, stream: &mut Stream) {
stream
.append(&CompactInteger::from(self.len()))
.append_slice(&self);
}
}
impl Deserializable for Bytes {
fn deserialize(reader: &mut Reader) -> Result<Self, Error> where Self: Sized {
let len = try!(reader.read::<CompactInteger>());
reader.read_slice(len.into()).map(|b| b.to_vec().into())
}
}
#[cfg(test)]
mod tests {
use bytes::Bytes;
use ser::{serialize, deserialize};
#[test]
fn test_bytes_deserialize() {
let raw = vec![0x02, 0x01, 0x45];
let expected: Bytes = "0145".into();
assert_eq!(expected, deserialize(&raw).unwrap());
}
#[test]
fn test_bytes_serialize() {
let expected = vec![0x02, 0x01, 0x45];
let bytes: Bytes = "0145".into();
assert_eq!(expected, serialize(&bytes));
}
}

View File

@ -1,4 +1,5 @@
mod compact_integer;
mod impls;
pub mod reader;
pub mod stream;

View File

@ -1,6 +1,5 @@
use std::{io, cmp};
use byteorder::{LittleEndian, ReadBytesExt};
use primitives::Bytes;
use ser::compact_integer::CompactInteger;
pub fn deserialize<T>(buffer: &[u8]) -> Result<T, Error> where T: Deserializable {
@ -119,17 +118,9 @@ impl Deserializable for u64 {
}
}
impl Deserializable for Bytes {
fn deserialize(reader: &mut Reader) -> Result<Self, Error> where Self: Sized {
let len = try!(reader.read::<CompactInteger>());
reader.read_slice(len.into()).map(|b| b.to_vec().into())
}
}
#[cfg(test)]
mod test {
use primitives::Bytes;
use super::{Reader, Error, deserialize};
use super::{Reader, Error};
#[test]
fn test_reader_read() {
@ -149,11 +140,4 @@ mod test {
assert!(reader.is_finished());
assert_eq!(Error::UnexpectedEnd, reader.read::<u8>().unwrap_err());
}
#[test]
fn test_bytes_deserialize() {
let raw = vec![0x02, 0x01, 0x45];
let expected: Bytes = "0145".into();
assert_eq!(expected, deserialize(&raw).unwrap());
}
}

View File

@ -1,7 +1,6 @@
//! Stream used for serialization.
use std::io::{self, Write};
use byteorder::{LittleEndian, WriteBytesExt};
use primitives::Bytes;
use ser::compact_integer::CompactInteger;
pub fn serialize(t: &Serializable) -> Vec<u8> {
@ -97,18 +96,9 @@ impl Serializable for u64 {
}
}
impl Serializable for Bytes {
fn serialize(&self, stream: &mut Stream) {
stream
.append(&CompactInteger::from(self.len()))
.append_slice(&self);
}
}
#[cfg(test)]
mod tests {
use primitives::Bytes;
use super::{Stream, serialize};
use super::Stream;
#[test]
fn test_stream_append() {
@ -129,12 +119,4 @@ mod tests {
assert_eq!(expected, stream.out());
}
#[test]
fn test_bytes_serialize() {
let expected = vec![0x02, 0x01, 0x45];
let bytes: Bytes = "0145".into();
assert_eq!(expected, serialize(&bytes));
}
}