Skip to main content

Witness

Struct Witness 

Source
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

Source

pub const fn new() -> Self

Constructs a new empty Witness.

Source

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.

Source

pub fn to_vec(&self) -> Vec<Vec<u8>>

Convenience method to create an array of byte-arrays from this witness.

Source

pub fn is_empty(&self) -> bool

Returns true if the witness contains no element.

Source

pub fn iter(&self) -> Iter<'_>

Returns a struct implementing Iterator.

Source

pub const fn len(&self) -> usize

Returns the number of elements this witness holds.

Source

pub fn size(&self) -> usize

Returns the number of bytes this witness contributes to a transactions total size.

§Panics

If the size calculation overflows.

Source

pub fn clear(&mut self)

Clears the witness.

Source

pub fn push<T: AsRef<[u8]>>(&mut self, new_element: T)

Pushes a new element on the witness, requires an allocation.

Source

pub fn last(&self) -> Option<&[u8]>

Returns the last element in the witness, if any.

Source

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);
Source

pub fn get(&self, index: usize) -> Option<&[u8]>

Returns a specific element from the witness by its index, if any.

Source

pub fn from_hex<I, T>(iter: I) -> Result<Self, DecodeVariableLengthBytesError>
where I: IntoIterator<Item = T>, T: AsRef<str>,

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.
Source§

fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self>

Generate an arbitrary value of Self from the given unstructured data. Read more
Source§

fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self, Error>

Generate an arbitrary value of Self from the entirety of the given unstructured data. Read more
Source§

fn size_hint(depth: usize) -> (usize, Option<usize>)

Get a size hint for how many bytes out of an Unstructured this type needs to construct itself. Read more
Source§

fn try_size_hint( depth: usize, ) -> Result<(usize, Option<usize>), MaxRecursionReached>

Get a size hint for how many bytes out of an Unstructured this type needs to construct itself. Read more
Source§

impl Clone for Witness

Source§

fn clone(&self) -> Witness

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

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 hex feature is enabled.
Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Decodable for Witness

Source§

type Decoder = WitnessDecoder

Associated decoder for the type.
Source§

fn decoder() -> Self::Decoder

Constructs a “default decoder” for the type.
Source§

impl Default for Witness

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for Witness

Available on crate feature serde only.
Source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Encodable for Witness

Source§

type Encoder<'e> = WitnessEncoder<'e> where Self: 'e

The encoder associated with this type. Conceptually, the encoder is like an iterator which yields byte slices.
Source§

fn encoder(&self) -> Self::Encoder<'_>

Constructs a “default encoder” for the type.
Source§

impl From<&[&[u8]]> for Witness

Source§

fn from(slice: &[&[u8]]) -> Self

Converts to this type from the input type.
Source§

impl<const N: usize> From<&[&[u8]; N]> for Witness

Source§

fn from(arr: &[&[u8]; N]) -> Self

Converts to this type from the input type.
Source§

impl<const N: usize, const M: usize> From<&[&[u8; M]; N]> for Witness

Source§

fn from(slice: &[&[u8; M]; N]) -> Self

Converts to this type from the input type.
Source§

impl<const N: usize> From<&[&[u8; N]]> for Witness

Source§

fn from(slice: &[&[u8; N]]) -> Self

Converts to this type from the input type.
Source§

impl<const N: usize, const M: usize> From<&[[u8; M]; N]> for Witness

Source§

fn from(slice: &[[u8; M]; N]) -> Self

Converts to this type from the input type.
Source§

impl<const N: usize> From<&[[u8; N]]> for Witness

Source§

fn from(slice: &[[u8; N]]) -> Self

Converts to this type from the input type.
Source§

impl From<&[Vec<u8>]> for Witness

Source§

fn from(slice: &[Vec<u8>]) -> Self

Converts to this type from the input type.
Source§

impl<const N: usize> From<[&[u8]; N]> for Witness

Source§

fn from(arr: [&[u8]; N]) -> Self

