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
impl HashFinder
Sourcepub fn new(leading_zeros: u8) -> Self
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);
Sourcepub fn find(&self) -> [u8; 32]
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"));Sourcepub fn check(&self, origin_hash: String) -> Result<bool, FromHexError>
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
impl Clone for HashFinder
Source§fn clone(&self) -> HashFinder
fn clone(&self) -> HashFinder
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for HashFinder
impl Debug for HashFinder
Source§impl Default for HashFinder
impl Default for HashFinder
Source§impl Ord for HashFinder
impl Ord for HashFinder
Source§fn cmp(&self, other: &HashFinder) -> Ordering
fn cmp(&self, other: &HashFinder) -> Ordering
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl PartialEq for HashFinder
impl PartialEq for HashFinder
Source§fn eq(&self, other: &HashFinder) -> bool
fn eq(&self, other: &HashFinder) -> bool
self and other values to be equal, and is used by ==.