From 44190b687676dec4d122c1ec727fff2d911ad582 Mon Sep 17 00:00:00 2001 From: lisicky Date: Mon, 14 Oct 2024 03:37:55 +0800 Subject: [PATCH] network deserialization fix --- packages/sidan-csl-rs/src/core/utils/evaluator.rs | 4 ++-- .../src/model/tx_builder_types/network.rs | 13 +++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/packages/sidan-csl-rs/src/core/utils/evaluator.rs b/packages/sidan-csl-rs/src/core/utils/evaluator.rs index 741bb08..470dd50 100644 --- a/packages/sidan-csl-rs/src/core/utils/evaluator.rs +++ b/packages/sidan-csl-rs/src/core/utils/evaluator.rs @@ -35,12 +35,12 @@ pub fn evaluate_tx_scripts_js( match serde_json::from_str(utxo_json.as_str()) { Ok(utxo) => deserialized_utxos.push(utxo), Err(e) => { - return WasmResult::new_error("failure".to_string(), format!("{:?}", e)); + return WasmResult::new_error("failure".to_string(), format!("Error in decoding UTXO: {:?}", e)); } } } - let deserialize_network = match serde_json::from_str(network.as_str()) { + let deserialize_network = match network.try_into() { Ok(network) => network, Err(e) => { return WasmResult::new_error("failure".to_string(), format!("Error in decoding network: {:?}", e)); diff --git a/packages/sidan-csl-rs/src/model/tx_builder_types/network.rs b/packages/sidan-csl-rs/src/model/tx_builder_types/network.rs index 2923107..a6becd2 100644 --- a/packages/sidan-csl-rs/src/model/tx_builder_types/network.rs +++ b/packages/sidan-csl-rs/src/model/tx_builder_types/network.rs @@ -8,3 +8,16 @@ pub enum Network { Preview, Custom(Vec>), } + +impl TryFrom for Network { + type Error = serde_json::Error; + + fn try_from(s: String) -> Result { + match s.to_lowercase().as_str() { + "mainnet" => Ok(Network::Mainnet), + "preprod" => Ok(Network::Preprod), + "preview" => Ok(Network::Preview), + _ => serde_json::from_str(&s), + } + } +} \ No newline at end of file