From 7556c3d923181e1df80273748738e5fd49ff5c67 Mon Sep 17 00:00:00 2001 From: Dr Maxim Orlovsky Date: Tue, 14 Mar 2023 12:20:34 +0100 Subject: [PATCH] fix encoding of bitcoin scripts --- src/bitcoin.rs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/bitcoin.rs b/src/bitcoin.rs index 22826293..974dabf3 100644 --- a/src/bitcoin.rs +++ b/src/bitcoin.rs @@ -90,15 +90,17 @@ impl Strategy for hlc::HashPreimage { impl LightningEncode for Script { #[inline] - fn lightning_encode(&self, e: E) -> Result { - self.as_bytes().lightning_encode(e) + fn lightning_encode(&self, mut e: E) -> Result { + e.write_all(self.as_bytes())?; + Ok(self.len()) } } impl LightningDecode for Script { - fn lightning_decode(d: D) -> Result { - let value = Vec::::lightning_decode(d)?; - let bytes = consensus::serialize(&value); + fn lightning_decode(mut d: D) -> Result { + let mut buf = vec![]; + d.read_to_end(&mut buf)?; + let bytes = consensus::serialize(&buf); consensus::deserialize(&bytes) .map_err(|err| Error::DataIntegrityError(err.to_string())) } @@ -124,10 +126,11 @@ mod test { fn real_clightning_scriptpubkey() { // Real scriptpubkey sent by clightning let msg_recv = [ - 0, 22, 0, 20, 42, 238, 172, 27, 222, 161, 61, 181, 251, 208, 97, + 0u8, 22, 0, 20, 42, 238, 172, 27, 222, 161, 61, 181, 251, 208, 97, 79, 71, 255, 98, 8, 213, 205, 114, 94, ]; - PubkeyScript::lightning_deserialize(&msg_recv).unwrap(); + let script = PubkeyScript::lightning_deserialize(&msg_recv).unwrap(); + assert_eq!(script.lightning_serialize().unwrap(), msg_recv); } }