Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix assignment of string literals to felt252 type in error messages #1238

Closed
wants to merge 1 commit into from

Conversation

0xsimka
Copy link

@0xsimka 0xsimka commented Dec 1, 2024

This update addresses an issue where string literals (such as 'Account: invalid caller') were incorrectly assigned to constants of type felt252. The type felt252 is intended for numeric values, not string literals. This resulted in a type mismatch, as felt252 cannot directly store strings.

To resolve this, the string literals have been replaced with their corresponding hashes using the Poseidon hash function. This ensures the values are correctly represented as felt252, which is the appropriate type for error messages in Cairo.

Importance:

This fix is critical as it prevents runtime errors and ensures that error messages are correctly represented within the Cairo contract environment. Without this change, the contract would fail when attempting to use string literals as felt252, potentially leading to unexpected behavior or reverts during execution.

Here is the corrected code:

use core::hash::PoseidonTrait;

pub mod Errors {
    pub const INVALID_CALLER: felt252 = PoseidonTrait::new()
        .update_with("Account: invalid caller")
        .finalize();
    pub const INVALID_SIGNATURE: felt252 = PoseidonTrait::new()
        .update_with("Account: invalid signature")
        .finalize();
    pub const INVALID_TX_VERSION: felt252 = PoseidonTrait::new()
        .update_with("Account: invalid tx version")
        .finalize();
    pub const UNAUTHORIZED: felt252 = PoseidonTrait::new()
        .update_with("Account: unauthorized")
        .finalize();
}

By using Poseidon for string hashing, the error constants are now correctly represented and can be used without issues.

  • Tests

@ericnordelo
Copy link
Member

These constants are correctly storing short strings which are represented as felts.

@ericnordelo ericnordelo closed this Dec 1, 2024
@0xsimka 0xsimka deleted the patch-1 branch December 1, 2024 16:12
@0xsimka
Copy link
Author

0xsimka commented Dec 1, 2024

These constants are correctly storing short strings which are represented as felts.

Got it, sorry.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants