diff --git a/core/src/types/event/fission.rs b/core/src/types/event/fission.rs index 18679d6..644490e 100644 --- a/core/src/types/event/fission.rs +++ b/core/src/types/event/fission.rs @@ -1,4 +1,4 @@ -use crate::{Error, Hash, Result, SerializeBytes}; +use crate::{Hash, Result, SerializeBytes}; use serde::{Deserialize, Serialize}; #[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)] @@ -13,20 +13,16 @@ impl SerializeBytes for Fission { const SIZE: usize = 3 * 32; fn to_slice(&self, out: &mut [u8]) { - out[..32].copy_from_slice(&*self.a); - out[32..64].copy_from_slice(&*self.b); - out[64..].copy_from_slice(&*self.c); + self.a.to_slice(&mut out[..Hash::SIZE]); + self.b.to_slice(&mut out[Hash::SIZE..Hash::SIZE * 2]); + self.c.to_slice(&mut out[Hash::SIZE * 2..Hash::SIZE * 3]); } fn from_slice(input: &[u8]) -> Result { - if input.len() < Self::SIZE { - return Err(Error::FailedDeserialization); - } - - let a = input[..32].try_into()?; - let b = input[Hash::SIZE..Hash::SIZE * 2].try_into()?; - let c = input[Hash::SIZE..Hash::SIZE * 2].try_into()?; - - Ok(Self { a, b, c }) + Ok(Self { + a: Hash::from_slice(&input[..Hash::SIZE])?, + b: Hash::from_slice(&input[Hash::SIZE..Hash::SIZE * 2])?, + c: Hash::from_slice(&input[Hash::SIZE * 2..Hash::SIZE * 3])?, + }) } } diff --git a/core/src/types/event/fusion.rs b/core/src/types/event/fusion.rs index 4e1022f..9568484 100644 --- a/core/src/types/event/fusion.rs +++ b/core/src/types/event/fusion.rs @@ -1,4 +1,4 @@ -use crate::{Error, Hash, Result, SerializeBytes}; +use crate::{Hash, Result, SerializeBytes}; use serde::{Deserialize, Serialize}; #[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)] @@ -13,20 +13,16 @@ impl SerializeBytes for Fusion { const SIZE: usize = 3 * 32; fn to_slice(&self, out: &mut [u8]) { - out[..32].copy_from_slice(&*self.a); - out[32..64].copy_from_slice(&*self.b); - out[64..].copy_from_slice(&*self.c); + self.a.to_slice(&mut out[..Hash::SIZE]); + self.b.to_slice(&mut out[Hash::SIZE..Hash::SIZE * 2]); + self.c.to_slice(&mut out[Hash::SIZE * 2..Hash::SIZE * 3]); } fn from_slice(input: &[u8]) -> Result { - if input.len() < Self::SIZE { - return Err(Error::FailedDeserialization); - } - - let a = input[..32].try_into()?; - let b = input[Hash::SIZE..Hash::SIZE * 2].try_into()?; - let c = input[Hash::SIZE..Hash::SIZE * 2].try_into()?; - - Ok(Self { a, b, c }) + Ok(Self { + a: Hash::from_slice(&input[..Hash::SIZE])?, + b: Hash::from_slice(&input[Hash::SIZE..Hash::SIZE * 2])?, + c: Hash::from_slice(&input[Hash::SIZE * 2..Hash::SIZE * 3])?, + }) } } diff --git a/core/src/types/note.rs b/core/src/types/note.rs index 4a6faa2..a71a33e 100644 --- a/core/src/types/note.rs +++ b/core/src/types/note.rs @@ -16,24 +16,16 @@ impl SerializeBytes for Note { fn to_slice(&self, out: &mut [u8]) { self.asset_id.to_slice(&mut out[..Hash::SIZE]); self.amount - .to_le_bytes() - .copy_from_slice(&mut out[Hash::SIZE..Hash::SIZE + u64::SIZE]); - self.nullifier.to_slice(&mut out[Hash::SIZE + u64::SIZE..]); + .to_slice(&mut out[Hash::SIZE..Hash::SIZE + u64::SIZE]); + self.nullifier + .to_slice(&mut out[Hash::SIZE + u64::SIZE..Self::SIZE]); } fn from_slice(input: &[u8]) -> Result { - if input.len() < Self::SIZE { - return Err(Error::FailedDeserialization); - } - Ok(Self { asset_id: Hash::from_slice(&input[..Hash::SIZE])?, - amount: u64::from_le_bytes( - input[Hash::SIZE..Hash::SIZE + u64::SIZE] - .try_into() - .unwrap(), - ), - nullifier: Signature::from_slice(&input[Hash::SIZE + u64::SIZE..])?, + amount: u64::from_slice(&input[Hash::SIZE..Hash::SIZE + u64::SIZE])?, + nullifier: Signature::from_slice(&input[Hash::SIZE + u64::SIZE..Self::SIZE])?, }) } } @@ -64,24 +56,16 @@ impl SerializeBytes for BlindedNote { fn to_slice(&self, out: &mut [u8]) { self.asset_id.to_slice(&mut out[..Hash::SIZE]); self.amount - .to_le_bytes() - .copy_from_slice(&mut out[Hash::SIZE..Hash::SIZE + u64::SIZE]); - self.secret.to_slice(&mut out[Hash::SIZE + u64::SIZE..]); + .to_slice(&mut out[Hash::SIZE..Hash::SIZE + u64::SIZE]); + self.secret + .to_slice(&mut out[Hash::SIZE + u64::SIZE..Self::SIZE]); } fn from_slice(input: &[u8]) -> Result { - if input.len() < Self::SIZE { - return Err(Error::FailedDeserialization); - } - Ok(Self { asset_id: Hash::from_slice(&input[..Hash::SIZE])?, - amount: u64::from_le_bytes( - input[Hash::SIZE..Hash::SIZE + u64::SIZE] - .try_into() - .unwrap(), - ), - secret: Hash::from_slice(&input[Hash::SIZE + u64::SIZE..])?, + amount: u64::from_slice(&input[Hash::SIZE..Hash::SIZE + u64::SIZE])?, + secret: Hash::from_slice(&input[Hash::SIZE + u64::SIZE..Self::SIZE])?, }) } } diff --git a/core/src/types/operation/join.rs b/core/src/types/operation/join.rs index 154203e..d1e4389 100644 --- a/core/src/types/operation/join.rs +++ b/core/src/types/operation/join.rs @@ -15,8 +15,12 @@ impl SerializeBytes for Join { self.inputs[1].to_slice(&mut out[Note::SIZE..]); } - fn from_slice(bytes: &[u8]) -> Result { - let (a, b) = <(Note, Note)>::from_slice(bytes)?; - Ok(Self { inputs: [a, b] }) + fn from_slice(input: &[u8]) -> Result { + Ok(Self { + inputs: [ + Note::from_slice(&input[..Note::SIZE])?, + Note::from_slice(&input[Note::SIZE..Note::SIZE * 2])?, + ], + }) } }