-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
feat(cast): add --int flag to from-rlp #9210
Conversation
crates/cast/src/lib.rs
Outdated
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()) | ||
} |
There was a problem hiding this comment.
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
crates/cast/src/lib.rs
Outdated
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:#?}")); | ||
} | ||
|
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
* bet * fmt * bet * bet * remove unneccessary validation
Motivation
Fixes #9197
Solution