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 fn new() -> Self

Creates a new empty Witness.

source

pub fn p2wpkh(signature: &Signature, pubkey: &PublicKey) -> Witness

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 serialized public key. Also useful for spending a P2SH-P2WPKH output.

It is expected that pubkey is related to the secret key used to create signature.

source

pub fn from_slice<T: AsRef<[u8]>>(slice: &[T]) -> Self

Creates a 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 fn len(&self) -> usize

Returns the number of elements this witness holds.

source

pub fn serialized_len(&self) -> usize

👎Deprecated since 0.31.0: use size instead

Returns the bytes required when this Witness is consensus encoded.

source

pub fn size(&self) -> usize

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

source

pub fn clear(&mut self)

Clear the witness.

source

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

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

source

pub fn push_bitcoin_signature( &mut self, signature: &SerializedSignature, hash_type: EcdsaSighashType )

👎Deprecated since 0.30.0: use push_ecdsa_signature instead

Pushes a DER-encoded ECDSA signature with a signature hash type as a new element on the witness, requires an allocation.

source

pub fn push_ecdsa_signature(&mut self, signature: &Signature)

Pushes, as a new element on the witness, an ECDSA signature.

Pushes the DER encoded signature + sighash_type, requires an allocation.

source

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

Returns the last element in the witness, if any.

source

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

Returns the second-to-last element in the witness, if any.

source

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

Return the nth element in the witness, if any

source

pub fn tapscript(&self) -> Option<&Script>

Get Tapscript following BIP341 rules regarding accounting for an annex.

This does not guarantee that this represents a P2TR Witness. It merely gets the second to last or third to last element depending on the first byte of the last element being equal to 0x50. See Script::is_v1_p2tr to check whether this is actually a Taproot witness.

Trait Implementations§

source§

impl Clone for Witness

source§

fn clone(&self) -> Witness

Returns a copy 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

source§

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

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

impl Decodable for Witness

source§

fn consensus_decode<R: Read + ?Sized>(r: &mut R) -> Result<Self, Error>

Decode an object with a well-defined format. Read more
source§

fn consensus_decode_from_finite_reader<R: Read + ?Sized>( reader: &mut R ) -> Result<Self, Error>

Decode Self from a size-limited reader. Read more
source§

impl Default for Witness

source§

fn default() -> Witness

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§

fn consensus_encode<W: Write + ?Sized>(&self, w: &mut W) -> Result<usize, Error>

Encodes an object with a well-defined format. Read more
source§

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

source§

fn from(slice: &[&[u8]]) -> 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 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 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

§

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

§

type IntoIter = Iter<'a>

Which kind of iterator are we turning this into?
§

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) -> Selfwhere Self: Sized,

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

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

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

fn clamp(self, min: Self, max: Self) -> Selfwhere Self: Sized + PartialOrd,

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

impl PartialEq for Witness

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method 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

This method 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

This method 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

This method 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

This method 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 StructuralEq for Witness

source§

impl StructuralPartialEq for Witness

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. 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 Twhere 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 Twhere T: Clone,

§

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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V

source§

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