diff --git a/src/utils/std.rs b/src/utils/std.rs index 9dc8784..811ea1a 100644 --- a/src/utils/std.rs +++ b/src/utils/std.rs @@ -1,4 +1,5 @@ use crate::lib::String; +use crate::XdrCodec; /// A trait used to convert any Stellar specific type `T` as a String /// This also immediately converts the standard Error to a user-defined Error `E` @@ -9,3 +10,17 @@ use crate::lib::String; pub trait StellarTypeToString> { fn as_encoded_string(&self) -> Result; } + +/// A trait used to convert a structure into Base64 String +pub trait StellarTypeToBase64String { + /// returns a Base64 encoded String or a vec of bytes [xdr] + fn as_base64_encoded_string(&self) -> String; +} + +impl StellarTypeToBase64String for T { + fn as_base64_encoded_string(&self) -> String { + let xdr = self.to_base64_xdr(); + // safe to use `unwrap`, since `to_base64_xdr()` will always return a valid vec of + String::from_utf8(xdr.clone()).unwrap() + } +} \ No newline at end of file