Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit 14f1a39

Browse files
davxymelekes
andauthored
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>
1 parent 9c92e49 commit 14f1a39

File tree

7 files changed

+91
-111
lines changed

7 files changed

+91
-111
lines changed

primitives/application-crypto/src/ecdsa.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,7 @@ use sp_std::vec::Vec;
2424
pub use sp_core::ecdsa::*;
2525

2626
mod app {
27-
use sp_core::testing::ECDSA;
28-
29-
crate::app_crypto!(super, ECDSA);
30-
31-
impl crate::traits::BoundToRuntimeAppPublic for Public {
32-
type Public = Self;
33-
}
27+
crate::app_crypto!(super, sp_core::testing::ECDSA);
3428
}
3529

3630
#[cfg(feature = "full_crypto")]

primitives/application-crypto/src/ed25519.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,7 @@ use sp_std::vec::Vec;
2424
pub use sp_core::ed25519::*;
2525

2626
mod app {
27-
use sp_core::testing::ED25519;
28-
29-
crate::app_crypto!(super, ED25519);
30-
31-
impl crate::traits::BoundToRuntimeAppPublic for Public {
32-
type Public = Self;
33-
}
27+
crate::app_crypto!(super, sp_core::testing::ED25519);
3428
}
3529

3630
#[cfg(feature = "full_crypto")]

primitives/application-crypto/src/lib.rs

Lines changed: 34 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,15 @@ mod traits;
4848

4949
pub use traits::*;
5050

51-
/// Declares Public, Pair, Signature types which are functionally equivalent to `$pair`, but are new
52-
/// Application-specific types whose identifier is `$key_type`.
51+
/// Declares `Public`, `Pair` and `Signature` types which are functionally equivalent
52+
/// to the corresponding types defined by `$module` but are new application-specific
53+
/// types whose identifier is `$key_type`.
5354
///
5455
/// ```rust
55-
/// # use sp_application_crypto::{app_crypto, wrap, ed25519, KeyTypeId};
56-
/// // Declare a new set of crypto types using Ed25519 logic that identifies as `KeyTypeId`
56+
/// # use sp_application_crypto::{app_crypto, ed25519, KeyTypeId};
57+
/// // Declare a new set of crypto types using ed25519 logic that identifies as `KeyTypeId`
5758
/// // of value `b"fuba"`.
58-
/// app_crypto!(ed25519, KeyTypeId(*b"_uba"));
59+
/// app_crypto!(ed25519, KeyTypeId(*b"fuba"));
5960
/// ```
6061
#[cfg(feature = "full_crypto")]
6162
#[macro_export]
@@ -78,14 +79,15 @@ macro_rules! app_crypto {
7879
};
7980
}
8081

81-
/// Declares Public, Pair, Signature types which are functionally equivalent to `$pair`, but are new
82-
/// Application-specific types whose identifier is `$key_type`.
82+
/// Declares `Public`, `Pair` and `Signature` types which are functionally equivalent
83+
/// to the corresponding types defined by `$module` but that are new application-specific
84+
/// types whose identifier is `$key_type`.
8385
///
8486
/// ```rust
85-
/// # use sp_application_crypto::{app_crypto, wrap, ed25519, KeyTypeId};
86-
/// // Declare a new set of crypto types using Ed25519 logic that identifies as `KeyTypeId`
87+
/// # use sp_application_crypto::{app_crypto, ed25519, KeyTypeId};
88+
/// // Declare a new set of crypto types using ed25519 logic that identifies as `KeyTypeId`
8789
/// // of value `b"fuba"`.
88-
/// app_crypto!(ed25519, KeyTypeId(*b"_uba"));
90+
/// app_crypto!(ed25519, KeyTypeId(*b"fuba"));
8991
/// ```
9092
#[cfg(not(feature = "full_crypto"))]
9193
#[macro_export]
@@ -107,8 +109,8 @@ macro_rules! app_crypto {
107109
};
108110
}
109111

