starknet.js icon indicating copy to clipboard operation
starknet.js copied to clipboard

Add provider function to get L1 message hash (where applicable)

Open amanusk opened this issue 2 years ago • 2 comments

Is your feature request related to a problem? Please describe. It would be great if the SDK supports fetching and computing the L1 message hash of transactions resulting from L1->L2 interactions

Describe the solution you'd like A provider function getL1MessageHash which receives an L2 tx hash, and returns the L1 message hash, if it is applicable.

Context L1 message hashes are computed in the following way link to docs.

A snippet of solidity code to compute this:

pragma solidity >=0.8.2 <0.9.0;
contract Hasher {
  function getL1ToL2MsgHash(
        address fromAddress,
        uint256 toAddress,
        uint256 selector,
        uint256[] calldata payload,
        uint256 nonce
    ) external view returns (bytes32) {
        return
            keccak256(
                abi.encodePacked(
                    uint256(uint160(fromAddress)),
                    toAddress,
                    nonce,
                    selector,
                    payload.length,
                    payload
                )
            );
    }
}

The required information to compute this on L2 is available when querying getTransactionByHash Example: mainnet tx: 0x0075f4ad64b9eca93b4e1bb48fa3db4260e476fcf65e7ab97bf5f60a2a3022ce

The result of getTransactionbyHash

{
  "jsonrpc": "2.0",
  "id": 0,
  "result": {
    "calldata": [
      "0xae0ee0a63a2ce6baeeffe56e7714fb4efe48d419",
      "0x1d8ec38501c2fddf0b17fcd38df2029fb059b2016889e6962e5f19449735108",
      "0x354a6ba7a180000",
      "0x0"
    ],
    "contract_address": "0x73314940630fd6dcda0d772d4c972c4e0a9946bef9dabf4ef84eda8ef542b82",
    "entry_point_selector": "0x2d757788a8d8d6f21d1cd40bce38a8222d70654214e96ff95d8086e684fbee5",
    "nonce": "0x15b1a",
    "transaction_hash": "0x75f4ad64b9eca93b4e1bb48fa3db4260e476fcf65e7ab97bf5f60a2a3022ce",
    "type": "L1_HANDLER",
    "version": "0x0"
  }
}

The first param of calldata == fromAddress. contract_address == toAddress nonce == nonce entry_point_selector == selector

payload and payload.length can be derived from the calldata field.

In case the L2 tx is not a result of an l1->l2 interaction, the function can return null (or error)

This can be a good first issue

amanusk avatar Aug 02 '23 07:08 amanusk

Hello, Why are you not using an Ethereum js library to perform this calculation (ethers.js, web3.js, ...)?

PhilippeR26 avatar Aug 03 '23 15:08 PhilippeR26

Of course, you can use any tool supporting hashes to make the calculation.

The information is available on Starknet, and can be interesting for developers. (e.g. for block explorers, indexers etc). We can provide them with this computation without each developer having to implement it on their own.

It should be possible to get the information w/o the need to connect to an L1 node

amanusk avatar Aug 14 '23 04:08 amanusk

I would like to work on this ODHack @ivpavici

0xibs avatar Apr 22 '24 06:04 0xibs

Hi @ivpavici, work is still in progress on this. Will create a draft PR soon so you can review

0xibs avatar Apr 25 '24 15:04 0xibs

From the docs, we are to use pedersen function to calculate the corresponding transaction on L2: image How do I use the pedersen function available in starknetjs to achieve this since it only takes two arguments? image

0xibs avatar May 01 '24 07:05 0xibs

@ivpavici @PhilippeR26 @amanusk

0xibs avatar May 01 '24 08:05 0xibs

https://github.com/starknet-io/starknet.js/blob/b3a2946a3d1e89fd16c542cf19e4fcedb6777ef5/src/utils/hash/classHash.ts#L39

PhilippeR26 avatar May 01 '24 09:05 PhilippeR26