Converts to this type from the input type.
Source§

impl<const N: usize, const M: usize> From<[&[u8; M]; N]> for Witness

Source§

fn from(slice: [&[u8; M]; N]) -> Self

Converts to this type from the input type.
Source§

impl<const N: usize, const M: usize> From<[[u8; M]; N]> for Witness

Source§

fn from(slice: [[u8; M]; N]) -> Self

Converts to this type from the input type.
Source§

impl From<Vec<&[u8]>> for Witness

Source§

fn from(vec: Vec<&[u8]>) -> Self

Converts to this type from the input type.
Source§

impl From<Vec<Vec<u8>>> for Witness

Source§

fn from(vec: Vec<Vec<u8>>) -> Self

Converts to this type from the input type.
Source§

impl<T: AsRef<[u8]>> FromIterator<T> for Witness

Source§

fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self

Creates a value from an iterator. Read more
Source§

impl Hash for Witness

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Index<usize> for Witness

Source§

type Output = [u8]

The returned type after indexing.
Source§

fn index(&self, index: usize) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<'a> IntoIterator for &'a Witness

Source§

type IntoIter = Iter<'a>

Which kind of iterator are we turning this into?
Source§

type Item = &'a [u8]

The type of the elements being iterated over.
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl Ord for Witness

Source§

fn cmp(&self, other: &Witness) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<T: Borrow<[u8]>> PartialEq<&[T]> for Witness

Source§

fn eq(&self, rhs: &&[T]) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<const N: usize, T: Borrow<[u8]>> PartialEq<&[T; N]> for Witness

Source§

fn eq(&self, rhs: &&[T; N]) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: Borrow<[u8]>> PartialEq<[T]> for Witness

Source§

fn eq(&self, rhs: &[T]) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<const N: usize, T: Borrow<[u8]>> PartialEq<[T; N]> for Witness

Source§

fn eq(&self, rhs: &[T; N]) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: Borrow<[u8]>> PartialEq<Arc<[T]>> for Witness

Available on target_has_atomic=ptr only.
Source§

fn eq(&self, rhs: &Arc<[T]>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: Borrow<[u8]>> PartialEq<Box<[T]>> for Witness

Source§

fn eq(&self, rhs: &Box<[T]>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: Borrow<[u8]>> PartialEq<Rc<[T]>> for Witness

Source§

fn eq(&self, rhs: &Rc<[T]>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: Borrow<[u8]>> PartialEq<Vec<T>> for Witness

Source§

fn eq(&self, rhs: &Vec<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: Borrow<[u8]>> PartialEq<Witness> for &[T]

Source§

fn eq(&self, rhs: &Witness) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<const N: usize, T: Borrow<[u8]>> PartialEq<Witness> for &[T; N]

Source§

fn eq(&self, rhs: &Witness) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: Borrow<[u8]>> PartialEq<Witness> for [T]

Source§

fn eq(&self, rhs: &Witness) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<const N: usize, T: Borrow<[u8]>> PartialEq<Witness> for [T; N]

Source§

fn eq(&self, rhs: &Witness) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: Borrow<[u8]>> PartialEq<Witness> for Arc<[T]>

Available on target_has_atomic=ptr only.
Source§

fn eq(&self, rhs: &Witness) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: Borrow<[u8]>> PartialEq<Witness> for Box<[T]>

Source§

fn eq(&self, rhs: &Witness) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: Borrow<[u8]>> PartialEq<Witness> for Rc<[T]>

Source§

fn eq(&self, rhs: &Witness) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: Borrow<[u8]>> PartialEq<Witness> for Vec<T>

Source§

fn eq(&self, rhs: &Witness) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq for Witness

Source§

fn eq(&self, other: &Witness) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for Witness

Source§

fn partial_cmp(&self, other: &Witness) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Serialize for Witness

Available on crate feature serde only.
Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Eq for Witness

Source§

impl StructuralPartialEq for Witness

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,