Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(dpp): add method for decoding identifier with unknown string encoding #2230

Merged
merged 4 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions packages/rs-platform-value/src/string_encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<u8>, Error> {
match encoding {
Encoding::Base58 => Ok(bs58::decode(encoded_value)
Expand Down
26 changes: 25 additions & 1 deletion packages/rs-platform-value/src/types/identifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
#[cfg(feature = "json")]
use serde_json::Value as JsonValue;

use crate::string_encoding::Encoding;
use crate::string_encoding::Encoding::Base58;

Check warning on line 14 in packages/rs-platform-value/src/types/identifier.rs

View workflow job for this annotation

GitHub Actions / Rust packages (dash-sdk) / Linting

unused import: `crate::string_encoding::Encoding::Base58`

warning: unused import: `crate::string_encoding::Encoding::Base58` --> packages/rs-platform-value/src/types/identifier.rs:14:5 | 14 | use crate::string_encoding::Encoding::Base58; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: `#[warn(unused_imports)]` on by default

Check warning on line 14 in packages/rs-platform-value/src/types/identifier.rs

View workflow job for this annotation

GitHub Actions / Rust packages (dpp) / Linting

unused import: `crate::string_encoding::Encoding::Base58`

warning: unused import: `crate::string_encoding::Encoding::Base58` --> packages/rs-platform-value/src/types/identifier.rs:14:5 | 14 | use crate::string_encoding::Encoding::Base58; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: `#[warn(unused_imports)]` on by default

Check warning on line 14 in packages/rs-platform-value/src/types/identifier.rs

View workflow job for this annotation

GitHub Actions / Rust packages (drive) / Linting

unused import: `crate::string_encoding::Encoding::Base58`

warning: unused import: `crate::string_encoding::Encoding::Base58` --> packages/rs-platform-value/src/types/identifier.rs:14:5 | 14 | use crate::string_encoding::Encoding::Base58; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: `#[warn(unused_imports)]` on by default

Check warning on line 14 in packages/rs-platform-value/src/types/identifier.rs

View workflow job for this annotation

GitHub Actions / Rust packages (drive-abci) / Linting

unused import: `crate::string_encoding::Encoding::Base58`

warning: unused import: `crate::string_encoding::Encoding::Base58` --> packages/rs-platform-value/src/types/identifier.rs:14:5 | 14 | use crate::string_encoding::Encoding::Base58; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: `#[warn(unused_imports)]` on by default

Check warning on line 14 in packages/rs-platform-value/src/types/identifier.rs

View workflow job for this annotation

GitHub Actions / Rust packages (wasm-dpp) / Linting

unused import: `crate::string_encoding::Encoding::Base58`

warning: unused import: `crate::string_encoding::Encoding::Base58` --> packages/rs-platform-value/src/types/identifier.rs:14:5 | 14 | use crate::string_encoding::Encoding::Base58; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: `#[warn(unused_imports)]` on by default
use crate::string_encoding::{Encoding, ALL_ENCODINGS};
use crate::types::encoding_string_to_encoding;
use crate::{string_encoding, Error, Value};

Expand Down Expand Up @@ -170,6 +171,29 @@
Identifier::from_bytes(&vec)
}

pub fn from_string_try_encodings(
encoded_value: &str,
encodings: &[Encoding],
) -> Result<Identifier, Error> {
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 encodings [{}]",
tried.join(", ")
)))
}

pub fn from_string_unknown_encoding(encoded_value: &str) -> Result<Identifier, Error> {
Identifier::from_string_try_encodings(encoded_value, &ALL_ENCODINGS)
}

pub fn from_string_with_encoding_string(
encoded_value: &str,
encoding_string: Option<&str>,
Expand Down
Loading