From 9b8a283f2b522ca7e948653efc0dc9da148b3b6b Mon Sep 17 00:00:00 2001 From: Alain Brenzikofer Date: Mon, 30 Sep 2019 18:10:07 +0200 Subject: [PATCH 01/24] introduced "with_crypto" feature and applied switches like in substrate-api-client fork --- Cargo.lock | 8 ++++- core/primitives/Cargo.toml | 30 ++++++++++++------- core/primitives/src/crypto.rs | 43 +++++++++++++++++---------- core/primitives/src/ed25519.rs | 45 ++++++++++++++++------------- core/primitives/src/lib.rs | 6 ++-- core/primitives/src/sr25519.rs | 53 +++++++++++++++++----------------- 6 files changed, 109 insertions(+), 76 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f3a76716ae4c0..4b5fc99d56627 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1194,6 +1194,11 @@ name = "hex" version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "hex" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "hex-literal" version = "0.2.0" @@ -5206,7 +5211,7 @@ dependencies = [ "ed25519-dalek 1.0.0-pre.1 (registry+https://github.com/rust-lang/crates.io-index)", "hash-db 0.15.2 (registry+https://github.com/rust-lang/crates.io-index)", "hash256-std-hasher 0.15.2 (registry+https://github.com/rust-lang/crates.io-index)", - "hex 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", + "hex 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "hex-literal 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "impl-serde 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -6694,6 +6699,7 @@ dependencies = [ "checksum heapsize 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "1679e6ea370dee694f91f1dc469bf94cf8f52051d147aec3e1f9497c6fc22461" "checksum heck 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "20564e78d53d2bb135c343b3f47714a56af2061f1c928fdb541dc7b9fdd94205" "checksum hex 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "805026a5d0141ffc30abb3be3173848ad46a1b1664fe632428479619a3644d77" +"checksum hex 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "023b39be39e3a2da62a94feb433e91e8bcd37676fbc8bea371daf52b7a769a3e" "checksum hex-literal 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c3da68162fdd2147e66682e78e729bd77f93b4c99656db058c5782d8c6b6225a" "checksum hex-literal-impl 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "06095d08c7c05760f11a071b3e1d4c5b723761c01bd8d7201c30a9536668a612" "checksum hmac 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7a13f4163aa0c5ca1be584aace0e2212b2e41be5478218d4f657f5f778b2ae2a" diff --git a/core/primitives/Cargo.toml b/core/primitives/Cargo.toml index f05907e3f205e..c162b10f707a4 100644 --- a/core/primitives/Cargo.toml +++ b/core/primitives/Cargo.toml @@ -9,7 +9,7 @@ rstd = { package = "sr-std", path = "../sr-std", default-features = false } codec = { package = "parity-scale-codec", version = "1.0.0", default-features = false, features = ["derive"] } rustc-hex = { version = "2.0", default-features = false } serde = { version = "1.0", optional = true, features = ["derive"] } -twox-hash = { version = "1.2.0", optional = true } +twox-hash = { version = "1.3.0", default-features = false } byteorder = { version = "1.3.1", default-features = false } primitive-types = { version = "0.5.0", default-features = false, features = ["codec"] } impl-serde = { version = "0.1", optional = true } @@ -17,19 +17,19 @@ log = { version = "0.4", optional = true } wasmi = { version = "0.5.0", optional = true } hash-db = { version = "0.15.2", default-features = false } hash256-std-hasher = { version = "0.15.2", default-features = false } -ed25519-dalek = { version = "1.0.0-pre.1", optional = true } +ed25519-dalek = { version = "1.0.0-pre.1", default-features = false, features = ["u64_backend"], optional = true } base58 = { version = "0.1", optional = true } -blake2-rfc = { version = "0.2.18", optional = true } -schnorrkel = { version = "0.8.4", features = ["preaudit_deprecated"], optional = true } +blake2-rfc = { version = "0.2.18", default-features = false, optional = true } +schnorrkel = { version = "0.8.5", features = ["preaudit_deprecated"], default-features = false, optional = true } rand = { version = "0.6", optional = true } sha2 = { version = "0.8", optional = true } substrate-bip39 = { version = "0.3.1", optional = true } tiny-bip39 = { version = "0.6.1", optional = true } -hex = { version = "0.3", optional = true } +hex = { version = "0.4", default-features = false, optional = true } regex = { version = "1.1", optional = true } num-traits = { version = "0.2", default-features = false } zeroize = { version = "0.9.2", default-features = false } -lazy_static = { version = "1.3", optional = true } +lazy_static = { version = "1.3", default-features = false, optional = true } parking_lot = { version = "0.9.0", optional = true } [dev-dependencies] @@ -49,6 +49,7 @@ bench = false [features] default = ["std"] std = [ + "with_crypto", "log", "wasmi", "lazy_static", @@ -65,10 +66,10 @@ std = [ "rstd/std", "serde", "rustc-hex/std", - "twox-hash", - "blake2-rfc", - "ed25519-dalek", - "hex", + "twox-hash/std", + "blake2-rfc/std", + "ed25519-dalek/std", + "hex/std", "base58", "substrate-bip39", "tiny-bip39", @@ -76,8 +77,15 @@ std = [ "byteorder/std", "rand", "sha2", - "schnorrkel", + "schnorrkel/std", "regex", "num-traits/std", "zeroize/std" ] + +with_crypto = [ + "ed25519-dalek", + "blake2-rfc", + "schnorrkel", + "hex", +] diff --git a/core/primitives/src/crypto.rs b/core/primitives/src/crypto.rs index e2838315d17d4..e083a4c15d5e5 100644 --- a/core/primitives/src/crypto.rs +++ b/core/primitives/src/crypto.rs @@ -18,6 +18,8 @@ //! Cryptographic utilities. // end::description[] +use rstd::vec::Vec; +use core::hash::Hash; #[cfg(feature = "std")] use rstd::convert::TryInto; use rstd::convert::TryFrom; @@ -30,8 +32,7 @@ use codec::{Encode, Decode}; use regex::Regex; #[cfg(feature = "std")] use base58::{FromBase58, ToBase58}; -#[cfg(feature = "std")] -use std::hash::Hash; + use zeroize::Zeroize; #[doc(hidden)] pub use rstd::ops::Deref; @@ -48,7 +49,7 @@ pub enum Infallible {} /// The length of the junction identifier. Note that this is also referred to as the /// `CHAIN_CODE_LENGTH` in the context of Schnorrkel. -#[cfg(feature = "std")] +#[cfg(feature = "with_crypto")] pub const JUNCTION_ID_LEN: usize = 32; /// Similar to `From`, except that the onus is on the part of the caller to ensure @@ -120,7 +121,7 @@ impl Drop for Protected { /// An error with the interpretation of a secret. #[derive(Debug, Clone, PartialEq, Eq)] -#[cfg(feature = "std")] +#[cfg(feature = "with_crypto")] pub enum SecretStringError { /// The overall format was invalid (e.g. the seed phrase contained symbols). InvalidFormat, @@ -140,7 +141,7 @@ pub enum SecretStringError { /// a new secret key from an existing secret key and, in the case of `SoftRaw` and `SoftIndex` /// a new public key from an existing public key. #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug, Encode, Decode)] -#[cfg(feature = "std")] +#[cfg(feature = "with_crypto")] pub enum DeriveJunction { /// Soft (vanilla) derivation. Public keys have a correspondent derivation. Soft([u8; JUNCTION_ID_LEN]), @@ -148,7 +149,7 @@ pub enum DeriveJunction { Hard([u8; JUNCTION_ID_LEN]), } -#[cfg(feature = "std")] +#[cfg(feature = "with_crypto")] impl DeriveJunction { /// Consume self to return a soft derive junction with the same chain code. pub fn soften(self) -> Self { DeriveJunction::Soft(self.unwrap_inner()) } @@ -209,7 +210,7 @@ impl DeriveJunction { } } -#[cfg(feature = "std")] +#[cfg(feature = "with_crypto")] impl> From for DeriveJunction { fn from(j: T) -> DeriveJunction { let j = j.as_ref(); @@ -236,7 +237,7 @@ impl> From for DeriveJunction { } /// An error type for SS58 decoding. -#[cfg(feature = "std")] +#[cfg(feature = "with_crypto")] #[derive(Clone, Copy, Eq, PartialEq, Debug)] pub enum PublicError { /// Bad alphabet. @@ -254,9 +255,10 @@ pub enum PublicError { } /// Key that can be encoded to/from SS58. -#[cfg(feature = "std")] +#[cfg(feature = "with_crypto")] pub trait Ss58Codec: Sized { /// Some if the string is a properly encoded SS58Check address. + #[cfg(feature = "std")] fn from_ss58check(s: &str) -> Result { Self::from_ss58check_with_version(s) .and_then(|(r, v)| match v { @@ -272,6 +274,7 @@ pub trait Ss58Codec: Sized { fn from_ss58check_with_version(s: &str) -> Result<(Self, Ss58AddressFormat), PublicError>; /// Some if the string is a properly encoded SS58Check address, optionally with /// a derivation path following. + #[cfg(feature = "std")] fn from_string(s: &str) -> Result { Self::from_string_with_version(s) .and_then(|(r, v)| match v { @@ -285,11 +288,14 @@ pub trait Ss58Codec: Sized { } /// Return the ss58-check string for this key. + #[cfg(feature = "std")] fn to_ss58check_with_version(&self, version: Ss58AddressFormat) -> String; /// Return the ss58-check string for this key. + #[cfg(feature = "std")] fn to_ss58check(&self) -> String { self.to_ss58check_with_version(*DEFAULT_VERSION.lock()) } /// Some if the string is a properly encoded SS58Check address, optionally with /// a derivation path following. + #[cfg(feature = "std")] fn from_string_with_version(s: &str) -> Result<(Self, Ss58AddressFormat), PublicError> { Self::from_ss58check_with_version(s) } @@ -324,7 +330,7 @@ lazy_static::lazy_static! { } /// A known address (sub)format/network ID for SS58. -#[cfg(feature = "std")] +#[cfg(feature = "with_crypto")] #[derive(Copy, Clone, PartialEq, Eq)] pub enum Ss58AddressFormat { /// Any Substrate network, direct checksum, standard account (*25519). @@ -339,7 +345,7 @@ pub enum Ss58AddressFormat { Custom(u8), } -#[cfg(feature = "std")] +#[cfg(feature = "with_crypto")] impl From for u8 { fn from(x: Ss58AddressFormat) -> u8 { match x { @@ -352,7 +358,7 @@ impl From for u8 { } } -#[cfg(feature = "std")] +#[cfg(feature = "with_crypto")] impl TryFrom for Ss58AddressFormat { type Error = (); fn try_from(x: u8) -> Result { @@ -366,7 +372,7 @@ impl TryFrom for Ss58AddressFormat { } } -#[cfg(feature = "std")] +#[cfg(feature = "with_crypto")] impl<'a> TryFrom<&'a str> for Ss58AddressFormat { type Error = (); fn try_from(x: &'a str) -> Result { @@ -537,7 +543,9 @@ mod dummy { type Seed = Dummy; type Signature = Dummy; type DeriveError = (); + #[cfg(feature = "std")] fn generate_with_phrase(_: Option<&str>) -> (Self, String, Self::Seed) { Default::default() } + #[cfg(feature = "std")] fn from_phrase(_: &str, _: Option<&str>) -> Result<(Self, Self::Seed), SecretStringError> { @@ -566,7 +574,7 @@ mod dummy { /// Trait suitable for typical cryptographic PKI key pair type. /// /// For now it just specifies how to create a key from a phrase and derivation path. -#[cfg(feature = "std")] +#[cfg(feature = "with_crypto")] pub trait Pair: CryptoType + Sized + Clone + Send + Sync + 'static { /// The type which is used to encode a public key. type Public: Public + Hash; @@ -586,6 +594,7 @@ pub trait Pair: CryptoType + Sized + Clone + Send + Sync + 'static { /// /// This is only for ephemeral keys really, since you won't have access to the secret key /// for storage. If you want a persistent key pair, use `generate_with_phrase` instead. + #[cfg(feature = "std")] fn generate() -> (Self, Self::Seed) { let mut csprng: OsRng = OsRng::new().expect("OS random generator works; qed"); let mut seed = Self::Seed::default(); @@ -599,9 +608,11 @@ pub trait Pair: CryptoType + Sized + Clone + Send + Sync + 'static { /// /// This is generally slower than `generate()`, so prefer that unless you need to persist /// the key from the current session. + #[cfg(feature = "std")] fn generate_with_phrase(password: Option<&str>) -> (Self, String, Self::Seed); /// Returns the KeyPair from the English BIP39 seed `phrase`, or `None` if it's invalid. + #[cfg(feature = "std")] fn from_phrase(phrase: &str, password: Option<&str>) -> Result<(Self, Self::Seed), SecretStringError>; /// Derive a child key from a series of given junctions. @@ -621,6 +632,7 @@ pub trait Pair: CryptoType + Sized + Clone + Send + Sync + 'static { fn from_seed_slice(seed: &[u8]) -> Result; /// Construct a key from a phrase, password and path. + #[cfg(feature = "std")] fn from_standard_components< I: Iterator >(phrase: &str, password: Option<&str>, path: I) -> Result; @@ -663,6 +675,7 @@ pub trait Pair: CryptoType + Sized + Clone + Send + Sync + 'static { /// be equivalent to no password at all. /// /// `None` is returned if no matches are found. + #[cfg(feature = "std")] fn from_string(s: &str, password_override: Option<&str>) -> Result { let hex_seed = if s.starts_with("0x") { &s[2..] @@ -732,7 +745,7 @@ impl UncheckedFrom for Outer where /// Type which has a particular kind of crypto associated with it. pub trait CryptoType { /// The pair key type of this crypto. - #[cfg(feature="std")] + #[cfg(feature = "with_crypto")] type Pair: Pair; } diff --git a/core/primitives/src/ed25519.rs b/core/primitives/src/ed25519.rs index 810e86767d8e3..57b4c9ae89b07 100644 --- a/core/primitives/src/ed25519.rs +++ b/core/primitives/src/ed25519.rs @@ -18,17 +18,18 @@ //! Simple Ed25519 API. // end::description[] +use rstd::vec::Vec; use crate::{hash::H256, hash::H512}; use codec::{Encode, Decode}; -#[cfg(feature = "std")] +#[cfg(feature = "with_crypto")] use blake2_rfc; #[cfg(feature = "std")] use substrate_bip39::seed_from_entropy; #[cfg(feature = "std")] use bip39::{Mnemonic, Language, MnemonicType}; -#[cfg(feature = "std")] +#[cfg(feature = "with_crypto")] use crate::crypto::{Pair as TraitPair, DeriveJunction, SecretStringError, Ss58Codec}; #[cfg(feature = "std")] use serde::{de, Serializer, Serialize, Deserializer, Deserialize}; @@ -37,7 +38,7 @@ use crate::{crypto::{Public as TraitPublic, UncheckedFrom, CryptoType, Derive}}; /// A secret seed. It's not called a "secret key" because ring doesn't expose the secret keys /// of the key pair (yeah, dumb); as such we're forced to remember the seed manually if we /// will need it later (such as for HDKD). -#[cfg(feature = "std")] +#[cfg(feature = "with_crypto")] type Seed = [u8; 32]; /// A public key. @@ -45,10 +46,10 @@ type Seed = [u8; 32]; pub struct Public(pub [u8; 32]); /// A key pair. -#[cfg(feature = "std")] +#[cfg(feature = "with_crypto")] pub struct Pair(ed25519_dalek::Keypair); -#[cfg(feature = "std")] +#[cfg(feature = "with_crypto")] impl Clone for Pair { fn clone(&self) -> Self { Pair(ed25519_dalek::Keypair { @@ -97,7 +98,7 @@ impl From for [u8; 32] { } } -#[cfg(feature = "std")] +#[cfg(feature = "with_crypto")] impl From for Public { fn from(x: Pair) -> Self { x.public() @@ -152,9 +153,9 @@ impl<'de> Deserialize<'de> for Public { } } -#[cfg(feature = "std")] -impl std::hash::Hash for Public { - fn hash(&self, state: &mut H) { +#[cfg(feature = "with_crypto")] +impl core::hash::Hash for Public { + fn hash(&self, state: &mut H) { self.0.hash(state); } } @@ -236,10 +237,10 @@ impl std::fmt::Debug for Signature { } } -#[cfg(feature = "std")] -impl std::hash::Hash for Signature { - fn hash(&self, state: &mut H) { - std::hash::Hash::hash(&self.0[..], state); +#[cfg(feature = "with_crypto")] +impl core::hash::Hash for Signature { + fn hash(&self, state: &mut H) { + core::hash::Hash::hash(&self.0[..], state); } } @@ -333,7 +334,7 @@ impl TraitPublic for Public { impl Derive for Public {} /// Derive a single hard junction. -#[cfg(feature = "std")] +#[cfg(feature = "with_crypto")] fn derive_hard_junction(secret_seed: &Seed, cc: &[u8; 32]) -> Seed { ("Ed25519HDKD", secret_seed, cc).using_encoded(|data| { let mut res = [0u8; 32]; @@ -343,13 +344,13 @@ fn derive_hard_junction(secret_seed: &Seed, cc: &[u8; 32]) -> Seed { } /// An error when deriving a key. -#[cfg(feature = "std")] +#[cfg(feature = "with_crypto")] pub enum DeriveError { /// A soft key was found in the path (and is unsupported). SoftKeyInPath, } -#[cfg(feature = "std")] +#[cfg(feature = "with_crypto")] impl TraitPair for Pair { type Public = Public; type Seed = Seed; @@ -359,6 +360,7 @@ impl TraitPair for Pair { /// Generate new secure (random) key pair and provide the recovery phrase. /// /// You can recover the same key later with `from_phrase`. + #[cfg(feature = "std")] fn generate_with_phrase(password: Option<&str>) -> (Pair, String, Seed) { let mnemonic = Mnemonic::new(MnemonicType::Words12, Language::English); let phrase = mnemonic.phrase(); @@ -372,6 +374,7 @@ impl TraitPair for Pair { } /// Generate key pair from given recovery phrase and password. + #[cfg(feature = "std")] fn from_phrase(phrase: &str, password: Option<&str>) -> Result<(Pair, Seed), SecretStringError> { let big_seed = seed_from_entropy( Mnemonic::from_phrase(phrase, Language::English) @@ -414,6 +417,7 @@ impl TraitPair for Pair { } /// Generate a key from the phrase, password and derivation path. + #[cfg(feature = "std")] fn from_standard_components>( phrase: &str, password: Option<&str>, @@ -470,7 +474,7 @@ impl TraitPair for Pair { } } -#[cfg(feature = "std")] +#[cfg(feature = "with_crypto")] impl Pair { /// Get the seed for this key. pub fn seed(&self) -> &Seed { @@ -479,6 +483,7 @@ impl Pair { /// Exactly as `from_string` except that if no matches are found then, the the first 32 /// characters are taken (padded with spaces as necessary) and used as the MiniSecretKey. + #[cfg(feature = "std")] pub fn from_legacy_string(s: &str, password_override: Option<&str>) -> Pair { Self::from_string(s, password_override).unwrap_or_else(|_| { let mut padded_seed: Seed = [' ' as u8; 32]; @@ -490,16 +495,16 @@ impl Pair { } impl CryptoType for Public { - #[cfg(feature="std")] + #[cfg(feature = "with_crypto")] type Pair = Pair; } impl CryptoType for Signature { - #[cfg(feature="std")] + #[cfg(feature = "with_crypto")] type Pair = Pair; } -#[cfg(feature = "std")] +#[cfg(feature = "with_crypto")] impl CryptoType for Pair { type Pair = Pair; } diff --git a/core/primitives/src/lib.rs b/core/primitives/src/lib.rs index 8086c9832a357..f5b0a99312924 100644 --- a/core/primitives/src/lib.rs +++ b/core/primitives/src/lib.rs @@ -44,9 +44,9 @@ pub use codec::{Encode, Decode};// << for macro #[cfg(feature = "std")] pub use impl_serde::serialize as bytes; -#[cfg(feature = "std")] +#[cfg(feature = "with_crypto")] pub mod hashing; -#[cfg(feature = "std")] +#[cfg(feature = "with_crypto")] pub use hashing::{blake2_128, blake2_256, twox_64, twox_128, twox_256}; #[cfg(feature = "std")] pub mod hexdisplay; @@ -73,7 +73,7 @@ mod tests; pub use self::hash::{H160, H256, H512, convert_hash}; pub use self::uint::U256; pub use changes_trie::ChangesTrieConfiguration; -#[cfg(feature = "std")] +#[cfg(feature = "with_crypto")] pub use crypto::{DeriveJunction, Pair, Public}; pub use hash_db::Hasher; diff --git a/core/primitives/src/sr25519.rs b/core/primitives/src/sr25519.rs index 0e573f49ce34c..b476035ddc07c 100644 --- a/core/primitives/src/sr25519.rs +++ b/core/primitives/src/sr25519.rs @@ -20,8 +20,8 @@ //! Note: `CHAIN_CODE_LENGTH` must be equal to `crate::crypto::JUNCTION_ID_LEN` //! for this to work. // end::description[] - -#[cfg(feature = "std")] +use rstd::vec::Vec; +#[cfg(feature = "with_crypto")] use schnorrkel::{signing_context, ExpansionMode, Keypair, SecretKey, MiniSecretKey, PublicKey, derive::{Derivation, ChainCode, CHAIN_CODE_LENGTH} }; @@ -29,7 +29,7 @@ use schnorrkel::{signing_context, ExpansionMode, Keypair, SecretKey, MiniSecretK use substrate_bip39::mini_secret_from_entropy; #[cfg(feature = "std")] use bip39::{Mnemonic, Language, MnemonicType}; -#[cfg(feature = "std")] +#[cfg(feature = "with_crypto")] use crate::crypto::{ Pair as TraitPair, DeriveJunction, Infallible, SecretStringError, Ss58Codec }; @@ -39,11 +39,11 @@ use codec::{Encode, Decode}; #[cfg(feature = "std")] use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; -#[cfg(feature = "std")] +#[cfg(feature = "with_crypto")] use schnorrkel::keys::{MINI_SECRET_KEY_LENGTH, SECRET_KEY_LENGTH}; // signing context -#[cfg(feature = "std")] +#[cfg(feature = "with_crypto")] const SIGNING_CTX: &[u8] = b"substrate"; /// An Schnorrkel/Ristretto x25519 ("sr25519") public key. @@ -51,10 +51,10 @@ const SIGNING_CTX: &[u8] = b"substrate"; pub struct Public(pub [u8; 32]); /// An Schnorrkel/Ristretto x25519 ("sr25519") key pair. -#[cfg(feature = "std")] +#[cfg(feature = "with_crypto")] pub struct Pair(Keypair); -#[cfg(feature = "std")] +#[cfg(feature = "with_crypto")] impl Clone for Pair { fn clone(&self) -> Self { Pair(schnorrkel::Keypair { @@ -151,9 +151,9 @@ impl<'de> Deserialize<'de> for Public { } } -#[cfg(feature = "std")] -impl std::hash::Hash for Public { - fn hash(&self, state: &mut H) { +#[cfg(feature = "with_crypto")] +impl core::hash::Hash for Public { + fn hash(&self, state: &mut H) { self.0.hash(state); } } @@ -230,7 +230,7 @@ impl AsMut<[u8]> for Signature { } } -#[cfg(feature = "std")] +#[cfg(feature = "with_crypto")] impl From for Signature { fn from(s: schnorrkel::Signature) -> Signature { Signature(s.to_bytes()) @@ -244,10 +244,10 @@ impl std::fmt::Debug for Signature { } } -#[cfg(feature = "std")] -impl std::hash::Hash for Signature { - fn hash(&self, state: &mut H) { - std::hash::Hash::hash(&self.0[..], state); +#[cfg(feature = "with_crypto")] +impl core::hash::Hash for Signature { + fn hash(&self, state: &mut H) { + core::hash::Hash::hash(&self.0[..], state); } } @@ -358,21 +358,21 @@ impl From for Pair { } } -#[cfg(feature = "std")] +#[cfg(feature = "with_crypto")] impl From for Pair { fn from(p: schnorrkel::Keypair) -> Pair { Pair(p) } } -#[cfg(feature = "std")] +#[cfg(feature = "with_crypto")] impl From for schnorrkel::Keypair { fn from(p: Pair) -> schnorrkel::Keypair { p.0 } } -#[cfg(feature = "std")] +#[cfg(feature = "with_crypto")] impl AsRef for Pair { fn as_ref(&self) -> &schnorrkel::Keypair { &self.0 @@ -380,16 +380,16 @@ impl AsRef for Pair { } /// Derive a single hard junction. -#[cfg(feature = "std")] +#[cfg(feature = "with_crypto")] fn derive_hard_junction(secret: &SecretKey, cc: &[u8; CHAIN_CODE_LENGTH]) -> SecretKey { secret.hard_derive_mini_secret_key(Some(ChainCode(cc.clone())), b"").0.expand(ExpansionMode::Ed25519) } /// The raw secret seed, which can be used to recreate the `Pair`. -#[cfg(feature = "std")] +#[cfg(feature = "with_crypto")] type Seed = [u8; MINI_SECRET_KEY_LENGTH]; -#[cfg(feature = "std")] +#[cfg(feature = "with_crypto")] impl TraitPair for Pair { type Public = Public; type Seed = Seed; @@ -438,6 +438,7 @@ impl TraitPair for Pair { } /// Generate a key from the phrase, password and derivation path. + #[cfg(feature = "std")] fn from_standard_components>( phrase: &str, password: Option<&str>, @@ -447,7 +448,7 @@ impl TraitPair for Pair { .derive(path) .map_err(|_| SecretStringError::InvalidPath) } - + #[cfg(feature = "std")] fn generate_with_phrase(password: Option<&str>) -> (Pair, String, Seed) { let mnemonic = Mnemonic::new(MnemonicType::Words12, Language::English); let phrase = mnemonic.phrase(); @@ -459,7 +460,7 @@ impl TraitPair for Pair { seed, ) } - + #[cfg(feature = "std")] fn from_phrase(phrase: &str, password: Option<&str>) -> Result<(Pair, Seed), SecretStringError> { Mnemonic::from_phrase(phrase, Language::English) .map_err(|_| SecretStringError::InvalidPhrase) @@ -520,16 +521,16 @@ impl Pair { } impl CryptoType for Public { - #[cfg(feature="std")] + #[cfg(feature = "with_crypto")] type Pair = Pair; } impl CryptoType for Signature { - #[cfg(feature="std")] + #[cfg(feature = "with_crypto")] type Pair = Pair; } -#[cfg(feature = "std")] +#[cfg(feature = "with_crypto")] impl CryptoType for Pair { type Pair = Pair; } From 9edc07e6ea16b4d66fbd1eba097e072ca2234446 Mon Sep 17 00:00:00 2001 From: Alain Brenzikofer Date: Mon, 30 Sep 2019 21:50:22 +0200 Subject: [PATCH 02/24] introduced "with_crypto" feature and applied switches like in substraTEE-worker fork --- core/application-crypto/Cargo.toml | 6 +++++- core/application-crypto/src/ed25519.rs | 3 ++- core/application-crypto/src/lib.rs | 26 ++++++++++++++++---------- core/application-crypto/src/sr25519.rs | 3 ++- core/application-crypto/src/traits.rs | 18 ++++++++++++------ 5 files changed, 37 insertions(+), 19 deletions(-) diff --git a/core/application-crypto/Cargo.toml b/core/application-crypto/Cargo.toml index 4e4f896f29860..1c5e2d0fa321c 100644 --- a/core/application-crypto/Cargo.toml +++ b/core/application-crypto/Cargo.toml @@ -18,4 +18,8 @@ sr-primitives = { path = "../sr-primitives" } [features] default = [ "std" ] -std = [ "primitives/std", "codec/std", "serde", "rstd/std", "runtime-io/std" ] +std = [ "with_crypto", "primitives/std", "codec/std", "serde", "rstd/std", "runtime-io/std" ] + +with_crypto = [ + "primitives/with_crypto" +] \ No newline at end of file diff --git a/core/application-crypto/src/ed25519.rs b/core/application-crypto/src/ed25519.rs index d5c4fd7badabf..7c42d0ecfbdeb 100644 --- a/core/application-crypto/src/ed25519.rs +++ b/core/application-crypto/src/ed25519.rs @@ -21,13 +21,14 @@ use crate::{RuntimePublic, KeyTypeId}; pub use primitives::ed25519::*; mod app { + use crate::Vec; use primitives::testing::ED25519; crate::app_crypto!(super, ED25519); } pub use app::Public as AppPublic; pub use app::Signature as AppSignature; -#[cfg(feature="std")] +#[cfg(feature = "with_crypto")] pub use app::Pair as AppPair; impl RuntimePublic for Public { diff --git a/core/application-crypto/src/lib.rs b/core/application-crypto/src/lib.rs index e3366a461a064..5dcac1ee5474c 100644 --- a/core/application-crypto/src/lib.rs +++ b/core/application-crypto/src/lib.rs @@ -23,7 +23,7 @@ #[doc(hidden)] pub use primitives::{self, crypto::{CryptoType, Public, Derive, IsWrappedBy, Wraps}}; #[doc(hidden)] -#[cfg(feature = "std")] +#[cfg(feature = "with_crypto")] pub use primitives::crypto::{SecretStringError, DeriveJunction, Ss58Codec, Pair}; pub use primitives::{crypto::{KeyTypeId, key_types}}; @@ -53,9 +53,9 @@ pub use traits::*; #[macro_export] macro_rules! app_crypto { ($module:ident, $key_type:expr) => { - #[cfg(feature="std")] + #[cfg(feature = "with_crypto")] $crate::app_crypto!($module::Pair, $module::Public, $module::Signature, $key_type); - #[cfg(not(feature="std"))] + #[cfg(not(feature = "with_crypto"))] $crate::app_crypto!($module::Public, $module::Signature, $key_type); }; ($pair:ty, $public:ty, $sig:ty, $key_type:expr) => { @@ -71,16 +71,19 @@ macro_rules! app_crypto { type Pair = Pair; } - #[cfg(feature = "std")] + #[cfg(feature = "with_crypto")] impl $crate::Pair for Pair { type Public = Public; type Seed = <$pair as $crate::Pair>::Seed; type Signature = Signature; type DeriveError = <$pair as $crate::Pair>::DeriveError; + + #[cfg(feature = "std")] fn generate_with_phrase(password: Option<&str>) -> (Self, String, Self::Seed) { let r = <$pair>::generate_with_phrase(password); (Self(r.0), r.1, r.2) } + #[cfg(feature = "std")] fn from_phrase(phrase: &str, password: Option<&str>) -> Result<(Self, Self::Seed), $crate::SecretStringError> { @@ -95,6 +98,7 @@ macro_rules! app_crypto { fn from_seed_slice(seed: &[u8]) -> Result { <$pair>::from_seed_slice(seed).map(Self) } + #[cfg(feature = "std")] fn from_standard_components< I: Iterator >( @@ -142,7 +146,8 @@ macro_rules! app_crypto { Clone, Default, Eq, PartialEq, Ord, PartialOrd, $crate::codec::Encode, $crate::codec::Decode, )] - #[cfg_attr(feature = "std", derive(Debug, Hash))] + #[cfg_attr(feature = "std", derive(Debug))] + #[cfg_attr(feature = "with_crypto", derive(Hash))] pub struct Public($public); } @@ -191,7 +196,7 @@ macro_rules! app_crypto { } impl $crate::CryptoType for Public { - #[cfg(feature="std")] + #[cfg(feature = "with_crypto")] type Pair = Pair; } @@ -202,7 +207,7 @@ macro_rules! app_crypto { impl $crate::AppKey for Public { type UntypedGeneric = $public; type Public = Public; - #[cfg(feature="std")] + #[cfg(feature = "with_crypto")] type Pair = Pair; type Signature = Signature; const ID: $crate::KeyTypeId = $key_type; @@ -240,7 +245,8 @@ macro_rules! app_crypto { $crate::wrap! { /// A generic `AppPublic` wrapper type over $public crypto; this has no specific App. #[derive(Clone, Default, Eq, PartialEq, $crate::codec::Encode, $crate::codec::Decode)] - #[cfg_attr(feature = "std", derive(Debug, Hash))] + #[cfg_attr(feature = "std", derive(Debug))] + #[cfg_attr(feature = "with_crypto", derive(Hash))] pub struct Signature($sig); } @@ -255,14 +261,14 @@ macro_rules! app_crypto { } impl $crate::CryptoType for Signature { - #[cfg(feature="std")] + #[cfg(feature = "with_crypto")] type Pair = Pair; } impl $crate::AppKey for Signature { type UntypedGeneric = $sig; type Public = Public; - #[cfg(feature="std")] + #[cfg(feature = "with_crypto")] type Pair = Pair; type Signature = Signature; const ID: $crate::KeyTypeId = $key_type; diff --git a/core/application-crypto/src/sr25519.rs b/core/application-crypto/src/sr25519.rs index 93565a628ffb6..3ef9d4ba43e00 100644 --- a/core/application-crypto/src/sr25519.rs +++ b/core/application-crypto/src/sr25519.rs @@ -21,13 +21,14 @@ use crate::{RuntimePublic, KeyTypeId}; pub use primitives::sr25519::*; mod app { + use crate::Vec; use primitives::testing::SR25519; crate::app_crypto!(super, SR25519); } pub use app::Public as AppPublic; pub use app::Signature as AppSignature; -#[cfg(feature="std")] +#[cfg(feature = "with_crypto")] pub use app::Pair as AppPair; impl RuntimePublic for Public { diff --git a/core/application-crypto/src/traits.rs b/core/application-crypto/src/traits.rs index aad076bd90012..bd3eda347aadd 100644 --- a/core/application-crypto/src/traits.rs +++ b/core/application-crypto/src/traits.rs @@ -15,7 +15,7 @@ // along with Substrate. If not, see . use primitives::crypto::{KeyTypeId, CryptoType, IsWrappedBy, Public}; -#[cfg(feature = "std")] +#[cfg(feature = "with_crypto")] use primitives::crypto::Pair; use codec::Codec; @@ -28,7 +28,7 @@ pub trait AppKey: 'static + Send + Sync + Sized + CryptoType + Clone { type Public: AppPublic; /// The corresponding key pair type in this application scheme. - #[cfg(feature="std")] + #[cfg(feature = "with_crypto")] type Pair: AppPair; /// The corresponding signature type in this application scheme. @@ -44,12 +44,18 @@ pub trait MaybeDebugHash: std::fmt::Debug + std::hash::Hash {} #[cfg(feature = "std")] impl MaybeDebugHash for T {} -/// Type which implements Debug and Hash in std, not when no-std (no-std variant). -#[cfg(not(feature = "std"))] +/// Type which implements Debug and Hash in std, not when no-std (no-std variant without crypto). +#[cfg(all(not(feature = "std"), not(feature = "with_crypto")))] pub trait MaybeDebugHash {} -#[cfg(not(feature = "std"))] +#[cfg(all(not(feature = "std"), not(feature = "with_crypto")))] impl MaybeDebugHash for T {} +/// Type which implements Debug and Hash in std, not when no-std (no-std variant with crypto). +#[cfg(all(not(feature = "std"), feature = "with_crypto"))] +pub trait MaybeDebugHash: core::hash::Hash {} +#[cfg(all(not(feature = "std"), feature = "with_crypto"))] +impl MaybeDebugHash for T {} + /// A application's public key. pub trait AppPublic: AppKey + Public + Ord + PartialOrd + Eq + PartialEq + MaybeDebugHash + codec::Codec { /// The wrapped type which is just a plain instance of `Public`. @@ -58,7 +64,7 @@ pub trait AppPublic: AppKey + Public + Ord + PartialOrd + Eq + PartialEq + Maybe } /// A application's key pair. -#[cfg(feature = "std")] +#[cfg(feature = "with_crypto")] pub trait AppPair: AppKey + Pair::Public> { /// The wrapped type which is just a plain instance of `Pair`. type Generic: IsWrappedBy + Pair::Public as AppPublic>::Generic>; From 7097676d22fa585192164ba56a24fc14c97f2e81 Mon Sep 17 00:00:00 2001 From: Alain Brenzikofer Date: Mon, 30 Sep 2019 22:15:08 +0200 Subject: [PATCH 03/24] distinguishing core::hash vs std::hash --- core/primitives/src/ed25519.rs | 19 +++++++++++++++++-- core/primitives/src/sr25519.rs | 10 ++++++++-- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/core/primitives/src/ed25519.rs b/core/primitives/src/ed25519.rs index 57b4c9ae89b07..514981d60fa83 100644 --- a/core/primitives/src/ed25519.rs +++ b/core/primitives/src/ed25519.rs @@ -153,7 +153,14 @@ impl<'de> Deserialize<'de> for Public { } } -#[cfg(feature = "with_crypto")] +#[cfg(all(feature = "with_crypto", feature = "std"))] +impl std::hash::Hash for Public { + fn hash(&self, state: &mut H) { + self.0.hash(state); + } +} + +#[cfg(all(feature = "with_crypto", not(feature = "std")))] impl core::hash::Hash for Public { fn hash(&self, state: &mut H) { self.0.hash(state); @@ -237,13 +244,21 @@ impl std::fmt::Debug for Signature { } } -#[cfg(feature = "with_crypto")] +#[cfg(all(feature = "with_crypto", feature = "std"))] +impl std::hash::Hash for Signature { + fn hash(&self, state: &mut H) { + std::hash::Hash::hash(&self.0[..], state); + } +} + +#[cfg(all(feature = "with_crypto", not(feature = "std")))] impl core::hash::Hash for Signature { fn hash(&self, state: &mut H) { core::hash::Hash::hash(&self.0[..], state); } } + impl Signature { /// A new instance from the given 64-byte `data`. /// diff --git a/core/primitives/src/sr25519.rs b/core/primitives/src/sr25519.rs index b476035ddc07c..12dfeb03ba5cb 100644 --- a/core/primitives/src/sr25519.rs +++ b/core/primitives/src/sr25519.rs @@ -151,13 +151,19 @@ impl<'de> Deserialize<'de> for Public { } } -#[cfg(feature = "with_crypto")] +#[cfg(all(feature = "with_crypto", feature = "std"))] +impl std::hash::Hash for Public { + fn hash(&self, state: &mut H) { + self.0.hash(state); + } +} + +#[cfg(all(feature = "with_crypto", not(feature = "std")))] impl core::hash::Hash for Public { fn hash(&self, state: &mut H) { self.0.hash(state); } } - /// An Schnorrkel/Ristretto x25519 ("sr25519") signature. /// /// Instead of importing it for the local module, alias it to be available as a public type From 5ab8bb28af037f41ccd33097c9f915b5a349d34b Mon Sep 17 00:00:00 2001 From: Alain Brenzikofer Date: Wed, 30 Oct 2019 07:18:02 +0100 Subject: [PATCH 04/24] @bkchr's review requests fulfilled --- Cargo.lock | 52 ++++++++++--------------- core/application-crypto/Cargo.toml | 9 +++-- core/application-crypto/src/ed25519.rs | 2 +- core/application-crypto/src/lib.rs | 20 +++++----- core/application-crypto/src/sr25519.rs | 2 +- core/application-crypto/src/traits.rs | 14 +++---- core/primitives/Cargo.toml | 8 +++- core/primitives/src/crypto.rs | 30 +++++++-------- core/primitives/src/ed25519.rs | 53 ++++++++++---------------- core/primitives/src/lib.rs | 6 +-- core/primitives/src/sr25519.rs | 46 ++++++++++------------ 11 files changed, 108 insertions(+), 134 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 353ae8ff0d048..176101fe5013c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -341,7 +341,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "either 1.5.3 (registry+https://github.com/rust-lang/crates.io-index)", - "iovec 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "iovec 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -738,17 +738,6 @@ name = "doc-comment" version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -[[package]] -name = "ed25519-dalek" -version = "0.9.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "clear_on_drop 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", - "curve25519-dalek 1.2.3 (registry+https://github.com/rust-lang/crates.io-index)", - "failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", - "rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "ed25519-dalek" version = "1.0.0-pre.2" @@ -1330,7 +1319,7 @@ dependencies = [ "http 0.1.18 (registry+https://github.com/rust-lang/crates.io-index)", "http-body 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "httparse 1.3.4 (registry+https://github.com/rust-lang/crates.io-index)", - "iovec 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "iovec 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", "itoa 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", "net2 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1422,7 +1411,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "iovec" -version = "0.1.3" +version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2225,7 +2214,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", "fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", - "iovec 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "iovec 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2251,7 +2240,7 @@ name = "mio-uds" version = "0.6.7" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "iovec 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "iovec 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)", "mio 0.6.19 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -2303,7 +2292,7 @@ dependencies = [ "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", "openssl 0.10.25 (registry+https://github.com/rust-lang/crates.io-index)", "openssl-probe 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", - "openssl-sys 0.9.50 (registry+https://github.com/rust-lang/crates.io-index)", + "openssl-sys 0.9.51 (registry+https://github.com/rust-lang/crates.io-index)", "schannel 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)", "security-framework 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "security-framework-sys 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2706,7 +2695,7 @@ dependencies = [ "foreign-types 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)", - "openssl-sys 0.9.50 (registry+https://github.com/rust-lang/crates.io-index)", + "openssl-sys 0.9.51 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -2716,7 +2705,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "openssl-sys" -version = "0.9.50" +version = "0.9.51" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "autocfg 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", @@ -3810,7 +3799,7 @@ dependencies = [ "substrate-state-machine 2.0.0", "substrate-test-runtime-client 2.0.0", "syn 0.15.44 (registry+https://github.com/rust-lang/crates.io-index)", - "trybuild 1.0.16 (registry+https://github.com/rust-lang/crates.io-index)", + "trybuild 1.0.17 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -4407,7 +4396,7 @@ dependencies = [ "srml-support 2.0.0", "substrate-inherents 2.0.0", "substrate-primitives 2.0.0", - "trybuild 1.0.16 (registry+https://github.com/rust-lang/crates.io-index)", + "trybuild 1.0.17 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -5231,7 +5220,7 @@ dependencies = [ "blake2-rfc 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)", "byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "criterion 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", - "ed25519-dalek 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)", + "ed25519-dalek 1.0.0-pre.2 (registry+https://github.com/rust-lang/crates.io-index)", "hash-db 0.15.2 (registry+https://github.com/rust-lang/crates.io-index)", "hash256-std-hasher 0.15.2 (registry+https://github.com/rust-lang/crates.io-index)", "hex 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -5903,7 +5892,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.29 (registry+https://github.com/rust-lang/crates.io-index)", - "iovec 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "iovec 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", "rustls 0.15.2 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-io 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", "webpki 0.19.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -5925,7 +5914,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.29 (registry+https://github.com/rust-lang/crates.io-index)", - "iovec 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "iovec 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", "mio 0.6.19 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-io 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-reactor 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", @@ -5989,7 +5978,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.29 (registry+https://github.com/rust-lang/crates.io-index)", - "iovec 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "iovec 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", "mio 0.6.19 (registry+https://github.com/rust-lang/crates.io-index)", @@ -6077,7 +6066,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "trybuild" -version = "1.0.16" +version = "1.0.17" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "glob 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -6687,7 +6676,6 @@ dependencies = [ "checksum digest 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "f3d0c8c8752312f9713efd397ff63acb9f85585afbf179282e720e7704954dd5" "checksum dns-parser 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c4d33be9473d06f75f58220f71f7a9317aca647dc061dbd3c361b0bef505fbea" "checksum doc-comment 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "923dea538cea0aa3025e8685b20d6ee21ef99c4f77e954a30febbaac5ec73a97" -"checksum ed25519-dalek 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d07e8b8a8386c3b89a7a4b329fdfa4cb545de2545e9e2ebbc3dd3929253e426" "checksum ed25519-dalek 1.0.0-pre.2 (registry+https://github.com/rust-lang/crates.io-index)" = "845aaacc16f01178f33349e7c992ecd0cee095aa5e577f0f4dee35971bd36455" "checksum either 1.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "bb1f6b1ce1c140482ea30ddd3335fc0024ac7ee112895426e0a629a6c20adfe3" "checksum elastic-array 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)" = "073be79b6538296faf81c631872676600616073817dd9a440c477ad09b408983" @@ -6742,8 +6730,8 @@ dependencies = [ "checksum heck 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "20564e78d53d2bb135c343b3f47714a56af2061f1c928fdb541dc7b9fdd94205" "checksum hex 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "805026a5d0141ffc30abb3be3173848ad46a1b1664fe632428479619a3644d77" "checksum hex 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "023b39be39e3a2da62a94feb433e91e8bcd37676fbc8bea371daf52b7a769a3e" -"checksum hex-literal 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c3da68162fdd2147e66682e78e729bd77f93b4c99656db058c5782d8c6b6225a" -"checksum hex-literal-impl 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "06095d08c7c05760f11a071b3e1d4c5b723761c01bd8d7201c30a9536668a612" +"checksum hex-literal 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "961de220ec9a91af2e1e5bd80d02109155695e516771762381ef8581317066e0" +"checksum hex-literal-impl 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "9d4c5c844e2fee0bf673d54c2c177f1713b3d2af2ff6e666b49cb7572e6cf42d" "checksum hmac 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7a13f4163aa0c5ca1be584aace0e2212b2e41be5478218d4f657f5f778b2ae2a" "checksum hmac 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "5dcb5e64cda4c23119ab41ba960d1e170a774c8e4b9d9e6a9bc18aabf5e59695" "checksum hmac-drbg 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "4fe727d41d2eec0a6574d887914347e5ff96a3b87177817e2a9820c5c87fecc2" @@ -6763,7 +6751,7 @@ dependencies = [ "checksum indexmap 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a61202fbe46c4a951e9404a720a0180bcf3212c750d735cb5c4ba4dc551299f3" "checksum integer-sqrt 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "ea155abb3ba6f382a75f1418988c05fe82959ed9ce727de427f9cfd425b0c903" "checksum interleaved-ordered 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "141340095b15ed7491bd3d4ced9d20cebfb826174b6bb03386381f62b01e3d77" -"checksum iovec 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "c9636900aa73ffed13cdbb199f17cd955670bb300927c8d25b517dfa136b6567" +"checksum iovec 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "b2b3ea6ff95e175473f8ffe6a7eb7c00d054240321b84c57051175fe3c1e075e" "checksum ipnet 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e61c2da0d0f700c77d2d313dbf4f93e41d235fa12c6681fee06621036df4c2af" "checksum itertools 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5b8467d9c1cebe26feb08c640139247fac215782d35371ade9a2136ed6085358" "checksum itoa 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)" = "501266b7edd0174f8530248f87f99c88fbe60ca4ef3dd486835b8d8d53136f7f" @@ -6854,7 +6842,7 @@ dependencies = [ "checksum opaque-debug 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2839e79665f131bdb5782e51f2c6c9599c133c6098982a54c794358bf432529c" "checksum openssl 0.10.25 (registry+https://github.com/rust-lang/crates.io-index)" = "2f372b2b53ce10fb823a337aaa674e3a7d072b957c6264d0f4ff0bd86e657449" "checksum openssl-probe 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "77af24da69f9d9341038eba93a073b1fdaaa1b788221b00a69bce9e762cb32de" -"checksum openssl-sys 0.9.50 (registry+https://github.com/rust-lang/crates.io-index)" = "2c42dcccb832556b5926bc9ae61e8775f2a61e725ab07ab3d1e7fcf8ae62c3b6" +"checksum openssl-sys 0.9.51 (registry+https://github.com/rust-lang/crates.io-index)" = "ba24190c8f0805d3bd2ce028f439fe5af1d55882bbe6261bed1dbc93b50dd6b1" "checksum output_vt100 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "53cdc5b785b7a58c5aad8216b3dfa114df64b0b06ae6e1501cef91df2fbdf8f9" "checksum owning_ref 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "49a4b8ea2179e6a2e27411d3bca09ca6dd630821cf6894c6c7c8467a8ee7ef13" "checksum parity-bytes 0.1.0 (git+https://github.com/paritytech/parity-common?rev=b0317f649ab2c665b7987b8475878fc4d2e1f81d)" = "" @@ -7026,7 +7014,7 @@ dependencies = [ "checksum trie-root 0.15.2 (registry+https://github.com/rust-lang/crates.io-index)" = "0b779f7c1c8fe9276365d9d5be5c4b5adeacf545117bb3f64c974305789c5c0b" "checksum trie-standardmap 0.15.2 (registry+https://github.com/rust-lang/crates.io-index)" = "c3161ba520ab28cd8e6b68e1126f1009f6e335339d1a73b978139011703264c8" "checksum try-lock 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e604eb7b43c06650e854be16a2a03155743d3752dd1c943f6829e26b7a36e382" -"checksum trybuild 1.0.16 (registry+https://github.com/rust-lang/crates.io-index)" = "10d8f366221c5a5ff8a62faa005e186fdce758949d34a9140b64a062951bae68" +"checksum trybuild 1.0.17 (registry+https://github.com/rust-lang/crates.io-index)" = "e6851bf8351876984fbab8a2391de6378947b898410d8714edd12164d2137127" "checksum twofish 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712d261e83e727c8e2dbb75dacac67c36e35db36a958ee504f2164fc052434e1" "checksum twox-hash 1.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3bfd5b7557925ce778ff9b9ef90e3ade34c524b5ff10e239c69a42d546d2af56" "checksum typeable 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "1410f6f91f21d1612654e7cc69193b0334f909dcf2c790c4826254fbb86f8887" diff --git a/core/application-crypto/Cargo.toml b/core/application-crypto/Cargo.toml index 578b140bd370c..663ca79d77045 100644 --- a/core/application-crypto/Cargo.toml +++ b/core/application-crypto/Cargo.toml @@ -18,8 +18,11 @@ sr-primitives = { path = "../sr-primitives" } [features] default = [ "std" ] -std = [ "with_crypto", "primitives/std", "codec/std", "serde", "rstd/std", "runtime-io/std" ] +std = [ "full_crypto", "primitives/std", "codec/std", "serde", "rstd/std", "runtime-io/std" ] -with_crypto = [ - "primitives/with_crypto" +# This feature enables all crypto primitives for `no_std` builds like microcontrollers +# or Intel SGX. +# For the regular wasm runtime builds this should not be used. +full_crypto = [ + "primitives/full_crypto" ] \ No newline at end of file diff --git a/core/application-crypto/src/ed25519.rs b/core/application-crypto/src/ed25519.rs index cc681816b2fde..37d72bcf394b1 100644 --- a/core/application-crypto/src/ed25519.rs +++ b/core/application-crypto/src/ed25519.rs @@ -28,7 +28,7 @@ mod app { pub use app::Public as AppPublic; pub use app::Signature as AppSignature; -#[cfg(feature = "with_crypto")] +#[cfg(feature = "full_crypto")] pub use app::Pair as AppPair; impl RuntimePublic for Public { diff --git a/core/application-crypto/src/lib.rs b/core/application-crypto/src/lib.rs index 5dcac1ee5474c..139560ecf6a0a 100644 --- a/core/application-crypto/src/lib.rs +++ b/core/application-crypto/src/lib.rs @@ -23,7 +23,7 @@ #[doc(hidden)] pub use primitives::{self, crypto::{CryptoType, Public, Derive, IsWrappedBy, Wraps}}; #[doc(hidden)] -#[cfg(feature = "with_crypto")] +#[cfg(feature = "full_crypto")] pub use primitives::crypto::{SecretStringError, DeriveJunction, Ss58Codec, Pair}; pub use primitives::{crypto::{KeyTypeId, key_types}}; @@ -53,9 +53,9 @@ pub use traits::*; #[macro_export] macro_rules! app_crypto { ($module:ident, $key_type:expr) => { - #[cfg(feature = "with_crypto")] + #[cfg(feature = "full_crypto")] $crate::app_crypto!($module::Pair, $module::Public, $module::Signature, $key_type); - #[cfg(not(feature = "with_crypto"))] + #[cfg(not(feature = "full_crypto"))] $crate::app_crypto!($module::Public, $module::Signature, $key_type); }; ($pair:ty, $public:ty, $sig:ty, $key_type:expr) => { @@ -71,7 +71,7 @@ macro_rules! app_crypto { type Pair = Pair; } - #[cfg(feature = "with_crypto")] + #[cfg(feature = "full_crypto")] impl $crate::Pair for Pair { type Public = Public; type Seed = <$pair as $crate::Pair>::Seed; @@ -147,7 +147,7 @@ macro_rules! app_crypto { $crate::codec::Decode, )] #[cfg_attr(feature = "std", derive(Debug))] - #[cfg_attr(feature = "with_crypto", derive(Hash))] + #[cfg_attr(feature = "full_crypto", derive(Hash))] pub struct Public($public); } @@ -196,7 +196,7 @@ macro_rules! app_crypto { } impl $crate::CryptoType for Public { - #[cfg(feature = "with_crypto")] + #[cfg(feature = "full_crypto")] type Pair = Pair; } @@ -207,7 +207,7 @@ macro_rules! app_crypto { impl $crate::AppKey for Public { type UntypedGeneric = $public; type Public = Public; - #[cfg(feature = "with_crypto")] + #[cfg(feature = "full_crypto")] type Pair = Pair; type Signature = Signature; const ID: $crate::KeyTypeId = $key_type; @@ -246,7 +246,7 @@ macro_rules! app_crypto { /// A generic `AppPublic` wrapper type over $public crypto; this has no specific App. #[derive(Clone, Default, Eq, PartialEq, $crate::codec::Encode, $crate::codec::Decode)] #[cfg_attr(feature = "std", derive(Debug))] - #[cfg_attr(feature = "with_crypto", derive(Hash))] + #[cfg_attr(feature = "full_crypto", derive(Hash))] pub struct Signature($sig); } @@ -261,14 +261,14 @@ macro_rules! app_crypto { } impl $crate::CryptoType for Signature { - #[cfg(feature = "with_crypto")] + #[cfg(feature = "full_crypto")] type Pair = Pair; } impl $crate::AppKey for Signature { type UntypedGeneric = $sig; type Public = Public; - #[cfg(feature = "with_crypto")] + #[cfg(feature = "full_crypto")] type Pair = Pair; type Signature = Signature; const ID: $crate::KeyTypeId = $key_type; diff --git a/core/application-crypto/src/sr25519.rs b/core/application-crypto/src/sr25519.rs index bc1bf7b6456d1..185f1ba725aad 100644 --- a/core/application-crypto/src/sr25519.rs +++ b/core/application-crypto/src/sr25519.rs @@ -28,7 +28,7 @@ mod app { pub use app::Public as AppPublic; pub use app::Signature as AppSignature; -#[cfg(feature = "with_crypto")] +#[cfg(feature = "full_crypto")] pub use app::Pair as AppPair; impl RuntimePublic for Public { diff --git a/core/application-crypto/src/traits.rs b/core/application-crypto/src/traits.rs index bd3eda347aadd..cf35d780ca507 100644 --- a/core/application-crypto/src/traits.rs +++ b/core/application-crypto/src/traits.rs @@ -15,7 +15,7 @@ // along with Substrate. If not, see . use primitives::crypto::{KeyTypeId, CryptoType, IsWrappedBy, Public}; -#[cfg(feature = "with_crypto")] +#[cfg(feature = "full_crypto")] use primitives::crypto::Pair; use codec::Codec; @@ -28,7 +28,7 @@ pub trait AppKey: 'static + Send + Sync + Sized + CryptoType + Clone { type Public: AppPublic; /// The corresponding key pair type in this application scheme. - #[cfg(feature = "with_crypto")] + #[cfg(feature = "full_crypto")] type Pair: AppPair; /// The corresponding signature type in this application scheme. @@ -45,15 +45,15 @@ pub trait MaybeDebugHash: std::fmt::Debug + std::hash::Hash {} impl MaybeDebugHash for T {} /// Type which implements Debug and Hash in std, not when no-std (no-std variant without crypto). -#[cfg(all(not(feature = "std"), not(feature = "with_crypto")))] +#[cfg(all(not(feature = "std"), not(feature = "full_crypto")))] pub trait MaybeDebugHash {} -#[cfg(all(not(feature = "std"), not(feature = "with_crypto")))] +#[cfg(all(not(feature = "std"), not(feature = "full_crypto")))] impl MaybeDebugHash for T {} /// Type which implements Debug and Hash in std, not when no-std (no-std variant with crypto). -#[cfg(all(not(feature = "std"), feature = "with_crypto"))] +#[cfg(all(not(feature = "std"), feature = "full_crypto"))] pub trait MaybeDebugHash: core::hash::Hash {} -#[cfg(all(not(feature = "std"), feature = "with_crypto"))] +#[cfg(all(not(feature = "std"), feature = "full_crypto"))] impl MaybeDebugHash for T {} /// A application's public key. @@ -64,7 +64,7 @@ pub trait AppPublic: AppKey + Public + Ord + PartialOrd + Eq + PartialEq + Maybe } /// A application's key pair. -#[cfg(feature = "with_crypto")] +#[cfg(feature = "full_crypto")] pub trait AppPair: AppKey + Pair::Public> { /// The wrapped type which is just a plain instance of `Pair`. type Generic: IsWrappedBy + Pair::Public as AppPublic>::Generic>; diff --git a/core/primitives/Cargo.toml b/core/primitives/Cargo.toml index b3a7ac4b75e63..7cd7a4527f5a8 100644 --- a/core/primitives/Cargo.toml +++ b/core/primitives/Cargo.toml @@ -51,7 +51,7 @@ bench = false [features] default = ["std"] std = [ - "with_crypto", + "full_crypto", "log", "wasmi", "lazy_static", @@ -86,9 +86,13 @@ std = [ "primitives-storage/std", ] -with_crypto = [ +# This feature enables all crypto primitives for `no_std` builds like microcontrollers +# or Intel SGX. +# For the regular wasm runtime builds this should not be used. +full_crypto = [ "ed25519-dalek", "blake2-rfc", "schnorrkel", "hex", + "twox-hash" ] diff --git a/core/primitives/src/crypto.rs b/core/primitives/src/crypto.rs index 9499894e13c18..af9a71e00b259 100644 --- a/core/primitives/src/crypto.rs +++ b/core/primitives/src/crypto.rs @@ -18,8 +18,7 @@ //! Cryptographic utilities. // end::description[] -use rstd::vec::Vec; -use core::hash::Hash; +use rstd::{vec::Vec, hash::Hash}; #[cfg(feature = "std")] use rstd::convert::TryInto; use rstd::convert::TryFrom; @@ -49,7 +48,7 @@ pub enum Infallible {} /// The length of the junction identifier. Note that this is also referred to as the /// `CHAIN_CODE_LENGTH` in the context of Schnorrkel. -#[cfg(feature = "with_crypto")] +#[cfg(feature = "full_crypto")] pub const JUNCTION_ID_LEN: usize = 32; /// Similar to `From`, except that the onus is on the part of the caller to ensure @@ -121,7 +120,7 @@ impl Drop for Protected { /// An error with the interpretation of a secret. #[derive(Debug, Clone, PartialEq, Eq)] -#[cfg(feature = "with_crypto")] +#[cfg(feature = "full_crypto")] pub enum SecretStringError { /// The overall format was invalid (e.g. the seed phrase contained symbols). InvalidFormat, @@ -141,7 +140,7 @@ pub enum SecretStringError { /// a new secret key from an existing secret key and, in the case of `SoftRaw` and `SoftIndex` /// a new public key from an existing public key. #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug, Encode, Decode)] -#[cfg(feature = "with_crypto")] +#[cfg(feature = "full_crypto")] pub enum DeriveJunction { /// Soft (vanilla) derivation. Public keys have a correspondent derivation. Soft([u8; JUNCTION_ID_LEN]), @@ -149,7 +148,7 @@ pub enum DeriveJunction { Hard([u8; JUNCTION_ID_LEN]), } -#[cfg(feature = "with_crypto")] +#[cfg(feature = "full_crypto")] impl DeriveJunction { /// Consume self to return a soft derive junction with the same chain code. pub fn soften(self) -> Self { DeriveJunction::Soft(self.unwrap_inner()) } @@ -210,7 +209,7 @@ impl DeriveJunction { } } -#[cfg(feature = "with_crypto")] +#[cfg(feature = "full_crypto")] impl> From for DeriveJunction { fn from(j: T) -> DeriveJunction { let j = j.as_ref(); @@ -237,7 +236,7 @@ impl> From for DeriveJunction { } /// An error type for SS58 decoding. -#[cfg(feature = "with_crypto")] +#[cfg(feature = "full_crypto")] #[derive(Clone, Copy, Eq, PartialEq, Debug)] pub enum PublicError { /// Bad alphabet. @@ -255,10 +254,9 @@ pub enum PublicError { } /// Key that can be encoded to/from SS58. -#[cfg(feature = "with_crypto")] +#[cfg(feature = "full_crypto")] pub trait Ss58Codec: Sized { /// Some if the string is a properly encoded SS58Check address. - #[cfg(feature = "std")] fn from_ss58check(s: &str) -> Result { Self::from_ss58check_with_version(s) .and_then(|(r, v)| match v { @@ -330,7 +328,7 @@ lazy_static::lazy_static! { } /// A known address (sub)format/network ID for SS58. -#[cfg(feature = "with_crypto")] +#[cfg(feature = "full_crypto")] #[derive(Copy, Clone, PartialEq, Eq)] pub enum Ss58AddressFormat { /// Any Substrate network, direct checksum, standard account (*25519). @@ -345,7 +343,7 @@ pub enum Ss58AddressFormat { Custom(u8), } -#[cfg(feature = "with_crypto")] +#[cfg(feature = "full_crypto")] impl From for u8 { fn from(x: Ss58AddressFormat) -> u8 { match x { @@ -358,7 +356,7 @@ impl From for u8 { } } -#[cfg(feature = "with_crypto")] +#[cfg(feature = "full_crypto")] impl TryFrom for Ss58AddressFormat { type Error = (); fn try_from(x: u8) -> Result { @@ -372,7 +370,7 @@ impl TryFrom for Ss58AddressFormat { } } -#[cfg(feature = "with_crypto")] +#[cfg(feature = "full_crypto")] impl<'a> TryFrom<&'a str> for Ss58AddressFormat { type Error = (); fn try_from(x: &'a str) -> Result { @@ -574,7 +572,7 @@ mod dummy { /// Trait suitable for typical cryptographic PKI key pair type. /// /// For now it just specifies how to create a key from a phrase and derivation path. -#[cfg(feature = "with_crypto")] +#[cfg(feature = "full_crypto")] pub trait Pair: CryptoType + Sized + Clone + Send + Sync + 'static { /// The type which is used to encode a public key. type Public: Public + Hash; @@ -744,7 +742,7 @@ impl UncheckedFrom for Outer where /// Type which has a particular kind of crypto associated with it. pub trait CryptoType { /// The pair key type of this crypto. - #[cfg(feature = "with_crypto")] + #[cfg(feature = "full_crypto")] type Pair: Pair; } diff --git a/core/primitives/src/ed25519.rs b/core/primitives/src/ed25519.rs index 5c02b2d4a2b67..7f03e3fb65906 100644 --- a/core/primitives/src/ed25519.rs +++ b/core/primitives/src/ed25519.rs @@ -23,13 +23,13 @@ use rstd::vec::Vec; use crate::{hash::H256, hash::H512}; use codec::{Encode, Decode}; -#[cfg(feature = "with_crypto")] +#[cfg(feature = "full_crypto")] use blake2_rfc; #[cfg(feature = "std")] use substrate_bip39::seed_from_entropy; #[cfg(feature = "std")] use bip39::{Mnemonic, Language, MnemonicType}; -#[cfg(feature = "with_crypto")] +#[cfg(feature = "full_crypto")] use crate::crypto::{Pair as TraitPair, DeriveJunction, SecretStringError, Ss58Codec}; #[cfg(feature = "std")] use serde::{de, Serializer, Serialize, Deserializer, Deserialize}; @@ -38,7 +38,7 @@ use crate::{crypto::{Public as TraitPublic, UncheckedFrom, CryptoType, Derive}}; /// A secret seed. It's not called a "secret key" because ring doesn't expose the secret keys /// of the key pair (yeah, dumb); as such we're forced to remember the seed manually if we /// will need it later (such as for HDKD). -#[cfg(feature = "with_crypto")] +#[cfg(feature = "full_crypto")] type Seed = [u8; 32]; /// A public key. @@ -46,10 +46,10 @@ type Seed = [u8; 32]; pub struct Public(pub [u8; 32]); /// A key pair. -#[cfg(feature = "with_crypto")] +#[cfg(feature = "full_crypto")] pub struct Pair(ed25519_dalek::Keypair); -#[cfg(feature = "with_crypto")] +#[cfg(feature = "full_crypto")] impl Clone for Pair { fn clone(&self) -> Self { Pair(ed25519_dalek::Keypair { @@ -98,7 +98,7 @@ impl From for [u8; 32] { } } -#[cfg(feature = "with_crypto")] +#[cfg(feature = "full_crypto")] impl From for Public { fn from(x: Pair) -> Self { x.public() @@ -153,16 +153,9 @@ impl<'de> Deserialize<'de> for Public { } } -#[cfg(all(feature = "with_crypto", feature = "std"))] -impl std::hash::Hash for Public { - fn hash(&self, state: &mut H) { - self.0.hash(state); - } -} - -#[cfg(all(feature = "with_crypto", not(feature = "std")))] -impl core::hash::Hash for Public { - fn hash(&self, state: &mut H) { +#[cfg(all(feature = "full_crypto", feature = "std"))] +impl rstd::hash::Hash for Public { + fn hash(&self, state: &mut H) { self.0.hash(state); } } @@ -244,19 +237,13 @@ impl std::fmt::Debug for Signature { } } -#[cfg(all(feature = "with_crypto", feature = "std"))] -impl std::hash::Hash for Signature { - fn hash(&self, state: &mut H) { - std::hash::Hash::hash(&self.0[..], state); +#[cfg(all(feature = "full_crypto", feature = "std"))] +impl rstd::hash::Hash for Signature { + fn hash(&self, state: &mut H) { + rstd::hash::Hash::hash(&self.0[..], state); } } -#[cfg(all(feature = "with_crypto", not(feature = "std")))] -impl core::hash::Hash for Signature { - fn hash(&self, state: &mut H) { - core::hash::Hash::hash(&self.0[..], state); - } -} impl Signature { @@ -349,7 +336,7 @@ impl TraitPublic for Public { impl Derive for Public {} /// Derive a single hard junction. -#[cfg(feature = "with_crypto")] +#[cfg(feature = "full_crypto")] fn derive_hard_junction(secret_seed: &Seed, cc: &[u8; 32]) -> Seed { ("Ed25519HDKD", secret_seed, cc).using_encoded(|data| { let mut res = [0u8; 32]; @@ -359,13 +346,13 @@ fn derive_hard_junction(secret_seed: &Seed, cc: &[u8; 32]) -> Seed { } /// An error when deriving a key. -#[cfg(feature = "with_crypto")] +#[cfg(feature = "full_crypto")] pub enum DeriveError { /// A soft key was found in the path (and is unsupported). SoftKeyInPath, } -#[cfg(feature = "with_crypto")] +#[cfg(feature = "full_crypto")] impl TraitPair for Pair { type Public = Public; type Seed = Seed; @@ -489,7 +476,7 @@ impl TraitPair for Pair { } } -#[cfg(feature = "with_crypto")] +#[cfg(feature = "full_crypto")] impl Pair { /// Get the seed for this key. pub fn seed(&self) -> &Seed { @@ -510,16 +497,16 @@ impl Pair { } impl CryptoType for Public { - #[cfg(feature = "with_crypto")] + #[cfg(feature = "full_crypto")] type Pair = Pair; } impl CryptoType for Signature { - #[cfg(feature = "with_crypto")] + #[cfg(feature = "full_crypto")] type Pair = Pair; } -#[cfg(feature = "with_crypto")] +#[cfg(feature = "full_crypto")] impl CryptoType for Pair { type Pair = Pair; } diff --git a/core/primitives/src/lib.rs b/core/primitives/src/lib.rs index 06def730c3838..06ada747cc7fc 100644 --- a/core/primitives/src/lib.rs +++ b/core/primitives/src/lib.rs @@ -44,9 +44,9 @@ pub use codec::{Encode, Decode};// << for macro #[cfg(feature = "std")] pub use impl_serde::serialize as bytes; -#[cfg(feature = "with_crypto")] +#[cfg(feature = "full_crypto")] pub mod hashing; -#[cfg(feature = "with_crypto")] +#[cfg(feature = "full_crypto")] pub use hashing::{blake2_128, blake2_256, twox_64, twox_128, twox_256}; #[cfg(feature = "std")] pub mod hexdisplay; @@ -72,7 +72,7 @@ mod tests; pub use self::hash::{H160, H256, H512, convert_hash}; pub use self::uint::U256; pub use changes_trie::ChangesTrieConfiguration; -#[cfg(feature = "with_crypto")] +#[cfg(feature = "full_crypto")] pub use crypto::{DeriveJunction, Pair, Public}; pub use hash_db::Hasher; diff --git a/core/primitives/src/sr25519.rs b/core/primitives/src/sr25519.rs index 12dfeb03ba5cb..cf864bff43926 100644 --- a/core/primitives/src/sr25519.rs +++ b/core/primitives/src/sr25519.rs @@ -21,7 +21,7 @@ //! for this to work. // end::description[] use rstd::vec::Vec; -#[cfg(feature = "with_crypto")] +#[cfg(feature = "full_crypto")] use schnorrkel::{signing_context, ExpansionMode, Keypair, SecretKey, MiniSecretKey, PublicKey, derive::{Derivation, ChainCode, CHAIN_CODE_LENGTH} }; @@ -29,7 +29,7 @@ use schnorrkel::{signing_context, ExpansionMode, Keypair, SecretKey, MiniSecretK use substrate_bip39::mini_secret_from_entropy; #[cfg(feature = "std")] use bip39::{Mnemonic, Language, MnemonicType}; -#[cfg(feature = "with_crypto")] +#[cfg(feature = "full_crypto")] use crate::crypto::{ Pair as TraitPair, DeriveJunction, Infallible, SecretStringError, Ss58Codec }; @@ -39,11 +39,11 @@ use codec::{Encode, Decode}; #[cfg(feature = "std")] use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; -#[cfg(feature = "with_crypto")] +#[cfg(feature = "full_crypto")] use schnorrkel::keys::{MINI_SECRET_KEY_LENGTH, SECRET_KEY_LENGTH}; // signing context -#[cfg(feature = "with_crypto")] +#[cfg(feature = "full_crypto")] const SIGNING_CTX: &[u8] = b"substrate"; /// An Schnorrkel/Ristretto x25519 ("sr25519") public key. @@ -51,10 +51,10 @@ const SIGNING_CTX: &[u8] = b"substrate"; pub struct Public(pub [u8; 32]); /// An Schnorrkel/Ristretto x25519 ("sr25519") key pair. -#[cfg(feature = "with_crypto")] +#[cfg(feature = "full_crypto")] pub struct Pair(Keypair); -#[cfg(feature = "with_crypto")] +#[cfg(feature = "full_crypto")] impl Clone for Pair { fn clone(&self) -> Self { Pair(schnorrkel::Keypair { @@ -151,19 +151,13 @@ impl<'de> Deserialize<'de> for Public { } } -#[cfg(all(feature = "with_crypto", feature = "std"))] -impl std::hash::Hash for Public { - fn hash(&self, state: &mut H) { +#[cfg(all(feature = "full_crypto", feature = "std"))] +impl rstd::hash::Hash for Public { + fn hash(&self, state: &mut H) { self.0.hash(state); } } -#[cfg(all(feature = "with_crypto", not(feature = "std")))] -impl core::hash::Hash for Public { - fn hash(&self, state: &mut H) { - self.0.hash(state); - } -} /// An Schnorrkel/Ristretto x25519 ("sr25519") signature. /// /// Instead of importing it for the local module, alias it to be available as a public type @@ -236,7 +230,7 @@ impl AsMut<[u8]> for Signature { } } -#[cfg(feature = "with_crypto")] +#[cfg(feature = "full_crypto")] impl From for Signature { fn from(s: schnorrkel::Signature) -> Signature { Signature(s.to_bytes()) @@ -250,7 +244,7 @@ impl std::fmt::Debug for Signature { } } -#[cfg(feature = "with_crypto")] +#[cfg(feature = "full_crypto")] impl core::hash::Hash for Signature { fn hash(&self, state: &mut H) { core::hash::Hash::hash(&self.0[..], state); @@ -364,21 +358,21 @@ impl From for Pair { } } -#[cfg(feature = "with_crypto")] +#[cfg(feature = "full_crypto")] impl From for Pair { fn from(p: schnorrkel::Keypair) -> Pair { Pair(p) } } -#[cfg(feature = "with_crypto")] +#[cfg(feature = "full_crypto")] impl From for schnorrkel::Keypair { fn from(p: Pair) -> schnorrkel::Keypair { p.0 } } -#[cfg(feature = "with_crypto")] +#[cfg(feature = "full_crypto")] impl AsRef for Pair { fn as_ref(&self) -> &schnorrkel::Keypair { &self.0 @@ -386,16 +380,16 @@ impl AsRef for Pair { } /// Derive a single hard junction. -#[cfg(feature = "with_crypto")] +#[cfg(feature = "full_crypto")] fn derive_hard_junction(secret: &SecretKey, cc: &[u8; CHAIN_CODE_LENGTH]) -> SecretKey { secret.hard_derive_mini_secret_key(Some(ChainCode(cc.clone())), b"").0.expand(ExpansionMode::Ed25519) } /// The raw secret seed, which can be used to recreate the `Pair`. -#[cfg(feature = "with_crypto")] +#[cfg(feature = "full_crypto")] type Seed = [u8; MINI_SECRET_KEY_LENGTH]; -#[cfg(feature = "with_crypto")] +#[cfg(feature = "full_crypto")] impl TraitPair for Pair { type Public = Public; type Seed = Seed; @@ -527,16 +521,16 @@ impl Pair { } impl CryptoType for Public { - #[cfg(feature = "with_crypto")] + #[cfg(feature = "full_crypto")] type Pair = Pair; } impl CryptoType for Signature { - #[cfg(feature = "with_crypto")] + #[cfg(feature = "full_crypto")] type Pair = Pair; } -#[cfg(feature = "with_crypto")] +#[cfg(feature = "full_crypto")] impl CryptoType for Pair { type Pair = Pair; } From ae0c8700ff15c5b146b878946f36414ba02d95d3 Mon Sep 17 00:00:00 2001 From: Alain Brenzikofer Date: Wed, 30 Oct 2019 08:05:21 +0100 Subject: [PATCH 05/24] fixes --- core/primitives/src/ed25519.rs | 4 +--- core/primitives/src/sr25519.rs | 6 +++--- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/core/primitives/src/ed25519.rs b/core/primitives/src/ed25519.rs index cabfcabb87979..cecb87cd5a76b 100644 --- a/core/primitives/src/ed25519.rs +++ b/core/primitives/src/ed25519.rs @@ -241,15 +241,13 @@ impl rstd::fmt::Debug for Signature { } } -#[cfg(all(feature = "full_crypto", feature = "std"))] +#[cfg(feature = "full_crypto")] impl rstd::hash::Hash for Signature { fn hash(&self, state: &mut H) { rstd::hash::Hash::hash(&self.0[..], state); } } - - impl Signature { /// A new instance from the given 64-byte `data`. /// diff --git a/core/primitives/src/sr25519.rs b/core/primitives/src/sr25519.rs index 93be4de1431ef..5838aa380b601 100644 --- a/core/primitives/src/sr25519.rs +++ b/core/primitives/src/sr25519.rs @@ -249,9 +249,9 @@ impl rstd::fmt::Debug for Signature { } #[cfg(feature = "full_crypto")] -impl core::hash::Hash for Signature { - fn hash(&self, state: &mut H) { - core::hash::Hash::hash(&self.0[..], state); +impl rstd::hash::Hash for Signature { + fn hash(&self, state: &mut H) { + rstd::hash::Hash::hash(&self.0[..], state); } } From cc60d56f6792520c875d4980d6655f3d39ededcf Mon Sep 17 00:00:00 2001 From: Alain Brenzikofer Date: Wed, 30 Oct 2019 08:40:43 +0100 Subject: [PATCH 06/24] revert dependency upgrade ed25519-dalek --- Cargo.lock | 3 ++- core/primitives/Cargo.toml | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e11784c0cafcf..d1f9b31553c1d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5460,7 +5460,7 @@ dependencies = [ "blake2-rfc 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)", "byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "criterion 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", - "ed25519-dalek 1.0.0-pre.2 (registry+https://github.com/rust-lang/crates.io-index)", + "ed25519-dalek 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)", "hash-db 0.15.2 (registry+https://github.com/rust-lang/crates.io-index)", "hash256-std-hasher 0.15.2 (registry+https://github.com/rust-lang/crates.io-index)", "hex 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -6953,6 +6953,7 @@ dependencies = [ "checksum digest 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "f3d0c8c8752312f9713efd397ff63acb9f85585afbf179282e720e7704954dd5" "checksum dns-parser 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c4d33be9473d06f75f58220f71f7a9317aca647dc061dbd3c361b0bef505fbea" "checksum doc-comment 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "923dea538cea0aa3025e8685b20d6ee21ef99c4f77e954a30febbaac5ec73a97" +"checksum ed25519-dalek 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d07e8b8a8386c3b89a7a4b329fdfa4cb545de2545e9e2ebbc3dd3929253e426" "checksum ed25519-dalek 1.0.0-pre.2 (registry+https://github.com/rust-lang/crates.io-index)" = "845aaacc16f01178f33349e7c992ecd0cee095aa5e577f0f4dee35971bd36455" "checksum either 1.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "bb1f6b1ce1c140482ea30ddd3335fc0024ac7ee112895426e0a629a6c20adfe3" "checksum elastic-array 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)" = "073be79b6538296faf81c631872676600616073817dd9a440c477ad09b408983" diff --git a/core/primitives/Cargo.toml b/core/primitives/Cargo.toml index 26a6ed41b9a81..85ecfa2067515 100644 --- a/core/primitives/Cargo.toml +++ b/core/primitives/Cargo.toml @@ -17,7 +17,7 @@ impl-serde = { version = "0.2.3", optional = true } wasmi = { version = "0.5.1", optional = true } hash-db = { version = "0.15.2", default-features = false } hash256-std-hasher = { version = "0.15.2", default-features = false } -ed25519-dalek = { version = "1.0.0-pre.1", default-features = false, features = ["u64_backend"], optional = true } +ed25519-dalek = { version = "0.9.1", default-features = false, features = ["u64_backend"], optional = true } base58 = { version = "0.1.0", optional = true } blake2-rfc = { version = "0.2.18", default-features = false, optional = true } schnorrkel = { version = "0.8.5", features = ["preaudit_deprecated"], default-features = false, optional = true } From 233b026a7a56f37abc2d6bf9230f065e1cf497d8 Mon Sep 17 00:00:00 2001 From: Alain Brenzikofer Date: Wed, 30 Oct 2019 09:07:27 +0100 Subject: [PATCH 07/24] added full_crypto features to all crates using app_crypto! macro --- core/application-crypto/src/traits.rs | 8 ++++---- core/consensus/babe/primitives/Cargo.toml | 6 ++++++ core/finality-grandpa/primitives/Cargo.toml | 6 ++++++ srml/im-online/Cargo.toml | 6 ++++++ 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/core/application-crypto/src/traits.rs b/core/application-crypto/src/traits.rs index 84242d0b1c01e..28073be1cebca 100644 --- a/core/application-crypto/src/traits.rs +++ b/core/application-crypto/src/traits.rs @@ -42,9 +42,9 @@ pub trait AppKey: 'static + Send + Sync + Sized + CryptoType + Clone { /// Type which implements Hash in std, not when no-std (std variant). #[cfg(feature = "std")] -pub trait MaybeHash: std::hash::Hash {} +pub trait MaybeHash: rstd::hash::Hash {} #[cfg(feature = "std")] -impl MaybeHash for T {} +impl 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")))] @@ -54,9 +54,9 @@ impl MaybeHash for T {} /// Type which implements Debug and Hash in std, not when no-std (no-std variant with crypto). #[cfg(all(not(feature = "std"), feature = "full_crypto"))] -pub trait MaybeDebugHash: core::hash::Hash {} +pub trait MaybeDebugHash: rstd::hash::Hash {} #[cfg(all(not(feature = "std"), feature = "full_crypto"))] -impl MaybeDebugHash for T {} +impl MaybeDebugHash for T {} /// A application's public key. pub trait AppPublic: diff --git a/core/consensus/babe/primitives/Cargo.toml b/core/consensus/babe/primitives/Cargo.toml index 79e817d081028..c877cec7fac2f 100644 --- a/core/consensus/babe/primitives/Cargo.toml +++ b/core/consensus/babe/primitives/Cargo.toml @@ -17,6 +17,7 @@ codec = { package = "parity-scale-codec", version = "1.0.0", default-features = [features] default = ["std"] std = [ + "full_crypto", "rstd/std", "sr-primitives/std", "substrate-client/std", @@ -25,3 +26,8 @@ std = [ "slots", "app-crypto/std", ] + +# This feature enables all crypto primitives for `no_std` builds like microcontrollers +# or Intel SGX. +# For the regular wasm runtime builds this should not be used. +full_crypto = [] diff --git a/core/finality-grandpa/primitives/Cargo.toml b/core/finality-grandpa/primitives/Cargo.toml index 02439d4150d81..b3dcc004fddf4 100644 --- a/core/finality-grandpa/primitives/Cargo.toml +++ b/core/finality-grandpa/primitives/Cargo.toml @@ -15,6 +15,7 @@ serde = { version = "1.0.101", optional = true, features = ["derive"] } [features] default = ["std"] std = [ + "full_crypto", "client/std", "codec/std", "sr-primitives/std", @@ -22,3 +23,8 @@ std = [ "serde", "app-crypto/std", ] + +# This feature enables all crypto primitives for `no_std` builds like microcontrollers +# or Intel SGX. +# For the regular wasm runtime builds this should not be used. +full_crypto = [] diff --git a/srml/im-online/Cargo.toml b/srml/im-online/Cargo.toml index b62cc2a8f9615..8c2bc3801c290 100644 --- a/srml/im-online/Cargo.toml +++ b/srml/im-online/Cargo.toml @@ -23,6 +23,7 @@ offchain = { package = "substrate-offchain", path = "../../core/offchain" } [features] default = ["std", "session/historical"] std = [ + "full_crypto", "app-crypto/std", "codec/std", "primitives/std", @@ -35,3 +36,8 @@ std = [ "support/std", "system/std", ] + +# This feature enables all crypto primitives for `no_std` builds like microcontrollers +# or Intel SGX. +# For the regular wasm runtime builds this should not be used. +full_crypto = [] From 53ec53aa928848c07114b9c12001fa44ea27446c Mon Sep 17 00:00:00 2001 From: Alain Brenzikofer Date: Wed, 30 Oct 2019 09:22:22 +0100 Subject: [PATCH 08/24] fixing CI complaints. --- core/consensus/aura/Cargo.toml | 8 ++++++++ core/primitives/src/crypto.rs | 4 +++- core/sr-primitives/Cargo.toml | 6 ++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/core/consensus/aura/Cargo.toml b/core/consensus/aura/Cargo.toml index a09d51095018e..5fd434d559a1d 100644 --- a/core/consensus/aura/Cargo.toml +++ b/core/consensus/aura/Cargo.toml @@ -37,3 +37,11 @@ test-client = { package = "substrate-test-runtime-client", path = "../../test-ru tokio = "0.1.22" env_logger = "0.7.0" tempfile = "3.1.0" + +[features] +default = ["full_crypto"] + +# This feature enables all crypto primitives for `no_std` builds like microcontrollers +# or Intel SGX. +# For the regular wasm runtime builds this should not be used. +full_crypto = [] \ No newline at end of file diff --git a/core/primitives/src/crypto.rs b/core/primitives/src/crypto.rs index b30f0e77d9749..271b1839bb8d7 100644 --- a/core/primitives/src/crypto.rs +++ b/core/primitives/src/crypto.rs @@ -769,7 +769,9 @@ pub trait Pair: CryptoType + Sized + Clone + Send + Sync + 'static { /// /// `None` is returned if no matches are found. #[cfg(feature = "std")] - fn from_string_with_seed(s: &str, password_override: Option<&str>) -> Result<(Self, Option), SecretStringError> { + fn from_string_with_seed(s: &str, password_override: Option<&str>) + -> Result<(Self, Option), SecretStringError> + { let re = Regex::new(r"^(?P[\d\w ]+)?(?P(//?[^/]+)*)(///(?P.*))?$") .expect("constructed from known-good static value; qed"); let cap = re.captures(s).ok_or(SecretStringError::InvalidFormat)?; diff --git a/core/sr-primitives/Cargo.toml b/core/sr-primitives/Cargo.toml index d3303014260f4..41ff55e3ebc34 100644 --- a/core/sr-primitives/Cargo.toml +++ b/core/sr-primitives/Cargo.toml @@ -26,6 +26,7 @@ substrate-offchain = { path = "../offchain" } bench = [] default = ["std"] std = [ + "full_crypto", "app-crypto/std", "arithmetic/std", "codec/std", @@ -36,3 +37,8 @@ std = [ "runtime_io/std", "serde", ] + +# This feature enables all crypto primitives for `no_std` builds like microcontrollers +# or Intel SGX. +# For the regular wasm runtime builds this should not be used. +full_crypto = [] From f6c504082516eb9e37e5270626c18da528d1cf69 Mon Sep 17 00:00:00 2001 From: Alain Brenzikofer Date: Wed, 30 Oct 2019 09:28:01 +0100 Subject: [PATCH 09/24] fix again --- core/consensus/aura/primitives/Cargo.toml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/core/consensus/aura/primitives/Cargo.toml b/core/consensus/aura/primitives/Cargo.toml index 7fd3f3d05d9bf..774eeb92e4706 100644 --- a/core/consensus/aura/primitives/Cargo.toml +++ b/core/consensus/aura/primitives/Cargo.toml @@ -15,9 +15,15 @@ sr-primitives = { path = "../../../sr-primitives", default-features = false } [features] default = ["std"] std = [ + "full_crypto", "rstd/std", "codec/std", "sr-primitives/std", "substrate-client/std", "app-crypto/std", ] + +# This feature enables all crypto primitives for `no_std` builds like microcontrollers +# or Intel SGX. +# For the regular wasm runtime builds this should not be used. +full_crypto = [] \ No newline at end of file From aa96e0f433ef073058558e83466da36c2146dafb Mon Sep 17 00:00:00 2001 From: Alain Brenzikofer Date: Wed, 30 Oct 2019 10:45:50 +0100 Subject: [PATCH 10/24] adding CI test for with_crypto feature --- .gitlab-ci.yml | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 769ee60d1e78e..d454af2748718 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -156,6 +156,23 @@ test-srml-staking: &test-srml-staking - time cargo test --release --verbose --no-default-features --features std - sccache -s +test-full-crypto-feature: &test-full-crypto-feature + stage: test + <<: *docker-env + variables: + # Enable debug assertions since we are running optimized builds for testing + # but still want to have debug assertions. + RUSTFLAGS: -Cdebug-assertions=y + RUST_BACKTRACE: 1 + except: + variables: + - $DEPLOY_TAG + script: + - cd core/primitives/ + - time cargo build --release --verbose --no-default-features --features full_crypto + - cd ../application-crypto + - time cargo build --release --verbose --no-default-features --features full_crypto + - sccache -s From f83bfe3790dad8f01edbee0b6955190b8fdf7af4 Mon Sep 17 00:00:00 2001 From: Alain Brenzikofer Date: Wed, 30 Oct 2019 11:02:31 +0100 Subject: [PATCH 11/24] added full_crypto for ecdsa. now builds wit h--no-deafault-features --features with_crypto --- core/primitives/Cargo.toml | 4 ++- core/primitives/src/crypto.rs | 3 ++ core/primitives/src/ecdsa.rs | 55 +++++++++++++++++++--------------- core/primitives/src/ed25519.rs | 6 ++-- core/primitives/src/sr25519.rs | 9 ++++-- 5 files changed, 47 insertions(+), 30 deletions(-) diff --git a/core/primitives/Cargo.toml b/core/primitives/Cargo.toml index 85ecfa2067515..daecd6ef8055f 100644 --- a/core/primitives/Cargo.toml +++ b/core/primitives/Cargo.toml @@ -31,7 +31,7 @@ num-traits = { version = "0.2.8", default-features = false } zeroize = { version = "0.10.1", default-features = false } lazy_static = { version = "1.4.0", default-features = false, optional = true } parking_lot = { version = "0.9.0", optional = true } -libsecp256k1 = { version = "0.3.0", optional = true } +libsecp256k1 = { version = "0.3.0", default-features = false, optional = true } tiny-keccak = { version = "1.5.0", optional = true } substrate-debug-derive = { version = "2.0.0", path = "./debug-derive" } externalities = { package = "substrate-externalities", path = "../externalities", optional = true } @@ -99,6 +99,8 @@ full_crypto = [ "ed25519-dalek", "blake2-rfc", "schnorrkel", + "libsecp256k1", "hex", + "sha2", "twox-hash" ] diff --git a/core/primitives/src/crypto.rs b/core/primitives/src/crypto.rs index 271b1839bb8d7..08c0a89af7842 100644 --- a/core/primitives/src/crypto.rs +++ b/core/primitives/src/crypto.rs @@ -257,6 +257,7 @@ pub enum PublicError { #[cfg(feature = "full_crypto")] pub trait Ss58Codec: Sized + AsMut<[u8]> + AsRef<[u8]> + Default { /// Some if the string is a properly encoded SS58Check address. + #[cfg(feature = "std")] fn from_ss58check(s: &str) -> Result { Self::from_ss58check_with_version(s) .and_then(|(r, v)| match v { @@ -269,6 +270,7 @@ pub trait Ss58Codec: Sized + AsMut<[u8]> + AsRef<[u8]> + Default { }) } /// Some if the string is a properly encoded SS58Check address. + #[cfg(feature = "std")] fn from_ss58check_with_version(s: &str) -> Result<(Self, Ss58AddressFormat), PublicError> { let mut res = Self::default(); let len = res.as_mut().len(); @@ -806,6 +808,7 @@ pub trait Pair: CryptoType + Sized + Clone + Send + Sync + 'static { /// Interprets the string `s` in order to generate a key pair. /// /// See [`from_string_with_seed`](Self::from_string_with_seed) for more extensive documentation. + #[cfg(feature = "std")] fn from_string(s: &str, password_override: Option<&str>) -> Result { Self::from_string_with_seed(s, password_override).map(|x| x.0) } diff --git a/core/primitives/src/ecdsa.rs b/core/primitives/src/ecdsa.rs index abff460c91034..691e9fba5e184 100644 --- a/core/primitives/src/ecdsa.rs +++ b/core/primitives/src/ecdsa.rs @@ -18,27 +18,31 @@ //! Simple ECDSA API. // end::description[] +use rstd::vec::Vec; + use rstd::cmp::Ordering; use codec::{Encode, Decode}; -#[cfg(feature = "std")] -use std::convert::{TryFrom, TryInto}; +#[cfg(feature = "full_crypto")] +use core::convert::{TryFrom, TryInto}; #[cfg(feature = "std")] use substrate_bip39::seed_from_entropy; #[cfg(feature = "std")] use bip39::{Mnemonic, Language, MnemonicType}; +#[cfg(feature = "full_crypto")] +use crate::{hashing::blake2_256, crypto::{Pair as TraitPair, DeriveJunction, SecretStringError}}; #[cfg(feature = "std")] -use crate::{hashing::blake2_256, crypto::{Pair as TraitPair, DeriveJunction, SecretStringError, Ss58Codec}}; +use crate::crypto::Ss58Codec; #[cfg(feature = "std")] use serde::{de, Serializer, Serialize, Deserializer, Deserialize}; use crate::crypto::{Public as TraitPublic, UncheckedFrom, CryptoType, Derive}; -#[cfg(feature = "std")] +#[cfg(feature = "full_crypto")] use secp256k1::{PublicKey, SecretKey}; /// A secret seed (which is bytewise essentially equivalent to a SecretKey). /// /// We need it as a different type because `Seed` is expected to be AsRef<[u8]>. -#[cfg(feature = "std")] +#[cfg(feature = "full_crypto")] type Seed = [u8; 32]; /// The ECDSA 33-byte compressed public key. @@ -72,7 +76,7 @@ impl Default for Public { } /// A key pair. -#[cfg(feature = "std")] +#[cfg(feature = "full_crypto")] #[derive(Clone)] pub struct Pair { public: PublicKey, @@ -117,7 +121,7 @@ impl From for [u8; 33] { } } -#[cfg(feature = "std")] +#[cfg(feature = "full_crypto")] impl From for Public { fn from(x: Pair) -> Self { x.public() @@ -160,9 +164,9 @@ impl<'de> Deserialize<'de> for Public { } } -#[cfg(feature = "std")] -impl std::hash::Hash for Public { - fn hash(&self, state: &mut H) { +#[cfg(feature = "full_crypto")] +impl rstd::hash::Hash for Public { + fn hash(&self, state: &mut H) { self.0.hash(state); } } @@ -238,10 +242,10 @@ impl std::fmt::Debug for Signature { } } -#[cfg(feature = "std")] -impl std::hash::Hash for Signature { - fn hash(&self, state: &mut H) { - std::hash::Hash::hash(&self.0[..], state); +#[cfg(feature = "full_crypto")] +impl rstd::hash::Hash for Signature { + fn hash(&self, state: &mut H) { + rstd::hash::Hash::hash(&self.0[..], state); } } @@ -255,7 +259,7 @@ impl Signature { } /// Recover the public key from this signature and a message. - #[cfg(feature = "std")] + #[cfg(feature = "full_crypto")] pub fn recover>(&self, message: M) -> Option { let message = secp256k1::Message::parse(&blake2_256(message.as_ref())); let sig: (_, _) = self.try_into().ok()?; @@ -264,7 +268,7 @@ impl Signature { } } -#[cfg(feature = "std")] +#[cfg(feature = "full_crypto")] impl From<(secp256k1::Signature, secp256k1::RecoveryId)> for Signature { fn from(x: (secp256k1::Signature, secp256k1::RecoveryId)) -> Signature { let mut r = Self::default(); @@ -274,7 +278,7 @@ impl From<(secp256k1::Signature, secp256k1::RecoveryId)> for Signature { } } -#[cfg(feature = "std")] +#[cfg(feature = "full_crypto")] impl<'a> TryFrom<&'a Signature> for (secp256k1::Signature, secp256k1::RecoveryId) { type Error = (); fn try_from(x: &'a Signature) -> Result<(secp256k1::Signature, secp256k1::RecoveryId), Self::Error> { @@ -329,7 +333,7 @@ impl TraitPublic for Public { impl Derive for Public {} /// Derive a single hard junction. -#[cfg(feature = "std")] +#[cfg(feature = "full_crypto")] fn derive_hard_junction(secret_seed: &Seed, cc: &[u8; 32]) -> Seed { ("Secp256k1HDKD", secret_seed, cc).using_encoded(|data| { let mut res = [0u8; 32]; @@ -339,13 +343,13 @@ fn derive_hard_junction(secret_seed: &Seed, cc: &[u8; 32]) -> Seed { } /// An error when deriving a key. -#[cfg(feature = "std")] +#[cfg(feature = "full_crypto")] pub enum DeriveError { /// A soft key was found in the path (and is unsupported). SoftKeyInPath, } -#[cfg(feature = "std")] +#[cfg(feature = "full_crypto")] impl TraitPair for Pair { type Public = Public; type Seed = Seed; @@ -355,6 +359,7 @@ impl TraitPair for Pair { /// Generate new secure (random) key pair and provide the recovery phrase. /// /// You can recover the same key later with `from_phrase`. + #[cfg(feature = "std")] fn generate_with_phrase(password: Option<&str>) -> (Pair, String, Seed) { let mnemonic = Mnemonic::new(MnemonicType::Words12, Language::English); let phrase = mnemonic.phrase(); @@ -368,6 +373,7 @@ impl TraitPair for Pair { } /// Generate key pair from given recovery phrase and password. + #[cfg(feature = "std")] fn from_phrase(phrase: &str, password: Option<&str>) -> Result<(Pair, Seed), SecretStringError> { let big_seed = seed_from_entropy( Mnemonic::from_phrase(phrase, Language::English) @@ -454,7 +460,7 @@ impl TraitPair for Pair { } } -#[cfg(feature = "std")] +#[cfg(feature = "full_crypto")] impl Pair { /// Get the seed for this key. pub fn seed(&self) -> Seed { @@ -463,6 +469,7 @@ impl Pair { /// Exactly as `from_string` except that if no matches are found then, the the first 32 /// characters are taken (padded with spaces as necessary) and used as the MiniSecretKey. + #[cfg(feature = "std")] pub fn from_legacy_string(s: &str, password_override: Option<&str>) -> Pair { Self::from_string(s, password_override).unwrap_or_else(|_| { let mut padded_seed: Seed = [' ' as u8; 32]; @@ -474,16 +481,16 @@ impl Pair { } impl CryptoType for Public { - #[cfg(feature="std")] + #[cfg(feature="full_crypto")] type Pair = Pair; } impl CryptoType for Signature { - #[cfg(feature="std")] + #[cfg(feature="full_crypto")] type Pair = Pair; } -#[cfg(feature = "std")] +#[cfg(feature="full_crypto")] impl CryptoType for Pair { type Pair = Pair; } diff --git a/core/primitives/src/ed25519.rs b/core/primitives/src/ed25519.rs index cecb87cd5a76b..0a25b8c806644 100644 --- a/core/primitives/src/ed25519.rs +++ b/core/primitives/src/ed25519.rs @@ -30,7 +30,9 @@ use substrate_bip39::seed_from_entropy; #[cfg(feature = "std")] use bip39::{Mnemonic, Language, MnemonicType}; #[cfg(feature = "full_crypto")] -use crate::crypto::{Pair as TraitPair, DeriveJunction, SecretStringError, Ss58Codec}; +use crate::crypto::{Pair as TraitPair, DeriveJunction, SecretStringError}; +#[cfg(feature = "std")] +use crate::crypto::Ss58Codec; #[cfg(feature = "std")] use serde::{de, Serializer, Serialize, Deserializer, Deserialize}; use crate::{crypto::{Public as TraitPublic, UncheckedFrom, CryptoType, Derive}}; @@ -42,7 +44,7 @@ use crate::{crypto::{Public as TraitPublic, UncheckedFrom, CryptoType, Derive}}; type Seed = [u8; 32]; /// A public key. -#[cfg_attr(feature = "std", derive(Hash))] +#[cfg_attr(feature = "full_crypto", derive(Hash))] #[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Encode, Decode, Default)] pub struct Public(pub [u8; 32]); diff --git a/core/primitives/src/sr25519.rs b/core/primitives/src/sr25519.rs index 5838aa380b601..7bec910d730ca 100644 --- a/core/primitives/src/sr25519.rs +++ b/core/primitives/src/sr25519.rs @@ -31,8 +31,11 @@ use substrate_bip39::mini_secret_from_entropy; use bip39::{Mnemonic, Language, MnemonicType}; #[cfg(feature = "full_crypto")] use crate::crypto::{ - Pair as TraitPair, DeriveJunction, Infallible, SecretStringError, Ss58Codec + Pair as TraitPair, DeriveJunction, Infallible, SecretStringError }; +#[cfg(feature = "std")] +use crate::crypto::Ss58Codec; + use crate::{crypto::{Public as TraitPublic, UncheckedFrom, CryptoType, Derive}}; use crate::hash::{H256, H512}; use codec::{Encode, Decode}; @@ -47,7 +50,7 @@ use schnorrkel::keys::{MINI_SECRET_KEY_LENGTH, SECRET_KEY_LENGTH}; const SIGNING_CTX: &[u8] = b"substrate"; /// An Schnorrkel/Ristretto x25519 ("sr25519") public key. -#[cfg_attr(feature = "std", derive(Hash))] +#[cfg_attr(feature = "full_crypto", derive(Hash))] #[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Encode, Decode, Default)] pub struct Public(pub [u8; 32]); @@ -440,7 +443,7 @@ impl TraitPair for Pair { _ => Err(SecretStringError::InvalidSeedLength) } } - + #[cfg(feature = "std")] fn generate_with_phrase(password: Option<&str>) -> (Pair, String, Seed) { let mnemonic = Mnemonic::new(MnemonicType::Words12, Language::English); let phrase = mnemonic.phrase(); From c6ee7918d0a18b99184bde5fb8ee38564f03ec29 Mon Sep 17 00:00:00 2001 From: Alain Brenzikofer Date: Wed, 30 Oct 2019 11:05:50 +0100 Subject: [PATCH 12/24] remove --release from CI test --- .gitlab-ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index d454af2748718..b8a03764e1fcb 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -169,9 +169,9 @@ test-full-crypto-feature: &test-full-crypto-feature - $DEPLOY_TAG script: - cd core/primitives/ - - time cargo build --release --verbose --no-default-features --features full_crypto + - time cargo build --verbose --no-default-features --features full_crypto - cd ../application-crypto - - time cargo build --release --verbose --no-default-features --features full_crypto + - time cargo build --verbose --no-default-features --features full_crypto - sccache -s From 536d6c3df57c6f4f53f7ecb0b968a541a1735eee Mon Sep 17 00:00:00 2001 From: Alain Brenzikofer Date: Wed, 30 Oct 2019 12:15:44 +0100 Subject: [PATCH 13/24] @bkchr requested changes. moved full_crypto CI test to build stage --- .gitlab-ci.yml | 40 ++++++++++++-------------- core/application-crypto/src/ed25519.rs | 2 +- core/application-crypto/src/sr25519.rs | 2 +- 3 files changed, 21 insertions(+), 23 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index b8a03764e1fcb..6a0525fe71d60 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -156,27 +156,6 @@ test-srml-staking: &test-srml-staking - time cargo test --release --verbose --no-default-features --features std - sccache -s -test-full-crypto-feature: &test-full-crypto-feature - stage: test - <<: *docker-env - variables: - # Enable debug assertions since we are running optimized builds for testing - # but still want to have debug assertions. - RUSTFLAGS: -Cdebug-assertions=y - RUST_BACKTRACE: 1 - except: - variables: - - $DEPLOY_TAG - script: - - cd core/primitives/ - - time cargo build --verbose --no-default-features --features full_crypto - - cd ../application-crypto - - time cargo build --verbose --no-default-features --features full_crypto - - sccache -s - - - - test-linux-stable-int: <<: *test-linux @@ -292,6 +271,25 @@ build-linux-subkey: - cp -r scripts/docker/subkey.Dockerfile ./artifacts/subkey/ - sccache -s +build-full-crypto-feature: + stage: build + <<: *collect-artifacts + <<: *docker-env + <<: *build-only + variables: + # Enable debug assertions since we are running optimized builds for testing + # but still want to have debug assertions. + RUST_BACKTRACE: 1 + except: + variables: + - $DEPLOY_TAG + script: + - cd core/primitives/ + - time cargo build --verbose --no-default-features --features full_crypto + - cd ../application-crypto + - time cargo build --verbose --no-default-features --features full_crypto + - sccache -s + build-rust-doc-release: stage: build <<: *docker-env diff --git a/core/application-crypto/src/ed25519.rs b/core/application-crypto/src/ed25519.rs index a6528b49f6791..99100e40834d6 100644 --- a/core/application-crypto/src/ed25519.rs +++ b/core/application-crypto/src/ed25519.rs @@ -21,7 +21,7 @@ use crate::{RuntimePublic, KeyTypeId}; pub use primitives::ed25519::*; mod app { - use crate::Vec; + use rstd::vec::Vec; use primitives::testing::ED25519; crate::app_crypto!(super, ED25519); diff --git a/core/application-crypto/src/sr25519.rs b/core/application-crypto/src/sr25519.rs index 8103011a3827e..ab5a4e3d0d257 100644 --- a/core/application-crypto/src/sr25519.rs +++ b/core/application-crypto/src/sr25519.rs @@ -21,7 +21,7 @@ use crate::{RuntimePublic, KeyTypeId}; pub use primitives::sr25519::*; mod app { - use crate::Vec; + use rstd::vec::Vec; use primitives::testing::SR25519; crate::app_crypto!(super, SR25519); From 291baeb1aefb483ab1ee9e22ecea7d1dfa95f840 Mon Sep 17 00:00:00 2001 From: Alain Brenzikofer Date: Wed, 30 Oct 2019 13:09:28 +0100 Subject: [PATCH 14/24] fixing no_std issue --- core/primitives/Cargo.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/primitives/Cargo.toml b/core/primitives/Cargo.toml index daecd6ef8055f..dfab3bce278bd 100644 --- a/core/primitives/Cargo.toml +++ b/core/primitives/Cargo.toml @@ -22,7 +22,7 @@ base58 = { version = "0.1.0", optional = true } blake2-rfc = { version = "0.2.18", default-features = false, optional = true } schnorrkel = { version = "0.8.5", features = ["preaudit_deprecated"], default-features = false, optional = true } rand = { version = "0.7.2", optional = true } -sha2 = { version = "0.8.0", optional = true } +sha2 = { version = "0.8.0", default-features = false, optional = true } substrate-bip39 = { version = "0.3.1", optional = true } tiny-bip39 = { version = "0.6.2", optional = true } hex = { version = "0.4", default-features = false, optional = true } @@ -81,7 +81,7 @@ std = [ "serde", "byteorder/std", "rand", - "sha2", + "sha2/std", "schnorrkel/std", "regex", "num-traits/std", From bcc7e32639ad2ab613f292f83796ec50713e3de3 Mon Sep 17 00:00:00 2001 From: Alain Brenzikofer Date: Wed, 30 Oct 2019 13:26:19 +0100 Subject: [PATCH 15/24] CI fresh copy from srml-staking --- .gitlab-ci.yml | 39 ++++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 6a0525fe71d60..114bfd926b646 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -220,6 +220,26 @@ node-exits: script: - ./ci/check_for_exit.sh + +test-full-crypto-feature: &test-full-crypto-feature + stage: test + <<: *docker-env + variables: + # Enable debug assertions since we are running optimized builds for testing + # but still want to have debug assertions. + RUSTFLAGS: -Cdebug-assertions=y + RUST_BACKTRACE: 1 + except: + variables: + - $DEPLOY_TAG + script: + - cd core/primitives/ + - time cargo build --verbose --no-default-features --features full_crypto + - cd ../application-crypto + - time cargo build --verbose --no-default-features --features full_crypto + - sccache -s + + #### stage: build build-linux-substrate: @@ -271,25 +291,6 @@ build-linux-subkey: - cp -r scripts/docker/subkey.Dockerfile ./artifacts/subkey/ - sccache -s -build-full-crypto-feature: - stage: build - <<: *collect-artifacts - <<: *docker-env - <<: *build-only - variables: - # Enable debug assertions since we are running optimized builds for testing - # but still want to have debug assertions. - RUST_BACKTRACE: 1 - except: - variables: - - $DEPLOY_TAG - script: - - cd core/primitives/ - - time cargo build --verbose --no-default-features --features full_crypto - - cd ../application-crypto - - time cargo build --verbose --no-default-features --features full_crypto - - sccache -s - build-rust-doc-release: stage: build <<: *docker-env From a13cf9adcbe21063522d0b47fc2ff8000e61db55 Mon Sep 17 00:00:00 2001 From: Alain Brenzikofer Date: Wed, 30 Oct 2019 14:21:40 +0100 Subject: [PATCH 16/24] gitlab CI with +nightly --- .gitlab-ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 114bfd926b646..137cf64d2f865 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -234,9 +234,9 @@ test-full-crypto-feature: &test-full-crypto-feature - $DEPLOY_TAG script: - cd core/primitives/ - - time cargo build --verbose --no-default-features --features full_crypto + - time cargo +nightly build --verbose --no-default-features --features full_crypto - cd ../application-crypto - - time cargo build --verbose --no-default-features --features full_crypto + - time cargo +nightly build --verbose --no-default-features --features full_crypto - sccache -s From 7f25925a89c18a2bfc786c59c6d91c2f4107e0a4 Mon Sep 17 00:00:00 2001 From: Alain Brenzikofer Date: Thu, 31 Oct 2019 17:34:40 +0100 Subject: [PATCH 17/24] solved no-feature-in-macro dilemma --- core/application-crypto/src/lib.rs | 18 +++++++++--------- core/consensus/aura/Cargo.toml | 8 ++------ core/consensus/aura/primitives/Cargo.toml | 6 ------ core/consensus/babe/primitives/Cargo.toml | 5 ----- core/finality-grandpa/primitives/Cargo.toml | 6 ------ core/sr-primitives/Cargo.toml | 8 +------- srml/im-online/Cargo.toml | 5 ----- 7 files changed, 12 insertions(+), 44 deletions(-) diff --git a/core/application-crypto/src/lib.rs b/core/application-crypto/src/lib.rs index 551ae44a53eb0..bc8855458a0a0 100644 --- a/core/application-crypto/src/lib.rs +++ b/core/application-crypto/src/lib.rs @@ -53,9 +53,9 @@ pub use traits::*; #[macro_export] macro_rules! app_crypto { ($module:ident, $key_type:expr) => { - #[cfg(feature = "full_crypto")] + #[cfg(any(feature = "full_crypto", feature = "std"))] $crate::app_crypto!($module::Pair, $module::Public, $module::Signature, $key_type); - #[cfg(not(feature = "full_crypto"))] + #[cfg(not(any(feature = "full_crypto", feature = "std")))] $crate::app_crypto!($module::Public, $module::Signature, $key_type); }; ($pair:ty, $public:ty, $sig:ty, $key_type:expr) => { @@ -71,7 +71,7 @@ macro_rules! app_crypto { type Pair = Pair; } - #[cfg(feature = "full_crypto")] + #[cfg(any(feature = "full_crypto", feature = "std"))] impl $crate::Pair for Pair { type Public = Public; type Seed = <$pair as $crate::Pair>::Seed; @@ -138,7 +138,7 @@ macro_rules! app_crypto { $crate::codec::Decode, $crate::RuntimeDebug, )] - #[cfg_attr(feature = "full_crypto", derive(Hash))] + #[cfg_attr(any(feature = "full_crypto", feature = "std"), derive(Hash))] pub struct Public($public); } @@ -187,7 +187,7 @@ macro_rules! app_crypto { } impl $crate::CryptoType for Public { - #[cfg(feature = "full_crypto")] + #[cfg(any(feature = "full_crypto", feature = "std"))] type Pair = Pair; } @@ -198,7 +198,7 @@ macro_rules! app_crypto { impl $crate::AppKey for Public { type UntypedGeneric = $public; type Public = Public; - #[cfg(feature = "full_crypto")] + #[cfg(any(feature = "full_crypto", feature = "std"))] type Pair = Pair; type Signature = Signature; const ID: $crate::KeyTypeId = $key_type; @@ -240,7 +240,7 @@ macro_rules! app_crypto { $crate::codec::Decode, $crate::RuntimeDebug, )] - #[cfg_attr(feature = "full_crypto", derive(Hash))] + #[cfg_attr(any(feature = "full_crypto", feature = "std"), derive(Hash))] pub struct Signature($sig); } @@ -256,14 +256,14 @@ macro_rules! app_crypto { } impl $crate::CryptoType for Signature { - #[cfg(feature = "full_crypto")] + #[cfg(any(feature = "full_crypto", feature = "std"))] type Pair = Pair; } impl $crate::AppKey for Signature { type UntypedGeneric = $sig; type Public = Public; - #[cfg(feature = "full_crypto")] + #[cfg(any(feature = "full_crypto", feature = "std"))] type Pair = Pair; type Signature = Signature; const ID: $crate::KeyTypeId = $key_type; diff --git a/core/consensus/aura/Cargo.toml b/core/consensus/aura/Cargo.toml index 5fd434d559a1d..03cb5df030673 100644 --- a/core/consensus/aura/Cargo.toml +++ b/core/consensus/aura/Cargo.toml @@ -39,9 +39,5 @@ env_logger = "0.7.0" tempfile = "3.1.0" [features] -default = ["full_crypto"] - -# This feature enables all crypto primitives for `no_std` builds like microcontrollers -# or Intel SGX. -# For the regular wasm runtime builds this should not be used. -full_crypto = [] \ No newline at end of file +default = ["std"] +std = [] \ No newline at end of file diff --git a/core/consensus/aura/primitives/Cargo.toml b/core/consensus/aura/primitives/Cargo.toml index 774eeb92e4706..7fd3f3d05d9bf 100644 --- a/core/consensus/aura/primitives/Cargo.toml +++ b/core/consensus/aura/primitives/Cargo.toml @@ -15,15 +15,9 @@ sr-primitives = { path = "../../../sr-primitives", default-features = false } [features] default = ["std"] std = [ - "full_crypto", "rstd/std", "codec/std", "sr-primitives/std", "substrate-client/std", "app-crypto/std", ] - -# This feature enables all crypto primitives for `no_std` builds like microcontrollers -# or Intel SGX. -# For the regular wasm runtime builds this should not be used. -full_crypto = [] \ No newline at end of file diff --git a/core/consensus/babe/primitives/Cargo.toml b/core/consensus/babe/primitives/Cargo.toml index c877cec7fac2f..b08185494ba9d 100644 --- a/core/consensus/babe/primitives/Cargo.toml +++ b/core/consensus/babe/primitives/Cargo.toml @@ -17,7 +17,6 @@ codec = { package = "parity-scale-codec", version = "1.0.0", default-features = [features] default = ["std"] std = [ - "full_crypto", "rstd/std", "sr-primitives/std", "substrate-client/std", @@ -27,7 +26,3 @@ std = [ "app-crypto/std", ] -# This feature enables all crypto primitives for `no_std` builds like microcontrollers -# or Intel SGX. -# For the regular wasm runtime builds this should not be used. -full_crypto = [] diff --git a/core/finality-grandpa/primitives/Cargo.toml b/core/finality-grandpa/primitives/Cargo.toml index b3dcc004fddf4..02439d4150d81 100644 --- a/core/finality-grandpa/primitives/Cargo.toml +++ b/core/finality-grandpa/primitives/Cargo.toml @@ -15,7 +15,6 @@ serde = { version = "1.0.101", optional = true, features = ["derive"] } [features] default = ["std"] std = [ - "full_crypto", "client/std", "codec/std", "sr-primitives/std", @@ -23,8 +22,3 @@ std = [ "serde", "app-crypto/std", ] - -# This feature enables all crypto primitives for `no_std` builds like microcontrollers -# or Intel SGX. -# For the regular wasm runtime builds this should not be used. -full_crypto = [] diff --git a/core/sr-primitives/Cargo.toml b/core/sr-primitives/Cargo.toml index 41ff55e3ebc34..bf0dc327e32da 100644 --- a/core/sr-primitives/Cargo.toml +++ b/core/sr-primitives/Cargo.toml @@ -26,7 +26,6 @@ substrate-offchain = { path = "../offchain" } bench = [] default = ["std"] std = [ - "full_crypto", "app-crypto/std", "arithmetic/std", "codec/std", @@ -36,9 +35,4 @@ std = [ "rstd/std", "runtime_io/std", "serde", -] - -# This feature enables all crypto primitives for `no_std` builds like microcontrollers -# or Intel SGX. -# For the regular wasm runtime builds this should not be used. -full_crypto = [] +] \ No newline at end of file diff --git a/srml/im-online/Cargo.toml b/srml/im-online/Cargo.toml index 8c2bc3801c290..ffa31a283a145 100644 --- a/srml/im-online/Cargo.toml +++ b/srml/im-online/Cargo.toml @@ -23,7 +23,6 @@ offchain = { package = "substrate-offchain", path = "../../core/offchain" } [features] default = ["std", "session/historical"] std = [ - "full_crypto", "app-crypto/std", "codec/std", "primitives/std", @@ -37,7 +36,3 @@ std = [ "system/std", ] -# This feature enables all crypto primitives for `no_std` builds like microcontrollers -# or Intel SGX. -# For the regular wasm runtime builds this should not be used. -full_crypto = [] From 104e5ca651bbba4784eae1aa93fcbab04cebf67b Mon Sep 17 00:00:00 2001 From: Alain Brenzikofer Date: Thu, 31 Oct 2019 17:39:15 +0100 Subject: [PATCH 18/24] cosmetics --- core/consensus/aura/Cargo.toml | 2 +- core/consensus/babe/primitives/Cargo.toml | 1 - srml/im-online/Cargo.toml | 1 - 3 files changed, 1 insertion(+), 3 deletions(-) diff --git a/core/consensus/aura/Cargo.toml b/core/consensus/aura/Cargo.toml index 03cb5df030673..27012705a104a 100644 --- a/core/consensus/aura/Cargo.toml +++ b/core/consensus/aura/Cargo.toml @@ -40,4 +40,4 @@ tempfile = "3.1.0" [features] default = ["std"] -std = [] \ No newline at end of file +std = [] diff --git a/core/consensus/babe/primitives/Cargo.toml b/core/consensus/babe/primitives/Cargo.toml index b08185494ba9d..79e817d081028 100644 --- a/core/consensus/babe/primitives/Cargo.toml +++ b/core/consensus/babe/primitives/Cargo.toml @@ -25,4 +25,3 @@ std = [ "slots", "app-crypto/std", ] - diff --git a/srml/im-online/Cargo.toml b/srml/im-online/Cargo.toml index ffa31a283a145..b62cc2a8f9615 100644 --- a/srml/im-online/Cargo.toml +++ b/srml/im-online/Cargo.toml @@ -35,4 +35,3 @@ std = [ "support/std", "system/std", ] - From b579288b709d240e62981bea8d1ef087f5e004fa Mon Sep 17 00:00:00 2001 From: brenzi Date: Thu, 31 Oct 2019 17:48:47 +0100 Subject: [PATCH 19/24] Update core/application-crypto/src/sr25519.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Bastian Köcher --- core/application-crypto/src/sr25519.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/core/application-crypto/src/sr25519.rs b/core/application-crypto/src/sr25519.rs index ab5a4e3d0d257..f6c2388a9bbdf 100644 --- a/core/application-crypto/src/sr25519.rs +++ b/core/application-crypto/src/sr25519.rs @@ -21,7 +21,6 @@ use crate::{RuntimePublic, KeyTypeId}; pub use primitives::sr25519::*; mod app { - use rstd::vec::Vec; use primitives::testing::SR25519; crate::app_crypto!(super, SR25519); From f38ec5fcc6f9ac8d8e0471df622cda6c3d0748c8 Mon Sep 17 00:00:00 2001 From: brenzi Date: Thu, 31 Oct 2019 17:49:01 +0100 Subject: [PATCH 20/24] Update core/application-crypto/src/ed25519.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Bastian Köcher --- core/application-crypto/src/ed25519.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/core/application-crypto/src/ed25519.rs b/core/application-crypto/src/ed25519.rs index 99100e40834d6..bd785acadba46 100644 --- a/core/application-crypto/src/ed25519.rs +++ b/core/application-crypto/src/ed25519.rs @@ -21,7 +21,6 @@ use crate::{RuntimePublic, KeyTypeId}; pub use primitives::ed25519::*; mod app { - use rstd::vec::Vec; use primitives::testing::ED25519; crate::app_crypto!(super, ED25519); From 272a8afce227d45189ba7db1e7d24ff4be32c06f Mon Sep 17 00:00:00 2001 From: Alain Brenzikofer Date: Thu, 31 Oct 2019 19:12:47 +0100 Subject: [PATCH 21/24] even more simple --- core/consensus/aura/Cargo.toml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/core/consensus/aura/Cargo.toml b/core/consensus/aura/Cargo.toml index 27012705a104a..a09d51095018e 100644 --- a/core/consensus/aura/Cargo.toml +++ b/core/consensus/aura/Cargo.toml @@ -37,7 +37,3 @@ test-client = { package = "substrate-test-runtime-client", path = "../../test-ru tokio = "0.1.22" env_logger = "0.7.0" tempfile = "3.1.0" - -[features] -default = ["std"] -std = [] From 42d02708917bedc96225743fe0f38e0bf67f0ea4 Mon Sep 17 00:00:00 2001 From: Alain Brenzikofer Date: Thu, 31 Oct 2019 19:16:12 +0100 Subject: [PATCH 22/24] undo line delete --- core/sr-primitives/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/sr-primitives/Cargo.toml b/core/sr-primitives/Cargo.toml index bf0dc327e32da..d3303014260f4 100644 --- a/core/sr-primitives/Cargo.toml +++ b/core/sr-primitives/Cargo.toml @@ -35,4 +35,4 @@ std = [ "rstd/std", "runtime_io/std", "serde", -] \ No newline at end of file +] From 1c721f291d229b09695f5801738c755230734f04 Mon Sep 17 00:00:00 2001 From: Alain Brenzikofer Date: Sun, 3 Nov 2019 12:44:17 +0100 Subject: [PATCH 23/24] refactoring app_crypto macro. splitting functionalities based on full_crypto feature --- core/application-crypto/src/lib.rs | 184 +++++++++++++++++++++++------ 1 file changed, 148 insertions(+), 36 deletions(-) diff --git a/core/application-crypto/src/lib.rs b/core/application-crypto/src/lib.rs index bc8855458a0a0..66214ce444437 100644 --- a/core/application-crypto/src/lib.rs +++ b/core/application-crypto/src/lib.rs @@ -50,17 +50,43 @@ pub use traits::*; /// // of value `b"fuba"`. /// app_crypto!(ed25519, KeyTypeId(*b"_uba")); /// ``` +#[cfg(feature = "full_crypto")] +#[macro_export] +macro_rules! app_crypto { + ($module:ident, $key_type:expr) => { + $crate::app_crypto_public_full_crypto!($module::Public, $key_type); + $crate::app_crypto_public_common!($module::Public, $module::Signature, $key_type); + $crate::app_crypto_signature_full_crypto!($module::Signature, $key_type); + $crate::app_crypto_signature_common!($module::Signature, $key_type); + $crate::app_crypto_pair!($module::Pair, $key_type); + }; +} + +/// Declares Public, Pair, Signature types which are functionally equivalent to `$pair`, but are new +/// Application-specific types whose identifier is `$key_type`. +/// +/// ```rust +///# use substrate_application_crypto::{app_crypto, wrap, ed25519, KeyTypeId}; +/// // Declare a new set of crypto types using Ed25519 logic that identifies as `KeyTypeId` +/// // of value `b"fuba"`. +/// app_crypto!(ed25519, KeyTypeId(*b"_uba")); +/// ``` +#[cfg(not(feature = "full_crypto"))] #[macro_export] macro_rules! app_crypto { ($module:ident, $key_type:expr) => { - #[cfg(any(feature = "full_crypto", feature = "std"))] - $crate::app_crypto!($module::Pair, $module::Public, $module::Signature, $key_type); - #[cfg(not(any(feature = "full_crypto", feature = "std")))] - $crate::app_crypto!($module::Public, $module::Signature, $key_type); + $crate::app_crypto_public_not_full_crypto!($module::Public, $key_type); + $crate::app_crypto_public_common!($module::Public, $module::Signature, $key_type); + $crate::app_crypto_signature_not_full_crypto!($module::Signature, $key_type); + $crate::app_crypto_signature_common!($module::Signature, $key_type); }; - ($pair:ty, $public:ty, $sig:ty, $key_type:expr) => { - $crate::app_crypto!($public, $sig, $key_type); +} +/// Declares Pair type which is functionally equivalent to `$pair`, but is new +/// Application-specific type whose identifier is `$key_type`. +#[macro_export] +macro_rules! app_crypto_pair { + ($pair:ty, $key_type:expr) => { $crate::wrap!{ /// A generic `AppPublic` wrapper type over $pair crypto; this has no specific App. #[derive(Clone)] @@ -71,7 +97,6 @@ macro_rules! app_crypto { type Pair = Pair; } - #[cfg(any(feature = "full_crypto", feature = "std"))] impl $crate::Pair for Pair { type Public = Public; type Seed = <$pair as $crate::Pair>::Seed; @@ -118,6 +143,7 @@ macro_rules! app_crypto { fn public(&self) -> Self::Public { Public(self.0.public()) } fn to_raw_vec(&self) -> Vec { self.0.to_raw_vec() } } + impl $crate::AppKey for Pair { type UntypedGeneric = $pair; type Public = Public; @@ -125,11 +151,53 @@ macro_rules! app_crypto { type Signature = Signature; const ID: $crate::KeyTypeId = $key_type; } + impl $crate::AppPair for Pair { type Generic = $pair; } }; - ($public:ty, $sig:ty, $key_type:expr) => { +} + +/// Declares Public type which is functionally equivalent to `$public`, but is new +/// Application-specific type whose identifier is `$key_type`. +/// can only be used together with `full_crypto` feature +/// For full functionality, app_crypto_public_common! must be called too. +#[macro_export] +macro_rules! app_crypto_public_full_crypto { + ($public:ty, $key_type:expr) => { + $crate::wrap!{ + /// A generic `AppPublic` wrapper type over $public crypto; this has no specific App. + #[derive( + Clone, Default, Eq, PartialEq, Ord, PartialOrd, + $crate::codec::Encode, + $crate::codec::Decode, + $crate::RuntimeDebug, + )] + #[derive(Hash)] + pub struct Public($public); + } + + impl $crate::CryptoType for Public { + type Pair = Pair; + } + + impl $crate::AppKey for Public { + type UntypedGeneric = $public; + type Public = Public; + type Pair = Pair; + type Signature = Signature; + const ID: $crate::KeyTypeId = $key_type; + } + } +} + +/// Declares Public type which is functionally equivalent to `$public`, but is new +/// Application-specific type whose identifier is `$key_type`. +/// can only be used without `full_crypto` feature +/// For full functionality, app_crypto_public_common! must be called too. +#[macro_export] +macro_rules! app_crypto_public_not_full_crypto { + ($public:ty, $key_type:expr) => { $crate::wrap!{ /// A generic `AppPublic` wrapper type over $public crypto; this has no specific App. #[derive( @@ -138,10 +206,26 @@ macro_rules! app_crypto { $crate::codec::Decode, $crate::RuntimeDebug, )] - #[cfg_attr(any(feature = "full_crypto", feature = "std"), derive(Hash))] pub struct Public($public); } + impl $crate::CryptoType for Public {} + + impl $crate::AppKey for Public { + type UntypedGeneric = $public; + type Public = Public; + type Signature = Signature; + const ID: $crate::KeyTypeId = $key_type; + } + } +} + +/// Declares Public type which is functionally equivalent to `$public`, but is new +/// Application-specific type whose identifier is `$key_type`. +/// For full functionality, app_crypto_public_(not)_full_crypto! must be called too. +#[macro_export] +macro_rules! app_crypto_public_common { + ($public:ty, $sig:ty, $key_type:expr) => { impl $crate::Derive for Public { #[cfg(feature = "std")] fn derive>(&self, @@ -186,24 +270,10 @@ macro_rules! app_crypto { fn as_mut(&mut self) -> &mut [u8] { self.0.as_mut() } } - impl $crate::CryptoType for Public { - #[cfg(any(feature = "full_crypto", feature = "std"))] - type Pair = Pair; - } - impl $crate::Public for Public { fn from_slice(x: &[u8]) -> Self { Self(<$public>::from_slice(x)) } } - impl $crate::AppKey for Public { - type UntypedGeneric = $public; - type Public = Public; - #[cfg(any(feature = "full_crypto", feature = "std"))] - type Pair = Pair; - type Signature = Signature; - const ID: $crate::KeyTypeId = $key_type; - } - impl $crate::AppPublic for Public { type Generic = $public; } @@ -232,7 +302,16 @@ macro_rules! app_crypto { <$public as $crate::RuntimePublic>::verify(self.as_ref(), msg, &signature.as_ref()) } } + } +} +/// Declares Signature type which is functionally equivalent to `$sig`, but is new +/// Application-specific type whose identifier is `$key_type`. +/// can only be used together with `full_crypto` feature +/// For full functionality, app_crypto_public_common! must be called too. +#[macro_export] +macro_rules! app_crypto_signature_full_crypto { + ($sig:ty, $key_type:expr) => { $crate::wrap! { /// A generic `AppPublic` wrapper type over $public crypto; this has no specific App. #[derive(Clone, Default, Eq, PartialEq, @@ -240,34 +319,67 @@ macro_rules! app_crypto { $crate::codec::Decode, $crate::RuntimeDebug, )] - #[cfg_attr(any(feature = "full_crypto", feature = "std"), derive(Hash))] - + #[derive(Hash)] pub struct Signature($sig); } - impl $crate::Deref for Signature { - type Target = [u8]; - - fn deref(&self) -> &Self::Target { self.0.as_ref() } + impl $crate::CryptoType for Signature { + type Pair = Pair; } - impl AsRef<[u8]> for Signature { - fn as_ref(&self) -> &[u8] { self.0.as_ref() } + impl $crate::AppKey for Signature { + type UntypedGeneric = $sig; + type Public = Public; + type Pair = Pair; + type Signature = Signature; + const ID: $crate::KeyTypeId = $key_type; } + } +} - impl $crate::CryptoType for Signature { - #[cfg(any(feature = "full_crypto", feature = "std"))] - type Pair = Pair; +/// Declares Signature type which is functionally equivalent to `$sig`, but is new +/// Application-specific type whose identifier is `$key_type`. +/// can only be used without `full_crypto` feature +/// For full functionality, app_crypto_public_common! must be called too. +#[macro_export] +macro_rules! app_crypto_signature_not_full_crypto { + ($sig:ty, $key_type:expr) => { + $crate::wrap! { + /// A generic `AppPublic` wrapper type over $public crypto; this has no specific App. + #[derive(Clone, Default, Eq, PartialEq, + $crate::codec::Encode, + $crate::codec::Decode, + $crate::RuntimeDebug, + )] + pub struct Signature($sig); } + + impl $crate::CryptoType for Signature {} impl $crate::AppKey for Signature { type UntypedGeneric = $sig; type Public = Public; - #[cfg(any(feature = "full_crypto", feature = "std"))] - type Pair = Pair; type Signature = Signature; const ID: $crate::KeyTypeId = $key_type; } + } +} + +/// Declares Signature type which is functionally equivalent to `$sig`, but is new +/// Application-specific type whose identifier is `$key_type`. +/// For full functionality, app_crypto_public_(not)_full_crypto! must be called too. +#[macro_export] +macro_rules! app_crypto_signature_common { + ($sig:ty, $key_type:expr) => { + impl $crate::Deref for Signature { + type Target = [u8]; + + fn deref(&self) -> &Self::Target { self.0.as_ref() } + } + + impl AsRef<[u8]> for Signature { + fn as_ref(&self) -> &[u8] { self.0.as_ref() } + } impl $crate::AppSignature for Signature { type Generic = $sig; From d36abf5d47f7c4fa55edb247132f42fcfbbcf45c Mon Sep 17 00:00:00 2001 From: Alain Brenzikofer Date: Mon, 4 Nov 2019 09:49:43 +0100 Subject: [PATCH 24/24] whitespace cosmetics --- core/primitives/src/crypto.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/primitives/src/crypto.rs b/core/primitives/src/crypto.rs index 08c0a89af7842..ad883c1dc3d86 100644 --- a/core/primitives/src/crypto.rs +++ b/core/primitives/src/crypto.rs @@ -771,7 +771,7 @@ pub trait Pair: CryptoType + Sized + Clone + Send + Sync + 'static { /// /// `None` is returned if no matches are found. #[cfg(feature = "std")] - fn from_string_with_seed(s: &str, password_override: Option<&str>) + fn from_string_with_seed(s: &str, password_override: Option<&str>) -> Result<(Self, Option), SecretStringError> { let re = Regex::new(r"^(?P[\d\w ]+)?(?P(//?[^/]+)*)(///(?P.*))?$")