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

feat(cast): add --int flag to from-rlp #9210

Merged
merged 5 commits into from
Oct 28, 2024

Conversation

TropicalDog17
Copy link
Contributor

Motivation

Fixes #9197

Solution

@TropicalDog17 TropicalDog17 changed the title bet feat(cast): add --int flag to from-rlp Oct 27, 2024
Comment on lines 1467 to 1499
pub fn from_rlp(value: impl AsRef<str>, as_int: bool) -> Result<String> {
fn validate_canonical_int(item: &Item) -> Result<()> {
use eyre::bail;

let bytes = match item {
Item::Data(b) => b,
_ => return Ok(()),
};

if bytes.is_empty() {
return Ok(());
}

// Check for leading zeros
if bytes[0] == 0 {
bail!("rlp: non-canonical integer (leading zero bytes)");
}

if bytes.len() == 1 && bytes[0] < 0x80 {
bail!("rlp: non-canonical size information");
}

Ok(())
}

let bytes = hex::decode(value.as_ref()).wrap_err("Could not decode hex")?;
let item = Item::decode(&mut &bytes[..]).wrap_err("Could not decode rlp")?;
if as_int {
validate_canonical_int(&item).wrap_err("Non-canonical integer")?;
}

Ok(item.to_string())
}
Copy link
Member

@klkvr klkvr Oct 27, 2024

Choose a reason for hiding this comment

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

I think if as_int is true we should parse and return it as U256, otherwise as Item

Comment on lines 1503 to 1508
if as_int {
validate_canonical_int(&bytes).wrap_err("Non-canonical integer")?;
let uint_value = U256::from_str(value.as_ref())?;
return Ok(format!("{uint_value:#?}"));
}

Copy link
Member

Choose a reason for hiding this comment

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

sorry I mean decode rlp as uint (just U256::decode), that way we don't need the extra validation because the decoding would just fail if it's an incorrect integer

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thank you so much, I've updated

@TropicalDog17 TropicalDog17 requested a review from klkvr October 28, 2024 09:27
@klkvr klkvr merged commit 00415bb into foundry-rs:master Oct 28, 2024
21 checks passed
rplusq pushed a commit to rplusq/foundry that referenced this pull request Nov 29, 2024
* bet

* fmt

* bet

* bet

* remove unneccessary validation
@grandizzy grandizzy added T-feature Type: feature C-cast Command: cast labels Dec 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-cast Command: cast T-feature Type: feature
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Add cast from-rlp --int to decode int values
3 participants