Skip to content

Commit d29bf4d

Browse files
committed
Copy ReadExt/WriteExt from bitcoin
Otherwise we miss them when upgrading to 0.32
1 parent 4aad862 commit d29bf4d

File tree

3 files changed

+46
-84
lines changed

3 files changed

+46
-84
lines changed

src/encode.rs

+11-62
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@ use crate::transaction::{Transaction, TxIn, TxOut};
2727

2828
pub use bitcoin::{self, consensus::encode::MAX_VEC_SIZE};
2929

30-
// Use the ReadExt/WriteExt traits as is from upstream
31-
pub use bitcoin::consensus::encode::{ReadExt, WriteExt};
32-
3330
use crate::taproot::TapLeafHash;
3431

3532
/// Encoding error
@@ -207,7 +204,7 @@ impl Decodable for sha256::Midstate {
207204
}
208205
}
209206

210-
pub(crate) fn consensus_encode_with_size<S: io::Write>(
207+
pub(crate) fn consensus_encode_with_size<S: crate::WriteExt>(
211208
data: &[u8],
212209
mut s: S,
213210
) -> Result<usize, Error> {
@@ -245,64 +242,16 @@ impl Decodable for crate::locktime::Time {
245242
}
246243
}
247244

248-
// TODO reuse bitcoin's `WriteExt::emit_varint`, `ReadExt::read_varint` when available
249-
250245
/// A variable sized integer.
251246
pub struct VarInt(pub u64);
252247
impl Encodable for VarInt {
253-
fn consensus_encode<W: io::Write>(&self, mut e: W) -> Result<usize, Error> {
254-
match self.0 {
255-
i @ 0..=0xFC => {
256-
e.emit_u8(i as u8)?;
257-
Ok(1)
258-
}
259-
i @ 0xFD..=0xFFFF => {
260-
e.emit_u8(0xFD)?;
261-
e.emit_u16(i as u16)?;
262-
Ok(3)
263-
}
264-
i @ 0x10000..=0xFFFFFFFF => {
265-
e.emit_u8(0xFE)?;
266-
e.emit_u32(i as u32)?;
267-
Ok(5)
268-
}
269-
i => {
270-
e.emit_u8(0xFF)?;
271-
e.emit_u64(i)?;
272-
Ok(9)
273-
}
274-
}
248+
fn consensus_encode<W: crate::WriteExt>(&self, mut e: W) -> Result<usize, Error> {
249+
Ok(e.emit_varint(self.0)?)
275250
}
276251
}
277252
impl Decodable for VarInt {
278-
fn consensus_decode<D: io::Read>(mut d: D) -> Result<Self, Error> {
279-
match d.read_u8()? {
280-
0xFF => {
281-
let x = d.read_u64()?;
282-
if x < 0x100000000 {
283-
Err(Error::NonMinimalVarInt)
284-
} else {
285-
Ok(VarInt(x))
286-
}
287-
}
288-
0xFE => {
289-
let x = d.read_u32()?;
290-
if x < 0x10000 {
291-
Err(Error::NonMinimalVarInt)
292-
} else {
293-
Ok(VarInt(x as u64))
294-
}
295-
}
296-
0xFD => {
297-
let x = d.read_u16()?;
298-
if x < 0xFD {
299-
Err(Error::NonMinimalVarInt)
300-
} else {
301-
Ok(VarInt(x as u64))
302-
}
303-
}
304-
n => Ok(VarInt(n as u64)),
305-
}
253+
fn consensus_decode<D: crate::ReadExt>(mut d: D) -> Result<Self, Error> {
254+
Ok(VarInt(d.read_varint()?))
306255
}
307256
}
308257
impl VarInt {
@@ -321,14 +270,14 @@ impl VarInt {
321270
macro_rules! impl_int {
322271
($ty:ident, $meth_dec:ident, $meth_enc:ident) => {
323272
impl Encodable for $ty {
324-
fn consensus_encode<W: io::Write>(&self, mut w: W) -> Result<usize, Error> {
273+
fn consensus_encode<W: crate::WriteExt>(&self, mut w: W) -> Result<usize, Error> {
325274
w.$meth_enc(*self)?;
326275
Ok(mem::size_of::<$ty>())
327276
}
328277
}
329278
impl Decodable for $ty {
330-
fn consensus_decode<R: io::Read>(mut r: R) -> Result<Self, Error> {
331-
Ok(ReadExt::$meth_dec(&mut r)?)
279+
fn consensus_decode<R: crate::ReadExt>(mut r: R) -> Result<Self, Error> {
280+
Ok(crate::ReadExt::$meth_dec(&mut r)?)
332281
}
333282
}
334283
};
@@ -411,7 +360,7 @@ macro_rules! impl_array {
411360
( $size:literal ) => {
412361
impl Encodable for [u8; $size] {
413362
#[inline]
414-
fn consensus_encode<W: WriteExt>(
363+
fn consensus_encode<W: crate::WriteExt>(
415364
&self,
416365
mut w: W,
417366
) -> core::result::Result<usize, Error> {
@@ -422,7 +371,7 @@ macro_rules! impl_array {
422371

423372
impl Decodable for [u8; $size] {
424373
#[inline]
425-
fn consensus_decode<R: ReadExt>(mut r: R) -> core::result::Result<Self, Error> {
374+
fn consensus_decode<R: crate::ReadExt>(mut r: R) -> core::result::Result<Self, Error> {
426375
let mut ret = [0; $size];
427376
r.read_slice(&mut ret)?;
428377
Ok(ret)
@@ -452,7 +401,7 @@ impl Encodable for Vec<u8> {
452401
}
453402
}
454403
impl Decodable for Vec<u8> {
455-
fn consensus_decode<D: io::Read>(mut d: D) -> Result<Self, Error> {
404+
fn consensus_decode<D: crate::ReadExt>(mut d: D) -> Result<Self, Error> {
456405
let s = VarInt::consensus_decode(&mut d)?.0 as usize;
457406
let mut v = vec![0; s];
458407
d.read_slice(&mut v)?;

src/lib.rs

+33-18
Original file line numberDiff line numberDiff line change
@@ -31,53 +31,68 @@ pub extern crate bitcoin;
3131
/// Re-export of secp256k1-zkp crate
3232
pub extern crate secp256k1_zkp;
3333
/// Re-export of serde crate
34-
#[cfg(feature = "serde")] #[macro_use] pub extern crate actual_serde as serde;
35-
#[cfg(all(test, feature = "serde"))] extern crate serde_test;
34+
#[cfg(feature = "serde")]
35+
#[macro_use]
36+
pub extern crate actual_serde as serde;
37+
#[cfg(all(test, feature = "serde"))]
38+
extern crate serde_test;
3639

37-
#[cfg(test)] extern crate rand;
38-
#[cfg(test)] extern crate bincode;
39-
#[cfg(any(test, feature = "serde_json"))] extern crate serde_json;
40+
#[cfg(test)]
41+
extern crate bincode;
42+
#[cfg(test)]
43+
extern crate rand;
44+
#[cfg(any(test, feature = "serde_json"))]
45+
extern crate serde_json;
4046

41-
#[macro_use] mod internal_macros;
47+
#[macro_use]
48+
mod internal_macros;
4249
pub mod address;
4350
pub mod blech32;
51+
mod blind;
4452
mod block;
4553
pub mod confidential;
4654
pub mod dynafed;
4755
pub mod encode;
4856
mod error;
57+
mod ext;
4958
mod fast_merkle_root;
5059
pub mod hash_types;
5160
pub mod hex;
52-
pub mod locktime;
5361
pub mod issuance;
62+
pub mod locktime;
5463
pub mod opcodes;
55-
pub mod script;
56-
mod transaction;
57-
mod blind;
5864
mod parse;
59-
pub mod sighash;
6065
pub mod pset;
61-
pub mod taproot;
6266
pub mod schnorr;
67+
pub mod script;
6368
#[cfg(feature = "serde")]
6469
mod serde_utils;
70+
pub mod sighash;
71+
pub mod taproot;
72+
mod transaction;
6573
// consider making upstream public
6674
mod endian;
6775
// re-export bitcoin deps which we re-use
6876
pub use bitcoin::bech32;
6977
pub use bitcoin::hashes;
7078
// export everything at the top level so it can be used as `elements::Transaction` etc.
71-
pub use crate::address::{Address, AddressParams, AddressError};
72-
pub use crate::transaction::{OutPoint, PeginData, PegoutData, EcdsaSighashType, TxIn, TxOut, TxInWitness, TxOutWitness, Transaction, AssetIssuance};
73-
pub use crate::transaction::Sequence;
74-
pub use crate::blind::{ConfidentialTxOutError, TxOutSecrets, SurjectionInput, TxOutError, VerificationError, BlindError, UnblindError, BlindValueProofs, BlindAssetProofs, RangeProofMessage};
75-
pub use crate::block::{BlockHeader, Block};
79+
pub use crate::address::{Address, AddressError, AddressParams};
80+
pub use crate::blind::{
81+
BlindAssetProofs, BlindError, BlindValueProofs, ConfidentialTxOutError, RangeProofMessage,
82+
SurjectionInput, TxOutError, TxOutSecrets, UnblindError, VerificationError,
83+
};
7684
pub use crate::block::ExtData as BlockExtData;
85+
pub use crate::block::{Block, BlockHeader};
86+
pub use crate::ext::{ReadExt, WriteExt};
7787
pub use crate::fast_merkle_root::fast_merkle_root;
7888
pub use crate::hash_types::*;
7989
pub use crate::issuance::{AssetId, ContractHash};
8090
pub use crate::locktime::LockTime;
91+
pub use crate::schnorr::{SchnorrSig, SchnorrSigError};
8192
pub use crate::script::Script;
8293
pub use crate::sighash::SchnorrSighashType;
83-
pub use crate::schnorr::{SchnorrSig, SchnorrSigError};
94+
pub use crate::transaction::Sequence;
95+
pub use crate::transaction::{
96+
AssetIssuance, EcdsaSighashType, OutPoint, PeginData, PegoutData, Transaction, TxIn,
97+
TxInWitness, TxOut, TxOutWitness,
98+
};

src/pset/raw.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@
2020
use std::{fmt, io};
2121

2222
use super::Error;
23-
use crate::encode::{
24-
self, deserialize, serialize, Decodable, Encodable, VarInt, WriteExt, MAX_VEC_SIZE,
25-
};
23+
use crate::encode::{self, deserialize, serialize, Decodable, Encodable, VarInt, MAX_VEC_SIZE};
2624
use crate::hex;
2725
/// A PSET key in its raw byte form.
2826
#[derive(Debug, PartialEq, Hash, Eq, Clone, Ord, PartialOrd)]
@@ -181,7 +179,7 @@ impl<Subtype> Encodable for ProprietaryKey<Subtype>
181179
where
182180
Subtype: Copy + From<u8> + Into<u8>,
183181
{
184-
fn consensus_encode<W: io::Write>(&self, mut e: W) -> Result<usize, encode::Error> {
182+
fn consensus_encode<W: crate::WriteExt>(&self, mut e: W) -> Result<usize, encode::Error> {
185183
let mut len = self.prefix.consensus_encode(&mut e)? + 1;
186184
e.emit_u8(self.subtype.into())?;
187185
len += e.write(&self.key)?;

0 commit comments

Comments
 (0)