diff --git a/bitcoin/examples/taproot-psbt.rs b/bitcoin/examples/taproot-psbt.rs index 9125559b6..9d418a50c 100644 --- a/bitcoin/examples/taproot-psbt.rs +++ b/bitcoin/examples/taproot-psbt.rs @@ -690,7 +690,7 @@ impl BeneficiaryWallet { script_witness.push(signature.to_vec()); } for (control_block, (script, _)) in input.tap_scripts.iter() { - script_witness.push(script.to_bytes()); + script_witness.push(script.to_vec()); script_witness.push(control_block.serialize()); } input.final_script_witness = Some(script_witness); diff --git a/bitcoin/src/bip152.rs b/bitcoin/src/bip152.rs index f7f79cf43..3cad34e46 100644 --- a/bitcoin/src/bip152.rs +++ b/bitcoin/src/bip152.rs @@ -9,11 +9,13 @@ use core::{convert, fmt, mem}; use std::error; use hashes::{sha256, siphash24}; -use internals::{impl_array_newtype, ToU64 as _}; +use internals::ToU64 as _; use io::{BufRead, Write}; use crate::consensus::encode::{self, Decodable, Encodable, ReadExt, WriteExt}; -use crate::internal_macros::{impl_array_newtype_stringify, impl_consensus_encoding}; +use crate::internal_macros::{ + impl_array_newtype, impl_array_newtype_stringify, impl_consensus_encoding, +}; use crate::prelude::Vec; use crate::transaction::TxIdentifier; use crate::{block, consensus, Block, BlockHash, Transaction}; diff --git a/bitcoin/src/bip32.rs b/bitcoin/src/bip32.rs index 8f03e2ab6..ca6f957fc 100644 --- a/bitcoin/src/bip32.rs +++ b/bitcoin/src/bip32.rs @@ -10,11 +10,11 @@ use core::str::FromStr; use core::{fmt, slice}; use hashes::{hash160, hash_newtype, sha512, GeneralHash, HashEngine, Hmac, HmacEngine}; -use internals::{impl_array_newtype, write_err}; +use internals::write_err; use secp256k1::{Secp256k1, XOnlyPublicKey}; use crate::crypto::key::{CompressedPublicKey, Keypair, PrivateKey}; -use crate::internal_macros::impl_array_newtype_stringify; +use crate::internal_macros::{impl_array_newtype, impl_array_newtype_stringify}; use crate::network::NetworkKind; use crate::prelude::{String, Vec}; diff --git a/bitcoin/src/blockdata/constants.rs b/bitcoin/src/blockdata/constants.rs index 47c5b84c2..684ca50b8 100644 --- a/bitcoin/src/blockdata/constants.rs +++ b/bitcoin/src/blockdata/constants.rs @@ -7,10 +7,9 @@ //! single transaction. use hashes::sha256d; -use internals::impl_array_newtype; use crate::block::{self, Block}; -use crate::internal_macros::impl_array_newtype_stringify; +use crate::internal_macros::{impl_array_newtype, impl_array_newtype_stringify}; use crate::locktime::absolute; use crate::network::{Network, Params}; use crate::opcodes::all::*; diff --git a/bitcoin/src/blockdata/script/tests.rs b/bitcoin/src/blockdata/script/tests.rs index 693f36e2b..5a9878a59 100644 --- a/bitcoin/src/blockdata/script/tests.rs +++ b/bitcoin/src/blockdata/script/tests.rs @@ -54,7 +54,7 @@ fn p2pk_pubkey_bytes_valid_key_and_valid_script_returns_expected_key() { let key = key_str.parse::().unwrap(); let p2pk = Script::builder().push_key(key).push_opcode(OP_CHECKSIG).into_script(); let actual = p2pk.p2pk_pubkey_bytes().unwrap(); - assert_eq!(actual.to_vec(), key.to_bytes()); + assert_eq!(actual.to_vec(), key.to_vec()); } #[test] @@ -109,7 +109,7 @@ fn p2pk_pubkey_bytes_compressed_key_returns_expected_key() { let key = compressed_key_str.parse::().unwrap(); let p2pk = Script::builder().push_key(key).push_opcode(OP_CHECKSIG).into_script(); let actual = p2pk.p2pk_pubkey_bytes().unwrap(); - assert_eq!(actual.to_vec(), key.to_bytes()); + assert_eq!(actual.to_vec(), key.to_vec()); } #[test] diff --git a/bitcoin/src/blockdata/transaction.rs b/bitcoin/src/blockdata/transaction.rs index 03a98f564..e9207099a 100644 --- a/bitcoin/src/blockdata/transaction.rs +++ b/bitcoin/src/blockdata/transaction.rs @@ -1604,7 +1604,7 @@ mod tests { .is_err()); // test that we get a failure if we corrupt a signature - let mut witness = spending.input[1].witness.to_bytes(); + let mut witness = spending.input[1].witness.to_vec(); witness[0][10] = 42; spending.input[1].witness = Witness::from_slice(&witness); diff --git a/bitcoin/src/blockdata/witness.rs b/bitcoin/src/blockdata/witness.rs index c8901a9c2..e7dc292f0 100644 --- a/bitcoin/src/blockdata/witness.rs +++ b/bitcoin/src/blockdata/witness.rs @@ -112,10 +112,6 @@ impl Encodable for Witness { crate::internal_macros::define_extension_trait! { /// Extension functionality for the [`Witness`] type. pub trait WitnessExt impl for Witness { - /// Convenience method to create an array of byte-arrays from this witness. - #[deprecated(since = "TBD", note = "use `to_bytes` instead")] - fn to_vec(&self) -> Vec> { self.to_bytes() } - /// Creates a witness required to spend a P2WPKH output. /// /// The witness will be made up of the DER encoded signature + sighash_type followed by the @@ -269,7 +265,7 @@ mod test { let expected_witness = vec![hex!( "304402207c800d698f4b0298c5aac830b822f011bb02df41eb114ade9a6702f364d5e39c0220366900d2a60cab903e77ef7dd415d46509b1f78ac78906e3296f495aa1b1b54101") ]; - assert_eq!(witness.to_bytes(), expected_witness); + assert_eq!(witness.to_vec(), expected_witness); } #[test] diff --git a/bitcoin/src/crypto/ecdsa.rs b/bitcoin/src/crypto/ecdsa.rs index b74756fdb..19fbb0f80 100644 --- a/bitcoin/src/crypto/ecdsa.rs +++ b/bitcoin/src/crypto/ecdsa.rs @@ -61,7 +61,7 @@ impl Signature { /// /// Note: this performs an extra heap allocation, you might prefer the /// [`serialize`](Self::serialize) method instead. - pub fn to_bytes(self) -> Vec { + pub fn to_vec(self) -> Vec { self.signature .serialize_der() .iter() @@ -70,10 +70,6 @@ impl Signature { .collect() } - /// Serializes an ECDSA signature (inner secp256k1 signature in DER format) into `Vec`. - #[deprecated(since = "TBD", note = "use `to_bytes` instead")] - pub fn to_vec(self) -> Vec { self.to_bytes() } - /// Serializes an ECDSA signature (inner secp256k1 signature in DER format) to a `writer`. #[inline] pub fn serialize_to_writer(&self, writer: &mut W) -> Result<(), io::Error> { @@ -345,6 +341,6 @@ mod tests { let mut buf = vec![]; sig.serialize_to_writer(&mut buf).expect("write failed"); - assert_eq!(sig.to_bytes(), buf) + assert_eq!(sig.to_vec(), buf) } } diff --git a/bitcoin/src/crypto/key.rs b/bitcoin/src/crypto/key.rs index 931fc8808..0bac43000 100644 --- a/bitcoin/src/crypto/key.rs +++ b/bitcoin/src/crypto/key.rs @@ -110,7 +110,11 @@ impl PublicKey { } /// Serializes the public key to bytes. - pub fn to_bytes(self) -> Vec { + #[deprecated(since = "TBD", note = "use to_vec instead")] + pub fn to_bytes(self) -> Vec { self.to_vec() } + + /// Serializes the public key to bytes. + pub fn to_vec(self) -> Vec { let mut buf = Vec::new(); self.write_into(&mut buf).expect("vecs don't error"); buf @@ -442,8 +446,13 @@ impl PrivateKey { } } + /// Serializes the private key to bytes. - pub fn to_bytes(self) -> Vec { self.inner[..].to_vec() } + #[deprecated(since = "TBD", note = "use to_vec instead")] + pub fn to_bytes(self) -> Vec { self.to_vec() } + + /// Serializes the private key to bytes. + pub fn to_vec(self) -> Vec { self.inner[..].to_vec() } /// Deserializes a private key from a slice. pub fn from_slice( diff --git a/bitcoin/src/crypto/taproot.rs b/bitcoin/src/crypto/taproot.rs index da72690b0..bfa857aa0 100644 --- a/bitcoin/src/crypto/taproot.rs +++ b/bitcoin/src/crypto/taproot.rs @@ -47,7 +47,7 @@ impl Signature { /// Serializes the signature. /// /// Note: this allocates on the heap, prefer [`serialize`](Self::serialize) if vec is not needed. - pub fn to_bytes(self) -> Vec { + pub fn to_vec(self) -> Vec { let mut ser_sig = self.signature.as_ref().to_vec(); if self.sighash_type == TapSighashType::Default { // default sighash type, don't add extra sighash byte @@ -57,10 +57,6 @@ impl Signature { ser_sig } - /// Serializes the signature. - #[deprecated(since = "TBD", note = "use `to_bytes` instead")] - pub fn to_vec(self) -> Vec { self.to_bytes() } - /// Serializes the signature to `writer`. #[inline] pub fn serialize_to_writer(&self, writer: &mut W) -> Result<(), io::Error> { diff --git a/bitcoin/src/internal_macros.rs b/bitcoin/src/internal_macros.rs index 7fa5588d5..70387b254 100644 --- a/bitcoin/src/internal_macros.rs +++ b/bitcoin/src/internal_macros.rs @@ -277,3 +277,126 @@ macro_rules! define_extension_trait { }; } pub(crate) use define_extension_trait; + +/// Implements standard array methods for a given wrapper type. +macro_rules! impl_array_newtype { + ($thing:ident, $ty:ty, $len:literal) => { + impl $thing { + /// Creates `Self` by wrapping `bytes`. + #[inline] + pub fn from_byte_array(bytes: [u8; $len]) -> Self { Self(bytes) } + + /// Returns a reference the underlying byte array. + #[inline] + pub fn as_byte_array(&self) -> &[u8; $len] { &self.0 } + + /// Returns the underlying byte array. + #[inline] + pub fn to_byte_array(self) -> [u8; $len] { + // We rely on `Copy` being implemented for $thing so conversion + // methods use the correct Rust naming conventions. + fn check_copy() {} + check_copy::<$thing>(); + + self.0 + } + + /// Copies the underlying bytes into a new `Vec`. + #[inline] + pub fn to_vec(&self) -> alloc::vec::Vec { self.0.to_vec() } + + /// Returns a slice of the underlying bytes. + #[inline] + pub fn as_bytes(&self) -> &[u8] { &self.0 } + + /// Copies the underlying bytes into a new `Vec`. + #[inline] + #[deprecated(since = "TBD", note = "use to_vec instead")] + pub fn to_bytes(&self) -> alloc::vec::Vec { self.to_vec() } + + /// Converts the object to a raw pointer. + #[inline] + pub fn as_ptr(&self) -> *const $ty { + let &$thing(ref dat) = self; + dat.as_ptr() + } + + /// Converts the object to a mutable raw pointer. + #[inline] + pub fn as_mut_ptr(&mut self) -> *mut $ty { + let &mut $thing(ref mut dat) = self; + dat.as_mut_ptr() + } + + /// Returns the length of the object as an array. + #[inline] + pub fn len(&self) -> usize { $len } + + /// Returns whether the object, as an array, is empty. Always false. + #[inline] + pub fn is_empty(&self) -> bool { false } + } + + impl<'a> core::convert::From<[$ty; $len]> for $thing { + fn from(data: [$ty; $len]) -> Self { $thing(data) } + } + + impl<'a> core::convert::From<&'a [$ty; $len]> for $thing { + fn from(data: &'a [$ty; $len]) -> Self { $thing(*data) } + } + + impl<'a> core::convert::TryFrom<&'a [$ty]> for $thing { + type Error = core::array::TryFromSliceError; + + fn try_from(data: &'a [$ty]) -> core::result::Result { + use core::convert::TryInto; + + Ok($thing(data.try_into()?)) + } + } + + impl AsRef<[$ty; $len]> for $thing { + fn as_ref(&self) -> &[$ty; $len] { &self.0 } + } + + impl AsMut<[$ty; $len]> for $thing { + fn as_mut(&mut self) -> &mut [$ty; $len] { &mut self.0 } + } + + impl AsRef<[$ty]> for $thing { + fn as_ref(&self) -> &[$ty] { &self.0 } + } + + impl AsMut<[$ty]> for $thing { + fn as_mut(&mut self) -> &mut [$ty] { &mut self.0 } + } + + impl core::borrow::Borrow<[$ty; $len]> for $thing { + fn borrow(&self) -> &[$ty; $len] { &self.0 } + } + + impl core::borrow::BorrowMut<[$ty; $len]> for $thing { + fn borrow_mut(&mut self) -> &mut [$ty; $len] { &mut self.0 } + } + + // The following two are valid because `[T; N]: Borrow<[T]>` + impl core::borrow::Borrow<[$ty]> for $thing { + fn borrow(&self) -> &[$ty] { &self.0 } + } + + impl core::borrow::BorrowMut<[$ty]> for $thing { + fn borrow_mut(&mut self) -> &mut [$ty] { &mut self.0 } + } + + impl core::ops::Index for $thing + where + [$ty]: core::ops::Index, + { + type Output = <[$ty] as core::ops::Index>::Output; + + #[inline] + fn index(&self, index: I) -> &Self::Output { &self.0[index] } + } + }; +} +pub(crate) use impl_array_newtype; diff --git a/bitcoin/src/psbt/serialize.rs b/bitcoin/src/psbt/serialize.rs index dfe840e2e..5b505d4b7 100644 --- a/bitcoin/src/psbt/serialize.rs +++ b/bitcoin/src/psbt/serialize.rs @@ -140,7 +140,7 @@ impl_psbt_hash_de_serialize!(sha256d::Hash); impl_psbt_de_serialize!(Vec); impl Serialize for ScriptBuf { - fn serialize(&self) -> Vec { self.to_bytes() } + fn serialize(&self) -> Vec { self.to_vec() } } impl Deserialize for ScriptBuf { @@ -172,7 +172,7 @@ impl Deserialize for secp256k1::PublicKey { } impl Serialize for ecdsa::Signature { - fn serialize(&self) -> Vec { self.to_bytes() } + fn serialize(&self) -> Vec { self.to_vec() } } impl Deserialize for ecdsa::Signature { @@ -265,7 +265,7 @@ impl Deserialize for XOnlyPublicKey { } impl Serialize for taproot::Signature { - fn serialize(&self) -> Vec { self.to_bytes() } + fn serialize(&self) -> Vec { self.to_vec() } } impl Deserialize for taproot::Signature { diff --git a/bitcoin/tests/psbt-sign-taproot.rs b/bitcoin/tests/psbt-sign-taproot.rs index 3d5a71d77..15ff88115 100644 --- a/bitcoin/tests/psbt-sign-taproot.rs +++ b/bitcoin/tests/psbt-sign-taproot.rs @@ -343,7 +343,7 @@ fn finalize_psbt_for_script_path_spend(mut psbt: Psbt) -> Psbt { script_witness.push(signature.to_vec()); } for (control_block, (script, _)) in input.tap_scripts.iter() { - script_witness.push(script.to_bytes()); + script_witness.push(script.to_vec()); script_witness.push(control_block.serialize()); } input.final_script_witness = Some(script_witness); diff --git a/internals/src/macros.rs b/internals/src/macros.rs index d1fcd1261..d0bad52d7 100644 --- a/internals/src/macros.rs +++ b/internals/src/macros.rs @@ -2,125 +2,6 @@ //! Various macros used by the Rust Bitcoin ecosystem. -/// Implements standard array methods for a given wrapper type. -#[macro_export] -macro_rules! impl_array_newtype { - ($thing:ident, $ty:ty, $len:literal) => { - impl $thing { - /// Creates `Self` by wrapping `bytes`. - #[inline] - pub fn from_byte_array(bytes: [u8; $len]) -> Self { Self(bytes) } - - /// Returns a reference the underlying byte array. - #[inline] - pub fn as_byte_array(&self) -> &[u8; $len] { &self.0 } - - /// Returns the underlying byte array. - #[inline] - pub fn to_byte_array(self) -> [u8; $len] { - // We rely on `Copy` being implemented for $thing so conversion - // methods use the correct Rust naming conventions. - fn check_copy() {} - check_copy::<$thing>(); - - self.0 - } - - /// Returns a slice of the underlying bytes. - #[inline] - pub fn as_bytes(&self) -> &[u8] { &self.0 } - - /// Copies the underlying bytes into a new `Vec`. - #[cfg(feature = "alloc")] - #[inline] - pub fn to_bytes(&self) -> alloc::vec::Vec { self.0.to_vec() } - - /// Converts the object to a raw pointer. - #[inline] - pub fn as_ptr(&self) -> *const $ty { - let &$thing(ref dat) = self; - dat.as_ptr() - } - - /// Converts the object to a mutable raw pointer. - #[inline] - pub fn as_mut_ptr(&mut self) -> *mut $ty { - let &mut $thing(ref mut dat) = self; - dat.as_mut_ptr() - } - - /// Returns the length of the object as an array. - #[inline] - pub fn len(&self) -> usize { $len } - - /// Returns whether the object, as an array, is empty. Always false. - #[inline] - pub fn is_empty(&self) -> bool { false } - } - - impl<'a> core::convert::From<[$ty; $len]> for $thing { - fn from(data: [$ty; $len]) -> Self { $thing(data) } - } - - impl<'a> core::convert::From<&'a [$ty; $len]> for $thing { - fn from(data: &'a [$ty; $len]) -> Self { $thing(*data) } - } - - impl<'a> core::convert::TryFrom<&'a [$ty]> for $thing { - type Error = core::array::TryFromSliceError; - - fn try_from(data: &'a [$ty]) -> core::result::Result { - use core::convert::TryInto; - - Ok($thing(data.try_into()?)) - } - } - - impl AsRef<[$ty; $len]> for $thing { - fn as_ref(&self) -> &[$ty; $len] { &self.0 } - } - - impl AsMut<[$ty; $len]> for $thing { - fn as_mut(&mut self) -> &mut [$ty; $len] { &mut self.0 } - } - - impl AsRef<[$ty]> for $thing { - fn as_ref(&self) -> &[$ty] { &self.0 } - } - - impl AsMut<[$ty]> for $thing { - fn as_mut(&mut self) -> &mut [$ty] { &mut self.0 } - } - - impl core::borrow::Borrow<[$ty; $len]> for $thing { - fn borrow(&self) -> &[$ty; $len] { &self.0 } - } - - impl core::borrow::BorrowMut<[$ty; $len]> for $thing { - fn borrow_mut(&mut self) -> &mut [$ty; $len] { &mut self.0 } - } - - // The following two are valid because `[T; N]: Borrow<[T]>` - impl core::borrow::Borrow<[$ty]> for $thing { - fn borrow(&self) -> &[$ty] { &self.0 } - } - - impl core::borrow::BorrowMut<[$ty]> for $thing { - fn borrow_mut(&mut self) -> &mut [$ty] { &mut self.0 } - } - - impl core::ops::Index for $thing - where - [$ty]: core::ops::Index, - { - type Output = <[$ty] as core::ops::Index>::Output; - - #[inline] - fn index(&self, index: I) -> &Self::Output { &self.0[index] } - } - }; -} - /// Implements `Debug` by calling through to `Display`. #[macro_export] macro_rules! debug_from_display { diff --git a/primitives/src/script/borrowed.rs b/primitives/src/script/borrowed.rs index 7717a70bc..86857deb8 100644 --- a/primitives/src/script/borrowed.rs +++ b/primitives/src/script/borrowed.rs @@ -97,6 +97,15 @@ impl Script { #[inline] pub fn as_mut_bytes(&mut self) -> &mut [u8] { &mut self.0 } + /// Returns a copy of the script data. + #[inline] + pub fn to_vec(&self) -> Vec { self.0.to_owned() } + + /// Returns a copy of the script data. + #[inline] + #[deprecated(since = "TBD", note = "use to_vec instead")] + pub fn to_bytes(&self) -> Vec { self.to_vec() } + /// Returns the length in bytes of the script. #[inline] pub fn len(&self) -> usize { self.0.len() } @@ -105,10 +114,6 @@ impl Script { #[inline] pub fn is_empty(&self) -> bool { self.0.is_empty() } - /// Returns a copy of the script data. - #[inline] - pub fn to_bytes(&self) -> Vec { self.0.to_owned() } - /// Converts a [`Box