Skip to content

Commit

Permalink
chore(dpp): add method for decoding identifier with unknown string en…
Browse files Browse the repository at this point in the history
…coding (#2230)
  • Loading branch information
QuantumExplorer authored Oct 9, 2024
1 parent 26e3c47 commit de5236e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
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 @@ use serde::{Deserialize, Serialize};
#[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 (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 (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 (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 (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

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
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 @@ impl Identifier {
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

0 comments on commit de5236e

Please sign in to comment.