Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serialize V4 ID field in Slate as Base64 #387

Merged
merged 1 commit into from
Apr 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -65,6 +65,7 @@ pub struct SlateV4 {
#[serde(skip_serializing_if = "Option::is_none")]
pub num_participants: Option<usize>,
/// Unique transaction ID, selected by sender
#[serde(with = "ser::uuid_base64")]
pub id: Uuid,
/// Slate state
#[serde(with = "ser::slate_state_v4")]
Expand Down