Skip to content

Commit

Permalink
Fixed call parameters preprocessing. Old preprocessor removed all \
Browse files Browse the repository at this point in the history
… and trimmed `"` chars from JSON string. This made impossible to pass JSON with string values that contains special chars.
  • Loading branch information
melsomino committed Jan 29, 2025
1 parent f28a10b commit 9ffa7ba
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
20 changes: 16 additions & 4 deletions tvm_debugger/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::path::PathBuf;

use clap::ArgAction;
use clap::Parser;
use serde_json::Value;
use tvm_block::Deserializable;
use tvm_block::Serializable;
use tvm_block::StateInit;
Expand Down Expand Up @@ -41,16 +42,16 @@ struct Args {
abi_file: Option<PathBuf>,

/// ABI header
#[arg(short('r'), long)]
abi_header: Option<serde_json::Value>,
#[arg(short('r'), long, value_parser = parse_json_object)]
abi_header: Option<Value>,

/// Contract function name
#[arg(short('m'), long)]
function_name: Option<String>,

/// Call parameters. Must be specified as a json string
#[arg(short('p'), long)]
call_parameters: Option<serde_json::Value>,
#[arg(short('p'), long, value_parser = parse_json_object)]
call_parameters: Option<Value>,

/// Contract address, that will be used for execution
#[arg(long, allow_hyphen_values(true))]
Expand Down Expand Up @@ -93,6 +94,17 @@ struct Args {
replace_code: Option<String>,
}

fn parse_json_object(s: &str) -> Result<Value, String> {
let s = s.trim_matches('"').trim_matches('\'');
if s.is_empty() {
Ok(Value::Object(serde_json::Map::new()))
} else if s.starts_with('{') && s.ends_with('}') {
Ok(serde_json::from_str::<Value>(s).map_err(|e| format!("Failed to parse json arg: {e}"))?)
} else {
Err(format!("Invalid json object: {s}"))
}
}

fn main() -> anyhow::Result<()> {
let args: Args = Args::parse();
if let Some(new_code) = args.replace_code {
Expand Down
14 changes: 5 additions & 9 deletions tvm_debugger/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,21 +97,17 @@ pub(crate) fn generate_message_body(args: &Args) -> anyhow::Result<SliceData> {
assert!(args.function_name.is_some());
let abi = load_abi_as_string(args.abi_file.as_ref().unwrap())?;
let function_name = args.function_name.as_ref().unwrap();
let header = args.abi_header.clone().map(|v| {
serde_json::to_string(&v)
.unwrap_or("{}".to_string())
.trim_matches(|c| c == '"')
.replace('\\', "")
.to_string()
});
let header = args
.abi_header
.clone()
.map(|v| serde_json::to_string(&v).unwrap_or("{}".to_string()).to_string());
let parameters = args
.call_parameters
.clone()
.map(|v| {
serde_json::to_string(&v)
.map_err(|err| anyhow::format_err!("Failed to serialize call parameters: {err}"))
.unwrap()
.trim_matches(|c| c == '"')
.replace('\\', "")
.to_string()
})
.unwrap_or("{}".to_string());
Expand Down

0 comments on commit 9ffa7ba

Please sign in to comment.