pub struct Witness { /* private fields */ }Expand description
The Witness is the data used to unlock bitcoin since the SegWit upgrade.
Can be logically seen as an array of bytestrings, i.e. Vec<Vec<u8>>, and it is serialized on the wire
in that format. You can convert between this type and Vec<Vec<u8>> by using Witness::from_slice
and Witness::to_vec.
For serialization and deserialization performance it is stored internally as a single Vec,
saving some allocations.
Implementations§
Source§impl Witness
impl Witness
Sourcepub fn from_slice<T: AsRef<[u8]>>(slice: &[T]) -> Self
pub fn from_slice<T: AsRef<[u8]>>(slice: &[T]) -> Self
Constructs a new Witness object from a slice of bytes slices where each slice is a witness item.
Sourcepub fn to_vec(&self) -> Vec<Vec<u8>>
pub fn to_vec(&self) -> Vec<Vec<u8>>
Convenience method to create an array of byte-arrays from this witness.
Sourcepub fn size(&self) -> usize
pub fn size(&self) -> usize
Returns the number of bytes this witness contributes to a transaction’s total size.
§Panics
If the size calculation overflows.
Sourcepub fn push<T: AsRef<[u8]>>(&mut self, new_element: T)
pub fn push<T: AsRef<[u8]>>(&mut self, new_element: T)
Pushes a new element on the witness, requires an allocation.
Sourcepub fn get_back(&self, index: usize) -> Option<&[u8]>
pub fn get_back(&self, index: usize) -> Option<&[u8]>
Retrieves an element from the end of the witness by its reverse index.
index is 0-based from the end, where 0 is the last element, 1 is the second-to-last, etc.
Returns None if the requested index is beyond the witness’s elements.
§Examples
use bitcoin_primitives::witness::Witness;
let mut witness = Witness::new();
witness.push(b"A");
witness.push(b"B");
witness.push(b"C");
witness.push(b"D");
assert_eq!(witness.get_back(0), Some(b"D".as_slice()));
assert_eq!(witness.get_back(1), Some(b"C".as_slice()));
assert_eq!(witness.get_back(2), Some(b"B".as_slice()));
assert_eq!(witness.get_back(3), Some(b"A".as_slice()));
assert_eq!(witness.get_back(4), None);Sourcepub fn get(&self, index: usize) -> Option<&[u8]>
pub fn get(&self, index: usize) -> Option<&[u8]>
Returns a specific element from the witness by its index, if any.
Sourcepub fn from_hex<I, T>(iter: I) -> Result<Self, DecodeVariableLengthBytesError>
pub fn from_hex<I, T>(iter: I) -> Result<Self, DecodeVariableLengthBytesError>
Constructs a new witness from a list of hex strings.
§Errors
This function will return an error if any of the hex strings are invalid.
Trait Implementations§
Source§impl<'a> Arbitrary<'a> for Witness
Available on crate feature arbitrary only.
impl<'a> Arbitrary<'a> for Witness
arbitrary only.Source§fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self>
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self>
Self from the given unstructured data. Read moreSource§fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self, Error>
fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self, Error>
Self from the entirety of the given
unstructured data. Read moreSource§fn size_hint(depth: usize) -> (usize, Option<usize>)
fn size_hint(depth: usize) -> (usize, Option<usize>)
Unstructured this type
needs to construct itself. Read moreSource§fn try_size_hint(
depth: usize,
) -> Result<(usize, Option<usize>), MaxRecursionReached>
fn try_size_hint( depth: usize, ) -> Result<(usize, Option<usize>), MaxRecursionReached>
Unstructured this type
needs to construct itself. Read moreSource§impl Debug for Witness
Debug implementation that displays the witness as a structured output containing:
impl Debug for Witness
Debug implementation that displays the witness as a structured output containing:
- Number of witness elements
- Total bytes across all elements
- List of hex-encoded witness elements if
hexfeature is enabled.
Source§impl<'de> Deserialize<'de> for Witness
Available on crate feature serde only.
impl<'de> Deserialize<'de> for Witness
serde only.Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
impl Eq for Witness
Source§impl<T: AsRef<[u8]>> FromIterator<T> for Witness
impl<T: AsRef<[u8]>> FromIterator<T> for Witness
Source§fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self
Source§impl<'a> IntoIterator for &'a Witness
impl<'a> IntoIterator for &'a Witness
Source§impl LowerHex for Witness
Available on crate feature hex only.Formats the witness as a hex string using its consensus encoding.
impl LowerHex for Witness
hex only.Formats the witness as a hex string using its consensus encoding.
This is the compact size encoded number of elements followed by each element prefixed with its compact size encoded length.
Source§impl Ord for Witness
impl Ord for Witness
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl<T: Borrow<[u8]>> PartialEq<Arc<[T]>> for Witness
Available on target_has_atomic=ptr only.
impl<T: Borrow<[u8]>> PartialEq<Arc<[T]>> for Witness
target_has_atomic=ptr only.Source§impl<T: Borrow<[u8]>> PartialEq<Witness> for Arc<[T]>
Available on target_has_atomic=ptr only.
impl<T: Borrow<[u8]>> PartialEq<Witness> for Arc<[T]>
target_has_atomic=ptr only.