// This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. // SPDX-License-Identifier: Apache-2.0 // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. //! Generic byte array which can be specialized with a marker type. use crate::{ crypto::{CryptoType, Derive, FromEntropy, Public, Signature, UncheckedFrom}, hash::{H256, H512}, }; use codec::{Decode, Encode, MaxEncodedLen}; use core::marker::PhantomData; use scale_info::TypeInfo; use sp_runtime_interface::pass_by::{self, PassBy, PassByInner}; #[cfg(feature = "serde")] use crate::crypto::Ss58Codec; #[cfg(feature = "serde")] use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; #[cfg(all(not(feature = "std"), feature = "serde"))] use sp_std::alloc::{format, string::String}; pub use public_bytes::*; pub use signature_bytes::*; /// Generic byte array holding some crypto-related raw data. /// /// The type is generic over a constant length `N` and a "tag" `T` which /// can be used to specialize the byte array without requiring newtypes. /// /// The tag `T` is held in a `PhantomDataT>`, a trick allowing /// `CryptoBytes` to be `Send` and `Sync` regardless of `T` properties /// ([ref](https://doc.rust-lang.org/nomicon/phantom-data.html#table-of-phantomdata-patterns)). #[derive(Encode, Decode, MaxEncodedLen)] #[repr(transparent)] pub struct CryptoBytes(pub [u8; N], PhantomData T>); impl Copy for CryptoBytes {} impl Clone for CryptoBytes { fn clone(&self) -> Self { Self(self.0, PhantomData) } } impl TypeInfo for CryptoBytes { type Identity = [u8; N]; fn type_info() -> scale_info::Type { Self::Identity::type_info() } } impl PartialOrd for CryptoBytes { fn partial_cmp(&self, other: &Self) -> Option { self.0.partial_cmp(&other.0) } } impl Ord for CryptoBytes { fn cmp(&self, other: &Self) -> core::cmp::Ordering { self.0.cmp(&other.0) } } impl PartialEq for CryptoBytes { fn eq(&self, other: &Self) -> bool { self.0.eq(&other.0) } } impl core::hash::Hash for CryptoBytes { fn hash(&self, state: &mut H) { self.0.hash(state) } } impl Eq for CryptoBytes {} impl Default for CryptoBytes { fn default() -> Self { Self([0_u8; N], PhantomData) } } impl PassByInner for CryptoBytes { type Inner = [u8; N]; fn into_inner(self) -> Self::Inner { self.0 } fn inner(&self) -> &Self::Inner { &self.0 } fn from_inner(inner: Self::Inner) -> Self { Self(inner, PhantomData) } } impl PassBy for CryptoBytes { type PassBy = pass_by::Inner; } impl AsRef<[u8]> for CryptoBytes { fn as_ref(&self) -> &[u8] { &self.0[..] } } impl AsMut<[u8]> for CryptoBytes { fn as_mut(&mut self) -> &mut [u8] { &mut self.0[..] } } impl From> for [u8; N] { fn from(v: CryptoBytes) -> [u8; N] { v.0 } } impl AsRef<[u8; N]> for CryptoBytes { fn as_ref(&self) -> &[u8; N] { &self.0 } } impl AsMut<[u8; N]> for CryptoBytes { fn as_mut(&mut self) -> &mut [u8; N] { &mut self.0 } } impl From<[u8; N]> for CryptoBytes { fn from(value: [u8; N]) -> Self { Self::from_raw(value) } } impl TryFrom<&[u8]> for CryptoBytes { type Error = (); fn try_from(data: &[u8]) -> Result { if data.len() != N { return Err(()) } let mut r = [0u8; N]; r.copy_from_slice(data); Ok(Self::from_raw(r)) } } impl UncheckedFrom<[u8; N]> for CryptoBytes { fn unchecked_from(data: [u8; N]) -> Self { Self::from_raw(data) } } impl core::ops::Deref for CryptoBytes { type Target = [u8]; fn deref(&self) -> &Self::Target { &self.0 } } impl CryptoBytes { /// Construct from raw array. pub fn from_raw(inner: [u8; N]) -> Self { Self(inner, PhantomData) } /// Construct from raw array. pub fn to_raw(self) -> [u8; N] { self.0 } /// Return a slice filled with raw data. pub fn as_array_ref(&self) -> &[u8; N] { &self.0 } } impl crate::ByteArray for CryptoBytes { const LEN: usize = N; } impl FromEntropy for CryptoBytes { fn from_entropy(input: &mut impl codec::Input) -> Result { let mut result = Self::default(); input.read(result.as_mut())?; Ok(result) } } impl From> for H256 { fn from(x: CryptoBytes<32, T>) -> H256 { H256::from(x.0) } } impl From> for H512 { fn from(x: CryptoBytes<64, T>) -> H512 { H512::from(x.0) } } impl UncheckedFrom for CryptoBytes<32, T> { fn unchecked_from(x: H256) -> Self { Self::from_h256(x) } } impl CryptoBytes<32, T> { /// A new instance from an H256. pub fn from_h256(x: H256) -> Self { Self::from_raw(x.into()) } } impl CryptoBytes<64, T> { /// A new instance from an H512. pub fn from_h512(x: H512) -> Self { Self::from_raw(x.into()) } } mod public_bytes { use super::*; /// Tag used for generic public key bytes. pub struct PublicTag; /// Generic encoded public key. pub type PublicBytes = CryptoBytes; impl Derive for PublicBytes where Self: CryptoType {} impl Public for PublicBytes where Self: CryptoType {} impl sp_std::fmt::Debug for PublicBytes where Self: CryptoType, { #[cfg(feature = "std")] fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { let s = self.to_ss58check(); write!(f, "{} ({}...)", crate::hexdisplay::HexDisplay::from(&self.as_ref()), &s[0..8]) } #[cfg(not(feature = "std"))] fn fmt(&self, _: &mut sp_std::fmt::Formatter) -> sp_std::fmt::Result { Ok(()) } } #[cfg(feature = "std")] impl std::fmt::Display for PublicBytes where Self: CryptoType, { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { write!(f, "{}", self.to_ss58check()) } } #[cfg(feature = "std")] impl std::str::FromStr for PublicBytes where Self: CryptoType, { type Err = crate::crypto::PublicError; fn from_str(s: &str) -> Result { Self::from_ss58check(s) } } #[cfg(feature = "serde")] impl Serialize for PublicBytes where Self: CryptoType, { fn serialize(&self, serializer: S) -> Result where S: Serializer, { serializer.serialize_str(&self.to_ss58check()) } } #[cfg(feature = "serde")] impl<'de, const N: usize, SubTag> Deserialize<'de> for PublicBytes where Self: CryptoType, { fn deserialize(deserializer: D) -> Result where D: Deserializer<'de>, { Self::from_ss58check(&String::deserialize(deserializer)?) .map_err(|e| de::Error::custom(format!("{:?}", e))) } } } mod signature_bytes { use super::*; /// Tag used for generic signature bytes. pub struct SignatureTag; /// Generic encoded signature. pub type SignatureBytes = CryptoBytes; impl Signature for SignatureBytes where Self: CryptoType {} #[cfg(feature = "serde")] impl Serialize for SignatureBytes where Self: CryptoType, { fn serialize(&self, serializer: S) -> Result where S: Serializer, { serializer.serialize_str(&array_bytes::bytes2hex("", self)) } } #[cfg(feature = "serde")] impl<'de, const N: usize, SubTag> Deserialize<'de> for SignatureBytes where Self: CryptoType, { fn deserialize(deserializer: D) -> Result where D: Deserializer<'de>, { let signature_hex = array_bytes::hex2bytes(&String::deserialize(deserializer)?) .map_err(|e| de::Error::custom(format!("{:?}", e)))?; Self::try_from(signature_hex.as_ref()) .map_err(|e| de::Error::custom(format!("{:?}", e))) } } impl sp_std::fmt::Debug for SignatureBytes where Self: CryptoType, { #[cfg(feature = "std")] fn fmt(&self, f: &mut sp_std::fmt::Formatter) -> sp_std::fmt::Result { write!(f, "{}", crate::hexdisplay::HexDisplay::from(&&self.0[..])) } #[cfg(not(feature = "std"))] fn fmt(&self, _: &mut sp_std::fmt::Formatter) -> sp_std::fmt::Result { Ok(()) } } }