Application Crypto cleanup (#13746)

* Adjust application crypto docs

* Blanket implementation for 'RuntimeAppPublic' trait

* Blanket implementation for 'BoundToRuntimeAppPublic' for 'RuntimeAppPublic'

* Relax type bounds

* Docs fix

* restore MaybeHash

* Commit suggestion

Co-authored-by: Anton <anton.kalyaev@gmail.com>

---------

Co-authored-by: Anton <anton.kalyaev@gmail.com>
This commit is contained in:
Davide Galassi
2023-03-30 10:30:35 +02:00
committed by GitHub
parent 29e31f2b0a
commit 7985495b8c
7 changed files with 91 additions and 111 deletions
@@ -24,13 +24,7 @@ use sp_std::vec::Vec;
pub use sp_core::ecdsa::*;
mod app {
use sp_core::testing::ECDSA;
crate::app_crypto!(super, ECDSA);
impl crate::traits::BoundToRuntimeAppPublic for Public {
type Public = Self;
}
crate::app_crypto!(super, sp_core::testing::ECDSA);
}
#[cfg(feature = "full_crypto")]
@@ -24,13 +24,7 @@ use sp_std::vec::Vec;
pub use sp_core::ed25519::*;
mod app {
use sp_core::testing::ED25519;
crate::app_crypto!(super, ED25519);
impl crate::traits::BoundToRuntimeAppPublic for Public {
type Public = Self;
}
crate::app_crypto!(super, sp_core::testing::ED25519);
}
#[cfg(feature = "full_crypto")]
@@ -48,14 +48,15 @@ mod traits;
pub use traits::*;
/// Declares Public, Pair, Signature types which are functionally equivalent to `$pair`, but are new
/// Application-specific types whose identifier is `$key_type`.
/// Declares `Public`, `Pair` and `Signature` types which are functionally equivalent
/// to the corresponding types defined by `$module` but are new application-specific
/// types whose identifier is `$key_type`.
///
/// ```rust
/// # use sp_application_crypto::{app_crypto, wrap, ed25519, KeyTypeId};
/// // Declare a new set of crypto types using Ed25519 logic that identifies as `KeyTypeId`
/// # use sp_application_crypto::{app_crypto, ed25519, KeyTypeId};
/// // Declare a new set of crypto types using ed25519 logic that identifies as `KeyTypeId`
/// // of value `b"fuba"`.
/// app_crypto!(ed25519, KeyTypeId(*b"_uba"));
/// app_crypto!(ed25519, KeyTypeId(*b"fuba"));
/// ```
#[cfg(feature = "full_crypto")]
#[macro_export]
@@ -78,14 +79,15 @@ macro_rules! app_crypto {
};
}
/// Declares Public, Pair, Signature types which are functionally equivalent to `$pair`, but are new
/// Application-specific types whose identifier is `$key_type`.
/// Declares `Public`, `Pair` and `Signature` types which are functionally equivalent
/// to the corresponding types defined by `$module` but that are new application-specific
/// types whose identifier is `$key_type`.
///
/// ```rust
/// # use sp_application_crypto::{app_crypto, wrap, ed25519, KeyTypeId};
/// // Declare a new set of crypto types using Ed25519 logic that identifies as `KeyTypeId`
/// # use sp_application_crypto::{app_crypto, ed25519, KeyTypeId};
/// // Declare a new set of crypto types using ed25519 logic that identifies as `KeyTypeId`
/// // of value `b"fuba"`.
/// app_crypto!(ed25519, KeyTypeId(*b"_uba"));
/// app_crypto!(ed25519, KeyTypeId(*b"fuba"));
/// ```
#[cfg(not(feature = "full_crypto"))]
#[macro_export]
@@ -107,8 +109,8 @@ macro_rules! app_crypto {
};
}
/// Declares Pair type which is functionally equivalent to `$pair`, but is new
/// Application-specific type whose identifier is `$key_type`.
/// Declares `Pair` type which is functionally equivalent to `$pair`, but is
/// new application-specific type whose identifier is `$key_type`.
#[macro_export]
macro_rules! app_crypto_pair {
($pair:ty, $key_type:expr, $crypto_type:expr) => {
@@ -208,10 +210,10 @@ macro_rules! app_crypto_pair_functions_if_std {
($pair:ty) => {};
}
/// Declares Public type which is functionally equivalent to `$public`, but is new
/// Application-specific type whose identifier is `$key_type`.
/// can only be used together with `full_crypto` feature
/// For full functionality, app_crypto_public_common! must be called too.
/// Declares `Public` type which is functionally equivalent to `$public` but is
/// new application-specific type whose identifier is `$key_type`.
/// For full functionality, `app_crypto_public_common!` must be called too.
/// Can only be used with `full_crypto` feature.
#[doc(hidden)]
#[macro_export]
macro_rules! app_crypto_public_full_crypto {
@@ -244,10 +246,10 @@ macro_rules! app_crypto_public_full_crypto {
};
}
/// Declares Public type which is functionally equivalent to `$public`, but is new
/// Application-specific type whose identifier is `$key_type`.
/// can only be used without `full_crypto` feature
/// For full functionality, app_crypto_public_common! must be called too.
/// Declares `Public` type which is functionally equivalent to `$public` but is
/// new application-specific type whose identifier is `$key_type`.
/// For full functionality, `app_crypto_public_common!` must be called too.
/// Can only be used without `full_crypto` feature.
#[doc(hidden)]
#[macro_export]
macro_rules! app_crypto_public_not_full_crypto {
@@ -276,9 +278,9 @@ macro_rules! app_crypto_public_not_full_crypto {
};
}
/// Declares Public type which is functionally equivalent to `$public`, but is new
/// Application-specific type whose identifier is `$key_type`.
/// For full functionality, app_crypto_public_(not)_full_crypto! must be called too.
/// Declares `Public` type which is functionally equivalent to `$public` but is
/// new application-specific type whose identifier is `$key_type`.
/// For full functionality, `app_crypto_public_(not)_full_crypto!` must be called too.
#[doc(hidden)]
#[macro_export]
macro_rules! app_crypto_public_common {
@@ -307,40 +309,6 @@ macro_rules! app_crypto_public_common {
type Generic = $public;
}
impl $crate::RuntimeAppPublic for Public
where
$public: $crate::RuntimePublic<Signature = $sig>,
{
const ID: $crate::KeyTypeId = $key_type;
const CRYPTO_ID: $crate::CryptoTypeId = $crypto_type;
type Signature = Signature;
fn all() -> $crate::Vec<Self> {
<$public as $crate::RuntimePublic>::all($key_type)
.into_iter()
.map(Self)
.collect()
}
fn generate_pair(seed: Option<$crate::Vec<u8>>) -> Self {
Self(<$public as $crate::RuntimePublic>::generate_pair($key_type, seed))
}
fn sign<M: AsRef<[u8]>>(&self, msg: &M) -> Option<Self::Signature> {
<$public as $crate::RuntimePublic>::sign(self.as_ref(), $key_type, msg)
.map(Signature)
}
fn verify<M: AsRef<[u8]>>(&self, msg: &M, signature: &Self::Signature) -> bool {
<$public as $crate::RuntimePublic>::verify(self.as_ref(), msg, &signature.as_ref())
}
fn to_raw_vec(&self) -> $crate::Vec<u8> {
<$public as $crate::RuntimePublic>::to_raw_vec(&self.0)
}
}
impl<'a> TryFrom<&'a [u8]> for Public {
type Error = ();
@@ -407,8 +375,8 @@ macro_rules! app_crypto_public_common_if_std {
/// Declares Signature type which is functionally equivalent to `$sig`, but is new
/// Application-specific type whose identifier is `$key_type`.
/// can only be used together with `full_crypto` feature
/// For full functionality, app_crypto_public_common! must be called too.
/// Can only be used with `full_crypto` feature
#[doc(hidden)]
#[macro_export]
macro_rules! app_crypto_signature_full_crypto {
@@ -439,10 +407,10 @@ macro_rules! app_crypto_signature_full_crypto {
};
}
/// Declares Signature type which is functionally equivalent to `$sig`, but is new
/// Application-specific type whose identifier is `$key_type`.
/// can only be used without `full_crypto` feature
/// For full functionality, app_crypto_public_common! must be called too.
/// Declares `Signature` type which is functionally equivalent to `$sig`, but is new
/// application-specific type whose identifier is `$key_type`.
/// For full functionality, `app_crypto_signature_common` must be called too.
/// Can only be used without `full_crypto` feature.
#[doc(hidden)]
#[macro_export]
macro_rules! app_crypto_signature_not_full_crypto {
@@ -452,8 +420,8 @@ macro_rules! app_crypto_signature_not_full_crypto {
#[derive(Clone, Eq, PartialEq,
$crate::codec::Encode,
$crate::codec::Decode,
$crate::scale_info::TypeInfo,
$crate::RuntimeDebug,
$crate::scale_info::TypeInfo,
)]
pub struct Signature($sig);
}
@@ -469,9 +437,9 @@ macro_rules! app_crypto_signature_not_full_crypto {
};
}
/// Declares Signature type which is functionally equivalent to `$sig`, but is new
/// Application-specific type whose identifier is `$key_type`.
/// For full functionality, app_crypto_public_(not)_full_crypto! must be called too.
/// Declares `Signature` type which is functionally equivalent to `$sig`, but is new
/// application-specific type whose identifier is `$key_type`.
/// For full functionality, app_crypto_signature_(not)_full_crypto! must be called too.
#[doc(hidden)]
#[macro_export]
macro_rules! app_crypto_signature_common {
@@ -24,13 +24,7 @@ use sp_std::vec::Vec;
pub use sp_core::sr25519::*;
mod app {
use sp_core::testing::SR25519;
crate::app_crypto!(super, SR25519);
impl crate::traits::BoundToRuntimeAppPublic for Public {
type Public = Self;
}
crate::app_crypto!(super, sp_core::testing::SR25519);
}
#[cfg(feature = "full_crypto")]
@@ -15,10 +15,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.
use codec::Codec;
use scale_info::TypeInfo;
#[cfg(feature = "full_crypto")]
use sp_core::crypto::Pair;
use codec::Codec;
use sp_core::crypto::{CryptoType, CryptoTypeId, IsWrappedBy, KeyTypeId, Public};
use sp_std::{fmt::Debug, vec::Vec};
@@ -60,15 +61,9 @@ pub trait MaybeHash {}
#[cfg(all(not(feature = "std"), not(feature = "full_crypto")))]
impl<T> MaybeHash for T {}
/// Type which implements Debug and Hash in std, not when no-std (no-std variant with crypto).
#[cfg(all(not(feature = "std"), feature = "full_crypto"))]
pub trait MaybeDebugHash: sp_std::hash::Hash {}
#[cfg(all(not(feature = "std"), feature = "full_crypto"))]
impl<T: sp_std::hash::Hash> MaybeDebugHash for T {}
/// A application's public key.
pub trait AppPublic:
AppCrypto + Public + Ord + PartialOrd + Eq + PartialEq + Debug + MaybeHash + codec::Codec
AppCrypto + Public + Ord + PartialOrd + Eq + PartialEq + Debug + MaybeHash + Codec
{
/// The wrapped type which is just a plain instance of `Public`.
type Generic: IsWrappedBy<Self>
@@ -79,7 +74,7 @@ pub trait AppPublic:
+ PartialEq
+ Debug
+ MaybeHash
+ codec::Codec;
+ Codec;
}
/// A application's key pair.
@@ -92,15 +87,15 @@ pub trait AppPair: AppCrypto + Pair<Public = <Self as AppCrypto>::Public> {
}
/// A application's signature.
pub trait AppSignature: AppCrypto + Eq + PartialEq + Debug + MaybeHash {
pub trait AppSignature: AppCrypto + Eq + PartialEq + Debug {
/// The wrapped type which is just a plain instance of `Signature`.
type Generic: IsWrappedBy<Self> + Eq + PartialEq + Debug + MaybeHash;
type Generic: IsWrappedBy<Self> + Eq + PartialEq + Debug;
}
/// A runtime interface for a public key.
pub trait RuntimePublic: Sized {
/// The signature that will be generated when signing with the corresponding private key.
type Signature: Codec + Debug + MaybeHash + Eq + PartialEq + Clone;
type Signature: Debug + Eq + PartialEq + Clone;
/// Returns all public keys for the given key type in the keystore.
fn all(key_type: KeyTypeId) -> crate::Vec<Self>;
@@ -132,11 +127,9 @@ pub trait RuntimePublic: Sized {
pub trait RuntimeAppPublic: Sized {
/// An identifier for this application-specific key type.
const ID: KeyTypeId;
/// The identifier of the crypto type of this application-specific key type.
const CRYPTO_ID: CryptoTypeId;
/// The signature that will be generated when signing with the corresponding private key.
type Signature: Codec + Debug + MaybeHash + Eq + PartialEq + Clone + scale_info::TypeInfo;
type Signature: Debug + Eq + PartialEq + Clone + TypeInfo + Codec;
/// Returns all public keys for this application in the keystore.
fn all() -> crate::Vec<Self>;
@@ -163,8 +156,50 @@ pub trait RuntimeAppPublic: Sized {
fn to_raw_vec(&self) -> Vec<u8>;
}
/// Something that bound to a fixed [`RuntimeAppPublic`].
impl<T> RuntimeAppPublic for T
where
T: AppPublic + AsRef<<T as AppPublic>::Generic>,
<T as AppPublic>::Generic: RuntimePublic,
<T as AppCrypto>::Signature: TypeInfo
+ Codec
+ From<<<T as AppPublic>::Generic as RuntimePublic>::Signature>
+ AsRef<<<T as AppPublic>::Generic as RuntimePublic>::Signature>,
{
const ID: KeyTypeId = <T as AppCrypto>::ID;
type Signature = <T as AppCrypto>::Signature;
fn all() -> crate::Vec<Self> {
<<T as AppPublic>::Generic as RuntimePublic>::all(Self::ID)
.into_iter()
.map(|p| p.into())
.collect()
}
fn generate_pair(seed: Option<Vec<u8>>) -> Self {
<<T as AppPublic>::Generic as RuntimePublic>::generate_pair(Self::ID, seed).into()
}
fn sign<M: AsRef<[u8]>>(&self, msg: &M) -> Option<Self::Signature> {
<<T as AppPublic>::Generic as RuntimePublic>::sign(self.as_ref(), Self::ID, msg)
.map(|s| s.into())
}
fn verify<M: AsRef<[u8]>>(&self, msg: &M, signature: &Self::Signature) -> bool {
<<T as AppPublic>::Generic as RuntimePublic>::verify(self.as_ref(), msg, signature.as_ref())
}
fn to_raw_vec(&self) -> Vec<u8> {
<<T as AppPublic>::Generic as RuntimePublic>::to_raw_vec(self.as_ref())
}
}
/// Something that is bound to a fixed [`RuntimeAppPublic`].
pub trait BoundToRuntimeAppPublic {
/// The [`RuntimeAppPublic`] this type is bound to.
type Public: RuntimeAppPublic;
}
impl<T: RuntimeAppPublic> BoundToRuntimeAppPublic for T {
type Public = Self;
}