Skip to content

Commit

Permalink
feat(cast): improve to/from rlp (#8454)
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes committed Jul 16, 2024
1 parent 4345e3e commit dafccd3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
18 changes: 14 additions & 4 deletions crates/cast/bin/opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,18 +259,28 @@ pub enum CastSubcommand {
},

/// RLP encodes hex data, or an array of hex data.
///
/// Accepts a hex-encoded string, or an array of hex-encoded strings.
/// Can be arbitrarily recursive.
///
/// Examples:
/// - `cast to-rlp "[]"` -> `0xc0`
/// - `cast to-rlp "0x22"` -> `0x22`
/// - `cast to-rlp "[\"0x61\"]"` -> `0xc161`
/// - `cast to-rlp "[\"0xf1\", \"f2\"]"` -> `0xc481f181f2`
#[command(visible_aliases = &["--to-rlp"])]
ToRlp {
/// The value to convert.
///
/// This is a hex-encoded string, or an array of hex-encoded strings.
/// Can be arbitrarily recursive.
value: Option<String>,
},

/// Decodes RLP encoded data.
///
/// Input must be hexadecimal.
/// Decodes RLP hex-encoded data.
#[command(visible_aliases = &["--from-rlp"])]
FromRlp {
/// The value to convert.
/// The RLP hex-encoded data.
value: Option<String>,
},

Expand Down
2 changes: 1 addition & 1 deletion crates/cast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1355,7 +1355,7 @@ impl SimpleCast {
/// assert_eq!(Cast::to_rlp("[]").unwrap(), "0xc0".to_string());
/// assert_eq!(Cast::to_rlp("0x22").unwrap(), "0x22".to_string());
/// assert_eq!(Cast::to_rlp("[\"0x61\"]",).unwrap(), "0xc161".to_string());
/// assert_eq!(Cast::to_rlp("[\"0xf1\",\"f2\"]").unwrap(), "0xc481f181f2".to_string());
/// assert_eq!(Cast::to_rlp("[\"0xf1\", \"f2\"]").unwrap(), "0xc481f181f2".to_string());
/// # Ok::<_, eyre::Report>(())
/// ```
pub fn to_rlp(value: &str) -> Result<String> {
Expand Down
12 changes: 7 additions & 5 deletions crates/cast/src/rlp_converter.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use alloy_primitives::hex;
use alloy_primitives::{hex, U256};
use alloy_rlp::{Buf, Decodable, Encodable, Header};
use eyre::Context;
use serde_json::Value;
use std::fmt;

Expand Down Expand Up @@ -49,11 +50,12 @@ impl Item {
return match value {
Value::Null => Ok(Self::Data(vec![])),
Value::Bool(_) => {
eyre::bail!("RLP input should not contain booleans")
eyre::bail!("RLP input can not contain booleans")
}
// If a value is passed without quotes we cast it to string
Value::Number(n) => Ok(Self::value_to_item(&Value::String(n.to_string()))?),
Value::String(s) => Ok(Self::Data(hex::decode(s).expect("Could not decode hex"))),
Value::Number(n) => {
Ok(Self::Data(n.to_string().parse::<U256>()?.to_be_bytes_trimmed_vec()))
}
Value::String(s) => Ok(Self::Data(hex::decode(s).wrap_err("Could not decode hex")?)),
Value::Array(values) => values.iter().map(Self::value_to_item).collect(),
Value::Object(_) => {
eyre::bail!("RLP input can not contain objects")
Expand Down

0 comments on commit dafccd3

Please sign in to comment.