Skip to content

Commit

Permalink
fix: remove MsgToL2 code and rework MsgToL1 hash function
Browse files Browse the repository at this point in the history
  • Loading branch information
glihm committed Nov 3, 2023
1 parent 8fac400 commit dd3060e
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 218 deletions.
216 changes: 0 additions & 216 deletions starknet-core/src/types/messaging.rs

This file was deleted.

2 changes: 0 additions & 2 deletions starknet-core/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ pub mod requests;
pub mod contract;
pub use contract::ContractArtifact;

pub mod messaging;

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum MaybePendingBlockWithTxHashes {
Expand Down
67 changes: 67 additions & 0 deletions starknet-core/src/types/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use sha3::{Digest, Keccak256};
use starknet_ff::FieldElement;

use super::{EthAddress, Hash256};
use crate::types::MsgToL1;

#[derive(Debug, Clone)]
pub struct MsgToL2 {
Expand Down Expand Up @@ -51,6 +52,42 @@ impl MsgToL2 {
}
}

impl MsgToL1 {
/// Calculates the message hash based on the algorithm documented here:
///
/// https://docs.starknet.io/documentation/architecture_and_concepts/Network_Architecture/messaging-mechanism/#structure_and_hashing_l2-l1
///
/// In the current version of the documentation, it's not stipulated
/// that `ToAddress` has to be padded, but it needs to.
/// In fact, the L1 Starknet Core Contract is padding the message
/// sender to compute the message hash.
///
/// Can be found in the file `StarknetMessaging.sol`, line 153 here: https://vscode.blockscan.com/ethereum/0x16938e4b59297060484fa56a12594d8d6f4177e8
pub fn hash(&self) -> Hash256 {
let mut hasher = Keccak256::new();

// FromAddress
hasher.update(self.from_address.to_bytes_be());

// ToAddress
hasher.update(self.to_address.to_bytes_be());

// Payload.length
hasher.update([0u8; 24]);
hasher.update((self.payload.len() as u64).to_be_bytes());

// Payload
for item in self.payload.iter() {
hasher.update(item.to_bytes_be());
}

let hash = hasher.finalize();

// Because we know hash is always 32 bytes
Hash256::from_bytes(unsafe { *(hash[..].as_ptr() as *const [u8; 32]) })
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -88,4 +125,34 @@ mod tests {

assert_eq!(msg.hash(), expected_hash);
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
fn test_msg_to_l1_hash() {
// Goerli-1 tx (L1): 59e37da138dcf7ab9ca4fc7fde15d3f113a06781c4181dfcf0d74753023060b1 Log #40.
// Goerli-1 tx (L2): 4e0bbc07ff29e5df13dfbcb7e4746fdde52c3649a6a69bd86b15397769722fd

let msg = MsgToL1 {
from_address: FieldElement::from_hex_be("0x0164cba33fb7152531f6b4cfc3fff26b4d7b26b4900e0881042edd607b428a92")
.unwrap(),
to_address: FieldElement::from_hex_be(
"0x000000000000000000000000b6dbfaa86bb683152e4fc2401260f9ca249519c0",
)
.unwrap(),
payload: vec![
FieldElement::from_hex_be("0x0").unwrap(),
FieldElement::from_hex_be("0x0").unwrap(),
FieldElement::from_hex_be("0x0182b8").unwrap(),
FieldElement::from_hex_be("0x0").unwrap(),
FieldElement::from_hex_be("0x0384").unwrap(),
FieldElement::from_hex_be("0x0").unwrap(),
],
};

let expected_hash =
Hash256::from_hex("0x326a04493fc8f24ac6c6ae7bdba23243ce03ec3aae53f0ed3a0d686eb8cac930")
.unwrap();

assert_eq!(msg.hash(), expected_hash);
}
}

0 comments on commit dd3060e

Please sign in to comment.