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
+3 -34
View File
@@ -19,8 +19,6 @@
//! Cryptographic utilities.
// end::description[]
#[cfg(feature = "std")]
use crate::hexdisplay::HexDisplay;
use crate::{ed25519, sr25519};
#[cfg(feature = "std")]
use base58::{FromBase58, ToBase58};
@@ -487,10 +485,7 @@ pub trait ByteArray: AsRef<[u8]> + AsMut<[u8]> + for<'a> TryFrom<&'a [u8], Error
}
/// Trait suitable for typical cryptographic PKI key public type.
pub trait Public: ByteArray + Derive + CryptoType + PartialEq + Eq + Clone + Send + Sync {
/// Return `CryptoTypePublicPair` from public key.
fn to_public_crypto_pair(&self) -> CryptoTypePublicPair;
}
pub trait Public: ByteArray + Derive + CryptoType + PartialEq + Eq + Clone + Send + Sync {}
/// An opaque 32-byte cryptographic identifier.
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Encode, Decode, MaxEncodedLen, TypeInfo)]
@@ -689,11 +684,7 @@ mod dummy {
b""
}
}
impl Public for Dummy {
fn to_public_crypto_pair(&self) -> CryptoTypePublicPair {
CryptoTypePublicPair(CryptoTypeId(*b"dumm"), <Self as ByteArray>::to_raw_vec(self))
}
}
impl Public for Dummy {}
impl Pair for Dummy {
type Public = Dummy;
@@ -1118,24 +1109,6 @@ impl<'a> TryFrom<&'a str> for KeyTypeId {
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
pub struct CryptoTypeId(pub [u8; 4]);
/// A type alias of CryptoTypeId & a public key
#[derive(Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Encode, Decode)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
pub struct CryptoTypePublicPair(pub CryptoTypeId, pub Vec<u8>);
#[cfg(feature = "std")]
impl sp_std::fmt::Display for CryptoTypePublicPair {
fn fmt(&self, f: &mut sp_std::fmt::Formatter) -> sp_std::fmt::Result {
let id = match str::from_utf8(&(self.0).0[..]) {
Ok(id) => id.to_string(),
Err(_) => {
format!("{:#?}", self.0)
},
};
write!(f, "{}-{}", id, HexDisplay::from(&self.1))
}
}
/// Known key types; this also functions as a global registry of key types for projects wishing to
/// avoid collisions with each other.
///
@@ -1223,11 +1196,7 @@ mod tests {
vec![]
}
}
impl Public for TestPublic {
fn to_public_crypto_pair(&self) -> CryptoTypePublicPair {
CryptoTypePublicPair(CryptoTypeId(*b"dumm"), self.to_raw_vec())
}
}
impl Public for TestPublic {}
impl Pair for TestPair {
type Public = TestPublic;
type Seed = [u8; 8];
+2 -19
View File
@@ -24,8 +24,7 @@ use sp_runtime_interface::pass_by::PassByInner;
#[cfg(feature = "std")]
use crate::crypto::Ss58Codec;
use crate::crypto::{
ByteArray, CryptoType, CryptoTypeId, CryptoTypePublicPair, Derive, Public as TraitPublic,
UncheckedFrom,
ByteArray, CryptoType, CryptoTypeId, Derive, Public as TraitPublic, UncheckedFrom,
};
#[cfg(feature = "full_crypto")]
use crate::{
@@ -103,23 +102,7 @@ impl ByteArray for Public {
const LEN: usize = 33;
}
impl TraitPublic for Public {
fn to_public_crypto_pair(&self) -> CryptoTypePublicPair {
CryptoTypePublicPair(CRYPTO_ID, self.to_raw_vec())
}
}
impl From<Public> for CryptoTypePublicPair {
fn from(key: Public) -> Self {
(&key).into()
}
}
impl From<&Public> for CryptoTypePublicPair {
fn from(key: &Public) -> Self {
CryptoTypePublicPair(CRYPTO_ID, key.to_raw_vec())
}
}
impl TraitPublic for Public {}
impl Derive for Public {}
+2 -20
View File
@@ -31,9 +31,7 @@ use scale_info::TypeInfo;
#[cfg(feature = "std")]
use crate::crypto::Ss58Codec;
use crate::crypto::{
CryptoType, CryptoTypeId, CryptoTypePublicPair, Derive, Public as TraitPublic, UncheckedFrom,
};
use crate::crypto::{CryptoType, CryptoTypeId, Derive, Public as TraitPublic, UncheckedFrom};
#[cfg(feature = "full_crypto")]
use crate::crypto::{DeriveError, DeriveJunction, Pair as TraitPair, SecretStringError};
#[cfg(feature = "full_crypto")]
@@ -355,26 +353,10 @@ impl ByteArray for Public {
const LEN: usize = 32;
}
impl TraitPublic for Public {
fn to_public_crypto_pair(&self) -> CryptoTypePublicPair {
CryptoTypePublicPair(CRYPTO_ID, self.to_raw_vec())
}
}
impl TraitPublic for Public {}
impl Derive for Public {}
impl From<Public> for CryptoTypePublicPair {
fn from(key: Public) -> Self {
(&key).into()
}
}
impl From<&Public> for CryptoTypePublicPair {
fn from(key: &Public) -> Self {
CryptoTypePublicPair(CRYPTO_ID, key.to_raw_vec())
}
}
/// Derive a single hard junction.
#[cfg(feature = "full_crypto")]
fn derive_hard_junction(secret_seed: &Seed, cc: &[u8; 32]) -> Seed {
+2 -21
View File
@@ -34,10 +34,7 @@ use schnorrkel::{
use sp_std::vec::Vec;
use crate::{
crypto::{
ByteArray, CryptoType, CryptoTypeId, CryptoTypePublicPair, Derive, Public as TraitPublic,
UncheckedFrom,
},
crypto::{ByteArray, CryptoType, CryptoTypeId, Derive, Public as TraitPublic, UncheckedFrom},
hash::{H256, H512},
};
use codec::{Decode, Encode, MaxEncodedLen};
@@ -386,23 +383,7 @@ impl ByteArray for Public {
const LEN: usize = 32;
}
impl TraitPublic for Public {
fn to_public_crypto_pair(&self) -> CryptoTypePublicPair {
CryptoTypePublicPair(CRYPTO_ID, self.to_raw_vec())
}
}
impl From<Public> for CryptoTypePublicPair {
fn from(key: Public) -> Self {
(&key).into()
}
}
impl From<&Public> for CryptoTypePublicPair {
fn from(key: &Public) -> Self {
CryptoTypePublicPair(CRYPTO_ID, key.to_raw_vec())
}
}
impl TraitPublic for Public {}
#[cfg(feature = "std")]
impl From<MiniSecretKey> for Pair {