2021-03-20 19:38:44 +00:00
|
|
|
//! Contains common types and functions related to serialization and integrity
|
2020-04-07 22:39:07 +00:00
|
|
|
use rand::Rng;
|
2020-04-10 20:01:48 +00:00
|
|
|
use serde::de::{self, Visitor};
|
|
|
|
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
|
|
|
use std::fmt;
|
2020-04-05 21:33:42 +00:00
|
|
|
|
2021-03-20 19:38:44 +00:00
|
|
|
/// An array of 32 bytes
|
2020-04-21 17:08:42 +00:00
|
|
|
#[derive(Default, PartialOrd, Ord, Clone, Hash, PartialEq, Copy)]
|
2020-04-07 16:10:20 +00:00
|
|
|
pub struct FixedBytes32([u8; 32]);
|
|
|
|
|
|
|
|
impl From<[u8; 32]> for FixedBytes32 {
|
|
|
|
fn from(x: [u8; 32]) -> FixedBytes32 {
|
|
|
|
FixedBytes32(x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::convert::AsRef<[u8]> for FixedBytes32 {
|
|
|
|
fn as_ref(&self) -> &[u8] {
|
|
|
|
&self.0[..]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Eq for FixedBytes32 {}
|
|
|
|
|
|
|
|
impl fmt::Debug for FixedBytes32 {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2020-04-17 16:27:29 +00:00
|
|
|
write!(f, "{}…", hex::encode(&self.0[..8]))
|
2020-04-07 16:10:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct FixedBytes32Visitor;
|
|
|
|
impl<'de> Visitor<'de> for FixedBytes32Visitor {
|
|
|
|
type Value = FixedBytes32;
|
|
|
|
|
|
|
|
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
formatter.write_str("a byte slice of size 32")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_bytes<E: de::Error>(self, value: &[u8]) -> Result<Self::Value, E> {
|
|
|
|
if value.len() == 32 {
|
|
|
|
let mut res = [0u8; 32];
|
|
|
|
res.copy_from_slice(value);
|
|
|
|
Ok(res.into())
|
|
|
|
} else {
|
2020-04-10 20:01:48 +00:00
|
|
|
Err(E::custom(format!(
|
|
|
|
"Invalid byte string length {}, expected 32",
|
|
|
|
value.len()
|
|
|
|
)))
|
2020-04-07 16:10:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'de> Deserialize<'de> for FixedBytes32 {
|
|
|
|
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<FixedBytes32, D::Error> {
|
|
|
|
deserializer.deserialize_bytes(FixedBytes32Visitor)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Serialize for FixedBytes32 {
|
|
|
|
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
|
|
|
|
serializer.serialize_bytes(&self.0[..])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FixedBytes32 {
|
2021-03-21 23:01:44 +00:00
|
|
|
/// Access the content as a slice
|
2020-04-07 16:10:20 +00:00
|
|
|
pub fn as_slice(&self) -> &[u8] {
|
|
|
|
&self.0[..]
|
|
|
|
}
|
2021-03-21 23:01:44 +00:00
|
|
|
/// Access the content as a mutable slice
|
2020-04-07 16:10:20 +00:00
|
|
|
pub fn as_slice_mut(&mut self) -> &mut [u8] {
|
|
|
|
&mut self.0[..]
|
|
|
|
}
|
2021-03-21 23:01:44 +00:00
|
|
|
/// Copy to a slice
|
2021-04-09 00:32:42 +00:00
|
|
|
pub fn to_vec(self) -> Vec<u8> {
|
2020-04-08 21:47:34 +00:00
|
|
|
self.0.to_vec()
|
|
|
|
}
|
2021-03-21 23:01:44 +00:00
|
|
|
/// Try building a FixedBytes32 from a slice
|
|
|
|
/// Return None if the slice is not 32 bytes long
|
2021-03-12 18:57:37 +00:00
|
|
|
pub fn try_from(by: &[u8]) -> Option<Self> {
|
|
|
|
if by.len() != 32 {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
let mut ret = [0u8; 32];
|
|
|
|
ret.copy_from_slice(by);
|
|
|
|
Some(Self(ret))
|
|
|
|
}
|
2020-04-07 16:10:20 +00:00
|
|
|
}
|
|
|
|
|
2021-03-20 19:38:44 +00:00
|
|
|
/// A 32 bytes UUID
|
2021-05-02 21:13:08 +00:00
|
|
|
pub type Uuid = FixedBytes32;
|
2021-03-20 19:38:44 +00:00
|
|
|
/// A 256 bit cryptographic hash, can be sha256 or blake2 depending on provenance
|
2020-04-07 16:10:20 +00:00
|
|
|
pub type Hash = FixedBytes32;
|
2020-04-05 21:33:42 +00:00
|
|
|
|
2021-03-20 19:38:44 +00:00
|
|
|
/// Compute the sha256 of a slice
|
2021-02-21 14:24:30 +00:00
|
|
|
pub fn sha256sum(data: &[u8]) -> Hash {
|
2021-02-23 16:52:28 +00:00
|
|
|
use sha2::{Digest, Sha256};
|
|
|
|
|
2020-04-07 22:39:07 +00:00
|
|
|
let mut hasher = Sha256::new();
|
2021-03-16 14:58:40 +00:00
|
|
|
hasher.update(data);
|
2020-04-07 22:39:07 +00:00
|
|
|
let mut hash = [0u8; 32];
|
2021-03-16 14:58:40 +00:00
|
|
|
hash.copy_from_slice(&hasher.finalize()[..]);
|
2020-04-07 22:39:07 +00:00
|
|
|
hash.into()
|
|
|
|
}
|
|
|
|
|
2021-03-20 19:38:44 +00:00
|
|
|
/// Compute the blake2 of a slice
|
2021-02-23 16:52:28 +00:00
|
|
|
pub fn blake2sum(data: &[u8]) -> Hash {
|
|
|
|
use blake2::{Blake2b, Digest};
|
|
|
|
|
|
|
|
let mut hasher = Blake2b::new();
|
|
|
|
hasher.update(data);
|
|
|
|
let mut hash = [0u8; 32];
|
|
|
|
hash.copy_from_slice(&hasher.finalize()[..32]);
|
|
|
|
hash.into()
|
|
|
|
}
|
|
|
|
|
2021-03-20 19:38:44 +00:00
|
|
|
/// A 64 bit non cryptographic hash
|
2021-02-23 16:52:28 +00:00
|
|
|
pub type FastHash = u64;
|
|
|
|
|
2021-03-20 19:38:44 +00:00
|
|
|
/// Compute a (non cryptographic) of a slice
|
2021-02-23 16:52:28 +00:00
|
|
|
pub fn fasthash(data: &[u8]) -> FastHash {
|
2021-03-19 11:19:40 +00:00
|
|
|
use xxhash_rust::xxh3::Xxh3;
|
2021-02-23 16:52:28 +00:00
|
|
|
|
2021-03-19 11:19:40 +00:00
|
|
|
let mut h = Xxh3::new();
|
|
|
|
h.update(data);
|
|
|
|
h.digest()
|
2021-02-23 16:52:28 +00:00
|
|
|
}
|
|
|
|
|
2021-03-20 19:38:44 +00:00
|
|
|
/// Generate a random 32 bytes UUID
|
2021-05-02 21:13:08 +00:00
|
|
|
pub fn gen_uuid() -> Uuid {
|
2020-04-07 22:39:07 +00:00
|
|
|
rand::thread_rng().gen::<[u8; 32]>().into()
|
|
|
|
}
|
|
|
|
|
2020-04-09 16:43:53 +00:00
|
|
|
// RMP serialization with names of fields and variants
|
|
|
|
|
2021-03-20 19:38:44 +00:00
|
|
|
/// Serialize to MessagePack
|
2020-04-09 16:43:53 +00:00
|
|
|
pub fn rmp_to_vec_all_named<T>(val: &T) -> Result<Vec<u8>, rmp_serde::encode::Error>
|
2020-04-10 20:01:48 +00:00
|
|
|
where
|
|
|
|
T: Serialize + ?Sized,
|
2020-04-09 16:43:53 +00:00
|
|
|
{
|
|
|
|
let mut wr = Vec::with_capacity(128);
|
|
|
|
let mut se = rmp_serde::Serializer::new(&mut wr)
|
|
|
|
.with_struct_map()
|
|
|
|
.with_string_variants();
|
|
|
|
val.serialize(&mut se)?;
|
|
|
|
Ok(wr)
|
|
|
|
}
|
|
|
|
|
2021-03-20 19:38:44 +00:00
|
|
|
/// Serialize to JSON, truncating long result
|
2020-04-16 17:28:02 +00:00
|
|
|
pub fn debug_serialize<T: Serialize>(x: T) -> String {
|
|
|
|
match serde_json::to_string(&x) {
|
|
|
|
Ok(ss) => {
|
|
|
|
if ss.len() > 100 {
|
2021-03-21 23:01:44 +00:00
|
|
|
// TODO this can panic if 100 is not a codepoint boundary, but inside a 2 Bytes
|
|
|
|
// (or more) codepoint
|
2020-04-16 17:28:02 +00:00
|
|
|
ss[..100].to_string()
|
|
|
|
} else {
|
|
|
|
ss
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(e) => format!("<JSON serialization error: {}>", e),
|
|
|
|
}
|
|
|
|
}
|