Skip to content

fix!(rpc): disallow non-prefixed hex strings in RPC input#3328

Open
sistemd wants to merge 1 commit intomainfrom
sistemd/enforce-hex-prefix
Open

fix!(rpc): disallow non-prefixed hex strings in RPC input#3328
sistemd wants to merge 1 commit intomainfrom
sistemd/enforce-hex-prefix

Conversation

@sistemd
Copy link
Copy Markdown
Contributor

@sistemd sistemd commented Apr 11, 2026

Changes the JSON-RPC deserialization logic to reject hex strings without the 0x prefix. This brings us closer in line with the Starknet API spec.

Closes: #3309


It goes without saying that this change could break many existing clients, so it might be worth considering a more gradual deprecation strategy.

Also, while we're on the topic, I noticed another inconsistency between our deserialization logic and the spec. Patterns like ^0x(0|[a-fA-F1-9]{1}[a-fA-F0-9]{0,62})$ do not allow empty strings after the 0x prefix, but our deserialization logic currently allows them and treats them as zero.

@sistemd sistemd requested a review from a team as a code owner April 11, 2026 22:12
Copy link
Copy Markdown
Contributor

@zvolin zvolin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this shouldn't say optional now

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor

@CHr15F0x CHr15F0x left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes the JSON-RPC deserialization logic to reject hex strings without
the `0x` prefix. This brings us closer in line with the Starknet API
spec.
@sistemd sistemd force-pushed the sistemd/enforce-hex-prefix branch from edcaba9 to 134afd1 Compare April 21, 2026 13:16
@sistemd
Copy link
Copy Markdown
Contributor Author

sistemd commented Apr 21, 2026

I tried running the test suites of both starknet.js and starknet.py against a local node (running with these changes) and both suites mostly pass. As for the tests that failed, it was not due to hex string formatting.

I also found something reassuring while looking at these libraries - they both have helpers for working with hex strings (and felts) that are used throughout their respective RPC clients and those match the format from the spec. For example:

starknet.js has these:

and starknet.py has this:

@sistemd sistemd enabled auto-merge (rebase) April 21, 2026 13:17
Comment thread CHANGELOG.md
### Changed

- `starknet_getEvents` now returns `block_number` for events from pre-confirmed and pre-latest blocks.
- BREAKING: JSON-RPC input no longer accepts hex strings that are missing the `0x` prefix, in line with the Starknet API spec.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We've never used BREAKING in the CHANGELOG.

I'd suggest we remove it. We'll highlight it in the release notes (+ major version change).

Comment on lines +881 to +892
let expected = ComputedClassHash::Cairo(class_hash!(
"056b96c1d1bbfa01af44b465763d1b71150fa00c6c9d54c3947f57e979ff68c3"
));

// Known contract which triggered a hash mismatch failure.
let extract = tokio::task::spawn_blocking(move || -> anyhow::Result<_> {
let hash = compute_class_hash(CAIRO_0_8_NEW_ATTRIBUTES)?;
Ok(hash)
});
let calculated_hash = extract.await.unwrap().unwrap();

assert_eq!(calculated_hash, expected);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this at all related to this PR? 👀

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a rebase related hiccup (I refactored this test to use the hash() helper and it was merged to main). This change should be reverted.

Comment thread crates/serde/src/lib.rs
@@ -323,22 +323,22 @@ pub fn starkhash_to_dec_str(h: &Felt) -> String {

/// A helper conversion function. Only use with __sequencer API related types__.
fn starkhash_from_dec_str(s: &str) -> Result<Felt, anyhow::Error> {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method seems to be duplicated in crates/common/src/event.rs‎. Might be worth consolidating in a shared space for everyone to consume from a single source of truth.

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.

Require "0x" prefix when deserializing hex numbers

4 participants