Skip to content

Commit

Permalink
Merge pull request #35 from pendulum-chain/to-base64-xdr-string-support
Browse files Browse the repository at this point in the history
conversion to base64 string
  • Loading branch information
b-yap authored Oct 21, 2024
2 parents 519b60d + 52e366e commit 80cd93d
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/utils/std.rs
Original file line number Diff line number Diff line change
@@ -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`
Expand All @@ -9,3 +10,17 @@ use crate::lib::String;
pub trait StellarTypeToString<T, E: From<sp_std::str::Utf8Error>> {
fn as_encoded_string(&self) -> Result<String, E>;
}

/// 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 <T: XdrCodec> 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()
}
}

0 comments on commit 80cd93d

Please sign in to comment.