Skip to content

Commit

Permalink
[bugfix] - wallet move call parsing array incorrectly (MystenLabs#2012)
Browse files Browse the repository at this point in the history
* * call args bugfix, arrays are parsed incorrectly to String
* unit test for SuiJsonValue from str

* minor fixes
  • Loading branch information
patrickkuo authored May 17, 2022
1 parent 553a4cf commit b3b306c
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
3 changes: 2 additions & 1 deletion sui_core/src/sui_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@ fn try_from_bcs_bytes(bytes: &[u8]) -> Result<JsonValue, anyhow::Error> {
impl std::str::FromStr for SuiJsonValue {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self, anyhow::Error> {
SuiJsonValue::new(serde_json::from_value(json!(s))?)
// Wrap input with json! if serde_json fails, the failure usually cause by missing quote escapes.
SuiJsonValue::new(serde_json::from_str(s).or_else(|_| serde_json::from_value(json!(s)))?)
}
}

Expand Down
46 changes: 46 additions & 0 deletions sui_core/src/unit_tests/sui_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
// SPDX-License-Identifier: Apache-2.0

use std::path::Path;
use std::str::FromStr;

use move_core_types::{
account_address::AccountAddress, identifier::Identifier, value::MoveTypeLayout,
};
use serde_json::{json, Value};
use test_fuzz::runtime::num_traits::ToPrimitive;

use sui_types::base_types::{ObjectID, SuiAddress, TransactionDigest};
use sui_types::object::Object;
use sui_types::SUI_FRAMEWORK_ADDRESS;
Expand Down Expand Up @@ -510,3 +513,46 @@ fn test_convert_number_array_from_bcs() {
assert_eq!(50000, value.as_u64().unwrap())
}
}

#[test]
fn test_from_str() {
// test number
let test = SuiJsonValue::from_str("10000").unwrap();
assert!(test.0.is_number());
// Test array
let test = SuiJsonValue::from_str("[10,10,10,10]").unwrap();
assert!(test.0.is_array());
assert_eq!(
vec![10, 10, 10, 10],
test.0
.as_array()
.unwrap()
.iter()
.map(|value| value.as_u64().unwrap().to_u8().unwrap())
.collect::<Vec<_>>()
);
// test bool
let test = SuiJsonValue::from_str("true").unwrap();
assert!(test.0.is_boolean());

// test id without quotes
let object_id = ObjectID::random().to_hex_literal();
let test = SuiJsonValue::from_str(&object_id).unwrap();
assert!(test.0.is_string());
assert_eq!(object_id, test.0.as_str().unwrap());

// test id with quotes
let test = SuiJsonValue::from_str(&format!("\"{}\"", &object_id)).unwrap();
assert!(test.0.is_string());
assert_eq!(object_id, test.0.as_str().unwrap());

// test string without quotes
let test = SuiJsonValue::from_str("Some string").unwrap();
assert!(test.0.is_string());
assert_eq!("Some string", test.0.as_str().unwrap());

// test string with quotes
let test = SuiJsonValue::from_str("\"Some string\"").unwrap();
assert!(test.0.is_string());
assert_eq!("Some string", test.0.as_str().unwrap())
}

0 comments on commit b3b306c

Please sign in to comment.