X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Futil%2Fser.rs;h=961d4f1020073f8d76553374ef0cec394e217828;hb=e9d3033e5caae5d708b52270416fff1973253d39;hp=fd0e9f7a383b2bb9e0530e183e5ee9203218e065;hpb=eff8af21103e43f763cb10ae6a75c1543a2d4068;p=rust-lightning diff --git a/lightning/src/util/ser.rs b/lightning/src/util/ser.rs index fd0e9f7a..961d4f10 100644 --- a/lightning/src/util/ser.rs +++ b/lightning/src/util/ser.rs @@ -1,7 +1,15 @@ +// This file is Copyright its original authors, visible in version control +// history. +// +// This file is licensed under the Apache License, Version 2.0 or the MIT license +// , at your option. +// You may not use this file except in accordance with one or both of these +// licenses. + //! A very simple serialization framework which is used to serialize/deserialize messages as well //! as ChannelsManagers and ChannelMonitors. -use std::result::Result; use std::io::{Read, Write}; use std::collections::HashMap; use std::hash::Hash; @@ -15,6 +23,7 @@ use bitcoin::blockdata::transaction::{OutPoint, Transaction, TxOut}; use bitcoin::consensus; use bitcoin::consensus::Encodable; use bitcoin::hashes::sha256d::Hash as Sha256dHash; +use bitcoin::hash_types::{Txid, BlockHash}; use std::marker::Sized; use ln::msgs::DecodeError; use ln::channelmanager::{PaymentPreimage, PaymentHash, PaymentSecret}; @@ -28,6 +37,8 @@ const MAX_BUF_SIZE: usize = 64 * 1024; /// buffers being written into. /// An impl is provided for any type that also impls std::io::Write which simply ignores size /// hints. +/// +/// (C-not exported) as we only export serialization to/from byte arrays instead pub trait Writer { /// Writes the given buf out. See std::io::Write::write_all for more fn write_all(&mut self, buf: &[u8]) -> Result<(), ::std::io::Error>; @@ -150,6 +161,8 @@ impl Read for ReadTrackingReader { } /// A trait that various rust-lightning types implement allowing them to be written out to a Writer +/// +/// (C-not exported) as we only export serialization to/from byte arrays instead pub trait Writeable { /// Writes self out to the given Writer fn write(&self, writer: &mut W) -> Result<(), ::std::io::Error>; @@ -177,6 +190,8 @@ impl<'a, T: Writeable> Writeable for &'a T { } /// A trait that various rust-lightning types implement allowing them to be read in from a Read +/// +/// (C-not exported) as we only export serialization to/from byte arrays instead pub trait Readable where Self: Sized { @@ -186,6 +201,8 @@ pub trait Readable /// 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. +/// +/// (C-not exported) as we only export serialization to/from byte arrays instead pub trait ReadableArgs

where Self: Sized { @@ -194,6 +211,8 @@ pub trait ReadableArgs

} /// A trait that various rust-lightning types implement allowing them to (maybe) be read in from a Read +/// +/// (C-not exported) as we only export serialization to/from byte arrays instead pub trait MaybeReadable where Self: Sized { @@ -632,6 +651,36 @@ impl Readable for Option } } +impl Writeable for Txid { + fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { + w.write_all(&self[..]) + } +} + +impl Readable for Txid { + fn read(r: &mut R) -> Result { + use bitcoin::hashes::Hash; + + let buf: [u8; 32] = Readable::read(r)?; + Ok(Txid::from_slice(&buf[..]).unwrap()) + } +} + +impl Writeable for BlockHash { + fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { + w.write_all(&self[..]) + } +} + +impl Readable for BlockHash { + fn read(r: &mut R) -> Result { + use bitcoin::hashes::Hash; + + let buf: [u8; 32] = Readable::read(r)?; + Ok(BlockHash::from_slice(&buf[..]).unwrap()) + } +} + impl Writeable for OutPoint { fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { self.txid.write(w)?;