Skip to main content

Hash

Struct Hash 

Source
pub struct Hash(/* private fields */);
Expand description

A 32-byte hash used for transaction IDs, block hashes, and merkle trees.

When displayed as a string, the bytes are reversed to match Bitcoin’s standard representation (little-endian internal, big-endian display).

Implementations§

Source§

impl Hash

Source

pub fn new(bytes: [u8; 32]) -> Self

Create a Hash from a raw 32-byte array.

The bytes are stored as-is (internal byte order).

§Arguments
  • bytes - The 32 bytes in internal (little-endian) order.
§Returns

A new Hash.

Source

pub fn from_bytes(bytes: &[u8]) -> Result<Self, PrimitivesError>

Create a Hash from a byte slice.

§Arguments
  • bytes - A slice that must be exactly 32 bytes.
§Returns

Ok(Hash) if the slice is 32 bytes, or an error otherwise.

Source

pub fn from_hex(hex_str: &str) -> Result<Self, PrimitivesError>

Create a Hash from a byte-reversed hex string.

This matches the Go SDK’s NewHashFromHex / Decode function. The hex string represents bytes in display order (reversed from internal storage). Short strings are zero-padded on the high end.

§Arguments
  • hex_str - A hex string of up to 64 characters.
§Returns

Ok(Hash) on success, or an error for invalid input.

Source

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

Return a copy of the internal bytes.

§Returns

A Vec<u8> containing the 32 hash bytes in internal order.

Source

pub fn set_bytes(&mut self, bytes: &[u8]) -> Result<(), PrimitivesError>

Set the hash bytes from a slice.

§Arguments
  • bytes - A slice that must be exactly 32 bytes.
§Returns

Ok(()) on success, or an error if the length is wrong.

Source

pub fn is_equal(&self, other: Option<&Hash>) -> bool

Check equality with another Hash reference.

Handles the None/null case like the Go SDK’s IsEqual method.

§Arguments
  • other - An optional reference to another Hash.
§Returns

true if both hashes are equal.

Source

pub fn as_bytes(&self) -> &[u8; 32]

Access the internal byte array as a reference.

§Returns

A reference to the 32-byte internal array.

Source

pub fn size(&self) -> usize

Return the size of the hash in bytes.

§Returns

Always returns 32.

Trait Implementations§

Source§

impl Clone for Hash

Source§

fn clone(&self) -> Hash

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 Hash

Source§

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

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

impl Default for Hash

Source§

fn default() -> Hash

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

impl<'de> Deserialize<'de> for Hash

Deserialize from a hex string in JSON.

Source§

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

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

impl Display for Hash

Display the hash as byte-reversed hex (Bitcoin convention).

Internal bytes [0x06, 0xe5, ...] display as "...e506".

Source§

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

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

impl FromStr for Hash

Parse a byte-reversed hex string into a Hash.

Equivalent to Hash::from_hex.

Source§

type Err = PrimitivesError

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl Hash for Hash

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 PartialEq for Hash

Source§

fn eq(&self, other: &Hash) -> 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 Serialize for Hash

Serialize as a hex string in JSON.

Source§

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

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

impl Copy for Hash

Source§

impl Eq for Hash

Source§

impl StructuralPartialEq for Hash

Auto Trait Implementations§

§

impl Freeze for Hash

§

impl RefUnwindSafe for Hash

§

impl Send for Hash

§

impl Sync for Hash

§

impl Unpin for Hash

§

impl UnsafeUnpin for Hash

§

impl UnwindSafe for Hash

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> 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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

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