110-
/// Declares Pair type which is functionally equivalent to `$pair`, but is new
111-
/// Application-specific type whose identifier is `$key_type`.
112+
/// Declares `Pair` type which is functionally equivalent to `$pair`, but is
113+
/// new application-specific type whose identifier is `$key_type`.
112114
#[macro_export]
113115
macro_rules! app_crypto_pair {
114116
($pair:ty, $key_type:expr, $crypto_type:expr) => {
@@ -208,10 +210,10 @@ macro_rules! app_crypto_pair_functions_if_std {
208210
($pair:ty) => {};
209211
}
210212

211-
/// Declares Public type which is functionally equivalent to `$public`, but is new
212-
/// Application-specific type whose identifier is `$key_type`.
213-
/// can only be used together with `full_crypto` feature
214-
/// For full functionality, app_crypto_public_common! must be called too.
213+
/// Declares `Public` type which is functionally equivalent to `$public` but is
214+
/// new application-specific type whose identifier is `$key_type`.
215+
/// For full functionality, `app_crypto_public_common!` must be called too.
216+
/// Can only be used with `full_crypto` feature.
215217
#[doc(hidden)]
216218
#[macro_export]
217219
macro_rules! app_crypto_public_full_crypto {
@@ -244,10 +246,10 @@ macro_rules! app_crypto_public_full_crypto {
244246
};
245247
}
246248

247-
/// Declares Public type which is functionally equivalent to `$public`, but is new
248-
/// Application-specific type whose identifier is `$key_type`.
249-
/// can only be used without `full_crypto` feature
250-
/// For full functionality, app_crypto_public_common! must be called too.
249+
/// Declares `Public` type which is functionally equivalent to `$public` but is
250+
/// new application-specific type whose identifier is `$key_type`.
251+
/// For full functionality, `app_crypto_public_common!` must be called too.
252+
/// Can only be used without `full_crypto` feature.
251253
#[doc(hidden)]
252254
#[macro_export]
253255
macro_rules! app_crypto_public_not_full_crypto {
@@ -276,9 +278,9 @@ macro_rules! app_crypto_public_not_full_crypto {
276278
};
277279
}
278280

279-
/// Declares Public type which is functionally equivalent to `$public`, but is new
280-
/// Application-specific type whose identifier is `$key_type`.
281-
/// For full functionality, app_crypto_public_(not)_full_crypto! must be called too.
281+
/// Declares `Public` type which is functionally equivalent to `$public` but is
282+
/// new application-specific type whose identifier is `$key_type`.
283+
/// For full functionality, `app_crypto_public_(not)_full_crypto!` must be called too.
282284
#[doc(hidden)]
283285
#[macro_export]
284286
macro_rules! app_crypto_public_common {
@@ -307,40 +309,6 @@ macro_rules! app_crypto_public_common {
307309
type Generic = $public;
308310
}
309311

310-
impl $crate::RuntimeAppPublic for Public
311-
where
312-
$public: $crate::RuntimePublic<Signature = $sig>,
313-
{
314-
const ID: $crate::KeyTypeId = $key_type;
315-
const CRYPTO_ID: $crate::CryptoTypeId = $crypto_type;
316-
317-
type Signature = Signature;
318-
319-
fn all() -> $crate::Vec<Self> {
320-
<$public as $crate::RuntimePublic>::all($key_type)
321-
.into_iter()
322-
.map(Self)
323-
.collect()
324-
}
325-
326-
fn generate_pair(seed: Option<$crate::Vec<u8>>) -> Self {
327-
Self(<$public as $crate::RuntimePublic>::generate_pair($key_type, seed))
328-
}
329-
330-
fn sign<M: AsRef<[u8]>>(&self, msg: &M) -> Option<Self::Signature> {
331-
<$public as $crate::RuntimePublic>::sign(self.as_ref(), $key_type, msg)
332-
.map(Signature)
333-
}
334-
335-
fn verify<M: AsRef<[u8]>>(&self, msg: &M, signature: &Self::Signature) -> bool {
336-
<$public as $crate::RuntimePublic>::verify(self.as_ref(), msg, &signature.as_ref())
337-
}
338-
339-
fn to_raw_vec(&self) -> $crate::Vec<u8> {
340-
<$public as $crate::RuntimePublic>::to_raw_vec(&self.0)
341-
}
342-
}
343-
344312
impl<'a> TryFrom<&'a [u8]> for Public {
345313
type Error = ();
346314

@@ -407,8 +375,8 @@ macro_rules! app_crypto_public_common_if_std {
407375

408376
/// Declares Signature type which is functionally equivalent to `$sig`, but is new
409377
/// Application-specific type whose identifier is `$key_type`.
410-
/// can only be used together with `full_crypto` feature
411378
/// For full functionality, app_crypto_public_common! must be called too.
379+
/// Can only be used with `full_crypto` feature
412380
#[doc(hidden)]
413381
#[macro_export]
414382
macro_rules! app_crypto_signature_full_crypto {
@@ -439,10 +407,10 @@ macro_rules! app_crypto_signature_full_crypto {
439407
};
440408
}
441409

442-
/// Declares Signature type which is functionally equivalent to `$sig`, but is new
443-
/// Application-specific type whose identifier is `$key_type`.
444-
/// can only be used without `full_crypto` feature
445-
/// For full functionality, app_crypto_public_common! must be called too.
410+
/// Declares `Signature` type which is functionally equivalent to `$sig`, but is new
411+
/// application-specific type whose identifier is `$key_type`.
412+
/// For full functionality, `app_crypto_signature_common` must be called too.
413+
/// Can only be used without `full_crypto` feature.
446414
#[doc(hidden)]
447415
#[macro_export]
448416
macro_rules! app_crypto_signature_not_full_crypto {
@@ -452,8 +420,8 @@ macro_rules! app_crypto_signature_not_full_crypto {
452420
#[derive(Clone, Eq, PartialEq,
453421
$crate::codec::Encode,
454422
$crate::codec::Decode,
455-
$crate::scale_info::TypeInfo,
456423
$crate::RuntimeDebug,
424+
$crate::scale_info::TypeInfo,
457425
)]
458426
pub struct Signature($sig);
459427
}
@@ -469,9 +437,9 @@ macro_rules! app_crypto_signature_not_full_crypto {
469437
};
470438
}
471439

472-
/// Declares Signature type which is functionally equivalent to `$sig`, but is new
473-
/// Application-specific type whose identifier is `$key_type`.
474-
/// For full functionality, app_crypto_public_(not)_full_crypto! must be called too.
440+
/// Declares `Signature` type which is functionally equivalent to `$sig`, but is new
441+
/// application-specific type whose identifier is `$key_type`.
442+
/// For full functionality, app_crypto_signature_(not)_full_crypto! must be called too.
475443
#[doc(hidden)]
476444
#[macro_export]
477445
macro_rules! app_crypto_signature_common {

primitives/application-crypto/src/sr25519.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,7 @@ use sp_std::vec::Vec;
2424
pub use sp_core::sr25519::*;
2525

2626
mod app {
27-
use sp_core::testing::SR25519;
28-
29-
crate::app_crypto!(super, SR25519);
30-
31-
impl crate::traits::BoundToRuntimeAppPublic for Public {
32-
type Public = Self;
33-
}
27+
crate::app_crypto!(super, sp_core::testing::SR25519);
3428
}
3529

3630
#[cfg(feature = "full_crypto")]

primitives/application-crypto/src/traits.rs

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@
1515
// See the License for the specific language governing permissions and
1616
// limitations under the License.
1717

18+
use codec::Codec;
19+
use scale_info::TypeInfo;
20+
1821
#[cfg(feature = "full_crypto")]
1922
use sp_core::crypto::Pair;
20-
21-
use codec::Codec;
2223
use sp_core::crypto::{CryptoType, CryptoTypeId, IsWrappedBy, KeyTypeId, Public};
2324
use sp_std::{fmt::Debug, vec::Vec};
2425

@@ -60,15 +61,9 @@ pub trait MaybeHash {}
6061
#[cfg(all(not(feature = "std"), not(feature = "full_crypto")))]
6162
impl<T> MaybeHash for T {}
6263

63-
/// Type which implements Debug and Hash in std, not when no-std (no-std variant with crypto).
64-
#[cfg(all(not(feature = "std"), feature = "full_crypto"))]
65-
pub trait MaybeDebugHash: sp_std::hash::Hash {}
66-
#[cfg(all(not(feature = "std"), feature = "full_crypto"))]
67-
impl<T: sp_std::hash::Hash> MaybeDebugHash for T {}
68-
6964
/// A application's public key.
7065
pub trait AppPublic:
71-
AppCrypto + Public + Ord + PartialOrd + Eq + PartialEq + Debug + MaybeHash + codec::Codec
66+
AppCrypto + Public + Ord + PartialOrd + Eq + PartialEq + Debug + MaybeHash + Codec
7267
{
7368
/// The wrapped type which is just a plain instance of `Public`.
7469
type Generic: IsWrappedBy<Self>
@@ -79,7 +74,7 @@ pub trait AppPublic:
7974
+ PartialEq
8075
+ Debug
8176
+ MaybeHash
82-
+ codec::Codec;
77+
+ Codec;
8378
}
8479

8580
/// A application's key pair.
@@ -92,15 +87,15 @@ pub trait AppPair: AppCrypto + Pair<Public = <Self as AppCrypto>::Public> {
9287
}
9388

9489
/// A application's signature.
95-
pub trait AppSignature: AppCrypto + Eq + PartialEq + Debug + MaybeHash {
90+
pub trait AppSignature: AppCrypto + Eq + PartialEq + Debug {
9691
/// The wrapped type which is just a plain instance of `Signature`.
97-
type Generic: IsWrappedBy<Self> + Eq + PartialEq + Debug + MaybeHash;
92+
type Generic: IsWrappedBy<Self> + Eq + PartialEq + Debug;
9893
}
9994

10095
/// A runtime interface for a public key.
10196
pub trait RuntimePublic: Sized {
10297
/// The signature that will be generated when signing with the corresponding private key.
103-
type Signature: Codec + Debug + MaybeHash + Eq + PartialEq + Clone;
98+
type Signature: Debug + Eq + PartialEq + Clone;
10499

105100
/// Returns all public keys for the given key type in the keystore.
106101
fn all(key_type: KeyTypeId) -> crate::Vec<Self>;
@@ -132,11 +127,9 @@ pub trait RuntimePublic: Sized {
132127
pub trait RuntimeAppPublic: Sized {
133128
/// An identifier for this application-specific key type.
134129
const ID: KeyTypeId;
135-
/// The identifier of the crypto type of this application-specific key type.
136-
const CRYPTO_ID: CryptoTypeId;
137130

138131
/// The signature that will be generated when signing with the corresponding private key.
139-
type Signature: Codec + Debug + MaybeHash + Eq + PartialEq + Clone + scale_info::TypeInfo;
132+
type Signature: Debug + Eq + PartialEq + Clone + TypeInfo + Codec;
140133

141134
/// Returns all public keys for this application in the keystore.
142135
fn all() -> crate::Vec<Self>;
@@ -163,8 +156,50 @@ pub trait RuntimeAppPublic: Sized {
163156
fn to_raw_vec(&self) -> Vec<u8>;
164157
}
165158

166-
/// Something that bound to a fixed [`RuntimeAppPublic`].
159+
impl<T> RuntimeAppPublic for T
160+
where
161+
T: AppPublic + AsRef<<T as AppPublic>::Generic>,
162+
<T as AppPublic>::Generic: RuntimePublic,
163+
<T as AppCrypto>::Signature: TypeInfo
164+
+ Codec
165+
+ From<<<T as AppPublic>::Generic as RuntimePublic>::Signature>
166+
+ AsRef<<<T as AppPublic>::Generic as RuntimePublic>::Signature>,
167+
{
168+
const ID: KeyTypeId = <T as AppCrypto>::ID;
169+
170+
type Signature = <T as AppCrypto>::Signature;
171+
172+
fn all() -> crate::Vec<Self> {
173+
<<T as AppPublic>::Generic as RuntimePublic>::all(Self::ID)
174+
.into_iter()
175+
.map(|p| p.into())
176+
.collect()
177+
}
178+
179+
fn generate_pair(seed: Option<Vec<u8>>) -> Self {
180+
<<T as AppPublic>::Generic as RuntimePublic>::generate_pair(Self::ID, seed).into()
181+
}
182+
183+
fn sign<M: AsRef<[u8]>>(&self, msg: &M) -> Option<Self::Signature> {
184+
<<T as AppPublic>::Generic as RuntimePublic>::sign(self.as_ref(), Self::ID, msg)
185+
.map(|s| s.into())
186+
}
187+
188+
fn verify<M: AsRef<[u8]>>(&self, msg: &M, signature: &Self::Signature) -> bool {
189+
<<T as AppPublic>::Generic as RuntimePublic>::verify(self.as_ref(), msg, signature.as_ref())
190+
}
191+
192+
fn to_raw_vec(&self) -> Vec<u8> {
193+
<<T as AppPublic>::Generic as RuntimePublic>::to_raw_vec(self.as_ref())
194+
}
195+
}
196+
197+
/// Something that is bound to a fixed [`RuntimeAppPublic`].
167198
pub trait BoundToRuntimeAppPublic {
168199
/// The [`RuntimeAppPublic`] this type is bound to.
169200
type Public: RuntimeAppPublic;
170201
}
202+
203+
impl<T: RuntimeAppPublic> BoundToRuntimeAppPublic for T {
204+
type Public = Self;
205+
}

primitives/core/src/crypto.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ pub trait ByteArray: AsRef<[u8]> + AsMut<[u8]> + for<'a> TryFrom<&'a [u8], Error
482482
}
483483
}
484484

485-
/// Trait suitable for typical cryptographic PKI key public type.
485+
/// Trait suitable for typical cryptographic key public type.
486486
pub trait Public: ByteArray + Derive + CryptoType + PartialEq + Eq + Clone + Send + Sync {}
487487

488488
/// An opaque 32-byte cryptographic identifier.

primitives/runtime/src/testing.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use crate::{
2626
PostDispatchInfoOf, SignedExtension, ValidateUnsigned,
2727
},
2828
transaction_validity::{TransactionSource, TransactionValidity, TransactionValidityError},
29-
ApplyExtrinsicResultWithInfo, CryptoTypeId, KeyTypeId,
29+
ApplyExtrinsicResultWithInfo, KeyTypeId,
3030
};
3131
use serde::{de::Error as DeError, Deserialize, Deserializer, Serialize, Serializer};
3232
use sp_core::{
@@ -115,7 +115,6 @@ impl UintAuthorityId {
115115

116116
impl sp_application_crypto::RuntimeAppPublic for UintAuthorityId {
117117
const ID: KeyTypeId = key_types::DUMMY;
118-
const CRYPTO_ID: CryptoTypeId = CryptoTypeId(*b"dumm");
119118

120119
type Signature = TestSignature;
121120

@@ -157,10 +156,6 @@ impl OpaqueKeys for UintAuthorityId {
157156
}
158157
}
159158

160-
impl crate::BoundToRuntimeAppPublic for UintAuthorityId {
161-
type Public = Self;
162-
}
163-
164159
impl traits::IdentifyAccount for UintAuthorityId {
165160
type AccountId = u64;
166161

0 commit comments

Comments
 (0)