diff --git a/bitcoin/src/bip152.rs b/bitcoin/src/bip152.rs index 8a6cc296..b9da6710 100644 --- a/bitcoin/src/bip152.rs +++ b/bitcoin/src/bip152.rs @@ -12,6 +12,7 @@ use std::error; use hashes::{sha256, siphash24, Hash}; use internals::impl_array_newtype; +use io::{Read, Write}; use crate::consensus::encode::{self, Decodable, Encodable, VarInt}; use crate::internal_macros::{impl_bytes_newtype, impl_consensus_encoding}; @@ -73,14 +74,14 @@ impl convert::AsRef for PrefilledTransaction { impl Encodable for PrefilledTransaction { #[inline] - fn consensus_encode(&self, w: &mut W) -> Result { + fn consensus_encode(&self, w: &mut W) -> Result { Ok(VarInt::from(self.idx).consensus_encode(w)? + self.tx.consensus_encode(w)?) } } impl Decodable for PrefilledTransaction { #[inline] - fn consensus_decode(r: &mut R) -> Result { + fn consensus_decode(r: &mut R) -> Result { let idx = VarInt::consensus_decode(r)?.0; let idx = u16::try_from(idx) .map_err(|_| encode::Error::ParseFailed("BIP152 prefilled tx index out of bounds"))?; @@ -129,14 +130,14 @@ impl ShortId { impl Encodable for ShortId { #[inline] - fn consensus_encode(&self, w: &mut W) -> Result { + fn consensus_encode(&self, w: &mut W) -> Result { self.0.consensus_encode(w) } } impl Decodable for ShortId { #[inline] - fn consensus_decode(r: &mut R) -> Result { + fn consensus_decode(r: &mut R) -> Result { Ok(ShortId(Decodable::consensus_decode(r)?)) } } @@ -258,7 +259,7 @@ impl Encodable for BlockTransactionsRequest { /// /// Panics if the index overflows [`u64::MAX`]. This happens when [`BlockTransactionsRequest::indexes`] /// contains an entry with the value [`u64::MAX`] as `u64` overflows during differential encoding. - fn consensus_encode(&self, w: &mut W) -> Result { + fn consensus_encode(&self, w: &mut W) -> Result { let mut len = self.block_hash.consensus_encode(w)?; // Manually encode indexes because they are differentially encoded VarInts. len += VarInt(self.indexes.len() as u64).consensus_encode(w)?; @@ -272,7 +273,7 @@ impl Encodable for BlockTransactionsRequest { } impl Decodable for BlockTransactionsRequest { - fn consensus_decode(r: &mut R) -> Result { + fn consensus_decode(r: &mut R) -> Result { Ok(BlockTransactionsRequest { block_hash: BlockHash::consensus_decode(r)?, indexes: { diff --git a/bitcoin/src/bip158.rs b/bitcoin/src/bip158.rs index 88875966..57b2f4d3 100644 --- a/bitcoin/src/bip158.rs +++ b/bitcoin/src/bip158.rs @@ -43,6 +43,7 @@ use core::fmt::{self, Display, Formatter}; use hashes::{sha256d, siphash24, Hash}; use internals::write_err; +use io::{Read, Write}; use crate::blockdata::block::{Block, BlockHash}; use crate::blockdata::script::Script; @@ -175,7 +176,7 @@ pub struct BlockFilterWriter<'a, W> { writer: GcsFilterWriter<'a, W>, } -impl<'a, W: io::Write> BlockFilterWriter<'a, W> { +impl<'a, W: Write> BlockFilterWriter<'a, W> { /// Creates a new [`BlockFilterWriter`] from `block`. pub fn new(writer: &'a mut W, block: &'a Block) -> BlockFilterWriter<'a, W> { let block_hash_as_int = block.block_hash().to_byte_array(); @@ -244,7 +245,7 @@ impl BlockFilterReader { where I: Iterator, I::Item: Borrow<[u8]>, - R: io::Read + ?Sized, + R: Read + ?Sized, { self.reader.match_any(reader, query) } @@ -254,7 +255,7 @@ impl BlockFilterReader { where I: Iterator, I::Item: Borrow<[u8]>, - R: io::Read + ?Sized, + R: Read + ?Sized, { self.reader.match_all(reader, query) } @@ -277,7 +278,7 @@ impl GcsFilterReader { where I: Iterator, I::Item: Borrow<[u8]>, - R: io::Read + ?Sized, + R: Read + ?Sized, { let n_elements: VarInt = Decodable::consensus_decode(reader).unwrap_or(VarInt(0)); // map hashes to [0, n_elements << grp] @@ -320,7 +321,7 @@ impl GcsFilterReader { where I: Iterator, I::Item: Borrow<[u8]>, - R: io::Read + ?Sized, + R: Read + ?Sized, { let n_elements: VarInt = Decodable::consensus_decode(reader).unwrap_or(VarInt(0)); // map hashes to [0, n_elements << grp] @@ -371,7 +372,7 @@ pub struct GcsFilterWriter<'a, W> { m: u64, } -impl<'a, W: io::Write> GcsFilterWriter<'a, W> { +impl<'a, W: Write> GcsFilterWriter<'a, W> { /// Creates a new [`GcsFilterWriter`] wrapping a generic writer, with specific seed to siphash. pub fn new(writer: &'a mut W, k0: u64, k1: u64, m: u64, p: u8) -> GcsFilterWriter<'a, W> { GcsFilterWriter { filter: GcsFilter::new(k0, k1, p), writer, elements: BTreeSet::new(), m } @@ -429,7 +430,7 @@ impl GcsFilter { n: u64, ) -> Result where - W: io::Write, + W: Write, { let mut wrote = 0; let mut q = n >> self.p; @@ -446,7 +447,7 @@ impl GcsFilter { /// Golomb-Rice decodes a number from a bit stream (parameter 2^k). fn golomb_rice_decode(&self, reader: &mut BitStreamReader) -> Result where - R: io::Read + ?Sized, + R: Read + ?Sized, { let mut q = 0u64; while reader.read(1)? == 1 { @@ -469,7 +470,7 @@ pub struct BitStreamReader<'a, R: ?Sized> { reader: &'a mut R, } -impl<'a, R: io::Read + ?Sized> BitStreamReader<'a, R> { +impl<'a, R: Read + ?Sized> BitStreamReader<'a, R> { /// Creates a new [`BitStreamReader`] that reads bitwise from a given `reader`. pub fn new(reader: &'a mut R) -> BitStreamReader<'a, R> { BitStreamReader { buffer: [0u8], reader, offset: 8 } @@ -516,7 +517,7 @@ pub struct BitStreamWriter<'a, W> { writer: &'a mut W, } -impl<'a, W: io::Write> BitStreamWriter<'a, W> { +impl<'a, W: Write> BitStreamWriter<'a, W> { /// Creates a new [`BitStreamWriter`] that writes bitwise to a given `writer`. pub fn new(writer: &'a mut W) -> BitStreamWriter<'a, W> { BitStreamWriter { buffer: [0u8], writer, offset: 0 } diff --git a/bitcoin/src/blockdata/block.rs b/bitcoin/src/blockdata/block.rs index 50a66f41..e66d5f22 100644 --- a/bitcoin/src/blockdata/block.rs +++ b/bitcoin/src/blockdata/block.rs @@ -11,6 +11,7 @@ use core::fmt; use hashes::{sha256d, Hash, HashEngine}; +use io::{Read, Write}; use super::Weight; use crate::blockdata::script; @@ -200,13 +201,13 @@ impl Default for Version { } impl Encodable for Version { - fn consensus_encode(&self, w: &mut W) -> Result { + fn consensus_encode(&self, w: &mut W) -> Result { self.0.consensus_encode(w) } } impl Decodable for Version { - fn consensus_decode(r: &mut R) -> Result { + fn consensus_decode(r: &mut R) -> Result { Decodable::consensus_decode(r).map(Version) } } diff --git a/bitcoin/src/blockdata/script/mod.rs b/bitcoin/src/blockdata/script/mod.rs index 05cf2957..c48e9a57 100644 --- a/bitcoin/src/blockdata/script/mod.rs +++ b/bitcoin/src/blockdata/script/mod.rs @@ -66,6 +66,7 @@ use core::fmt; use core::ops::{Deref, DerefMut}; use hashes::{hash160, sha256}; +use io::{Read, Write}; #[cfg(feature = "serde")] use serde; @@ -579,21 +580,21 @@ impl<'de> serde::Deserialize<'de> for ScriptBuf { impl Encodable for Script { #[inline] - fn consensus_encode(&self, w: &mut W) -> Result { + fn consensus_encode(&self, w: &mut W) -> Result { crate::consensus::encode::consensus_encode_with_size(&self.0, w) } } impl Encodable for ScriptBuf { #[inline] - fn consensus_encode(&self, w: &mut W) -> Result { + fn consensus_encode(&self, w: &mut W) -> Result { self.0.consensus_encode(w) } } impl Decodable for ScriptBuf { #[inline] - fn consensus_decode_from_finite_reader( + fn consensus_decode_from_finite_reader( r: &mut R, ) -> Result { Ok(ScriptBuf(Decodable::consensus_decode_from_finite_reader(r)?)) diff --git a/bitcoin/src/blockdata/transaction.rs b/bitcoin/src/blockdata/transaction.rs index 7a617be5..62daaef6 100644 --- a/bitcoin/src/blockdata/transaction.rs +++ b/bitcoin/src/blockdata/transaction.rs @@ -16,6 +16,7 @@ use core::{cmp, fmt, str}; use hashes::{self, sha256d, Hash}; use internals::write_err; +use io::{Read, Write}; use super::Weight; use crate::blockdata::fee_rate::FeeRate; @@ -1024,13 +1025,13 @@ impl Version { } impl Encodable for Version { - fn consensus_encode(&self, w: &mut W) -> Result { + fn consensus_encode(&self, w: &mut W) -> Result { self.0.consensus_encode(w) } } impl Decodable for Version { - fn consensus_decode(r: &mut R) -> Result { + fn consensus_decode(r: &mut R) -> Result { Decodable::consensus_decode(r).map(Version) } } @@ -1038,13 +1039,13 @@ impl Decodable for Version { impl_consensus_encoding!(TxOut, value, script_pubkey); impl Encodable for OutPoint { - fn consensus_encode(&self, w: &mut W) -> Result { + fn consensus_encode(&self, w: &mut W) -> Result { let len = self.txid.consensus_encode(w)?; Ok(len + self.vout.consensus_encode(w)?) } } impl Decodable for OutPoint { - fn consensus_decode(r: &mut R) -> Result { + fn consensus_decode(r: &mut R) -> Result { Ok(OutPoint { txid: Decodable::consensus_decode(r)?, vout: Decodable::consensus_decode(r)?, @@ -1053,7 +1054,7 @@ impl Decodable for OutPoint { } impl Encodable for TxIn { - fn consensus_encode(&self, w: &mut W) -> Result { + fn consensus_encode(&self, w: &mut W) -> Result { let mut len = 0; len += self.previous_output.consensus_encode(w)?; len += self.script_sig.consensus_encode(w)?; @@ -1063,7 +1064,7 @@ impl Encodable for TxIn { } impl Decodable for TxIn { #[inline] - fn consensus_decode_from_finite_reader( + fn consensus_decode_from_finite_reader( r: &mut R, ) -> Result { Ok(TxIn { @@ -1076,19 +1077,19 @@ impl Decodable for TxIn { } impl Encodable for Sequence { - fn consensus_encode(&self, w: &mut W) -> Result { + fn consensus_encode(&self, w: &mut W) -> Result { self.0.consensus_encode(w) } } impl Decodable for Sequence { - fn consensus_decode(r: &mut R) -> Result { + fn consensus_decode(r: &mut R) -> Result { Decodable::consensus_decode(r).map(Sequence) } } impl Encodable for Transaction { - fn consensus_encode(&self, w: &mut W) -> Result { + fn consensus_encode(&self, w: &mut W) -> Result { let mut len = 0; len += self.version.consensus_encode(w)?; @@ -1112,7 +1113,7 @@ impl Encodable for Transaction { } impl Decodable for Transaction { - fn consensus_decode_from_finite_reader( + fn consensus_decode_from_finite_reader( r: &mut R, ) -> Result { let version = Version::consensus_decode_from_finite_reader(r)?; diff --git a/bitcoin/src/consensus/encode.rs b/bitcoin/src/consensus/encode.rs index 8253eaa7..f7d0f6a4 100644 --- a/bitcoin/src/consensus/encode.rs +++ b/bitcoin/src/consensus/encode.rs @@ -20,7 +20,7 @@ use core::{fmt, mem, u32}; use hashes::{sha256, sha256d, Hash}; use internals::write_err; -use io::{Cursor, Read}; +use io::{Cursor, Read, Write}; use crate::bip152::{PrefilledTransaction, ShortId}; use crate::bip158::{FilterHash, FilterHeader}; @@ -137,7 +137,7 @@ pub fn deserialize_partial(data: &[u8]) -> Result<(T, usize), Erro } /// Extensions of `Write` to encode data as per Bitcoin consensus. -pub trait WriteExt: io::Write { +pub trait WriteExt: Write { /// Outputs a 64-bit unsigned integer. fn emit_u64(&mut self, v: u64) -> Result<(), io::Error>; /// Outputs a 32-bit unsigned integer. @@ -164,7 +164,7 @@ pub trait WriteExt: io::Write { } /// Extensions of `Read` to decode data as per Bitcoin consensus. -pub trait ReadExt: io::Read { +pub trait ReadExt: Read { /// Reads a 64-bit unsigned integer. fn read_u64(&mut self) -> Result; /// Reads a 32-bit unsigned integer. @@ -210,7 +210,7 @@ macro_rules! decoder_fn { }; } -impl WriteExt for W { +impl WriteExt for W { encoder_fn!(emit_u64, u64); encoder_fn!(emit_u32, u32); encoder_fn!(emit_u16, u16); @@ -267,7 +267,7 @@ pub trait Encodable { /// /// The number of bytes written on success. The only errors returned are errors propagated from /// the writer. - fn consensus_encode(&self, writer: &mut W) -> Result; + fn consensus_encode(&self, writer: &mut W) -> Result; } /// Data which can be encoded in a consensus-consistent way. @@ -301,7 +301,7 @@ pub trait Decodable: Sized { /// avoid creating redundant `Take` wrappers. Failure to do so might result only in a tiny /// performance hit. #[inline] - fn consensus_decode_from_finite_reader( + fn consensus_decode_from_finite_reader( reader: &mut R, ) -> Result { // This method is always strictly less general than, `consensus_decode`, so it's safe and @@ -319,7 +319,7 @@ pub trait Decodable: Sized { /// for types that override [`Self::consensus_decode_from_finite_reader`] /// instead. #[inline] - fn consensus_decode(reader: &mut R) -> Result { + fn consensus_decode(reader: &mut R) -> Result { Self::consensus_decode_from_finite_reader(&mut reader.take(MAX_VEC_SIZE as u64)) } } @@ -357,13 +357,13 @@ macro_rules! impl_int_encodable { ($ty:ident, $meth_dec:ident, $meth_enc:ident) => { impl Decodable for $ty { #[inline] - fn consensus_decode(r: &mut R) -> Result { + fn consensus_decode(r: &mut R) -> Result { ReadExt::$meth_dec(r) } } impl Encodable for $ty { #[inline] - fn consensus_encode( + fn consensus_encode( &self, w: &mut W, ) -> Result { @@ -416,7 +416,7 @@ impl_var_int_from!(u8, u16, u32, u64, usize); impl Encodable for VarInt { #[inline] - fn consensus_encode(&self, w: &mut W) -> Result { + fn consensus_encode(&self, w: &mut W) -> Result { match self.0 { 0..=0xFC => { (self.0 as u8).consensus_encode(w)?; @@ -443,7 +443,7 @@ impl Encodable for VarInt { impl Decodable for VarInt { #[inline] - fn consensus_decode(r: &mut R) -> Result { + fn consensus_decode(r: &mut R) -> Result { let n = ReadExt::read_u8(r)?; match n { 0xFF => { @@ -477,7 +477,7 @@ impl Decodable for VarInt { impl Encodable for bool { #[inline] - fn consensus_encode(&self, w: &mut W) -> Result { + fn consensus_encode(&self, w: &mut W) -> Result { w.emit_bool(*self)?; Ok(1) } @@ -485,14 +485,14 @@ impl Encodable for bool { impl Decodable for bool { #[inline] - fn consensus_decode(r: &mut R) -> Result { + fn consensus_decode(r: &mut R) -> Result { ReadExt::read_bool(r) } } impl Encodable for String { #[inline] - fn consensus_encode(&self, w: &mut W) -> Result { + fn consensus_encode(&self, w: &mut W) -> Result { let b = self.as_bytes(); let vi_len = VarInt(b.len() as u64).consensus_encode(w)?; w.emit_slice(b)?; @@ -502,7 +502,7 @@ impl Encodable for String { impl Decodable for String { #[inline] - fn consensus_decode(r: &mut R) -> Result { + fn consensus_decode(r: &mut R) -> Result { String::from_utf8(Decodable::consensus_decode(r)?) .map_err(|_| self::Error::ParseFailed("String was not valid UTF8")) } @@ -510,7 +510,7 @@ impl Decodable for String { impl Encodable for Cow<'static, str> { #[inline] - fn consensus_encode(&self, w: &mut W) -> Result { + fn consensus_encode(&self, w: &mut W) -> Result { let b = self.as_bytes(); let vi_len = VarInt(b.len() as u64).consensus_encode(w)?; w.emit_slice(b)?; @@ -520,7 +520,7 @@ impl Encodable for Cow<'static, str> { impl Decodable for Cow<'static, str> { #[inline] - fn consensus_decode(r: &mut R) -> Result, Error> { + fn consensus_decode(r: &mut R) -> Result, Error> { String::from_utf8(Decodable::consensus_decode(r)?) .map_err(|_| self::Error::ParseFailed("String was not valid UTF8")) .map(Cow::Owned) @@ -542,7 +542,7 @@ macro_rules! impl_array { impl Decodable for [u8; $size] { #[inline] - fn consensus_decode(r: &mut R) -> Result { + fn consensus_decode(r: &mut R) -> Result { let mut ret = [0; $size]; r.read_slice(&mut ret)?; Ok(ret) @@ -563,7 +563,7 @@ impl_array!(33); impl Decodable for [u16; 8] { #[inline] - fn consensus_decode(r: &mut R) -> Result { + fn consensus_decode(r: &mut R) -> Result { let mut res = [0; 8]; for item in &mut res { *item = Decodable::consensus_decode(r)?; @@ -574,7 +574,7 @@ impl Decodable for [u16; 8] { impl Encodable for [u16; 8] { #[inline] - fn consensus_encode(&self, w: &mut W) -> Result { + fn consensus_encode(&self, w: &mut W) -> Result { for c in self.iter() { c.consensus_encode(w)?; } @@ -586,7 +586,7 @@ macro_rules! impl_vec { ($type: ty) => { impl Encodable for Vec<$type> { #[inline] - fn consensus_encode( + fn consensus_encode( &self, w: &mut W, ) -> Result { @@ -601,7 +601,7 @@ macro_rules! impl_vec { impl Decodable for Vec<$type> { #[inline] - fn consensus_decode_from_finite_reader( + fn consensus_decode_from_finite_reader( r: &mut R, ) -> Result { let len = VarInt::consensus_decode_from_finite_reader(r)?.0; @@ -643,7 +643,7 @@ impl_vec!((u32, Address)); #[cfg(feature = "std")] impl_vec!(AddrV2Message); -pub(crate) fn consensus_encode_with_size( +pub(crate) fn consensus_encode_with_size( data: &[u8], w: &mut W, ) -> Result { @@ -662,7 +662,7 @@ struct ReadBytesFromFiniteReaderOpts { /// This function relies on reader being bound in amount of data /// it returns for OOM protection. See [`Decodable::consensus_decode_from_finite_reader`]. #[inline] -fn read_bytes_from_finite_reader( +fn read_bytes_from_finite_reader( d: &mut D, mut opts: ReadBytesFromFiniteReaderOpts, ) -> Result, Error> { @@ -684,14 +684,14 @@ fn read_bytes_from_finite_reader( impl Encodable for Vec { #[inline] - fn consensus_encode(&self, w: &mut W) -> Result { + fn consensus_encode(&self, w: &mut W) -> Result { consensus_encode_with_size(self, w) } } impl Decodable for Vec { #[inline] - fn consensus_decode_from_finite_reader(r: &mut R) -> Result { + fn consensus_decode_from_finite_reader(r: &mut R) -> Result { let len = VarInt::consensus_decode(r)?.0 as usize; // most real-world vec of bytes data, wouldn't be larger than 128KiB let opts = ReadBytesFromFiniteReaderOpts { len, chunk_size: 128 * 1024 }; @@ -701,14 +701,14 @@ impl Decodable for Vec { impl Encodable for Box<[u8]> { #[inline] - fn consensus_encode(&self, w: &mut W) -> Result { + fn consensus_encode(&self, w: &mut W) -> Result { consensus_encode_with_size(self, w) } } impl Decodable for Box<[u8]> { #[inline] - fn consensus_decode_from_finite_reader(r: &mut R) -> Result { + fn consensus_decode_from_finite_reader(r: &mut R) -> Result { >::consensus_decode_from_finite_reader(r).map(From::from) } } @@ -721,7 +721,7 @@ fn sha2_checksum(data: &[u8]) -> [u8; 4] { impl Encodable for CheckedData { #[inline] - fn consensus_encode(&self, w: &mut W) -> Result { + fn consensus_encode(&self, w: &mut W) -> Result { u32::try_from(self.data.len()) .expect("network message use u32 as length") .consensus_encode(w)?; @@ -733,7 +733,7 @@ impl Encodable for CheckedData { impl Decodable for CheckedData { #[inline] - fn consensus_decode_from_finite_reader(r: &mut R) -> Result { + fn consensus_decode_from_finite_reader(r: &mut R) -> Result { let len = u32::consensus_decode_from_finite_reader(r)? as usize; let checksum = <[u8; 4]>::consensus_decode_from_finite_reader(r)?; @@ -749,19 +749,19 @@ impl Decodable for CheckedData { } impl<'a, T: Encodable> Encodable for &'a T { - fn consensus_encode(&self, w: &mut W) -> Result { + fn consensus_encode(&self, w: &mut W) -> Result { (**self).consensus_encode(w) } } impl<'a, T: Encodable> Encodable for &'a mut T { - fn consensus_encode(&self, w: &mut W) -> Result { + fn consensus_encode(&self, w: &mut W) -> Result { (**self).consensus_encode(w) } } impl Encodable for rc::Rc { - fn consensus_encode(&self, w: &mut W) -> Result { + fn consensus_encode(&self, w: &mut W) -> Result { (**self).consensus_encode(w) } } @@ -769,7 +769,7 @@ impl Encodable for rc::Rc { /// Note: This will fail to compile on old Rust for targets that don't support atomics #[cfg(any(not(rust_v_1_60), target_has_atomic = "ptr"))] impl Encodable for sync::Arc { - fn consensus_encode(&self, w: &mut W) -> Result { + fn consensus_encode(&self, w: &mut W) -> Result { (**self).consensus_encode(w) } } @@ -779,7 +779,7 @@ macro_rules! tuple_encode { impl <$($x: Encodable),*> Encodable for ($($x),*) { #[inline] #[allow(non_snake_case)] - fn consensus_encode( + fn consensus_encode( &self, w: &mut W, ) -> Result { @@ -793,7 +793,7 @@ macro_rules! tuple_encode { impl<$($x: Decodable),*> Decodable for ($($x),*) { #[inline] #[allow(non_snake_case)] - fn consensus_decode(r: &mut R) -> Result { + fn consensus_decode(r: &mut R) -> Result { Ok(($({let $x = Decodable::consensus_decode(r)?; $x }),*)) } } @@ -809,37 +809,37 @@ tuple_encode!(T0, T1, T2, T3, T4, T5, T6); tuple_encode!(T0, T1, T2, T3, T4, T5, T6, T7); impl Encodable for sha256d::Hash { - fn consensus_encode(&self, w: &mut W) -> Result { + fn consensus_encode(&self, w: &mut W) -> Result { self.as_byte_array().consensus_encode(w) } } impl Decodable for sha256d::Hash { - fn consensus_decode(r: &mut R) -> Result { + fn consensus_decode(r: &mut R) -> Result { Ok(Self::from_byte_array(<::Bytes>::consensus_decode(r)?)) } } impl Encodable for sha256::Hash { - fn consensus_encode(&self, w: &mut W) -> Result { + fn consensus_encode(&self, w: &mut W) -> Result { self.as_byte_array().consensus_encode(w) } } impl Decodable for sha256::Hash { - fn consensus_decode(r: &mut R) -> Result { + fn consensus_decode(r: &mut R) -> Result { Ok(Self::from_byte_array(<::Bytes>::consensus_decode(r)?)) } } impl Encodable for TapLeafHash { - fn consensus_encode(&self, w: &mut W) -> Result { + fn consensus_encode(&self, w: &mut W) -> Result { self.as_byte_array().consensus_encode(w) } } impl Decodable for TapLeafHash { - fn consensus_decode(r: &mut R) -> Result { + fn consensus_decode(r: &mut R) -> Result { Ok(Self::from_byte_array(<::Bytes>::consensus_decode(r)?)) } } diff --git a/bitcoin/src/consensus/serde.rs b/bitcoin/src/consensus/serde.rs index 98eb0585..0fa8578f 100644 --- a/bitcoin/src/consensus/serde.rs +++ b/bitcoin/src/consensus/serde.rs @@ -15,6 +15,7 @@ use core::marker::PhantomData; use serde::de::{SeqAccess, Unexpected, Visitor}; use serde::ser::SerializeSeq; use serde::{Deserializer, Serializer}; +use io::{Read, Write}; use super::encode::Error as ConsensusError; use super::{Decodable, Encodable}; @@ -268,7 +269,7 @@ impl<'a, W: fmt::Write, E: EncodeBytes> IoWrapper<'a, W, E> { fn actually_flush(&mut self) -> fmt::Result { self.encoder.flush(&mut self.writer) } } -impl<'a, W: fmt::Write, E: EncodeBytes> io::Write for IoWrapper<'a, W, E> { +impl<'a, W: fmt::Write, E: EncodeBytes> Write for IoWrapper<'a, W, E> { fn write(&mut self, bytes: &[u8]) -> io::Result { match self.encoder.encode_chunk(&mut self.writer, bytes) { Ok(()) => Ok(bytes.len()), @@ -338,7 +339,7 @@ struct BinWriter { error: Option, } -impl io::Write for BinWriter { +impl Write for BinWriter { fn write(&mut self, buf: &[u8]) -> io::Result { self.write_all(buf).map(|_| buf.len()) } fn write_all(&mut self, buf: &[u8]) -> io::Result<()> { @@ -441,7 +442,7 @@ impl>> IterReader { } } -impl>> io::Read for IterReader { +impl>> Read for IterReader { fn read(&mut self, buf: &mut [u8]) -> io::Result { let mut count = 0; for (dst, src) in buf.iter_mut().zip(&mut self.iterator) { diff --git a/bitcoin/src/crypto/key.rs b/bitcoin/src/crypto/key.rs index a4378011..c623aceb 100644 --- a/bitcoin/src/crypto/key.rs +++ b/bitcoin/src/crypto/key.rs @@ -11,6 +11,7 @@ use core::str::FromStr; use hashes::{hash160, Hash}; use hex::FromHex; +use io::{Read, Write}; use internals::write_err; use crate::crypto::ecdsa; @@ -70,7 +71,7 @@ impl PublicKey { } /// Write the public key into a writer - pub fn write_into(&self, writer: &mut W) -> Result<(), io::Error> { + pub fn write_into(&self, writer: &mut W) -> Result<(), io::Error> { self.with_serialized(|bytes| writer.write_all(bytes)) } @@ -78,7 +79,7 @@ impl PublicKey { /// /// This internally reads the first byte before reading the rest, so /// use of a `BufReader` is recommended. - pub fn read_from(reader: &mut R) -> Result { + pub fn read_from(reader: &mut R) -> Result { let mut bytes = [0; 65]; reader.read_exact(&mut bytes[0..1])?; diff --git a/bitcoin/src/crypto/sighash.rs b/bitcoin/src/crypto/sighash.rs index db6e380a..4b805281 100644 --- a/bitcoin/src/crypto/sighash.rs +++ b/bitcoin/src/crypto/sighash.rs @@ -16,12 +16,13 @@ use core::{fmt, str}; use hashes::{hash_newtype, sha256, sha256d, sha256t_hash_newtype, Hash}; use internals::write_err; +use io::Write; use crate::blockdata::witness::Witness; use crate::consensus::{encode, Encodable}; use crate::prelude::*; use crate::taproot::{LeafVersion, TapLeafHash, TAPROOT_ANNEX_PREFIX}; -use crate::{io, Amount, Script, ScriptBuf, Sequence, Transaction, TxIn, TxOut}; +use crate::{Amount, Script, ScriptBuf, Sequence, Transaction, TxIn, TxOut}; /// Used for signature hash for invalid use of SIGHASH_SINGLE. #[rustfmt::skip] @@ -673,7 +674,7 @@ impl> SighashCache { /// Encodes the BIP341 signing data for any flag type into a given object implementing the /// [`io::Write`] trait. - pub fn taproot_encode_signing_data_to>( + pub fn taproot_encode_signing_data_to>( &mut self, writer: &mut W, input_index: usize, @@ -860,7 +861,7 @@ impl> SighashCache { /// `script_code` is dependent on the type of the spend transaction. For p2wpkh use /// [`Script::p2wpkh_script_code`], for p2wsh just pass in the witness script. (Also see /// [`Self::p2wpkh_signature_hash`] and [`SighashCache::p2wsh_signature_hash`].) - pub fn segwit_v0_encode_signing_data_to( + pub fn segwit_v0_encode_signing_data_to( &mut self, writer: &mut W, input_index: usize, @@ -984,7 +985,7 @@ impl> SighashCache { /// /// This function can't handle the SIGHASH_SINGLE bug internally, so it returns [`EncodeSigningDataResult`] /// that must be handled by the caller (see [`EncodeSigningDataResult::is_sighash_single_bug`]). - pub fn legacy_encode_signing_data_to>( + pub fn legacy_encode_signing_data_to>( &self, writer: &mut W, input_index: usize, @@ -1011,7 +1012,7 @@ impl> SighashCache { return EncodeSigningDataResult::SighashSingleBug; } - fn encode_signing_data_to_inner( + fn encode_signing_data_to_inner( self_: &Transaction, writer: &mut W, input_index: usize, @@ -1239,7 +1240,7 @@ impl<'a> Annex<'a> { } impl<'a> Encodable for Annex<'a> { - fn consensus_encode(&self, w: &mut W) -> Result { + fn consensus_encode(&self, w: &mut W) -> Result { encode::consensus_encode_with_size(self.0, w) } } diff --git a/bitcoin/src/lib.rs b/bitcoin/src/lib.rs index d65fe1e6..a2eb28f5 100644 --- a/bitcoin/src/lib.rs +++ b/bitcoin/src/lib.rs @@ -142,7 +142,7 @@ pub use crate::{ #[cfg(not(feature = "std"))] mod io_extras { - use crate::io; + use crate::io::Write; /// A writer which will move data into the void. pub struct Sink { @@ -152,7 +152,7 @@ mod io_extras { /// Creates an instance of a writer which will successfully consume all data. pub const fn sink() -> Sink { Sink { _priv: () } } - impl io::Write for Sink { + impl Write for Sink { #[inline] fn write(&mut self, buf: &[u8]) -> io::Result { Ok(buf.len()) } diff --git a/bitcoin/src/merkle_tree/block.rs b/bitcoin/src/merkle_tree/block.rs index 79fedf02..3194454a 100644 --- a/bitcoin/src/merkle_tree/block.rs +++ b/bitcoin/src/merkle_tree/block.rs @@ -41,6 +41,7 @@ use core::fmt; use hashes::Hash; +use io::{Read, Write}; use self::MerkleBlockError::*; use crate::blockdata::block::{self, Block, TxMerkleNode}; @@ -143,14 +144,14 @@ impl MerkleBlock { } impl Encodable for MerkleBlock { - fn consensus_encode(&self, w: &mut W) -> Result { + fn consensus_encode(&self, w: &mut W) -> Result { let len = self.header.consensus_encode(w)? + self.txn.consensus_encode(w)?; Ok(len) } } impl Decodable for MerkleBlock { - fn consensus_decode(r: &mut R) -> Result { + fn consensus_decode(r: &mut R) -> Result { Ok(MerkleBlock { header: Decodable::consensus_decode(r)?, txn: Decodable::consensus_decode(r)?, @@ -433,7 +434,7 @@ impl PartialMerkleTree { } impl Encodable for PartialMerkleTree { - fn consensus_encode(&self, w: &mut W) -> Result { + fn consensus_encode(&self, w: &mut W) -> Result { let mut ret = self.num_transactions.consensus_encode(w)?; ret += self.hashes.consensus_encode(w)?; @@ -451,7 +452,7 @@ impl Encodable for PartialMerkleTree { } impl Decodable for PartialMerkleTree { - fn consensus_decode_from_finite_reader( + fn consensus_decode_from_finite_reader( r: &mut R, ) -> Result { let num_transactions: u32 = Decodable::consensus_decode(r)?; diff --git a/bitcoin/src/merkle_tree/mod.rs b/bitcoin/src/merkle_tree/mod.rs index 71159a78..0e79117a 100644 --- a/bitcoin/src/merkle_tree/mod.rs +++ b/bitcoin/src/merkle_tree/mod.rs @@ -19,6 +19,7 @@ use core::cmp::min; use core::iter; use hashes::Hash; +use io::Write; use crate::consensus::encode::Encodable; use crate::prelude::*; @@ -40,7 +41,7 @@ pub use self::block::{MerkleBlock, MerkleBlockError, PartialMerkleTree}; pub fn calculate_root_inline(hashes: &mut [T]) -> Option where T: Hash + Encodable, - ::Engine: io::Write, + ::Engine: Write, { match hashes.len() { 0 => None, @@ -58,7 +59,7 @@ where pub fn calculate_root(mut hashes: I) -> Option where T: Hash + Encodable, - ::Engine: io::Write, + ::Engine: Write, I: Iterator, { let first = hashes.next()?; @@ -90,7 +91,7 @@ where fn merkle_root_r(hashes: &mut [T]) -> T where T: Hash + Encodable, - ::Engine: io::Write, + ::Engine: Write, { if hashes.len() == 1 { return hashes[0]; diff --git a/bitcoin/src/p2p/address.rs b/bitcoin/src/p2p/address.rs index bb7c9a07..014cadd4 100644 --- a/bitcoin/src/p2p/address.rs +++ b/bitcoin/src/p2p/address.rs @@ -9,6 +9,8 @@ use core::{fmt, iter}; use std::net::{Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6, ToSocketAddrs}; +use io::{Read, Write}; + use crate::consensus::encode::{self, Decodable, Encodable, ReadExt, VarInt, WriteExt}; use crate::p2p::ServiceFlags; use crate::prelude::*; @@ -56,7 +58,7 @@ impl Address { impl Encodable for Address { #[inline] - fn consensus_encode(&self, w: &mut W) -> Result { + fn consensus_encode(&self, w: &mut W) -> Result { let mut len = self.services.consensus_encode(w)?; for word in &self.address { @@ -73,7 +75,7 @@ impl Encodable for Address { impl Decodable for Address { #[inline] - fn consensus_decode(r: &mut R) -> Result { + fn consensus_decode(r: &mut R) -> Result { Ok(Address { services: Decodable::consensus_decode(r)?, address: read_be_address(r)?, @@ -83,12 +85,12 @@ impl Decodable for Address { } /// Read a big-endian address from reader. -fn read_be_address(r: &mut R) -> Result<[u16; 8], encode::Error> { +fn read_be_address(r: &mut R) -> Result<[u16; 8], encode::Error> { let mut address = [0u16; 8]; let mut buf = [0u8; 2]; for word in &mut address { - io::Read::read_exact(r, &mut buf)?; + Read::read_exact(r, &mut buf)?; *word = u16::from_be_bytes(buf) } Ok(address) @@ -140,8 +142,8 @@ pub enum AddrV2 { } impl Encodable for AddrV2 { - fn consensus_encode(&self, w: &mut W) -> Result { - fn encode_addr( + fn consensus_encode(&self, w: &mut W) -> Result { + fn encode_addr( w: &mut W, network: u8, bytes: &[u8], @@ -165,7 +167,7 @@ impl Encodable for AddrV2 { } impl Decodable for AddrV2 { - fn consensus_decode(r: &mut R) -> Result { + fn consensus_decode(r: &mut R) -> Result { let network_id = u8::consensus_decode(r)?; let len = VarInt::consensus_decode(r)?.0; if len > 512 { @@ -269,7 +271,7 @@ impl AddrV2Message { } impl Encodable for AddrV2Message { - fn consensus_encode(&self, w: &mut W) -> Result { + fn consensus_encode(&self, w: &mut W) -> Result { let mut len = 0; len += self.time.consensus_encode(w)?; len += VarInt(self.services.to_u64()).consensus_encode(w)?; @@ -283,7 +285,7 @@ impl Encodable for AddrV2Message { } impl Decodable for AddrV2Message { - fn consensus_decode(r: &mut R) -> Result { + fn consensus_decode(r: &mut R) -> Result { Ok(AddrV2Message { time: Decodable::consensus_decode(r)?, services: ServiceFlags::from(VarInt::consensus_decode(r)?.0), diff --git a/bitcoin/src/p2p/message.rs b/bitcoin/src/p2p/message.rs index f12dcb4f..2c8dc699 100644 --- a/bitcoin/src/p2p/message.rs +++ b/bitcoin/src/p2p/message.rs @@ -9,6 +9,7 @@ use core::{fmt, iter}; use hashes::{sha256d, Hash}; +use io::{Read, Write}; use crate::blockdata::{block, transaction}; use crate::consensus::encode::{self, CheckedData, Decodable, Encodable, VarInt}; @@ -98,7 +99,7 @@ impl AsRef for CommandString { impl Encodable for CommandString { #[inline] - fn consensus_encode(&self, w: &mut W) -> Result { + fn consensus_encode(&self, w: &mut W) -> Result { let mut rawbytes = [0u8; 12]; let strbytes = self.0.as_bytes(); debug_assert!(strbytes.len() <= 12); @@ -109,7 +110,7 @@ impl Encodable for CommandString { impl Decodable for CommandString { #[inline] - fn consensus_decode(r: &mut R) -> Result { + fn consensus_decode(r: &mut R) -> Result { let rawbytes: [u8; 12] = Decodable::consensus_decode(r)?; let rv = iter::FromIterator::from_iter(rawbytes.iter().filter_map(|&u| { if u > 0 { @@ -334,7 +335,7 @@ struct HeaderSerializationWrapper<'a>(&'a Vec); impl<'a> Encodable for HeaderSerializationWrapper<'a> { #[inline] - fn consensus_encode(&self, w: &mut W) -> Result { + fn consensus_encode(&self, w: &mut W) -> Result { let mut len = 0; len += VarInt::from(self.0.len()).consensus_encode(w)?; for header in self.0.iter() { @@ -346,7 +347,7 @@ impl<'a> Encodable for HeaderSerializationWrapper<'a> { } impl Encodable for NetworkMessage { - fn consensus_encode(&self, writer: &mut W) -> Result { + fn consensus_encode(&self, writer: &mut W) -> Result { match self { NetworkMessage::Version(ref dat) => dat.consensus_encode(writer), NetworkMessage::Addr(ref dat) => dat.consensus_encode(writer), @@ -391,7 +392,7 @@ impl Encodable for NetworkMessage { } impl Encodable for RawNetworkMessage { - fn consensus_encode(&self, w: &mut W) -> Result { + fn consensus_encode(&self, w: &mut W) -> Result { let mut len = 0; len += self.magic.consensus_encode(w)?; len += self.command().consensus_encode(w)?; @@ -406,7 +407,7 @@ struct HeaderDeserializationWrapper(Vec); impl Decodable for HeaderDeserializationWrapper { #[inline] - fn consensus_decode_from_finite_reader( + fn consensus_decode_from_finite_reader( r: &mut R, ) -> Result { let len = VarInt::consensus_decode(r)?.0; @@ -425,13 +426,13 @@ impl Decodable for HeaderDeserializationWrapper { } #[inline] - fn consensus_decode(r: &mut R) -> Result { + fn consensus_decode(r: &mut R) -> Result { Self::consensus_decode_from_finite_reader(&mut r.take(MAX_MSG_SIZE as u64)) } } impl Decodable for RawNetworkMessage { - fn consensus_decode_from_finite_reader( + fn consensus_decode_from_finite_reader( r: &mut R, ) -> Result { let magic = Decodable::consensus_decode_from_finite_reader(r)?; @@ -530,7 +531,7 @@ impl Decodable for RawNetworkMessage { } #[inline] - fn consensus_decode(r: &mut R) -> Result { + fn consensus_decode(r: &mut R) -> Result { Self::consensus_decode_from_finite_reader(&mut r.take(MAX_MSG_SIZE as u64)) } } diff --git a/bitcoin/src/p2p/message_blockdata.rs b/bitcoin/src/p2p/message_blockdata.rs index e4995f3e..550029d1 100644 --- a/bitcoin/src/p2p/message_blockdata.rs +++ b/bitcoin/src/p2p/message_blockdata.rs @@ -7,6 +7,7 @@ //! use hashes::{sha256d, Hash as _}; +use io::{Read, Write}; use crate::blockdata::block::BlockHash; use crate::blockdata::transaction::{Txid, Wtxid}; @@ -61,7 +62,7 @@ impl Inventory { impl Encodable for Inventory { #[inline] - fn consensus_encode(&self, w: &mut W) -> Result { + fn consensus_encode(&self, w: &mut W) -> Result { macro_rules! encode_inv { ($code:expr, $item:expr) => { u32::consensus_encode(&$code, w)? + $item.consensus_encode(w)? @@ -82,7 +83,7 @@ impl Encodable for Inventory { impl Decodable for Inventory { #[inline] - fn consensus_decode(r: &mut R) -> Result { + fn consensus_decode(r: &mut R) -> Result { let inv_type: u32 = Decodable::consensus_decode(r)?; Ok(match inv_type { 0 => Inventory::Error, diff --git a/bitcoin/src/p2p/message_bloom.rs b/bitcoin/src/p2p/message_bloom.rs index fa62a89a..3e89c933 100644 --- a/bitcoin/src/p2p/message_bloom.rs +++ b/bitcoin/src/p2p/message_bloom.rs @@ -5,6 +5,8 @@ //! This module describes BIP37 Connection Bloom filtering network messages. //! +use io::{Read, Write}; + use crate::consensus::{encode, Decodable, Encodable, ReadExt}; use crate::internal_macros::impl_consensus_encoding; @@ -35,7 +37,7 @@ pub enum BloomFlags { } impl Encodable for BloomFlags { - fn consensus_encode(&self, w: &mut W) -> Result { + fn consensus_encode(&self, w: &mut W) -> Result { w.write_all(&[match self { BloomFlags::None => 0, BloomFlags::All => 1, @@ -46,7 +48,7 @@ impl Encodable for BloomFlags { } impl Decodable for BloomFlags { - fn consensus_decode(r: &mut R) -> Result { + fn consensus_decode(r: &mut R) -> Result { Ok(match r.read_u8()? { 0 => BloomFlags::None, 1 => BloomFlags::All, diff --git a/bitcoin/src/p2p/message_network.rs b/bitcoin/src/p2p/message_network.rs index afc1e02b..f957a4a3 100644 --- a/bitcoin/src/p2p/message_network.rs +++ b/bitcoin/src/p2p/message_network.rs @@ -7,6 +7,7 @@ //! use hashes::sha256d; +use io::{Read, Write}; use crate::consensus::{encode, Decodable, Encodable, ReadExt}; use crate::internal_macros::impl_consensus_encoding; @@ -102,14 +103,14 @@ pub enum RejectReason { } impl Encodable for RejectReason { - fn consensus_encode(&self, w: &mut W) -> Result { + fn consensus_encode(&self, w: &mut W) -> Result { w.write_all(&[*self as u8])?; Ok(1) } } impl Decodable for RejectReason { - fn consensus_decode(r: &mut R) -> Result { + fn consensus_decode(r: &mut R) -> Result { Ok(match r.read_u8()? { 0x01 => RejectReason::Malformed, 0x10 => RejectReason::Invalid, diff --git a/bitcoin/src/p2p/mod.rs b/bitcoin/src/p2p/mod.rs index a8ec758c..0c0381fe 100644 --- a/bitcoin/src/p2p/mod.rs +++ b/bitcoin/src/p2p/mod.rs @@ -25,6 +25,7 @@ use core::{fmt, ops}; use hex::FromHex; use internals::{debug_from_display, write_err}; +use io::{Read, Write}; use crate::consensus::encode::{self, Decodable, Encodable}; use crate::prelude::{Borrow, BorrowMut, String, ToOwned}; @@ -190,14 +191,14 @@ impl ops::BitXorAssign for ServiceFlags { impl Encodable for ServiceFlags { #[inline] - fn consensus_encode(&self, w: &mut W) -> Result { + fn consensus_encode(&self, w: &mut W) -> Result { self.0.consensus_encode(w) } } impl Decodable for ServiceFlags { #[inline] - fn consensus_decode(r: &mut R) -> Result { + fn consensus_decode(r: &mut R) -> Result { Ok(ServiceFlags(Decodable::consensus_decode(r)?)) } } @@ -283,13 +284,13 @@ impl fmt::UpperHex for Magic { } impl Encodable for Magic { - fn consensus_encode(&self, writer: &mut W) -> Result { + fn consensus_encode(&self, writer: &mut W) -> Result { self.0.consensus_encode(writer) } } impl Decodable for Magic { - fn consensus_decode(reader: &mut R) -> Result { + fn consensus_decode(reader: &mut R) -> Result { Ok(Magic(Decodable::consensus_decode(reader)?)) } } diff --git a/bitcoin/src/psbt/map/global.rs b/bitcoin/src/psbt/map/global.rs index cc50ef06..7803df2b 100644 --- a/bitcoin/src/psbt/map/global.rs +++ b/bitcoin/src/psbt/map/global.rs @@ -70,7 +70,7 @@ impl Map for Psbt { } impl Psbt { - pub(crate) fn decode_global(r: &mut R) -> Result { + pub(crate) fn decode_global(r: &mut R) -> Result { let mut r = r.take(MAX_VEC_SIZE as u64); let mut tx: Option = None; let mut version: Option = None; diff --git a/bitcoin/src/psbt/raw.rs b/bitcoin/src/psbt/raw.rs index cc48168f..db4af227 100644 --- a/bitcoin/src/psbt/raw.rs +++ b/bitcoin/src/psbt/raw.rs @@ -8,6 +8,8 @@ use core::fmt; +use io::{Read, Write}; + use super::serialize::{Deserialize, Serialize}; use crate::consensus::encode::{ self, deserialize, serialize, Decodable, Encodable, ReadExt, VarInt, WriteExt, MAX_VEC_SIZE, @@ -72,7 +74,7 @@ impl fmt::Display for Key { } impl Key { - pub(crate) fn decode(r: &mut R) -> Result { + pub(crate) fn decode(r: &mut R) -> Result { let VarInt(byte_size): VarInt = Decodable::consensus_decode(r)?; if byte_size == 0 { @@ -135,7 +137,7 @@ impl Deserialize for Pair { } impl Pair { - pub(crate) fn decode(r: &mut R) -> Result { + pub(crate) fn decode(r: &mut R) -> Result { Ok(Pair { key: Key::decode(r)?, value: Decodable::consensus_decode(r)? }) } } @@ -144,7 +146,7 @@ impl Encodable for ProprietaryKey where Subtype: Copy + From + Into, { - fn consensus_encode(&self, w: &mut W) -> Result { + fn consensus_encode(&self, w: &mut W) -> Result { let mut len = self.prefix.consensus_encode(w)? + 1; w.emit_u8(self.subtype.into())?; w.write_all(&self.key)?; @@ -157,7 +159,7 @@ impl Decodable for ProprietaryKey where Subtype: Copy + From + Into, { - fn consensus_decode(r: &mut R) -> Result { + fn consensus_decode(r: &mut R) -> Result { let prefix = Vec::::consensus_decode(r)?; let subtype = Subtype::from(r.read_u8()?); let key = read_to_end(r)?; @@ -194,7 +196,7 @@ where } // core2 doesn't have read_to_end -pub(crate) fn read_to_end(d: &mut D) -> Result, io::Error> { +pub(crate) fn read_to_end(d: &mut D) -> Result, io::Error> { let mut result = vec![]; let mut buf = [0u8; 64]; loop { diff --git a/bitcoin/src/taproot/mod.rs b/bitcoin/src/taproot/mod.rs index 4532a031..d51b7111 100644 --- a/bitcoin/src/taproot/mod.rs +++ b/bitcoin/src/taproot/mod.rs @@ -14,6 +14,7 @@ use core::iter::FusedIterator; use hashes::{sha256t_hash_newtype, Hash, HashEngine}; use internals::write_err; +use io::Write; use secp256k1::{self, Scalar, Secp256k1}; use crate::consensus::Encodable; @@ -1109,7 +1110,7 @@ impl ControlBlock { /// # Returns /// /// The number of bytes written to the writer. - pub fn encode(&self, writer: &mut W) -> io::Result { + pub fn encode(&self, writer: &mut W) -> io::Result { let first_byte: u8 = i32::from(self.output_key_parity) as u8 | self.leaf_version.to_consensus(); writer.write_all(&[first_byte])?;