2014-07-18 13:56:17 +00:00
|
|
|
// Rust Bitcoin Library
|
|
|
|
// Written in 2014 by
|
|
|
|
// Andrew Poelstra <apoelstra@wpsoftware.net>
|
|
|
|
//
|
|
|
|
// 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 <http://creativecommons.org/publicdomain/zero/1.0/>.
|
|
|
|
//
|
|
|
|
|
2018-08-08 21:38:50 +00:00
|
|
|
//! Network Serialization
|
2014-07-18 13:56:17 +00:00
|
|
|
//!
|
|
|
|
//! This module defines the `Serializable` trait which is used for
|
|
|
|
//! (de)serializing Bitcoin objects for transmission on the network.
|
|
|
|
//! It also defines (de)serialization routines for many primitives.
|
|
|
|
//!
|
|
|
|
|
2015-04-11 01:55:59 +00:00
|
|
|
use std::io::{Cursor, Read, Write};
|
2015-04-07 01:51:11 +00:00
|
|
|
use byteorder::{LittleEndian, WriteBytesExt, ReadBytesExt};
|
2018-07-24 18:43:03 +00:00
|
|
|
use hex::encode as hex_encode;
|
2014-07-18 13:56:17 +00:00
|
|
|
|
2014-08-01 16:01:39 +00:00
|
|
|
use network::encodable::{ConsensusDecodable, ConsensusEncodable};
|
2014-07-18 13:56:17 +00:00
|
|
|
use util::hash::Sha256dHash;
|
2015-04-07 01:51:11 +00:00
|
|
|
use util;
|
2014-07-18 13:56:17 +00:00
|
|
|
|
2014-08-01 16:01:39 +00:00
|
|
|
/// Objects which are referred to by hash
|
|
|
|
pub trait BitcoinHash {
|
2015-04-07 01:51:11 +00:00
|
|
|
/// Produces a Sha256dHash which can be used to refer to the object
|
|
|
|
fn bitcoin_hash(&self) -> Sha256dHash;
|
2014-07-18 13:56:17 +00:00
|
|
|
}
|
|
|
|
|
2014-08-01 16:01:39 +00:00
|
|
|
impl BitcoinHash for Vec<u8> {
|
2015-04-07 01:51:11 +00:00
|
|
|
#[inline]
|
|
|
|
fn bitcoin_hash(&self) -> Sha256dHash {
|
|
|
|
Sha256dHash::from_data(&self[..])
|
|
|
|
}
|
2014-07-18 13:56:17 +00:00
|
|
|
}
|
|
|
|
|
2014-08-01 16:01:39 +00:00
|
|
|
/// Encode an object into a vector
|
2015-04-11 01:55:59 +00:00
|
|
|
pub fn serialize<T: ?Sized>(data: &T) -> Result<Vec<u8>, util::Error>
|
2015-04-07 01:51:11 +00:00
|
|
|
where T: ConsensusEncodable<RawEncoder<Cursor<Vec<u8>>>>,
|
|
|
|
{
|
|
|
|
let mut encoder = RawEncoder::new(Cursor::new(vec![]));
|
|
|
|
try!(data.consensus_encode(&mut encoder));
|
|
|
|
Ok(encoder.into_inner().into_inner())
|
2014-07-18 13:56:17 +00:00
|
|
|
}
|
|
|
|
|
2014-08-23 03:53:49 +00:00
|
|
|
/// Encode an object into a hex-encoded string
|
2015-04-11 01:55:59 +00:00
|
|
|
pub fn serialize_hex<T: ?Sized>(data: &T) -> Result<String, util::Error>
|
2015-04-07 01:51:11 +00:00
|
|
|
where T: ConsensusEncodable<RawEncoder<Cursor<Vec<u8>>>>
|
|
|
|
{
|
|
|
|
let serial = try!(serialize(data));
|
2018-07-24 18:43:03 +00:00
|
|
|
Ok(hex_encode(serial))
|
2014-08-23 03:53:49 +00:00
|
|
|
}
|
|
|
|
|
2014-08-01 16:01:39 +00:00
|
|
|
/// Deserialize an object from a vector
|
2015-04-07 01:51:11 +00:00
|
|
|
pub fn deserialize<'a, T>(data: &'a [u8]) -> Result<T, util::Error>
|
|
|
|
where T: ConsensusDecodable<RawDecoder<Cursor<&'a [u8]>>>
|
|
|
|
{
|
|
|
|
let mut decoder = RawDecoder::new(Cursor::new(data));
|
|
|
|
ConsensusDecodable::consensus_decode(&mut decoder)
|
2014-07-29 03:12:10 +00:00
|
|
|
}
|
|
|
|
|
2014-08-01 16:01:39 +00:00
|
|
|
/// An encoder for raw binary data
|
|
|
|
pub struct RawEncoder<W> {
|
2015-04-07 01:51:11 +00:00
|
|
|
writer: W
|
2014-07-18 13:56:17 +00:00
|
|
|
}
|
|
|
|
|
2014-08-01 16:01:39 +00:00
|
|
|
/// An decoder for raw binary data
|
|
|
|
pub struct RawDecoder<R> {
|
2015-04-07 01:51:11 +00:00
|
|
|
reader: R
|
2014-07-18 13:56:17 +00:00
|
|
|
}
|
|
|
|
|
2015-04-07 01:51:11 +00:00
|
|
|
impl<W: Write> RawEncoder<W> {
|
|
|
|
/// Constructor
|
|
|
|
pub fn new(writer: W) -> RawEncoder<W> { RawEncoder { writer: writer } }
|
|
|
|
/// Returns the underlying Writer
|
|
|
|
pub fn into_inner(self) -> W { self.writer }
|
2014-07-18 13:56:17 +00:00
|
|
|
}
|
|
|
|
|
2015-04-07 01:51:11 +00:00
|
|
|
impl<R: Read> RawDecoder<R> {
|
2014-08-01 16:01:39 +00:00
|
|
|
/// Constructor
|
2015-04-07 01:51:11 +00:00
|
|
|
pub fn new(reader: R) -> RawDecoder<R> { RawDecoder { reader: reader } }
|
2014-08-01 16:01:39 +00:00
|
|
|
/// Returns the underlying Reader
|
2015-04-07 01:51:11 +00:00
|
|
|
pub fn into_inner(self) -> R { self.reader }
|
2014-07-18 13:56:17 +00:00
|
|
|
}
|
|
|
|
|
2014-08-01 16:01:39 +00:00
|
|
|
/// A simple Encoder trait
|
2015-04-06 00:10:37 +00:00
|
|
|
pub trait SimpleEncoder {
|
2015-04-11 01:55:59 +00:00
|
|
|
/// An encoding error
|
|
|
|
type Error;
|
2018-07-24 18:43:03 +00:00
|
|
|
|
2015-04-11 01:55:59 +00:00
|
|
|
/// Output a 64-bit uint
|
|
|
|
fn emit_u64(&mut self, v: u64) -> Result<(), Self::Error>;
|
|
|
|
/// Output a 32-bit uint
|
|
|
|
fn emit_u32(&mut self, v: u32) -> Result<(), Self::Error>;
|
|
|
|
/// Output a 16-bit uint
|
|
|
|
fn emit_u16(&mut self, v: u16) -> Result<(), Self::Error>;
|
|
|
|
/// Output a 8-bit uint
|
|
|
|
fn emit_u8(&mut self, v: u8) -> Result<(), Self::Error>;
|
|
|
|
|
|
|
|
/// Output a 64-bit int
|
|
|
|
fn emit_i64(&mut self, v: i64) -> Result<(), Self::Error>;
|
|
|
|
/// Output a 32-bit int
|
|
|
|
fn emit_i32(&mut self, v: i32) -> Result<(), Self::Error>;
|
|
|
|
/// Output a 16-bit int
|
|
|
|
fn emit_i16(&mut self, v: i16) -> Result<(), Self::Error>;
|
|
|
|
/// Output a 8-bit int
|
|
|
|
fn emit_i8(&mut self, v: i8) -> Result<(), Self::Error>;
|
|
|
|
|
|
|
|
/// Output a boolean
|
|
|
|
fn emit_bool(&mut self, v: bool) -> Result<(), Self::Error>;
|
2014-07-18 13:56:17 +00:00
|
|
|
}
|
|
|
|
|
2014-08-01 16:01:39 +00:00
|
|
|
/// A simple Decoder trait
|
2015-04-06 00:10:37 +00:00
|
|
|
pub trait SimpleDecoder {
|
2015-04-11 01:55:59 +00:00
|
|
|
/// A decoding error
|
|
|
|
type Error;
|
|
|
|
|
|
|
|
/// Read a 64-bit uint
|
|
|
|
fn read_u64(&mut self) -> Result<u64, Self::Error>;
|
|
|
|
/// Read a 32-bit uint
|
|
|
|
fn read_u32(&mut self) -> Result<u32, Self::Error>;
|
|
|
|
/// Read a 16-bit uint
|
|
|
|
fn read_u16(&mut self) -> Result<u16, Self::Error>;
|
|
|
|
/// Read a 8-bit uint
|
|
|
|
fn read_u8(&mut self) -> Result<u8, Self::Error>;
|
|
|
|
|
|
|
|
/// Read a 64-bit int
|
|
|
|
fn read_i64(&mut self) -> Result<i64, Self::Error>;
|
|
|
|
/// Read a 32-bit int
|
|
|
|
fn read_i32(&mut self) -> Result<i32, Self::Error>;
|
|
|
|
/// Read a 16-bit int
|
|
|
|
fn read_i16(&mut self) -> Result<i16, Self::Error>;
|
|
|
|
/// Read a 8-bit int
|
|
|
|
fn read_i8(&mut self) -> Result<i8, Self::Error>;
|
|
|
|
|
|
|
|
/// Read a boolean
|
|
|
|
fn read_bool(&mut self) -> Result<bool, Self::Error>;
|
|
|
|
|
|
|
|
/// Signal a decoding error
|
|
|
|
fn error(&mut self, err: String) -> Self::Error;
|
2015-04-07 01:51:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! encoder_fn {
|
|
|
|
($name:ident, $val_type:ty, $writefn:ident) => {
|
|
|
|
#[inline]
|
|
|
|
fn $name(&mut self, v: $val_type) -> Result<(), util::Error> {
|
|
|
|
self.writer.$writefn::<LittleEndian>(v).map_err(util::Error::ByteOrder)
|
|
|
|
}
|
|
|
|
}
|
2014-07-18 13:56:17 +00:00
|
|
|
}
|
|
|
|
|
2015-04-07 01:51:11 +00:00
|
|
|
macro_rules! decoder_fn {
|
|
|
|
($name:ident, $val_type:ty, $readfn:ident) => {
|
|
|
|
#[inline]
|
|
|
|
fn $name(&mut self) -> Result<$val_type, util::Error> {
|
|
|
|
self.reader.$readfn::<LittleEndian>().map_err(util::Error::ByteOrder)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-07-18 13:56:17 +00:00
|
|
|
|
2015-04-06 00:10:37 +00:00
|
|
|
impl<W: Write> SimpleEncoder for RawEncoder<W> {
|
2015-04-07 01:51:11 +00:00
|
|
|
type Error = util::Error;
|
|
|
|
|
|
|
|
encoder_fn!(emit_u64, u64, write_u64);
|
|
|
|
encoder_fn!(emit_u32, u32, write_u32);
|
|
|
|
encoder_fn!(emit_u16, u16, write_u16);
|
|
|
|
encoder_fn!(emit_i64, i64, write_i64);
|
|
|
|
encoder_fn!(emit_i32, i32, write_i32);
|
|
|
|
encoder_fn!(emit_i16, i16, write_i16);
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn emit_i8(&mut self, v: i8) -> Result<(), util::Error> {
|
|
|
|
self.writer.write_i8(v).map_err(util::Error::ByteOrder)
|
|
|
|
}
|
|
|
|
#[inline]
|
|
|
|
fn emit_u8(&mut self, v: u8) -> Result<(), util::Error> {
|
|
|
|
self.writer.write_u8(v).map_err(util::Error::ByteOrder)
|
|
|
|
}
|
|
|
|
#[inline]
|
|
|
|
fn emit_bool(&mut self, v: bool) -> Result<(), util::Error> {
|
|
|
|
self.writer.write_i8(if v {1} else {0}).map_err(util::Error::ByteOrder)
|
|
|
|
}
|
2014-07-18 13:56:17 +00:00
|
|
|
}
|
|
|
|
|
2015-04-06 00:10:37 +00:00
|
|
|
impl<R: Read> SimpleDecoder for RawDecoder<R> {
|
2015-04-07 01:51:11 +00:00
|
|
|
type Error = util::Error;
|
|
|
|
|
|
|
|
decoder_fn!(read_u64, u64, read_u64);
|
|
|
|
decoder_fn!(read_u32, u32, read_u32);
|
|
|
|
decoder_fn!(read_u16, u16, read_u16);
|
|
|
|
decoder_fn!(read_i64, i64, read_i64);
|
|
|
|
decoder_fn!(read_i32, i32, read_i32);
|
|
|
|
decoder_fn!(read_i16, i16, read_i16);
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn read_u8(&mut self) -> Result<u8, util::Error> {
|
|
|
|
self.reader.read_u8().map_err(util::Error::ByteOrder)
|
|
|
|
}
|
|
|
|
#[inline]
|
|
|
|
fn read_i8(&mut self) -> Result<i8, util::Error> {
|
|
|
|
self.reader.read_i8().map_err(util::Error::ByteOrder)
|
|
|
|
}
|
|
|
|
#[inline]
|
|
|
|
fn read_bool(&mut self) -> Result<bool, util::Error> {
|
|
|
|
match self.reader.read_i8() {
|
|
|
|
Ok(bit) => Ok(bit != 0),
|
|
|
|
Err(e) => Err(util::Error::ByteOrder(e))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn error(&mut self, err: String) -> util::Error {
|
|
|
|
util::Error::Detail(err, Box::new(util::Error::ParseFailed))
|
2014-07-20 23:52:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-01 16:01:39 +00:00
|
|
|
// Aren't really any tests here.. the main functions are serialize and
|
|
|
|
// deserialize, which get the crap tested out of them it every other
|
|
|
|
// module.
|
2014-07-18 13:56:17 +00:00
|
|
|
|