X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=src%2Futil%2Fser.rs;h=a2ef16b5e2462c51dd22f7c4444cd0cf7b22460a;hb=757bcc2951b6750d39ef1d841593be1e7f1c57cd;hp=92cc66b81e30af9599f35bcd8cec1bcb088d2713;hpb=92ff499bdbc115fbb897e5aa8bf758fc66987e95;p=rust-lightning diff --git a/src/util/ser.rs b/src/util/ser.rs index 92cc66b8..a2ef16b5 100644 --- a/src/util/ser.rs +++ b/src/util/ser.rs @@ -2,19 +2,21 @@ //! as ChannelsManagers and ChannelMonitors. use std::result::Result; -use std::io::Read; +use std::io::{Read, Write}; use std::collections::HashMap; use std::hash::Hash; -use secp256k1::{Secp256k1, Signature}; -use secp256k1::key::PublicKey; -use bitcoin::util::hash::Sha256dHash; +use secp256k1::Signature; +use secp256k1::key::{PublicKey, SecretKey}; use bitcoin::blockdata::script::Script; +use bitcoin::blockdata::transaction::OutPoint; +use bitcoin_hashes::sha256d::Hash as Sha256dHash; use std::marker::Sized; use ln::msgs::DecodeError; +use ln::channelmanager::{PaymentPreimage, PaymentHash}; use util::byte_utils; -use util::byte_utils::{be64_to_array, be32_to_array, be16_to_array, slice_to_be16, slice_to_be32, slice_to_be64}; +use util::byte_utils::{be64_to_array, be48_to_array, be32_to_array, be16_to_array, slice_to_be16, slice_to_be32, slice_to_be48, slice_to_be64}; const MAX_BUF_SIZE: usize = 64 * 1024; @@ -30,7 +32,7 @@ pub trait Writer { fn size_hint(&mut self, size: usize); } -impl Writer for W { +impl Writer for W { #[inline] fn write_all(&mut self, buf: &[u8]) -> Result<(), ::std::io::Error> { ::write_all(self, buf) @@ -39,6 +41,20 @@ impl Writer for W { fn size_hint(&mut self, _size: usize) { } } +pub(crate) struct WriterWriteAdaptor<'a, W: Writer + 'a>(pub &'a mut W); +impl<'a, W: Writer + 'a> Write for WriterWriteAdaptor<'a, W> { + fn write_all(&mut self, buf: &[u8]) -> Result<(), ::std::io::Error> { + self.0.write_all(buf) + } + fn write(&mut self, buf: &[u8]) -> Result { + self.0.write_all(buf)?; + Ok(buf.len()) + } + fn flush(&mut self) -> Result<(), ::std::io::Error> { + Ok(()) + } +} + struct VecWriter(Vec); impl Writer for VecWriter { fn write_all(&mut self, buf: &[u8]) -> Result<(), ::std::io::Error> { @@ -82,6 +98,32 @@ pub trait Readable fn read(reader: &mut R) -> Result; } +/// A trait that various higher-level rust-lightning types implement allowing them to be read in +/// from a Read given some additional set of arguments which is required to deserialize. +pub trait ReadableArgs + where Self: Sized, + R: Read +{ + /// Reads a Self in from the given Read + fn read(reader: &mut R, params: P) -> Result; +} + +pub(crate) struct U48(pub u64); +impl Writeable for U48 { + #[inline] + fn write(&self, writer: &mut W) -> Result<(), ::std::io::Error> { + writer.write_all(&be48_to_array(self.0)) + } +} +impl Readable for U48 { + #[inline] + fn read(reader: &mut R) -> Result { + let mut buf = [0; 6]; + reader.read_exact(&mut buf)?; + Ok(U48(slice_to_be48(&buf))) + } +} + macro_rules! impl_writeable_primitive { ($val_type:ty, $meth_write:ident, $len: expr, $meth_read:ident) => { impl Writeable for $val_type { @@ -162,6 +204,10 @@ macro_rules! impl_array { } //TODO: performance issue with [u8; size] with impl_array!() +impl_array!(3); // for rgb +impl_array!(4); // for IPv4 +impl_array!(10); // for OnionV2 +impl_array!(16); // for IPv6 impl_array!(32); // for channel id & hmac impl_array!(33); // for PublicKey impl_array!(64); // for Signature @@ -261,39 +307,34 @@ impl Readable for Script { } } -impl Writeable for Option