Struct Witness

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§

§

impl Witness

pub const fn new() -> Witness

Creates a new empty Witness.

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.

pub fn p2tr_key_spend(signature: &Signature) -> Witness

Creates a witness required to do a key path spend of a P2TR output.

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

Creates a Witness object from a slice of bytes slices where each slice is a witness item.

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

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

pub fn is_empty(&self) -> bool

Returns true if the witness contains no element.

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

Returns a struct implementing Iterator.

pub fn len(&self) -> usize

Returns the number of elements this witness holds.

pub fn size(&self) -> usize

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

pub fn clear(&mut self)

Clear the witness.

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

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

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.

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

Returns the last element in the witness, if any.

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

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

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

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

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

Return the nth element in the witness, if any

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

👎Deprecated: use taproot_leaf_script and check leaf version, if applicable

Get leaf script following BIP341 rules regarding accounting for an annex.

This method is broken: it’s called tapscript but it’s actually returning a leaf script. We’re not going to fix it because someone might be relying on it thinking leaf script and tapscript are the same thing (they are not). Instead, this is deprecated and will be removed in the next breaking release. You need to use taproot_leaf_script and if you intended to use it as leaf script, just access the script field of the returned type. If you intended tapscript specifically you have to check the version first and bail if it’s not LeafVersion::TapScript.

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_p2tr to check whether this is actually a Taproot witness.

pub fn taproot_leaf_script(&self) -> Option<LeafScript<&Script>>

Returns the leaf script with its version but without the merkle proof.

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 and the associated version.

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

Get the taproot control block following BIP341 rules.

This does not guarantee that this represents a P2TR Witness. It merely gets the last or second to last element depending on the first byte of the last element being equal to 0x50.

See Script::is_p2tr to check whether this is actually a Taproot witness.

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

Get the taproot annex following BIP341 rules.

This does not guarantee that this represents a P2TR Witness.

See Script::is_p2tr to check whether this is actually a Taproot witness.

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

Get the p2wsh witness script following BIP141 rules.

This does not guarantee that this represents a P2WS Witness. See Script::is_p2wsh to check whether this is actually a P2WSH witness.

Trait Implementations§

§

impl Clone for Witness

§

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
§

impl Debug for Witness

§

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

Formats the value using the given formatter. Read more
§

impl Decodable for Witness

§

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

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

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

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

impl Default for Witness

§

fn default() -> Witness

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

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

§

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

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

impl Encodable for Witness

§

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

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

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

§

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

Converts to this type from the input type.
§

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

§

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

Converts to this type from the input type.
§

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

§

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

Converts to this type from the input type.
§

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

§

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

Converts to this type from the input type.
§

impl Hash for Witness

§

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

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
§

impl Index<usize> for Witness

§

type Output = [u8]

The returned type after indexing.
§

fn index(&self, index: usize) -> &<Witness as Index<usize>>::Output

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

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

fn into_iter(self) -> <&'a Witness as IntoIterator>::IntoIter

Creates an iterator from a value. Read more
§

impl Ord for Witness

§

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
§

impl PartialEq for Witness

§

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

impl PartialOrd for Witness

§

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
§

impl Readable for Witness

§

fn read<R>(r: &mut R) -> Result<Witness, DecodeError>
where R: Read,

Reads a Self in from the given Read.
§

impl Serialize for Witness

§

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

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

impl Writeable for Witness

§

fn write<W>(&self, writer: &mut W) -> Result<(), Error>
where W: Writer,

Writes self out to the given Writer.
§

fn encode(&self) -> Vec<u8>

Writes self out to a Vec<u8>.
§

fn serialized_length(&self) -> usize

Gets the length of this object after it has been serialized. This can be overridden to optimize cases where we prepend an object with its length.
§

impl Eq for Witness

§

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
§

impl<T> Any for T
where T: Any,

§

fn into_any(self: Box<T>) -> Box<dyn Any>

§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

§

fn type_name(&self) -> &'static str

§

impl<T> AnyEq for T
where T: Any + PartialEq,

§

fn equals(&self, other: &(dyn Any + 'static)) -> bool

§

fn as_any(&self) -> &(dyn Any + 'static)

§

impl<T> AnySync for T
where T: Any + Send + Sync,

§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync>

§

impl<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

§

fn implicit( self, class: Class, constructed: bool, tag: u32, ) -> TaggedParser<'a, Implicit, Self, E>

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
§

impl<Q, K> Comparable<K> for Q
where Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

§

fn compare(&self, key: &K) -> Ordering

Compare self to key and return their ordering.
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

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

§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> IntoRequest<T> for T

Source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
§

impl<T> MaybeReadable for T
where T: Readable,

§

fn read<R>(reader: &mut R) -> Result<Option<T>, DecodeError>
where R: Read,

Reads a Self in from the given Read.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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.
§

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

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

impl<T> DartSafe for T

Source§

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

§

impl<T> ErasedDestructor for T
where T: 'static,

§

impl<T> TaskRetFutTrait for T
where T: Send,