Skip to content

Commit

Permalink
Added custom Decode for Key and Hash
Browse files Browse the repository at this point in the history
  • Loading branch information
xgreenx committed Nov 15, 2021
1 parent 2e40d9b commit 7cf279b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
21 changes: 9 additions & 12 deletions crates/env/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,22 +268,19 @@ impl<'a> TryFrom<&'a [u8]> for AccountId {
/// This is a mirror of the `Hash` type used in the default configuration
/// of PALLET contracts.
#[derive(
Debug,
Copy,
Clone,
PartialEq,
Eq,
Ord,
PartialOrd,
Hash,
Encode,
Decode,
From,
Default,
Debug, Copy, Clone, PartialEq, Eq, Ord, PartialOrd, Hash, Encode, From, Default,
)]
#[cfg_attr(feature = "std", derive(TypeInfo))]
pub struct Hash([u8; 32]);

impl Decode for Hash {
fn decode<I: scale::Input>(input: &mut I) -> Result<Self, scale::Error> {
let mut hash = Hash { 0: [0; 32] };
input.read(hash.0.as_mut_slice())?;
Ok(hash)
}
}

impl<'a> TryFrom<&'a [u8]> for Hash {
type Error = TryFromSliceError;

Expand Down
5 changes: 3 additions & 2 deletions crates/primitives/src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,9 @@ impl scale::Decode for Key {
where
I: scale::Input,
{
let bytes = <[u8; 32] as scale::Decode>::decode(input)?;
Ok(Self::from(bytes))
let mut key = Key { 0: [0; 32] };
input.read(key.0.as_mut_slice())?;
Ok(key)
}

#[inline]
Expand Down

0 comments on commit 7cf279b

Please sign in to comment.