From b0fc192f13f611588670436b9a84e7127f805679 Mon Sep 17 00:00:00 2001 From: Quantum Explorer Date: Wed, 9 Oct 2024 11:16:09 +0700 Subject: [PATCH 1/4] chore: add utility for decoding identifier with unknown encoding --- .../rs-platform-value/src/types/identifier.rs | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/packages/rs-platform-value/src/types/identifier.rs b/packages/rs-platform-value/src/types/identifier.rs index 07bdcbd425e..c882387b0d1 100644 --- a/packages/rs-platform-value/src/types/identifier.rs +++ b/packages/rs-platform-value/src/types/identifier.rs @@ -170,6 +170,29 @@ impl Identifier { Identifier::from_bytes(&vec) } + pub fn from_string_unknown_encoding(encoded_value: &str) -> Result { + // Attempt to decode as Hex + if let Ok(vec) = string_encoding::decode(encoded_value, Encoding::Hex) { + return Identifier::from_bytes(&vec); + } + + // Attempt to decode as Base58 + if let Ok(vec) = string_encoding::decode(encoded_value, Encoding::Base58) { + return Identifier::from_bytes(&vec); + } + + // Attempt to decode as Base64 + if let Ok(vec) = string_encoding::decode(encoded_value, Encoding::Base64) { + return Identifier::from_bytes(&vec); + } + + // If all decoding attempts fail, return an error + Err(Error::StringDecodingError( + "Failed to decode string with any known encoding (tried hex, base58, base64)" + .to_string(), + )) + } + pub fn from_string_with_encoding_string( encoded_value: &str, encoding_string: Option<&str>, From d8bdd66023a6b9a9c244cc782198c48d57f0852c Mon Sep 17 00:00:00 2001 From: Quantum Explorer Date: Wed, 9 Oct 2024 11:31:50 +0700 Subject: [PATCH 2/4] chore: add utility for decoding identifier with unknown encoding --- packages/rs-platform-value/src/types/identifier.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/packages/rs-platform-value/src/types/identifier.rs b/packages/rs-platform-value/src/types/identifier.rs index c882387b0d1..ea01c31b5b3 100644 --- a/packages/rs-platform-value/src/types/identifier.rs +++ b/packages/rs-platform-value/src/types/identifier.rs @@ -173,17 +173,23 @@ impl Identifier { pub fn from_string_unknown_encoding(encoded_value: &str) -> Result { // Attempt to decode as Hex if let Ok(vec) = string_encoding::decode(encoded_value, Encoding::Hex) { - return Identifier::from_bytes(&vec); + if vec.len() == 32 { + return Identifier::from_bytes(&vec); + } } // Attempt to decode as Base58 if let Ok(vec) = string_encoding::decode(encoded_value, Encoding::Base58) { - return Identifier::from_bytes(&vec); + if vec.len() == 32 { + return Identifier::from_bytes(&vec); + } } // Attempt to decode as Base64 if let Ok(vec) = string_encoding::decode(encoded_value, Encoding::Base64) { - return Identifier::from_bytes(&vec); + if vec.len() == 32 { + return Identifier::from_bytes(&vec); + } } // If all decoding attempts fail, return an error From df6b4acd6d31798ebaa5a06e45a3b33b521856f2 Mon Sep 17 00:00:00 2001 From: Quantum Explorer Date: Wed, 9 Oct 2024 11:41:03 +0700 Subject: [PATCH 3/4] better approach --- .../rs-platform-value/src/string_encoding.rs | 14 ++++++ .../rs-platform-value/src/types/identifier.rs | 45 +++++++++---------- 2 files changed, 34 insertions(+), 25 deletions(-) diff --git a/packages/rs-platform-value/src/string_encoding.rs b/packages/rs-platform-value/src/string_encoding.rs index c31d1728393..bce2c69fbbf 100644 --- a/packages/rs-platform-value/src/string_encoding.rs +++ b/packages/rs-platform-value/src/string_encoding.rs @@ -3,13 +3,27 @@ use base64; use base64::prelude::BASE64_STANDARD; use base64::Engine; use bs58; +use std::fmt; +#[derive(Debug, Copy, Clone)] pub enum Encoding { Base58, Base64, Hex, } +pub const ALL_ENCODINGS: [Encoding; 3] = [Encoding::Hex, Encoding::Base58, Encoding::Base64]; + +impl fmt::Display for Encoding { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Encoding::Base58 => write!(f, "Base58"), + Encoding::Base64 => write!(f, "Base64"), + Encoding::Hex => write!(f, "Hex"), + } + } +} + pub fn decode(encoded_value: &str, encoding: Encoding) -> Result, Error> { match encoding { Encoding::Base58 => Ok(bs58::decode(encoded_value) diff --git a/packages/rs-platform-value/src/types/identifier.rs b/packages/rs-platform-value/src/types/identifier.rs index ea01c31b5b3..28a389548bd 100644 --- a/packages/rs-platform-value/src/types/identifier.rs +++ b/packages/rs-platform-value/src/types/identifier.rs @@ -11,7 +11,8 @@ use serde::{Deserialize, Serialize}; #[cfg(feature = "json")] use serde_json::Value as JsonValue; -use crate::string_encoding::Encoding; +use crate::string_encoding::Encoding::Base58; +use crate::string_encoding::{Encoding, ALL_ENCODINGS}; use crate::types::encoding_string_to_encoding; use crate::{string_encoding, Error, Value}; @@ -170,33 +171,27 @@ impl Identifier { Identifier::from_bytes(&vec) } - pub fn from_string_unknown_encoding(encoded_value: &str) -> Result { - // Attempt to decode as Hex - if let Ok(vec) = string_encoding::decode(encoded_value, Encoding::Hex) { - if vec.len() == 32 { - return Identifier::from_bytes(&vec); - } - } - - // Attempt to decode as Base58 - if let Ok(vec) = string_encoding::decode(encoded_value, Encoding::Base58) { - if vec.len() == 32 { - return Identifier::from_bytes(&vec); - } - } - - // Attempt to decode as Base64 - if let Ok(vec) = string_encoding::decode(encoded_value, Encoding::Base64) { - if vec.len() == 32 { - return Identifier::from_bytes(&vec); + pub fn from_string_try_encodings( + encoded_value: &str, + encodings: &[Encoding], + ) -> Result { + let mut tried = vec![]; + for encoding in encodings { + if let Ok(vec) = string_encoding::decode(encoded_value, *encoding) { + if vec.len() == 32 { + return Identifier::from_bytes(&vec); + } } + tried.push(encoding.to_string()); } + Err(Error::StringDecodingError(format!( + "Failed to decode string with any known encoding (tried {})", + tried.join(", ") + ))) + } - // If all decoding attempts fail, return an error - Err(Error::StringDecodingError( - "Failed to decode string with any known encoding (tried hex, base58, base64)" - .to_string(), - )) + pub fn from_string_unknown_encoding(encoded_value: &str) -> Result { + Identifier::from_string_try_encodings(encoded_value, &ALL_ENCODINGS) } pub fn from_string_with_encoding_string( From c37735c9d878468da667f110cbbd080ac05bb8d7 Mon Sep 17 00:00:00 2001 From: Quantum Explorer Date: Wed, 9 Oct 2024 11:43:10 +0700 Subject: [PATCH 4/4] better approach --- packages/rs-platform-value/src/types/identifier.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/rs-platform-value/src/types/identifier.rs b/packages/rs-platform-value/src/types/identifier.rs index 28a389548bd..19bfbf82b2d 100644 --- a/packages/rs-platform-value/src/types/identifier.rs +++ b/packages/rs-platform-value/src/types/identifier.rs @@ -185,7 +185,7 @@ impl Identifier { tried.push(encoding.to_string()); } Err(Error::StringDecodingError(format!( - "Failed to decode string with any known encoding (tried {})", + "Failed to decode string with encodings [{}]", tried.join(", ") ))) }