From 569f9038bec1f0ba09cfb01c35222a9365ee77d6 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Fri, 7 Feb 2020 20:05:37 -0500 Subject: [PATCH] Impl (de)serialization for bitcoin::Transaction. There is little risk of misusing this as there's not much in the way of other ways you may want to serialize bitcoin::Transaction --- lightning/src/util/ser.rs | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/lightning/src/util/ser.rs b/lightning/src/util/ser.rs index 1b98e341f..44fdd1c0c 100644 --- a/lightning/src/util/ser.rs +++ b/lightning/src/util/ser.rs @@ -11,7 +11,9 @@ use std::cmp; use secp256k1::Signature; use secp256k1::key::{PublicKey, SecretKey}; use bitcoin::blockdata::script::Script; -use bitcoin::blockdata::transaction::OutPoint; +use bitcoin::blockdata::transaction::{OutPoint, Transaction}; +use bitcoin::consensus; +use bitcoin::consensus::Encodable; use bitcoin_hashes::sha256d::Hash as Sha256dHash; use std::marker::Sized; use ln::msgs::DecodeError; @@ -625,6 +627,27 @@ impl Readable for OutPoint { } } +impl Writeable for Transaction { + fn write(&self, writer: &mut W) -> Result<(), ::std::io::Error> { + match self.consensus_encode(WriterWriteAdaptor(writer)) { + Ok(_) => Ok(()), + Err(consensus::encode::Error::Io(e)) => Err(e), + Err(_) => panic!("We shouldn't get a consensus::encode::Error unless our Write generated an std::io::Error"), + } + } +} + +impl Readable for Transaction { + fn read(r: &mut R) -> Result { + match consensus::encode::Decodable::consensus_decode(r) { + Ok(t) => Ok(t), + Err(consensus::encode::Error::Io(ref e)) if e.kind() == ::std::io::ErrorKind::UnexpectedEof => Err(DecodeError::ShortRead), + Err(consensus::encode::Error::Io(e)) => Err(DecodeError::Io(e)), + Err(_) => Err(DecodeError::InvalidValue), + } + } +} + impl> Readable for Mutex { fn read(r: &mut R) -> Result { let t: T = Readable::read(r)?; -- 2.39.5