moved hash to primitives
This commit is contained in:
parent
ebed1b0b71
commit
2a43085bda
|
@ -3,7 +3,7 @@
|
||||||
//! https://en.bitcoin.it/wiki/Protocol_documentation#tx
|
//! https://en.bitcoin.it/wiki/Protocol_documentation#tx
|
||||||
|
|
||||||
use hex::FromHex;
|
use hex::FromHex;
|
||||||
use primitives::Bytes;
|
use bytes::Bytes;
|
||||||
use ser::{
|
use ser::{
|
||||||
Deserializable, Reader, Error as ReaderError, deserialize,
|
Deserializable, Reader, Error as ReaderError, deserialize,
|
||||||
Serializable, Stream, serialize
|
Serializable, Stream, serialize
|
||||||
|
|
|
@ -27,7 +27,7 @@ pub mod script;
|
||||||
pub mod ser;
|
pub mod ser;
|
||||||
|
|
||||||
pub mod crypto;
|
pub mod crypto;
|
||||||
pub mod hash;
|
|
||||||
pub mod network;
|
pub mod network;
|
||||||
|
|
||||||
pub use rustc_serialize::hex;
|
pub use rustc_serialize::hex;
|
||||||
|
pub use primitives::{hash, bytes};
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
use std::{fmt, ops, cmp, str};
|
use std::{fmt, ops, cmp, str};
|
||||||
use hex::{ToHex, FromHex, FromHexError};
|
use hex::{ToHex, FromHex, FromHexError};
|
||||||
use ser::{Stream, Serializable, Reader, Deserializable, Error as ReaderError};
|
|
||||||
|
|
||||||
macro_rules! impl_hash {
|
macro_rules! impl_hash {
|
||||||
($name: ident, $size: expr) => {
|
($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 {
|
impl $name {
|
||||||
pub fn reversed(&self) -> Self {
|
pub fn reversed(&self) -> Self {
|
||||||
let mut result = self.clone();
|
let mut result = self.clone();
|
|
@ -1,3 +1,2 @@
|
||||||
mod bytes;
|
pub mod bytes;
|
||||||
|
pub mod hash;
|
||||||
pub use self::bytes::Bytes;
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use primitives::Bytes;
|
use bytes::Bytes;
|
||||||
use script::{Opcode, Script, Num};
|
use script::{Opcode, Script, Num};
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use std::{cmp, mem};
|
use std::{cmp, mem};
|
||||||
use primitives::Bytes;
|
use bytes::Bytes;
|
||||||
use keys::{Signature, Public};
|
use keys::{Signature, Public};
|
||||||
use chain::SEQUENCE_LOCKTIME_DISABLE_FLAG;
|
use chain::SEQUENCE_LOCKTIME_DISABLE_FLAG;
|
||||||
use crypto::{sha1, sha256, dhash160, dhash256, ripemd160};
|
use crypto::{sha1, sha256, dhash160, dhash256, ripemd160};
|
||||||
|
@ -891,7 +891,7 @@ pub fn eval_script(
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use hex::FromHex;
|
use hex::FromHex;
|
||||||
use primitives::Bytes;
|
use bytes::Bytes;
|
||||||
use chain::Transaction;
|
use chain::Transaction;
|
||||||
use script::{
|
use script::{
|
||||||
Opcode, Script, VerificationFlags, Builder, Error, Num, TransactionInputSigner,
|
Opcode, Script, VerificationFlags, Builder, Error, Num, TransactionInputSigner,
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
//! Script numeric.
|
//! Script numeric.
|
||||||
use std::ops;
|
use std::ops;
|
||||||
use primitives::Bytes;
|
use bytes::Bytes;
|
||||||
use script::Error;
|
use script::Error;
|
||||||
|
|
||||||
/// Numeric opcodes (OP_1ADD, etc) are restricted to operating on 4-byte integers.
|
/// Numeric opcodes (OP_1ADD, etc) are restricted to operating on 4-byte integers.
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
use std::{fmt, ops};
|
use std::{fmt, ops};
|
||||||
use hex::ToHex;
|
use hex::ToHex;
|
||||||
use primitives::Bytes;
|
use bytes::Bytes;
|
||||||
use script::{Opcode, Error};
|
use script::{Opcode, Error};
|
||||||
|
|
||||||
/// Maximum number of bytes pushable to the stack
|
/// Maximum number of bytes pushable to the stack
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use script::{Script, Builder};
|
use script::{Script, Builder};
|
||||||
use primitives::Bytes;
|
use bytes::Bytes;
|
||||||
use keys::KeyPair;
|
use keys::KeyPair;
|
||||||
use crypto::dhash256;
|
use crypto::dhash256;
|
||||||
use hash::H256;
|
use hash::H256;
|
||||||
|
@ -224,7 +224,7 @@ impl TransactionInputSigner {
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use hex::FromHex;
|
use hex::FromHex;
|
||||||
use primitives::Bytes;
|
use bytes::Bytes;
|
||||||
use hash::H256;
|
use hash::H256;
|
||||||
use keys::{KeyPair, Private, Address};
|
use keys::{KeyPair, Private, Address};
|
||||||
use chain::{OutPoint, TransactionOutput, Transaction};
|
use chain::{OutPoint, TransactionOutput, Transaction};
|
||||||
|
|
|
@ -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));
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,4 +1,5 @@
|
||||||
mod compact_integer;
|
mod compact_integer;
|
||||||
|
mod impls;
|
||||||
pub mod reader;
|
pub mod reader;
|
||||||
pub mod stream;
|
pub mod stream;
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
use std::{io, cmp};
|
use std::{io, cmp};
|
||||||
use byteorder::{LittleEndian, ReadBytesExt};
|
use byteorder::{LittleEndian, ReadBytesExt};
|
||||||
use primitives::Bytes;
|
|
||||||
use ser::compact_integer::CompactInteger;
|
use ser::compact_integer::CompactInteger;
|
||||||
|
|
||||||
pub fn deserialize<T>(buffer: &[u8]) -> Result<T, Error> where T: Deserializable {
|
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)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use primitives::Bytes;
|
use super::{Reader, Error};
|
||||||
use super::{Reader, Error, deserialize};
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_reader_read() {
|
fn test_reader_read() {
|
||||||
|
@ -149,11 +140,4 @@ mod test {
|
||||||
assert!(reader.is_finished());
|
assert!(reader.is_finished());
|
||||||
assert_eq!(Error::UnexpectedEnd, reader.read::<u8>().unwrap_err());
|
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());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
//! Stream used for serialization.
|
//! Stream used for serialization.
|
||||||
use std::io::{self, Write};
|
use std::io::{self, Write};
|
||||||
use byteorder::{LittleEndian, WriteBytesExt};
|
use byteorder::{LittleEndian, WriteBytesExt};
|
||||||
use primitives::Bytes;
|
|
||||||
use ser::compact_integer::CompactInteger;
|
use ser::compact_integer::CompactInteger;
|
||||||
|
|
||||||
pub fn serialize(t: &Serializable) -> Vec<u8> {
|
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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use primitives::Bytes;
|
use super::Stream;
|
||||||
use super::{Stream, serialize};
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_stream_append() {
|
fn test_stream_append() {
|
||||||
|
@ -129,12 +119,4 @@ mod tests {
|
||||||
|
|
||||||
assert_eq!(expected, stream.out());
|
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));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue