Add `Buf` suffix to `TaprootMerkleBranch`
This type actually contains a `Vec` but we would prefer to have an unsized type. Rename it first so that we can reuse the name later.
This commit is contained in:
parent
cfccd389a9
commit
277045bad7
|
@ -1,6 +1,6 @@
|
||||||
// SPDX-License-Identifier: CC0-1.0
|
// SPDX-License-Identifier: CC0-1.0
|
||||||
|
|
||||||
//! Contains `TaprootMerkleBranch` and its associated types.
|
//! Contains `TaprootMerkleBranchBuf` and its associated types.
|
||||||
|
|
||||||
use hashes::Hash;
|
use hashes::Hash;
|
||||||
|
|
||||||
|
@ -15,9 +15,9 @@ use crate::prelude::{Borrow, BorrowMut, Box, Vec};
|
||||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||||
#[cfg_attr(feature = "serde", serde(into = "Vec<TapNodeHash>"))]
|
#[cfg_attr(feature = "serde", serde(into = "Vec<TapNodeHash>"))]
|
||||||
#[cfg_attr(feature = "serde", serde(try_from = "Vec<TapNodeHash>"))]
|
#[cfg_attr(feature = "serde", serde(try_from = "Vec<TapNodeHash>"))]
|
||||||
pub struct TaprootMerkleBranch(Vec<TapNodeHash>);
|
pub struct TaprootMerkleBranchBuf(Vec<TapNodeHash>);
|
||||||
|
|
||||||
impl TaprootMerkleBranch {
|
impl TaprootMerkleBranchBuf {
|
||||||
/// Returns a reference to the slice of hashes.
|
/// Returns a reference to the slice of hashes.
|
||||||
#[deprecated(since = "0.32.0", note = "use `as_slice` instead")]
|
#[deprecated(since = "0.32.0", note = "use `as_slice` instead")]
|
||||||
#[inline]
|
#[inline]
|
||||||
|
@ -54,7 +54,7 @@ impl TaprootMerkleBranch {
|
||||||
} else {
|
} else {
|
||||||
let inner = node_hashes.iter().copied().map(TapNodeHash::from_byte_array).collect();
|
let inner = node_hashes.iter().copied().map(TapNodeHash::from_byte_array).collect();
|
||||||
|
|
||||||
Ok(TaprootMerkleBranch(inner))
|
Ok(TaprootMerkleBranchBuf(inner))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -70,7 +70,7 @@ impl TaprootMerkleBranch {
|
||||||
if collection.as_ref().len() > TAPROOT_CONTROL_MAX_NODE_COUNT {
|
if collection.as_ref().len() > TAPROOT_CONTROL_MAX_NODE_COUNT {
|
||||||
Err(InvalidMerkleTreeDepthError(collection.as_ref().len()))
|
Err(InvalidMerkleTreeDepthError(collection.as_ref().len()))
|
||||||
} else {
|
} else {
|
||||||
Ok(TaprootMerkleBranch(collection.into()))
|
Ok(TaprootMerkleBranchBuf(collection.into()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -113,7 +113,7 @@ impl TaprootMerkleBranch {
|
||||||
|
|
||||||
macro_rules! impl_try_from {
|
macro_rules! impl_try_from {
|
||||||
($from:ty) => {
|
($from:ty) => {
|
||||||
impl TryFrom<$from> for TaprootMerkleBranch {
|
impl TryFrom<$from> for TaprootMerkleBranchBuf {
|
||||||
type Error = InvalidMerkleTreeDepthError;
|
type Error = InvalidMerkleTreeDepthError;
|
||||||
|
|
||||||
/// Constructs a new Merkle proof from list of hashes.
|
/// Constructs a new Merkle proof from list of hashes.
|
||||||
|
@ -123,7 +123,7 @@ macro_rules! impl_try_from {
|
||||||
/// If inner proof length is more than [`TAPROOT_CONTROL_MAX_NODE_COUNT`] (128).
|
/// If inner proof length is more than [`TAPROOT_CONTROL_MAX_NODE_COUNT`] (128).
|
||||||
#[inline]
|
#[inline]
|
||||||
fn try_from(v: $from) -> Result<Self, Self::Error> {
|
fn try_from(v: $from) -> Result<Self, Self::Error> {
|
||||||
TaprootMerkleBranch::from_collection(v)
|
TaprootMerkleBranchBuf::from_collection(v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -135,7 +135,7 @@ impl_try_from!(Box<[TapNodeHash]>);
|
||||||
macro_rules! impl_try_from_array {
|
macro_rules! impl_try_from_array {
|
||||||
($($len:expr),* $(,)?) => {
|
($($len:expr),* $(,)?) => {
|
||||||
$(
|
$(
|
||||||
impl From<[TapNodeHash; $len]> for TaprootMerkleBranch {
|
impl From<[TapNodeHash; $len]> for TaprootMerkleBranchBuf {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn from(a: [TapNodeHash; $len]) -> Self {
|
fn from(a: [TapNodeHash; $len]) -> Self {
|
||||||
Self(a.to_vec())
|
Self(a.to_vec())
|
||||||
|
@ -146,7 +146,7 @@ macro_rules! impl_try_from_array {
|
||||||
}
|
}
|
||||||
// Implement for all values [0, 128] inclusive.
|
// Implement for all values [0, 128] inclusive.
|
||||||
//
|
//
|
||||||
// The reason zero is included is that `TaprootMerkleBranch` doesn't contain the hash of the node
|
// The reason zero is included is that `TaprootMerkleBranchBuf` doesn't contain the hash of the node
|
||||||
// that's being proven - it's not needed because the script is already right before control block.
|
// that's being proven - it's not needed because the script is already right before control block.
|
||||||
impl_try_from_array!(
|
impl_try_from_array!(
|
||||||
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
|
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
|
||||||
|
@ -157,12 +157,12 @@ impl_try_from_array!(
|
||||||
117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128
|
117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128
|
||||||
);
|
);
|
||||||
|
|
||||||
impl From<TaprootMerkleBranch> for Vec<TapNodeHash> {
|
impl From<TaprootMerkleBranchBuf> for Vec<TapNodeHash> {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn from(branch: TaprootMerkleBranch) -> Self { branch.0 }
|
fn from(branch: TaprootMerkleBranchBuf) -> Self { branch.0 }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl IntoIterator for TaprootMerkleBranch {
|
impl IntoIterator for TaprootMerkleBranchBuf {
|
||||||
type IntoIter = IntoIter;
|
type IntoIter = IntoIter;
|
||||||
type Item = TapNodeHash;
|
type Item = TapNodeHash;
|
||||||
|
|
||||||
|
@ -170,7 +170,7 @@ impl IntoIterator for TaprootMerkleBranch {
|
||||||
fn into_iter(self) -> Self::IntoIter { IntoIter(self.0.into_iter()) }
|
fn into_iter(self) -> Self::IntoIter { IntoIter(self.0.into_iter()) }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> IntoIterator for &'a TaprootMerkleBranch {
|
impl<'a> IntoIterator for &'a TaprootMerkleBranchBuf {
|
||||||
type IntoIter = core::slice::Iter<'a, TapNodeHash>;
|
type IntoIter = core::slice::Iter<'a, TapNodeHash>;
|
||||||
type Item = &'a TapNodeHash;
|
type Item = &'a TapNodeHash;
|
||||||
|
|
||||||
|
@ -178,7 +178,7 @@ impl<'a> IntoIterator for &'a TaprootMerkleBranch {
|
||||||
fn into_iter(self) -> Self::IntoIter { self.0.iter() }
|
fn into_iter(self) -> Self::IntoIter { self.0.iter() }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> IntoIterator for &'a mut TaprootMerkleBranch {
|
impl<'a> IntoIterator for &'a mut TaprootMerkleBranchBuf {
|
||||||
type IntoIter = core::slice::IterMut<'a, TapNodeHash>;
|
type IntoIter = core::slice::IterMut<'a, TapNodeHash>;
|
||||||
type Item = &'a mut TapNodeHash;
|
type Item = &'a mut TapNodeHash;
|
||||||
|
|
||||||
|
@ -186,41 +186,41 @@ impl<'a> IntoIterator for &'a mut TaprootMerkleBranch {
|
||||||
fn into_iter(self) -> Self::IntoIter { self.0.iter_mut() }
|
fn into_iter(self) -> Self::IntoIter { self.0.iter_mut() }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl core::ops::Deref for TaprootMerkleBranch {
|
impl core::ops::Deref for TaprootMerkleBranchBuf {
|
||||||
type Target = [TapNodeHash];
|
type Target = [TapNodeHash];
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn deref(&self) -> &Self::Target { &self.0 }
|
fn deref(&self) -> &Self::Target { &self.0 }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl core::ops::DerefMut for TaprootMerkleBranch {
|
impl core::ops::DerefMut for TaprootMerkleBranchBuf {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 }
|
fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AsRef<[TapNodeHash]> for TaprootMerkleBranch {
|
impl AsRef<[TapNodeHash]> for TaprootMerkleBranchBuf {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn as_ref(&self) -> &[TapNodeHash] { &self.0 }
|
fn as_ref(&self) -> &[TapNodeHash] { &self.0 }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AsMut<[TapNodeHash]> for TaprootMerkleBranch {
|
impl AsMut<[TapNodeHash]> for TaprootMerkleBranchBuf {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn as_mut(&mut self) -> &mut [TapNodeHash] { &mut self.0 }
|
fn as_mut(&mut self) -> &mut [TapNodeHash] { &mut self.0 }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Borrow<[TapNodeHash]> for TaprootMerkleBranch {
|
impl Borrow<[TapNodeHash]> for TaprootMerkleBranchBuf {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn borrow(&self) -> &[TapNodeHash] { &self.0 }
|
fn borrow(&self) -> &[TapNodeHash] { &self.0 }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl BorrowMut<[TapNodeHash]> for TaprootMerkleBranch {
|
impl BorrowMut<[TapNodeHash]> for TaprootMerkleBranchBuf {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn borrow_mut(&mut self) -> &mut [TapNodeHash] { &mut self.0 }
|
fn borrow_mut(&mut self) -> &mut [TapNodeHash] { &mut self.0 }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Iterator over node hashes within Taproot Merkle branch.
|
/// Iterator over node hashes within Taproot Merkle branch.
|
||||||
///
|
///
|
||||||
/// This is created by `into_iter` method on `TaprootMerkleBranch` (via `IntoIterator` trait).
|
/// This is created by `into_iter` method on `TaprootMerkleBranchBuf` (via `IntoIterator` trait).
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct IntoIter(alloc::vec::IntoIter<TapNodeHash>);
|
pub struct IntoIter(alloc::vec::IntoIter<TapNodeHash>);
|
||||||
|
|
||||||
|
|
|
@ -27,7 +27,7 @@ use crate::{Script, ScriptBuf};
|
||||||
#[doc(inline)]
|
#[doc(inline)]
|
||||||
pub use crate::crypto::taproot::{SigFromSliceError, Signature};
|
pub use crate::crypto::taproot::{SigFromSliceError, Signature};
|
||||||
#[doc(inline)]
|
#[doc(inline)]
|
||||||
pub use merkle_branch::TaprootMerkleBranch;
|
pub use merkle_branch::TaprootMerkleBranchBuf;
|
||||||
|
|
||||||
// Taproot test vectors from BIP-341 state the hashes without any reversing
|
// Taproot test vectors from BIP-341 state the hashes without any reversing
|
||||||
sha256t_tag! {
|
sha256t_tag! {
|
||||||
|
@ -190,7 +190,7 @@ pub struct LeafScript<S> {
|
||||||
}
|
}
|
||||||
|
|
||||||
// type alias for versioned tap script corresponding Merkle proof
|
// type alias for versioned tap script corresponding Merkle proof
|
||||||
type ScriptMerkleProofMap = BTreeMap<(ScriptBuf, LeafVersion), BTreeSet<TaprootMerkleBranch>>;
|
type ScriptMerkleProofMap = BTreeMap<(ScriptBuf, LeafVersion), BTreeSet<TaprootMerkleBranchBuf>>;
|
||||||
|
|
||||||
/// Represents Taproot spending information.
|
/// Represents Taproot spending information.
|
||||||
///
|
///
|
||||||
|
@ -221,7 +221,7 @@ pub struct TaprootSpendInfo {
|
||||||
output_key_parity: secp256k1::Parity,
|
output_key_parity: secp256k1::Parity,
|
||||||
/// The tweaked output key.
|
/// The tweaked output key.
|
||||||
output_key: TweakedPublicKey,
|
output_key: TweakedPublicKey,
|
||||||
/// Map from (script, leaf_version) to (sets of) [`TaprootMerkleBranch`]. More than one control
|
/// Map from (script, leaf_version) to (sets of) [`TaprootMerkleBranchBuf`]. More than one control
|
||||||
/// block for a given script is only possible if it appears in multiple branches of the tree. In
|
/// block for a given script is only possible if it appears in multiple branches of the tree. In
|
||||||
/// all cases, keeping one should be enough for spending funds, but we keep all of the paths so
|
/// all cases, keeping one should be enough for spending funds, but we keep all of the paths so
|
||||||
/// that a full tree can be constructed again from spending data if required.
|
/// that a full tree can be constructed again from spending data if required.
|
||||||
|
@ -1037,7 +1037,7 @@ pub struct LeafNode {
|
||||||
/// The [`TapLeaf`]
|
/// The [`TapLeaf`]
|
||||||
leaf: TapLeaf,
|
leaf: TapLeaf,
|
||||||
/// The Merkle proof (hashing partners) to get this node.
|
/// The Merkle proof (hashing partners) to get this node.
|
||||||
merkle_branch: TaprootMerkleBranch,
|
merkle_branch: TaprootMerkleBranchBuf,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl LeafNode {
|
impl LeafNode {
|
||||||
|
@ -1091,9 +1091,9 @@ impl LeafNode {
|
||||||
pub fn leaf_version(&self) -> Option<LeafVersion> { self.leaf.as_script().map(|x| x.1) }
|
pub fn leaf_version(&self) -> Option<LeafVersion> { self.leaf.as_script().map(|x| x.1) }
|
||||||
|
|
||||||
/// Returns reference to the Merkle proof (hashing partners) to get this
|
/// Returns reference to the Merkle proof (hashing partners) to get this
|
||||||
/// node in form of [`TaprootMerkleBranch`].
|
/// node in form of [`TaprootMerkleBranchBuf`].
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn merkle_branch(&self) -> &TaprootMerkleBranch { &self.merkle_branch }
|
pub fn merkle_branch(&self) -> &TaprootMerkleBranchBuf { &self.merkle_branch }
|
||||||
|
|
||||||
/// Returns a reference to the leaf of this [`ScriptLeaf`].
|
/// Returns a reference to the leaf of this [`ScriptLeaf`].
|
||||||
#[inline]
|
#[inline]
|
||||||
|
@ -1109,7 +1109,7 @@ pub struct ScriptLeaf<'leaf> {
|
||||||
/// The script.
|
/// The script.
|
||||||
script: &'leaf Script,
|
script: &'leaf Script,
|
||||||
/// The Merkle proof (hashing partners) to get this node.
|
/// The Merkle proof (hashing partners) to get this node.
|
||||||
merkle_branch: &'leaf TaprootMerkleBranch,
|
merkle_branch: &'leaf TaprootMerkleBranchBuf,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'leaf> ScriptLeaf<'leaf> {
|
impl<'leaf> ScriptLeaf<'leaf> {
|
||||||
|
@ -1120,7 +1120,7 @@ impl<'leaf> ScriptLeaf<'leaf> {
|
||||||
pub fn script(&self) -> &Script { self.script }
|
pub fn script(&self) -> &Script { self.script }
|
||||||
|
|
||||||
/// Obtains a reference to the Merkle proof of the leaf.
|
/// Obtains a reference to the Merkle proof of the leaf.
|
||||||
pub fn merkle_branch(&self) -> &TaprootMerkleBranch { self.merkle_branch }
|
pub fn merkle_branch(&self) -> &TaprootMerkleBranchBuf { self.merkle_branch }
|
||||||
|
|
||||||
/// Obtains a script leaf from the leaf node if the leaf is not hidden.
|
/// Obtains a script leaf from the leaf node if the leaf is not hidden.
|
||||||
pub fn from_leaf_node(leaf_node: &'leaf LeafNode) -> Option<Self> {
|
pub fn from_leaf_node(leaf_node: &'leaf LeafNode) -> Option<Self> {
|
||||||
|
@ -1140,7 +1140,7 @@ pub struct ControlBlock {
|
||||||
/// The internal key.
|
/// The internal key.
|
||||||
pub internal_key: UntweakedPublicKey,
|
pub internal_key: UntweakedPublicKey,
|
||||||
/// The Merkle proof of a script associated with this leaf.
|
/// The Merkle proof of a script associated with this leaf.
|
||||||
pub merkle_branch: TaprootMerkleBranch,
|
pub merkle_branch: TaprootMerkleBranchBuf,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ControlBlock {
|
impl ControlBlock {
|
||||||
|
@ -1172,7 +1172,7 @@ impl ControlBlock {
|
||||||
&sl[1..TAPROOT_CONTROL_BASE_SIZE].try_into().expect("Slice should be exactly 32 bytes"),
|
&sl[1..TAPROOT_CONTROL_BASE_SIZE].try_into().expect("Slice should be exactly 32 bytes"),
|
||||||
)
|
)
|
||||||
.map_err(TaprootError::InvalidInternalKey)?;
|
.map_err(TaprootError::InvalidInternalKey)?;
|
||||||
let merkle_branch = TaprootMerkleBranch::decode(&sl[TAPROOT_CONTROL_BASE_SIZE..])?;
|
let merkle_branch = TaprootMerkleBranchBuf::decode(&sl[TAPROOT_CONTROL_BASE_SIZE..])?;
|
||||||
Ok(ControlBlock { leaf_version, output_key_parity, internal_key, merkle_branch })
|
Ok(ControlBlock { leaf_version, output_key_parity, internal_key, merkle_branch })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1923,7 +1923,7 @@ mod test {
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.to_byte_array(),
|
.to_byte_array(),
|
||||||
);
|
);
|
||||||
let merkle_branch = TaprootMerkleBranch::from([hash1, hash2]);
|
let merkle_branch = TaprootMerkleBranchBuf::from([hash1, hash2]);
|
||||||
// use serde_test to test serialization and deserialization
|
// use serde_test to test serialization and deserialization
|
||||||
serde_test::assert_tokens(
|
serde_test::assert_tokens(
|
||||||
&merkle_branch.readable(),
|
&merkle_branch.readable(),
|
||||||
|
|
Loading…
Reference in New Issue