From d6f15306282e3de848a09c9aa9cba6f95a7811f0 Mon Sep 17 00:00:00 2001
From: Michal Kucharczyk <1728078+michalkucharczyk@users.noreply.github.com>
Date: Thu, 26 Oct 2023 15:35:43 +0200
Subject: [PATCH] wip: application-crypto 2

---
 .../primitives/application-crypto/Cargo.toml  |  6 ++
 .../application-crypto/src/ecdsa.rs           |  4 +-
 .../application-crypto/src/ed25519.rs         |  4 +-
 .../primitives/application-crypto/src/lib.rs  | 85 +++++++++++++++++--
 .../application-crypto/src/sr25519.rs         |  4 +-
 .../application-crypto/src/traits.rs          | 14 +--
 6 files changed, 88 insertions(+), 29 deletions(-)

diff --git a/substrate/primitives/application-crypto/Cargo.toml b/substrate/primitives/application-crypto/Cargo.toml
index 7c5e3173077..69ba2a19c65 100644
--- a/substrate/primitives/application-crypto/Cargo.toml
+++ b/substrate/primitives/application-crypto/Cargo.toml
@@ -22,6 +22,9 @@ serde = { version = "1.0.188", default-features = false, optional = true, featur
 sp-std = { path = "../std", default-features = false}
 sp-io = { path = "../io", default-features = false}
 
+#todo: remove this:
+sp-runtime-interface = { path = "../runtime-interface", default-features = false, optional = true }
+
 [features]
 default = [ "std" ]
 std = [
@@ -34,6 +37,9 @@ std = [
 	"sp-std/std",
 ]
 
+#todo: remove this:
+compile = [ "sp-io/disable_panic_handler", "sp-runtime-interface/disable_target_static_assertions" ]
+
 # Serde support without relying on std features.
 serde = [ "dep:serde", "scale-info/serde", "sp-core/serde" ]
 
diff --git a/substrate/primitives/application-crypto/src/ecdsa.rs b/substrate/primitives/application-crypto/src/ecdsa.rs
index 27ffe12579f..439b51dc604 100644
--- a/substrate/primitives/application-crypto/src/ecdsa.rs
+++ b/substrate/primitives/application-crypto/src/ecdsa.rs
@@ -27,9 +27,7 @@ mod app {
 	crate::app_crypto!(super, sp_core::testing::ECDSA);
 }
 
-#[cfg(feature = "full_crypto")]
-pub use app::Pair as AppPair;
-pub use app::{Public as AppPublic, Signature as AppSignature};
+pub use app::{Pair as AppPair, Public as AppPublic, Signature as AppSignature};
 
 impl RuntimePublic for Public {
 	type Signature = Signature;
diff --git a/substrate/primitives/application-crypto/src/ed25519.rs b/substrate/primitives/application-crypto/src/ed25519.rs
index bc05018370e..addefe7daf6 100644
--- a/substrate/primitives/application-crypto/src/ed25519.rs
+++ b/substrate/primitives/application-crypto/src/ed25519.rs
@@ -27,9 +27,7 @@ mod app {
 	crate::app_crypto!(super, sp_core::testing::ED25519);
 }
 
-#[cfg(feature = "full_crypto")]
-pub use app::Pair as AppPair;
-pub use app::{Public as AppPublic, Signature as AppSignature};
+pub use app::{Pair as AppPair, Public as AppPublic, Signature as AppSignature};
 
 impl RuntimePublic for Public {
 	type Signature = Signature;
diff --git a/substrate/primitives/application-crypto/src/lib.rs b/substrate/primitives/application-crypto/src/lib.rs
index 5384220bc9c..6388ac4befd 100644
--- a/substrate/primitives/application-crypto/src/lib.rs
+++ b/substrate/primitives/application-crypto/src/lib.rs
@@ -20,12 +20,9 @@
 #![warn(missing_docs)]
 #![cfg_attr(not(feature = "std"), no_std)]
 
-pub use sp_core::crypto::{key_types, CryptoTypeId, KeyTypeId};
+pub use sp_core::crypto::{key_types, CryptoTypeId, DeriveJunction, KeyTypeId, Ss58Codec};
 #[doc(hidden)]
-#[cfg(feature = "full_crypto")]
 pub use sp_core::crypto::{DeriveError, Pair, SecretStringError};
-#[cfg(any(feature = "full_crypto", feature = "serde"))]
-pub use sp_core::crypto::{DeriveJunction, Ss58Codec};
 #[doc(hidden)]
 pub use sp_core::{
 	self,
@@ -114,6 +111,7 @@ macro_rules! app_crypto {
 			$module::CRYPTO_ID
 		);
 		$crate::app_crypto_signature_common!($module::Signature, $key_type);
+		$crate::app_crypto_pair_not_full_crypto!($module::Pair, $key_type, $module::CRYPTO_ID);
 	};
 }
 
@@ -191,6 +189,72 @@ macro_rules! app_crypto_pair {
 	};
 }
 
+/// Declares `Pair` type which is functionally equivalent to `$pair`, but is
+/// new application-specific type whose identifier is `$key_type`.
+/// Can only be used without `full_crypto` feature.
+/// `verify` and `sign` methods are not implemented.
+#[macro_export]
+macro_rules! app_crypto_pair_not_full_crypto {
+	($pair:ty, $key_type:expr, $crypto_type:expr) => {
+		$crate::wrap! {
+			/// A generic `AppPublic` wrapper type over $pair crypto; this has no specific App.
+			#[derive(Clone)]
+			pub struct Pair($pair);
+		}
+
+		impl $crate::CryptoType for Pair {
+			type Pair = Pair;
+		}
+
+		impl $crate::Pair for Pair {
+			type Public = Public;
+			type Seed = <$pair as $crate::Pair>::Seed;
+			type Signature = Signature;
+
+			$crate::app_crypto_pair_functions_if_std!($pair);
+
+			fn derive<Iter: Iterator<Item = $crate::DeriveJunction>>(
+				&self,
+				path: Iter,
+				seed: Option<Self::Seed>,
+			) -> Result<(Self, Option<Self::Seed>), $crate::DeriveError> {
+				self.0.derive(path, seed).map(|x| (Self(x.0), x.1))
+			}
+			fn from_seed(seed: &Self::Seed) -> Self {
+				Self(<$pair>::from_seed(seed))
+			}
+			fn from_seed_slice(seed: &[u8]) -> Result<Self, $crate::SecretStringError> {
+				<$pair>::from_seed_slice(seed).map(Self)
+			}
+			fn public(&self) -> Self::Public {
+				Public(self.0.public())
+			}
+			fn to_raw_vec(&self) -> $crate::Vec<u8> {
+				self.0.to_raw_vec()
+			}
+		}
+
+		impl $crate::AppCrypto for Pair {
+			type Public = Public;
+			type Pair = Pair;
+			type Signature = Signature;
+			const ID: $crate::KeyTypeId = $key_type;
+			const CRYPTO_ID: $crate::CryptoTypeId = $crypto_type;
+		}
+
+		impl $crate::AppPair for Pair {
+			type Generic = $pair;
+		}
+
+		impl Pair {
+			/// Convert into wrapped generic key pair type.
+			pub fn into_inner(self) -> $pair {
+				self.0
+			}
+		}
+	};
+}
+
 /// Implements functions for the `Pair` trait when `feature = "std"` is enabled.
 #[doc(hidden)]
 #[cfg(feature = "std")]
@@ -265,7 +329,7 @@ macro_rules! app_crypto_public_not_full_crypto {
 		$crate::wrap! {
 			/// A generic `AppPublic` wrapper type over $public crypto; this has no specific App.
 			#[derive(
-				Clone, Eq, PartialEq, Ord, PartialOrd,
+				Clone, Eq, Hash, PartialEq, Ord, PartialOrd,
 				$crate::codec::Encode,
 				$crate::codec::Decode,
 				$crate::RuntimeDebug,
@@ -275,10 +339,13 @@ macro_rules! app_crypto_public_not_full_crypto {
 			pub struct Public($public);
 		}
 
-		impl $crate::CryptoType for Public {}
+		impl $crate::CryptoType for Public {
+			type Pair = Pair;
+		}
 
 		impl $crate::AppCrypto for Public {
 			type Public = Public;
+			type Pair = Pair;
 			type Signature = Signature;
 			const ID: $crate::KeyTypeId = $key_type;
 			const CRYPTO_ID: $crate::CryptoTypeId = $crypto_type;
@@ -447,13 +514,17 @@ macro_rules! app_crypto_signature_not_full_crypto {
 				$crate::RuntimeDebug,
 				$crate::scale_info::TypeInfo,
 			)]
+			#[derive(Hash)]
 			pub struct Signature($sig);
 		}
 
-		impl $crate::CryptoType for Signature {}
+		impl $crate::CryptoType for Signature {
+			type Pair = Pair;
+		}
 
 		impl $crate::AppCrypto for Signature {
 			type Public = Public;
+			type Pair = Pair;
 			type Signature = Signature;
 			const ID: $crate::KeyTypeId = $key_type;
 			const CRYPTO_ID: $crate::CryptoTypeId = $crypto_type;
diff --git a/substrate/primitives/application-crypto/src/sr25519.rs b/substrate/primitives/application-crypto/src/sr25519.rs
index 7c91bfa7bb5..d411cc253c0 100644
--- a/substrate/primitives/application-crypto/src/sr25519.rs
+++ b/substrate/primitives/application-crypto/src/sr25519.rs
@@ -27,9 +27,7 @@ mod app {
 	crate::app_crypto!(super, sp_core::testing::SR25519);
 }
 
-#[cfg(feature = "full_crypto")]
-pub use app::Pair as AppPair;
-pub use app::{Public as AppPublic, Signature as AppSignature};
+pub use app::{Pair as AppPair, Public as AppPublic, Signature as AppSignature};
 
 impl RuntimePublic for Public {
 	type Signature = Signature;
diff --git a/substrate/primitives/application-crypto/src/traits.rs b/substrate/primitives/application-crypto/src/traits.rs
index e9b1080f63d..0b59abf272d 100644
--- a/substrate/primitives/application-crypto/src/traits.rs
+++ b/substrate/primitives/application-crypto/src/traits.rs
@@ -18,9 +18,7 @@
 use codec::Codec;
 use scale_info::TypeInfo;
 
-#[cfg(feature = "full_crypto")]
-use sp_core::crypto::Pair;
-use sp_core::crypto::{CryptoType, CryptoTypeId, IsWrappedBy, KeyTypeId, Public};
+use sp_core::crypto::{CryptoType, CryptoTypeId, IsWrappedBy, KeyTypeId, Pair, Public};
 use sp_std::{fmt::Debug, vec::Vec};
 
 /// Application-specific cryptographic object.
@@ -45,24 +43,14 @@ pub trait AppCrypto: 'static + Sized + CryptoType {
 	type Signature: AppSignature;
 
 	/// 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).
-#[cfg(any(feature = "std", feature = "full_crypto"))]
 pub trait MaybeHash: sp_std::hash::Hash {}
-#[cfg(any(feature = "std", feature = "full_crypto"))]
 impl<T: sp_std::hash::Hash> MaybeHash for T {}
 
-/// Type which implements Hash in std, not when no-std (no-std variant).
-#[cfg(all(not(feature = "std"), not(feature = "full_crypto")))]
-pub trait MaybeHash {}
-#[cfg(all(not(feature = "std"), not(feature = "full_crypto")))]
-impl<T> MaybeHash for T {}
-
 /// Application-specific key pair.
-#[cfg(feature = "full_crypto")]
 pub trait AppPair:
 	AppCrypto + Pair<Public = <Self as AppCrypto>::Public, Signature = <Self as AppCrypto>::Signature>
 {
-- 
GitLab