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

Commit

Permalink
added full_crypto for ecdsa. now builds wit h--no-deafault-features -…
Browse files Browse the repository at this point in the history
…-features with_crypto
  • Loading branch information
Alain Brenzikofer committed Oct 30, 2019
1 parent aa96e0f commit f83bfe3
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 30 deletions.
4 changes: 3 additions & 1 deletion core/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -99,6 +99,8 @@ full_crypto = [
"ed25519-dalek",
"blake2-rfc",
"schnorrkel",
"libsecp256k1",
"hex",
"sha2",
"twox-hash"
]
3 changes: 3 additions & 0 deletions core/primitives/src/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, PublicError> {
Self::from_ss58check_with_version(s)
.and_then(|(r, v)| match v {
Expand All @@ -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();
Expand Down Expand Up @@ -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, SecretStringError> {
Self::from_string_with_seed(s, password_override).map(|x| x.0)
}
Expand Down
55 changes: 31 additions & 24 deletions core/primitives/src/ecdsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -117,7 +121,7 @@ impl From<Public> for [u8; 33] {
}
}

#[cfg(feature = "std")]
#[cfg(feature = "full_crypto")]
impl From<Pair> for Public {
fn from(x: Pair) -> Self {
x.public()
Expand Down Expand Up @@ -160,9 +164,9 @@ impl<'de> Deserialize<'de> for Public {
}
}

#[cfg(feature = "std")]
impl std::hash::Hash for Public {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
#[cfg(feature = "full_crypto")]
impl rstd::hash::Hash for Public {
fn hash<H: rstd::hash::Hasher>(&self, state: &mut H) {
self.0.hash(state);
}
}
Expand Down Expand Up @@ -238,10 +242,10 @@ impl std::fmt::Debug for Signature {
}
}

#[cfg(feature = "std")]
impl std::hash::Hash for Signature {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
std::hash::Hash::hash(&self.0[..], state);
#[cfg(feature = "full_crypto")]
impl rstd::hash::Hash for Signature {
fn hash<H: rstd::hash::Hasher>(&self, state: &mut H) {
rstd::hash::Hash::hash(&self.0[..], state);
}
}

Expand All @@ -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<M: AsRef<[u8]>>(&self, message: M) -> Option<Public> {
let message = secp256k1::Message::parse(&blake2_256(message.as_ref()));
let sig: (_, _) = self.try_into().ok()?;
Expand All @@ -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();
Expand All @@ -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> {
Expand Down Expand Up @@ -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];
Expand All @@ -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;
Expand All @@ -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();
Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -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];
Expand All @@ -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;
}
Expand Down
6 changes: 4 additions & 2 deletions core/primitives/src/ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}};
Expand All @@ -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]);

Expand Down
9 changes: 6 additions & 3 deletions core/primitives/src/sr25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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]);

Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit f83bfe3

Please sign in to comment.