Skip to main content

HashFinder

Struct HashFinder 

Source
pub struct HashFinder { /* private fields */ }
Expand description

HashFinder is a Structure for finding cryptographic hashes that meet a specified difficulty target, defined by a number of leading zeros. The core idea is to search for a hash that is lower than a computed target value.

Implementations§

Source§

impl HashFinder

Source

pub fn new(leading_zeros: u8) -> Self

Returns a HashFinder struct with a specified number of target leading zeros

§Example
use pow_account::HashFinder;

let hash_finder = HashFinder::new(4);
Source

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

Finds an origin hash

This function attempts to find a cryptographic hash that is an origin for a target hash that has a specific number of leading zeroes

§Parameters
  • leading_zeros: The number of leading zeros to generate in the hash.
§Returns

This function returns a 32-byte array containing the generated hash.

§Example
use pow_account::HashFinder;
use blake2::{Blake2s256, Digest};

let origin_hash = HashFinder::new(4).find();

let mut hasher = Blake2s256::new();
hasher.update(&origin_hash);
let target_hash: [u8; 32] = hasher.finalize().into();

let target_hash_hex = hex::encode(target_hash);

assert!(target_hash_hex.starts_with("0000"));
Source

pub fn check(&self, origin_hash: String) -> Result<bool, FromHexError>

Determines whether a given hash serves as the origin for a target hash that satisfies a specified number of leading zeros.

This function takes a hexadecimal string representing an origin hash and checks if its hash satisfies the leading zero requirement specified by the leading_zeros value.

§Parameters
  • hash: A string containing the hexadecimal representation of the hash to check.
§Returns

This function returns Ok(true) if the hash meets the requirement, otherwise it returns Ok(false). If the input string is not a valid hexadecimal representation, it returns an Err with a description of the error.

§Errors

This function returns an error if the provided hash string is not a valid hexadecimal representation.

§Examples
use pow_account::HashFinder;

let origin_hash = HashFinder::new(4).find();
let origin_hash_hex = hex::encode(origin_hash);
let result = HashFinder::new(3).check(origin_hash_hex);
assert!(result.unwrap());

let origin_hash = String::from("3ca727c7fefed674268797882ff4b26c8e28873ee6fbfae71d9ccc35e24444d4");
let result = HashFinder::new(3).check(origin_hash);
assert!(result.unwrap());

let origin_hash = String::from("51ad0600f06b0d57300a37952cea658410488748400628c8a2e7d712892d806e");
let result = HashFinder::default().check(origin_hash);
assert!(result.unwrap());

Trait Implementations§

Source§

impl Clone for HashFinder

Source§

fn clone(&self) -> HashFinder

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for HashFinder

Source§

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

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

impl Default for HashFinder

Source§

fn default() -> Self

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

impl Ord for HashFinder

Source§

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

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

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

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

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

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

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

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

impl PartialEq for HashFinder

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · 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 HashFinder

Source§

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

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · 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 (const: unstable) · 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 (const: unstable) · 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 (const: unstable) · 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 Copy for HashFinder

Source§

impl Eq for HashFinder

Source§

impl StructuralPartialEq for HashFinder

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