Skip to content

Commit d6e644d

Browse files
committed
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
1 parent f52b477 commit d6e644d

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

lightning/src/util/ser.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ use std::cmp;
1111
use secp256k1::Signature;
1212
use secp256k1::key::{PublicKey, SecretKey};
1313
use bitcoin::blockdata::script::Script;
14-
use bitcoin::blockdata::transaction::OutPoint;
14+
use bitcoin::blockdata::transaction::{OutPoint, Transaction};
15+
use bitcoin::consensus;
16+
use bitcoin::consensus::Encodable;
1517
use bitcoin_hashes::sha256d::Hash as Sha256dHash;
1618
use std::marker::Sized;
1719
use ln::msgs::DecodeError;
@@ -625,6 +627,27 @@ impl<R: Read> Readable<R> for OutPoint {
625627
}
626628
}
627629

630+
impl Writeable for Transaction {
631+
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
632+
match self.consensus_encode(WriterWriteAdaptor(writer)) {
633+
Ok(_) => Ok(()),
634+
Err(consensus::encode::Error::Io(e)) => Err(e),
635+
Err(_) => panic!("We shouldn't get a consensus::encode::Error unless our Write generated an std::io::Error"),
636+
}
637+
}
638+
}
639+
640+
impl<R: Read> Readable<R> for Transaction {
641+
fn read(r: &mut R) -> Result<Self, DecodeError> {
642+
match consensus::encode::Decodable::consensus_decode(r) {
643+
Ok(t) => Ok(t),
644+
Err(consensus::encode::Error::Io(ref e)) if e.kind() == ::std::io::ErrorKind::UnexpectedEof => Err(DecodeError::ShortRead),
645+
Err(consensus::encode::Error::Io(e)) => Err(DecodeError::Io(e)),
646+
Err(_) => Err(DecodeError::InvalidValue),
647+
}
648+
}
649+
}
650+
628651
impl<R: Read, T: Readable<R>> Readable<R> for Mutex<T> {
629652
fn read(r: &mut R) -> Result<Self, DecodeError> {
630653
let t: T = Readable::read(r)?;

0 commit comments

Comments
 (0)