From f0490118b3dcbf3d52fd305ea777c0c9d6b6d7d0 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Wed, 24 Oct 2018 10:33:42 -0400 Subject: [PATCH] impl some additional (de)serializers, including a u48 wrapper type --- src/util/ser.rs | 56 +++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 52 insertions(+), 4 deletions(-) diff --git a/src/util/ser.rs b/src/util/ser.rs index 5429c0e66..84cfa8d16 100644 --- a/src/util/ser.rs +++ b/src/util/ser.rs @@ -2,19 +2,19 @@ //! 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 secp256k1::key::{PublicKey, SecretKey}; use bitcoin::util::hash::Sha256dHash; use bitcoin::blockdata::script::Script; use std::marker::Sized; use ln::msgs::DecodeError; 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 +30,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 +39,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> { @@ -92,6 +106,22 @@ pub trait ReadableArgs 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 { @@ -310,6 +340,24 @@ impl Readable for PublicKey { } } +impl Writeable for SecretKey { + fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { + let mut ser = [0; 32]; + ser.copy_from_slice(&self[..]); + ser.write(w) + } +} + +impl Readable for SecretKey { + fn read(r: &mut R) -> Result { + let buf: [u8; 32] = Readable::read(r)?; + match SecretKey::from_slice(&Secp256k1::without_caps(), &buf) { + Ok(key) => Ok(key), + Err(_) => return Err(DecodeError::InvalidValue), + } + } +} + impl Writeable for Sha256dHash { fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { self.as_bytes().write(w) -- 2.39.5