-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathhash.cairo
75 lines (69 loc) · 2.29 KB
/
hash.cairo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//! SPDX-License-Identifier: MIT
//!
//! Hash utilities.
use starknet::ContractAddress;
/// Computes the hash of a message that is sent from Starknet to the Appchain.
///
/// <https://github.com/starkware-libs/cairo-lang/blob/caba294d82eeeccc3d86a158adb8ba209bf2d8fc/src/starkware/starknet/solidity/StarknetMessaging.sol#L88>
///
/// # Arguments
///
/// * `from_address` - Contract address of the message sender on the Appchain.
/// * `to_address` - Contract address to send the message to on the Appchain.
/// * `selector` - The `l1_handler` function selector of the contract on the Appchain
/// to execute.
/// * `payload` - The message payload.
/// * `nonce` - Nonce of the message.
///
/// # Returns
///
/// The hash of the message from Starknet to the Appchain.
fn compute_message_hash_sn_to_appc(
from_address: ContractAddress,
to_address: ContractAddress,
selector: felt252,
payload: Span<felt252>,
nonce: felt252
) -> felt252 {
let mut hash_data = array![
from_address.into(), to_address.into(), nonce, selector, payload.len().into(),
];
let mut i = 0_usize;
loop {
if i == payload.len() {
break;
}
hash_data.append((*payload[i]));
i += 1;
};
core::poseidon::poseidon_hash_span(hash_data.span())
}
/// Computes the hash of a message that is sent from the Appchain to Starknet.
///
/// <https://github.com/starkware-libs/cairo-lang/blob/caba294d82eeeccc3d86a158adb8ba209bf2d8fc/src/starkware/starknet/solidity/StarknetMessaging.sol#L137>
///
/// # Arguments
///
/// * `from_address` - Contract address of the message sender on the Appchain.
/// * `to_address` - Contract address to send the message to on the Appchain.
/// * `payload` - The message payload.
///
/// # Returns
///
/// The hash of the message from the Appchain to Starknet.
fn compute_message_hash_appc_to_sn(
from_address: ContractAddress, to_address: ContractAddress, payload: Span<felt252>
) -> felt252 {
let mut hash_data: Array<felt252> = array![
from_address.into(), to_address.into(), payload.len().into(),
];
let mut i = 0_usize;
loop {
if i == payload.len() {
break;
}
hash_data.append((*payload[i]));
i += 1;
};
core::poseidon::poseidon_hash_span(hash_data.span())
}