Skip to content

Conversation

@thiagodeev
Copy link
Collaborator

@thiagodeev thiagodeev commented Sep 24, 2025

This PR aims to fix an error in the SNIP-12 selector type encoding.

The SNIP only says that this type should be keccak-hashed (and that's exactly what Starknet.go currently does, it just takes the JSON value and hashes it), but there are some other cases when this is not correct.

Here's one edge case: instead of a user passing a method name string (like transfer), they pass the already keccak-hashed hex string (e.g. 0x2f0b3c5710379609eb5495f1ecd348cb28167711b73609fe565a72734550354). In this case, hashing the JSON value will lead to a different encoding, as we would be hashing a different value than the function name, leading to a different final message hash of the entire typedData.

This is exactly what was happening in Starknet.go. The AVNU paymaster was returning a typedData with an already hashed selector string as the selector value, and we are hashing it again.
Now, we will do the same as Starknet.js, which is check if the JSON value is a hex string, and skip the hashing process if true. That way we can avoid this error.

It's not SNIP-12 compliant, but we can say this is more like a SNIP-12 description problem than an implementation problem.

Starknet.js refs:

- Introduced a new helper function isHexString to determine if a value is a valid hex string.
- Updated the encodePieceOfData function to skip hashing for already hashed selectors, avoiding wrong struct hashes due to hashing the wrong value.
- Introduced new test cases for the TypedData name "avnu_paymaster_invoke" in TestGetMessageHash, TestGetTypeHash, and TestGetStructHash.
- Each test case includes the corresponding address and expected hash values to ensure correct functionality.
@thiagodeev thiagodeev force-pushed the thiagodeev/fix-snip-12-selector-encode branch from f1daff3 to 1fe644b Compare September 24, 2025 12:45
@thiagodeev thiagodeev merged commit 5ffef1a into main Sep 24, 2025
8 checks passed
@thiagodeev thiagodeev deleted the thiagodeev/fix-snip-12-selector-encode branch September 24, 2025 13:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: ✅ Done

Development

Successfully merging this pull request may close these issues.

2 participants