Skip to content
This repository was archived by the owner on Jul 22, 2024. It is now read-only.

Commit 8f40853

Browse files
authored
Add docs for hash_utils (#771)
1 parent f544959 commit 8f40853

File tree

1 file changed

+82
-1
lines changed

1 file changed

+82
-1
lines changed

src/hash_utils.rs

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,54 @@ use num_traits::Pow;
55
use starknet_crypto::{pedersen_hash, FieldElement};
66
use std::vec;
77

8+
/// Computes the contract address.
9+
///
10+
/// It is calculated using the following formula:
11+
/// ```text
12+
/// contract_address := pedersen(
13+
/// “STARKNET_CONTRACT_ADDRESS”,
14+
/// caller_address,
15+
/// salt,
16+
/// class_hash,
17+
/// pedersen(constructor_calldata))
18+
///```
19+
///
20+
/// # Arguments
21+
///
22+
/// * `salt` - Part of the deploy transaction
23+
/// * `class_hash` - The hash value of the contract class.
24+
/// * `constructor_calldata` - A slice of `Felt252` elements representing the constructor calldata.
25+
/// * `deployer_address` - The address of the deployer/caller of the contract.
26+
///
27+
/// # Returns
28+
///
29+
/// Returns a `Result` containing the computed contract address.
30+
/// If any errors occur during the hash computation or other operations, an `Err` variant containing
31+
/// a `SyscallHandlerError` is returned.
32+
///
33+
/// # Examples
34+
///
35+
/// ```
36+
/// use starknet_in_rust::{hash_utils::calculate_contract_address, utils::Address, Felt252};
37+
///
38+
/// let salt = Felt252::from(123_u16);
39+
/// let class_hash = Felt252::from(456_u16);
40+
/// let constructor_calldata = vec![
41+
/// Felt252::from(10_u16),
42+
/// Felt252::from(20_u16),
43+
/// Felt252::from(30_u16),
44+
/// ];
45+
/// let deployer_address = Address(Felt252::from(789_u16));
46+
///
47+
/// match calculate_contract_address(&salt, &class_hash, &constructor_calldata, deployer_address) {
48+
/// Ok(contract_address) => {
49+
/// println!("Computed contract address: {:?}", contract_address);
50+
/// }
51+
/// Err(err) => {
52+
/// println!("Error occurred: {:?}", err);
53+
/// }
54+
/// }
55+
/// ```
856
pub fn calculate_contract_address(
957
salt: &Felt252,
1058
class_hash: &Felt252,
@@ -28,7 +76,40 @@ pub fn calculate_contract_address(
2876
Ok(raw_address.mod_floor(&l2_address_upper_bound))
2977
}
3078

31-
pub(crate) fn compute_hash_on_elements(vec: &[Felt252]) -> Result<Felt252, SyscallHandlerError> {
79+
/// Computes Pedersen hash for a slice of `Felt252` elements.
80+
///
81+
/// # Arguments
82+
///
83+
/// * `vec` - A slice of `Felt252` elements representing the input vector.
84+
///
85+
/// # Returns
86+
///
87+
/// Returns a `Result` containing the computed Pedersen hash value as `Felt252`.
88+
/// If any errors occur during the conversion or hash computation, an `Err` variant containing a `SyscallHandlerError`
89+
/// is returned.
90+
///
91+
/// # Examples
92+
///
93+
/// ```
94+
/// use starknet_in_rust::Felt252;
95+
/// use starknet_in_rust::hash_utils::compute_hash_on_elements;
96+
///
97+
/// let input_vec = vec![
98+
/// Felt252::from(10_u16),
99+
/// Felt252::from(20_u16),
100+
/// Felt252::from(30_u16),
101+
/// ];
102+
///
103+
/// match compute_hash_on_elements(&input_vec) {
104+
/// Ok(hash_value) => {
105+
/// println!("Computed hash value: {:?}", hash_value);
106+
/// }
107+
/// Err(err) => {
108+
/// println!("Error occurred: {:?}", err);
109+
/// }
110+
/// }
111+
/// ```
112+
pub fn compute_hash_on_elements(vec: &[Felt252]) -> Result<Felt252, SyscallHandlerError> {
32113
let mut felt_vec = vec
33114
.iter()
34115
.map(|num| {

0 commit comments

Comments
 (0)