Keystore overhaul (final) (#13683)

* Introduce keystore specialized sign methods

* Get rid of 'AppKey::UntypedGeneric' associated type.

Untyped generics are accessible using associated types 'Generic' associated type.
I.e. <T as AppKey>::Public::Generic

* Get rid of 'CryptoTypePublicPair'

* Trivial fix

* Small refactory of local keystore implementations

* Remove 'crypto_id' method from 'Public'

* Trivial rename of 'AppKey' to 'AppCrypto'

* Remove unused import

* Improve docs

* Better signature related errors for authority-discovery

* Apply review suggestion

* Apply review suggestions

Co-authored-by: Koute <koute@users.noreply.github.com>

* Authority discoverty signing error revisited

* Signing error revisited for babe and aura as well

* Further cleanup

---------

Co-authored-by: Koute <koute@users.noreply.github.com>
This commit is contained in:
Davide Galassi
2023-03-24 14:46:02 +01:00
committed by GitHub
parent 370e71cb20
commit 40e1704e1c
27 changed files with 552 additions and 616 deletions
@@ -27,10 +27,7 @@ pub use sp_core::crypto::{DeriveError, DeriveJunction, Pair, SecretStringError,
#[doc(hidden)]
pub use sp_core::{
self,
crypto::{
ByteArray, CryptoType, CryptoTypePublicPair, Derive, IsWrappedBy, Public, UncheckedFrom,
Wraps,
},
crypto::{ByteArray, CryptoType, Derive, IsWrappedBy, Public, UncheckedFrom, Wraps},
RuntimeDebug,
};
@@ -170,8 +167,7 @@ macro_rules! app_crypto_pair {
}
}
impl $crate::AppKey for Pair {
type UntypedGeneric = $pair;
impl $crate::AppCrypto for Pair {
type Public = Public;
type Pair = Pair;
type Signature = Signature;
@@ -238,8 +234,7 @@ macro_rules! app_crypto_public_full_crypto {
type Pair = Pair;
}
impl $crate::AppKey for Public {
type UntypedGeneric = $public;
impl $crate::AppCrypto for Public {
type Public = Public;
type Pair = Pair;
type Signature = Signature;
@@ -272,8 +267,7 @@ macro_rules! app_crypto_public_not_full_crypto {
impl $crate::CryptoType for Public {}
impl $crate::AppKey for Public {
type UntypedGeneric = $public;
impl $crate::AppCrypto for Public {
type Public = Public;
type Signature = Signature;
const ID: $crate::KeyTypeId = $key_type;
@@ -306,11 +300,8 @@ macro_rules! app_crypto_public_common {
impl $crate::ByteArray for Public {
const LEN: usize = <$public>::LEN;
}
impl $crate::Public for Public {
fn to_public_crypto_pair(&self) -> $crate::CryptoTypePublicPair {
$crate::CryptoTypePublicPair($crypto_type, $crate::ByteArray::to_raw_vec(self))
}
}
impl $crate::Public for Public {}
impl $crate::AppPublic for Public {
type Generic = $public;
@@ -350,18 +341,6 @@ macro_rules! app_crypto_public_common {
}
}
impl From<Public> for $crate::CryptoTypePublicPair {
fn from(key: Public) -> Self {
(&key).into()
}
}
impl From<&Public> for $crate::CryptoTypePublicPair {
fn from(key: &Public) -> Self {
$crate::CryptoTypePublicPair($crypto_type, $crate::ByteArray::to_raw_vec(key))
}
}
impl<'a> TryFrom<&'a [u8]> for Public {
type Error = ();
@@ -450,8 +429,7 @@ macro_rules! app_crypto_signature_full_crypto {
type Pair = Pair;
}
impl $crate::AppKey for Signature {
type UntypedGeneric = $sig;
impl $crate::AppCrypto for Signature {
type Public = Public;
type Pair = Pair;
type Signature = Signature;
@@ -482,8 +460,7 @@ macro_rules! app_crypto_signature_not_full_crypto {
impl $crate::CryptoType for Signature {}
impl $crate::AppKey for Signature {
type UntypedGeneric = $sig;
impl $crate::AppCrypto for Signature {
type Public = Public;
type Signature = Signature;
const ID: $crate::KeyTypeId = $key_type;
@@ -23,24 +23,22 @@ use sp_core::crypto::{CryptoType, CryptoTypeId, IsWrappedBy, KeyTypeId, Public};
use sp_std::{fmt::Debug, vec::Vec};
/// An application-specific key.
pub trait AppKey: 'static + Send + Sync + Sized + CryptoType + Clone {
/// The corresponding type as a generic crypto type.
type UntypedGeneric: IsWrappedBy<Self>;
pub trait AppCrypto: 'static + Send + Sync + Sized + CryptoType + Clone {
/// Identifier for application-specific key type.
const ID: KeyTypeId;
/// Identifier of the crypto type of this application-specific key type.
const CRYPTO_ID: CryptoTypeId;
/// The corresponding public key type in this application scheme.
type Public: AppPublic;
/// The corresponding key pair type in this application scheme.
#[cfg(feature = "full_crypto")]
type Pair: AppPair;
/// The corresponding signature type in this application scheme.
type Signature: AppSignature;
/// 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 corresponding key pair type in this application scheme.
#[cfg(feature = "full_crypto")]
type Pair: AppPair;
}
/// Type which implements Hash in std, not when no-std (std variant).
@@ -63,7 +61,7 @@ impl<T: sp_std::hash::Hash> MaybeDebugHash for T {}
/// A application's public key.
pub trait AppPublic:
AppKey + Public + Ord + PartialOrd + Eq + PartialEq + Debug + MaybeHash + codec::Codec
AppCrypto + Public + Ord + PartialOrd + Eq + PartialEq + Debug + MaybeHash + codec::Codec
{
/// The wrapped type which is just a plain instance of `Public`.
type Generic: IsWrappedBy<Self>
@@ -79,14 +77,15 @@ pub trait AppPublic:
/// A application's key pair.
#[cfg(feature = "full_crypto")]
pub trait AppPair: AppKey + Pair<Public = <Self as AppKey>::Public> {
pub trait AppPair: AppCrypto + Pair<Public = <Self as AppCrypto>::Public> {
/// The wrapped type which is just a plain instance of `Pair`.
type Generic: IsWrappedBy<Self>
+ Pair<Public = <<Self as AppKey>::Public as AppPublic>::Generic>;
+ Pair<Public = <<Self as AppCrypto>::Public as AppPublic>::Generic>
+ Pair<Signature = <<Self as AppCrypto>::Signature as AppSignature>::Generic>;
}
/// A application's signature.
pub trait AppSignature: AppKey + Eq + PartialEq + Debug + MaybeHash {
pub trait AppSignature: AppCrypto + Eq + PartialEq + Debug + MaybeHash {
/// The wrapped type which is just a plain instance of `Signature`.
type Generic: IsWrappedBy<Self> + Eq + PartialEq + Debug + MaybeHash;
}