diff --git a/tooling/noirc_abi/src/input_parser/json.rs b/tooling/noirc_abi/src/input_parser/json.rs index 518685b7cf3..e2b1a83ee6b 100644 --- a/tooling/noirc_abi/src/input_parser/json.rs +++ b/tooling/noirc_abi/src/input_parser/json.rs @@ -59,7 +59,7 @@ pub(crate) fn serialize_to_json( #[derive(Debug, Deserialize, Serialize, Clone)] #[serde(untagged)] -pub(super) enum JsonTypes { +pub enum JsonTypes { // This is most likely going to be a hex string // But it is possible to support UTF-8 String(String), @@ -78,7 +78,7 @@ pub(super) enum JsonTypes { } impl JsonTypes { - pub(super) fn try_from_input_value( + pub fn try_from_input_value( value: &InputValue, abi_type: &AbiType, ) -> Result { @@ -133,7 +133,7 @@ impl JsonTypes { } impl InputValue { - fn try_from_json( + pub fn try_from_json( value: JsonTypes, param_type: &AbiType, arg_name: &str, diff --git a/tooling/noirc_abi/src/input_parser/mod.rs b/tooling/noirc_abi/src/input_parser/mod.rs index 0fa3537d9b3..7bbcb42296c 100644 --- a/tooling/noirc_abi/src/input_parser/mod.rs +++ b/tooling/noirc_abi/src/input_parser/mod.rs @@ -8,7 +8,7 @@ use serde::Serialize; use crate::errors::InputParserError; use crate::{Abi, AbiType}; -mod json; +pub mod json; mod toml; /// This is what all formats eventually transform into diff --git a/tooling/noirc_abi_wasm/src/lib.rs b/tooling/noirc_abi_wasm/src/lib.rs index 24e0e6506fb..ea03aa8abe7 100644 --- a/tooling/noirc_abi_wasm/src/lib.rs +++ b/tooling/noirc_abi_wasm/src/lib.rs @@ -7,7 +7,11 @@ use getrandom as _; use acvm::acir::native_types::WitnessMap; use iter_extended::try_btree_map; -use noirc_abi::{errors::InputParserError, input_parser::InputValue, Abi, MAIN_RETURN_NAME}; +use noirc_abi::{ + errors::InputParserError, + input_parser::{json::JsonTypes, InputValue}, + Abi, MAIN_RETURN_NAME, +}; use serde::Serialize; use std::collections::BTreeMap; @@ -16,11 +20,9 @@ use wasm_bindgen::{prelude::wasm_bindgen, JsValue}; mod errors; mod js_witness_map; -mod temp; use errors::JsAbiError; use js_witness_map::JsWitnessMap; -use temp::{input_value_from_json_type, JsonTypes}; #[wasm_bindgen(typescript_custom_section)] const INPUT_MAP: &'static str = r#" @@ -92,7 +94,7 @@ pub fn abi_encode( .map(|return_value| { let toml_return_value = JsValueSerdeExt::into_serde(&JsValue::from(return_value)) .expect("could not decode return value"); - input_value_from_json_type( + InputValue::try_from_json( toml_return_value, abi.return_type.as_ref().unwrap(), MAIN_RETURN_NAME, @@ -107,7 +109,7 @@ pub fn abi_encode( let value = inputs .get(&arg_name) .ok_or_else(|| InputParserError::MissingArgument(arg_name.clone()))?; - input_value_from_json_type(value.clone(), &abi_type, &arg_name) + InputValue::try_from_json(value.clone(), &abi_type, &arg_name) .map(|input_value| (arg_name, input_value)) })?; diff --git a/tooling/noirc_abi_wasm/src/temp.rs b/tooling/noirc_abi_wasm/src/temp.rs deleted file mode 100644 index f833ecb756e..00000000000 --- a/tooling/noirc_abi_wasm/src/temp.rs +++ /dev/null @@ -1,136 +0,0 @@ -//! This module contains vendored code from `noirc_abi` for converting JSON to `InputValue`s. -//! This should be removed in time. - -use acvm::FieldElement; -use iter_extended::{try_btree_map, try_vecmap}; -use noirc_abi::{errors::InputParserError, input_parser::InputValue, AbiType}; -use serde::{Deserialize, Serialize}; -use std::collections::BTreeMap; - -#[derive(Debug, Deserialize, Serialize, Clone)] -#[serde(untagged)] -pub(super) enum JsonTypes { - // This is most likely going to be a hex string - // But it is possible to support UTF-8 - String(String), - // Just a regular integer, that can fit in 64 bits. - // - // The JSON spec does not specify any limit on the size of integer number types, - // however we restrict the allowable size. Values which do not fit in a u64 should be passed - // as a string. - Integer(u64), - // Simple boolean flag - Bool(bool), - // Array of JsonTypes - Array(Vec), - // Struct of JsonTypes - Table(BTreeMap), -} - -impl JsonTypes { - pub(super) fn try_from_input_value( - value: &InputValue, - abi_type: &AbiType, - ) -> Result { - let json_value = match (value, abi_type) { - (InputValue::Field(f), AbiType::Field | AbiType::Integer { .. }) => { - let f_str = format!("0x{}", f.to_hex()); - JsonTypes::String(f_str) - } - (InputValue::Field(f), AbiType::Boolean) => JsonTypes::Bool(f.is_one()), - - (InputValue::Vec(vector), AbiType::Array { typ, .. }) => { - let array = - try_vecmap(vector, |value| JsonTypes::try_from_input_value(value, typ))?; - JsonTypes::Array(array) - } - - (InputValue::String(s), AbiType::String { .. }) => JsonTypes::String(s.to_string()), - - (InputValue::Struct(map), AbiType::Struct { fields, .. }) => { - let map_with_json_types = try_btree_map(fields, |(key, field_type)| { - JsonTypes::try_from_input_value(&map[key], field_type) - .map(|json_value| (key.to_owned(), json_value)) - })?; - JsonTypes::Table(map_with_json_types) - } - - (InputValue::Vec(vector), AbiType::Tuple { fields }) => { - let fields = try_vecmap(vector.iter().zip(fields), |(value, typ)| { - JsonTypes::try_from_input_value(value, typ) - })?; - JsonTypes::Array(fields) - } - - _ => return Err(InputParserError::AbiTypeMismatch(abi_type.clone())), - }; - Ok(json_value) - } -} - -pub(super) fn input_value_from_json_type( - value: JsonTypes, - param_type: &AbiType, - arg_name: &str, -) -> Result { - let input_value = match (value, param_type) { - (JsonTypes::String(string), AbiType::String { .. }) => InputValue::String(string), - ( - JsonTypes::String(string), - AbiType::Field | AbiType::Integer { .. } | AbiType::Boolean, - ) => InputValue::Field(parse_str_to_field(&string)?), - - ( - JsonTypes::Integer(integer), - AbiType::Field | AbiType::Integer { .. } | AbiType::Boolean, - ) => { - let new_value = FieldElement::from(i128::from(integer)); - - InputValue::Field(new_value) - } - - (JsonTypes::Bool(boolean), AbiType::Boolean) => InputValue::Field(boolean.into()), - - (JsonTypes::Array(array), AbiType::Array { typ, .. }) => { - let array_elements = - try_vecmap(array, |value| input_value_from_json_type(value, typ, arg_name))?; - InputValue::Vec(array_elements) - } - - (JsonTypes::Table(table), AbiType::Struct { fields, .. }) => { - let native_table = try_btree_map(fields, |(field_name, abi_type)| { - // Check that json contains a value for each field of the struct. - let field_id = format!("{arg_name}.{field_name}"); - let value = table - .get(field_name) - .ok_or_else(|| InputParserError::MissingArgument(field_id.clone()))?; - input_value_from_json_type(value.clone(), abi_type, &field_id) - .map(|input_value| (field_name.to_string(), input_value)) - })?; - - InputValue::Struct(native_table) - } - - (JsonTypes::Array(array), AbiType::Tuple { fields }) => { - let tuple_fields = try_vecmap(array.into_iter().zip(fields), |(value, typ)| { - input_value_from_json_type(value, typ, arg_name) - })?; - InputValue::Vec(tuple_fields) - } - - (_, _) => return Err(InputParserError::AbiTypeMismatch(param_type.clone())), - }; - - Ok(input_value) -} - -fn parse_str_to_field(value: &str) -> Result { - if value.starts_with("0x") { - FieldElement::from_hex(value).ok_or_else(|| InputParserError::ParseHexStr(value.to_owned())) - } else { - value - .parse::() - .map_err(|err_msg| InputParserError::ParseStr(err_msg.to_string())) - .map(FieldElement::from) - } -}