Replies: 4 comments 9 replies
-
I think this can work, but note that you have a Cairo module - |
Beta Was this translation helpful? Give feedback.
-
It's faster because it's iterative and avoids the function call overhead. I'm not sure about another similar implementation. Internally we do use hash chains, e.g., for calculating a transaction hash. But the implementation is very similar to what you wrote, so it's pretty much the same idea. |
Beta Was this translation helpful? Give feedback.
-
@martriay I personally think it would be more readable to pass the |
Beta Was this translation helpful? Give feedback.
-
Hey @martriay I think one unexpected result from your hash function is: hash_calldata([12]) == 12 where as I'd expect a function called hash_state has also the advantage of already being implemented in python and Cairo. hash_message would then look like this: (from https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/starknet/services/api/gateway/transaction_hash.py#L44) def hash_message(to, selector, calldata, account_address, nonce):
calldata_hash = compute_hash_on_elements(calldata)
return compute_hash_on_elements([to, selector, calldata_hash, account_address, nonce]) |
Beta Was this translation helpful? Give feedback.
-
While prototyping the Account contract, I had to come up with a way to encode user intent in a verifiable way. This is, I needed to hash user messages in a way that could be validated by the Account contract before executing any transaction. And since Cairo's
hash2
takes two parameters, I came up with a simple algorithm:<to, selector, calldata, account, nonce>
. (account
is needed to prevent replays accross accounts)This is how the message hash looks today in python (from
Signer.py
):And this is how it's implemented on Cairo:
But...
While this seems to be good enough, I haven't thought thoroughly about it and there could be issues. It could be expensive, unsafe, hard to extend or generalize, or I don't know! What I do know is many projects are already relying on this implementation (e.g. Argent) so it makes sense to open the discussion around design of this critical piece of infrastructure.
So, what do you think?
Beta Was this translation helpful? Give feedback.
All reactions