Adds with_pair! macro to application-crypto (#4885)

* Adds `with_pair!` macro to application-crypto

This macro will "generate" the given code only when the crypto pair is
available. So, when either the `std` or the `full_crypto` feature is
enabled.

* Fix example
This commit is contained in:
Bastian Köcher
2020-02-14 15:46:41 +01:00
committed by GitHub
parent d3a3e288b6
commit 91989cab46
6 changed files with 54 additions and 24 deletions
+4 -2
View File
@@ -102,9 +102,10 @@ pub mod sr25519 {
app_crypto!(sr25519, IM_ONLINE); app_crypto!(sr25519, IM_ONLINE);
} }
sp_application_crypto::with_pair! {
/// An i'm online keypair using sr25519 as its crypto. /// An i'm online keypair using sr25519 as its crypto.
#[cfg(feature = "std")]
pub type AuthorityPair = app_sr25519::Pair; pub type AuthorityPair = app_sr25519::Pair;
}
/// An i'm online signature using sr25519 as its crypto. /// An i'm online signature using sr25519 as its crypto.
pub type AuthoritySignature = app_sr25519::Signature; pub type AuthoritySignature = app_sr25519::Signature;
@@ -119,9 +120,10 @@ pub mod ed25519 {
app_crypto!(ed25519, IM_ONLINE); app_crypto!(ed25519, IM_ONLINE);
} }
sp_application_crypto::with_pair! {
/// An i'm online keypair using ed25519 as its crypto. /// An i'm online keypair using ed25519 as its crypto.
#[cfg(feature = "std")]
pub type AuthorityPair = app_ed25519::Pair; pub type AuthorityPair = app_ed25519::Pair;
}
/// An i'm online signature using ed25519 as its crypto. /// An i'm online signature using ed25519 as its crypto.
pub type AuthoritySignature = app_ed25519::Signature; pub type AuthoritySignature = app_ed25519::Signature;
@@ -436,3 +436,30 @@ macro_rules! wrap {
} }
} }
} }
/// Generate the given code if the pair type is available.
///
/// The pair type is available when `feature = "std"` || `feature = "full_crypto"`.
///
/// # Example
///
/// ```
/// sp_application_crypto::with_pair! {
/// pub type Pair = ();
/// }
/// ```
#[macro_export]
#[cfg(any(feature = "std", feature = "full_crypto"))]
macro_rules! with_pair {
( $( $def:tt )* ) => {
$( $def )*
}
}
#[doc(hidden)]
#[macro_export]
#[cfg(all(not(feature = "std"), not(feature = "full_crypto")))]
macro_rules! with_pair {
( $( $def:tt )* ) => {}
}
@@ -25,9 +25,10 @@ mod app {
app_crypto!(sr25519, AUTHORITY_DISCOVERY); app_crypto!(sr25519, AUTHORITY_DISCOVERY);
} }
sp_application_crypto::with_pair! {
/// An authority discovery authority keypair. /// An authority discovery authority keypair.
#[cfg(feature = "std")]
pub type AuthorityPair = app::Pair; pub type AuthorityPair = app::Pair;
}
/// An authority discovery authority identifier. /// An authority discovery authority identifier.
pub type AuthorityId = app::Public; pub type AuthorityId = app::Public;
@@ -30,9 +30,10 @@ pub mod sr25519 {
app_crypto!(sr25519, AURA); app_crypto!(sr25519, AURA);
} }
sp_application_crypto::with_pair! {
/// An Aura authority keypair using S/R 25519 as its crypto. /// An Aura authority keypair using S/R 25519 as its crypto.
#[cfg(feature = "std")]
pub type AuthorityPair = app_sr25519::Pair; pub type AuthorityPair = app_sr25519::Pair;
}
/// An Aura authority signature using S/R 25519 as its crypto. /// An Aura authority signature using S/R 25519 as its crypto.
pub type AuthoritySignature = app_sr25519::Signature; pub type AuthoritySignature = app_sr25519::Signature;
@@ -47,9 +48,10 @@ pub mod ed25519 {
app_crypto!(ed25519, AURA); app_crypto!(ed25519, AURA);
} }
sp_application_crypto::with_pair! {
/// An Aura authority keypair using Ed25519 as its crypto. /// An Aura authority keypair using Ed25519 as its crypto.
#[cfg(feature = "std")]
pub type AuthorityPair = app_ed25519::Pair; pub type AuthorityPair = app_ed25519::Pair;
}
/// An Aura authority signature using Ed25519 as its crypto. /// An Aura authority signature using Ed25519 as its crypto.
pub type AuthoritySignature = app_ed25519::Signature; pub type AuthoritySignature = app_ed25519::Signature;
@@ -6,7 +6,7 @@ edition = "2018"
license = "GPL-3.0" license = "GPL-3.0"
[dependencies] [dependencies]
app-crypto = { version = "2.0.0", default-features = false, package = "sp-application-crypto", path = "../application-crypto" } sp-application-crypto = { version = "2.0.0", default-features = false, path = "../application-crypto" }
codec = { package = "parity-scale-codec", version = "1.0.0", default-features = false, features = ["derive"] } codec = { package = "parity-scale-codec", version = "1.0.0", default-features = false, features = ["derive"] }
sp-std = { version = "2.0.0", default-features = false, path = "../std" } sp-std = { version = "2.0.0", default-features = false, path = "../std" }
serde = { version = "1.0.101", optional = true, features = ["derive"] } serde = { version = "1.0.101", optional = true, features = ["derive"] }
@@ -16,13 +16,10 @@ sp-runtime = { version = "2.0.0", default-features = false, path = "../runtime"
[features] [features]
default = ["std"] default = ["std"]
std = [ std = [
"app-crypto/std", "sp-application-crypto/std",
"codec/std", "codec/std",
"sp-std/std", "sp-std/std",
"serde", "serde",
"sp-api/std", "sp-api/std",
"sp-runtime/std", "sp-runtime/std",
] ]
full_crypto = [
"app-crypto/full_crypto"
]
@@ -29,13 +29,14 @@ use sp_std::borrow::Cow;
use sp_std::vec::Vec; use sp_std::vec::Vec;
mod app { mod app {
use app_crypto::{app_crypto, key_types::GRANDPA, ed25519}; use sp_application_crypto::{app_crypto, key_types::GRANDPA, ed25519};
app_crypto!(ed25519, GRANDPA); app_crypto!(ed25519, GRANDPA);
} }
sp_application_crypto::with_pair! {
/// The grandpa crypto scheme defined via the keypair type. /// The grandpa crypto scheme defined via the keypair type.
#[cfg(any(feature = "std", feature = "full_crypto"))]
pub type AuthorityPair = app::Pair; pub type AuthorityPair = app::Pair;
}
/// Identity of a Grandpa authority. /// Identity of a Grandpa authority.
pub type AuthorityId = app::Public; pub type AuthorityId = app::Public;