Skip to content

Commit

Permalink
Add helper cep18
Browse files Browse the repository at this point in the history
  • Loading branch information
gRoussac committed Jan 8, 2025
1 parent 1e3792e commit 7888c0c
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/helpers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,29 @@ pub fn get_base64_key_from_package_hash(formatted_hash: &str) -> Result<String,
Ok(general_purpose::STANDARD.encode(key)) // base64.encode
}

/// Converts a formatted key hash to a base64-encoded string (CEP-18 key encoding).
///
/// # Arguments
///
/// * `formatted_hash` - A hex-formatted string representing the key hash.
/// Example: "hash-b485c074cef7ccaccd0302949d2043ab7133abdb14cfa87e8392945c0bd80a5f"
///
/// # Returns
///
/// Returns a `Result` containing the base64-encoded string on success.
/// Example: "AbSFwHTO98yszQMClJ0gQ6txM6vbFM+ofoOSlFwL2Apf"
///
/// # Errors
///
/// This function returns an error if:
/// - The input string is not a valid formatted key hash.
/// - The conversion to bytes or base64 encoding fails.
pub fn get_base64_key_from_key_hash(formatted_hash: &str) -> Result<String, Box<SdkError>> {
let key = Key::from_formatted_str(formatted_hash)?;
let key = key.to_bytes().unwrap();
Ok(general_purpose::STANDARD.encode(key)) // base64.encode
}

/// Gets the time to live (TTL) value or returns the default value if not provided.
///
/// # Arguments
Expand Down Expand Up @@ -816,4 +839,17 @@ mod tests {
// Check the result against the expected output
assert_eq!(result, expected_output.to_string());
}

#[test]
fn test_get_base64_key_from_key_hash() {
// Test with a known input and expected output
let input_hash = "hash-b485c074cef7ccaccd0302949d2043ab7133abdb14cfa87e8392945c0bd80a5f";
let expected_output = "AbSFwHTO98yszQMClJ0gQ6txM6vbFM+ofoOSlFwL2Apf";

// Call the function under test
let result = get_base64_key_from_key_hash(input_hash).unwrap();

// Check the result against the expected output
assert_eq!(result, expected_output.to_string());
}
}
29 changes: 29 additions & 0 deletions src/js/interns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,35 @@ pub fn get_base64_key_from_package_hash_js_alias(
})
}

/// Converts a formatted key hash to a base64-encoded string (CEP-18 key encoding) for use in JavaScript.
///
/// This function acts as a wrapper around `get_base64_key_from_key_hash` and maps errors to JavaScript-compatible errors.
///
/// # Arguments
///
/// * `formatted_key_hash` - A hex-formatted string representing the key hash.
/// Example: "hash-b485c074cef7ccaccd0302949d2043ab7133abdb14cfa87e8392945c0bd80a5f"
///
/// # Returns
///
/// Returns a `Result` containing the base64-encoded string on success.
/// Example: "AbSFwHTO98yszQMClJ0gQ6txM6vbFM+ofoOSlFwL2Apf"
///
/// # Errors
///
/// This function returns a `JsError` if:
/// - The input string is not a valid formatted key hash.
/// - The conversion to bytes or base64 encoding fails.
///
/// The error message is formatted as a JavaScript-compatible string.
#[wasm_bindgen(js_name = "keyHashToBase64Key")]
pub fn get_base64_key_from_key_hash_js_alias(formatted_key_hash: &str) -> Result<String, JsError> {
get_base64_key_from_key_hash(formatted_key_hash).map_err(|err| {

Check failure on line 307 in src/js/interns.rs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-22.04, 20.x)

cannot find function `get_base64_key_from_key_hash` in this scope
let error_text = format!("Error serializing package hash: {:?}", err);
JsError::new(&error_text)
})
}

/// Gets the current timestamp.
///
/// # Returns
Expand Down

0 comments on commit 7888c0c

Please sign in to comment.