Skip to content

Commit 7888c0c

Browse files
committed
Add helper cep18
1 parent 1e3792e commit 7888c0c

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

src/helpers/mod.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,29 @@ pub fn get_base64_key_from_package_hash(formatted_hash: &str) -> Result<String,
203203
Ok(general_purpose::STANDARD.encode(key)) // base64.encode
204204
}
205205

206+
/// Converts a formatted key hash to a base64-encoded string (CEP-18 key encoding).
207+
///
208+
/// # Arguments
209+
///
210+
/// * `formatted_hash` - A hex-formatted string representing the key hash.
211+
/// Example: "hash-b485c074cef7ccaccd0302949d2043ab7133abdb14cfa87e8392945c0bd80a5f"
212+
///
213+
/// # Returns
214+
///
215+
/// Returns a `Result` containing the base64-encoded string on success.
216+
/// Example: "AbSFwHTO98yszQMClJ0gQ6txM6vbFM+ofoOSlFwL2Apf"
217+
///
218+
/// # Errors
219+
///
220+
/// This function returns an error if:
221+
/// - The input string is not a valid formatted key hash.
222+
/// - The conversion to bytes or base64 encoding fails.
223+
pub fn get_base64_key_from_key_hash(formatted_hash: &str) -> Result<String, Box<SdkError>> {
224+
let key = Key::from_formatted_str(formatted_hash)?;
225+
let key = key.to_bytes().unwrap();
226+
Ok(general_purpose::STANDARD.encode(key)) // base64.encode
227+
}
228+
206229
/// Gets the time to live (TTL) value or returns the default value if not provided.
207230
///
208231
/// # Arguments
@@ -816,4 +839,17 @@ mod tests {
816839
// Check the result against the expected output
817840
assert_eq!(result, expected_output.to_string());
818841
}
842+
843+
#[test]
844+
fn test_get_base64_key_from_key_hash() {
845+
// Test with a known input and expected output
846+
let input_hash = "hash-b485c074cef7ccaccd0302949d2043ab7133abdb14cfa87e8392945c0bd80a5f";
847+
let expected_output = "AbSFwHTO98yszQMClJ0gQ6txM6vbFM+ofoOSlFwL2Apf";
848+
849+
// Call the function under test
850+
let result = get_base64_key_from_key_hash(input_hash).unwrap();
851+
852+
// Check the result against the expected output
853+
assert_eq!(result, expected_output.to_string());
854+
}
819855
}

src/js/interns.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,35 @@ pub fn get_base64_key_from_package_hash_js_alias(
281281
})
282282
}
283283

284+
/// Converts a formatted key hash to a base64-encoded string (CEP-18 key encoding) for use in JavaScript.
285+
///
286+
/// This function acts as a wrapper around `get_base64_key_from_key_hash` and maps errors to JavaScript-compatible errors.
287+
///
288+
/// # Arguments
289+
///
290+
/// * `formatted_key_hash` - A hex-formatted string representing the key hash.
291+
/// Example: "hash-b485c074cef7ccaccd0302949d2043ab7133abdb14cfa87e8392945c0bd80a5f"
292+
///
293+
/// # Returns
294+
///
295+
/// Returns a `Result` containing the base64-encoded string on success.
296+
/// Example: "AbSFwHTO98yszQMClJ0gQ6txM6vbFM+ofoOSlFwL2Apf"
297+
///
298+
/// # Errors
299+
///
300+
/// This function returns a `JsError` if:
301+
/// - The input string is not a valid formatted key hash.
302+
/// - The conversion to bytes or base64 encoding fails.
303+
///
304+
/// The error message is formatted as a JavaScript-compatible string.
305+
#[wasm_bindgen(js_name = "keyHashToBase64Key")]
306+
pub fn get_base64_key_from_key_hash_js_alias(formatted_key_hash: &str) -> Result<String, JsError> {
307+
get_base64_key_from_key_hash(formatted_key_hash).map_err(|err| {
308+
let error_text = format!("Error serializing package hash: {:?}", err);
309+
JsError::new(&error_text)
310+
})
311+
}
312+
284313
/// Gets the current timestamp.
285314
///
286315
/// # Returns

0 commit comments

Comments
 (0)