@@ -203,6 +203,29 @@ pub fn get_base64_key_from_package_hash(formatted_hash: &str) -> Result<String,
203
203
Ok ( general_purpose:: STANDARD . encode ( key) ) // base64.encode
204
204
}
205
205
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
+
206
229
/// Gets the time to live (TTL) value or returns the default value if not provided.
207
230
///
208
231
/// # Arguments
@@ -816,4 +839,17 @@ mod tests {
816
839
// Check the result against the expected output
817
840
assert_eq ! ( result, expected_output. to_string( ) ) ;
818
841
}
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
+ }
819
855
}
0 commit comments