Skip to content

Commit

Permalink
serialize slate v4 ID as base64 (#387)
Browse files Browse the repository at this point in the history
  • Loading branch information
yeastplume authored Apr 21, 2020
1 parent f726d35 commit f4e0143
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
31 changes: 31 additions & 0 deletions libwallet/src/slate_versions/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,37 @@ pub mod slate_state_v4 {
}
}

/// Serializes an secp256k1 pubkey to base64
pub mod uuid_base64 {
use base64;
use serde::{Deserialize, Deserializer, Serializer};
use uuid::Uuid;

///
pub fn serialize<S>(id: &Uuid, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&base64::encode(&id.as_bytes()))
}

///
pub fn deserialize<'de, D>(deserializer: D) -> Result<Uuid, D::Error>
where
D: Deserializer<'de>,
{
use serde::de::Error;
String::deserialize(deserializer)
.and_then(|string| {
base64::decode(&string).map_err(|err| Error::custom(err.to_string()))
})
.and_then(|bytes: Vec<u8>| {
let mut b = [0u8; 16];
b.copy_from_slice(&bytes[0..16]);
Ok(Uuid::from_bytes(b))
})
}
}
// Test serialization methods of components that are being used
#[cfg(test)]
mod test {
Expand Down
1 change: 1 addition & 0 deletions libwallet/src/slate_versions/v4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ pub struct SlateV4 {
#[serde(with = "ser::version_info_v4")]
pub ver: VersionCompatInfoV4,
/// Unique transaction ID, selected by sender
#[serde(with = "ser::uuid_base64")]
pub id: Uuid,
/// Slate state
#[serde(with = "ser::slate_state_v4")]
Expand Down

0 comments on commit f4e0143

Please sign in to comment.