From 9ffa7ba17a6e56958df734f82e0304869260a363 Mon Sep 17 00:00:00 2001 From: Michael Vlasov Date: Wed, 29 Jan 2025 14:04:12 +0100 Subject: [PATCH] Fixed call parameters preprocessing. Old preprocessor removed all `\` and trimmed `"` chars from JSON string. This made impossible to pass JSON with string values that contains special chars. --- tvm_debugger/src/main.rs | 20 ++++++++++++++++---- tvm_debugger/src/message.rs | 14 +++++--------- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/tvm_debugger/src/main.rs b/tvm_debugger/src/main.rs index 4a09450c..5b2fd731 100644 --- a/tvm_debugger/src/main.rs +++ b/tvm_debugger/src/main.rs @@ -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; @@ -41,16 +42,16 @@ struct Args { abi_file: Option, /// ABI header - #[arg(short('r'), long)] - abi_header: Option, + #[arg(short('r'), long, value_parser = parse_json_object)] + abi_header: Option, /// Contract function name #[arg(short('m'), long)] function_name: Option, /// Call parameters. Must be specified as a json string - #[arg(short('p'), long)] - call_parameters: Option, + #[arg(short('p'), long, value_parser = parse_json_object)] + call_parameters: Option, /// Contract address, that will be used for execution #[arg(long, allow_hyphen_values(true))] @@ -93,6 +94,17 @@ struct Args { replace_code: Option, } +fn parse_json_object(s: &str) -> Result { + 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::(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 { diff --git a/tvm_debugger/src/message.rs b/tvm_debugger/src/message.rs index 476ed602..5d28f645 100644 --- a/tvm_debugger/src/message.rs +++ b/tvm_debugger/src/message.rs @@ -97,21 +97,17 @@ pub(crate) fn generate_message_body(args: &Args) -> anyhow::Result { 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());