From 64a18c6fd26124a8caecd281720ff6dad1368a7f Mon Sep 17 00:00:00 2001 From: Caio Date: Wed, 13 Nov 2019 10:35:03 -0300 Subject: [PATCH] Implement Copy, Deref and FromStr to (ed25519|sr25519)::Public --- core/primitives/src/ed25519.rs | 20 +++++++++++++++++++- core/primitives/src/sr25519.rs | 20 +++++++++++++++++++- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/core/primitives/src/ed25519.rs b/core/primitives/src/ed25519.rs index c0894b8782d49..2b79f92d80cbf 100644 --- a/core/primitives/src/ed25519.rs +++ b/core/primitives/src/ed25519.rs @@ -38,6 +38,7 @@ use crate::crypto::Ss58Codec; use serde::{de, Serializer, Serialize, Deserializer, Deserialize}; use crate::{crypto::{Public as TraitPublic, UncheckedFrom, CryptoType, Derive}}; use runtime_interface::pass_by::PassByInner; +use rstd::ops::Deref; /// 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 @@ -47,7 +48,7 @@ type Seed = [u8; 32]; /// A public key. #[cfg_attr(feature = "full_crypto", derive(Hash))] -#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Encode, Decode, Default, PassByInner)] +#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Encode, Decode, Default, PassByInner)] pub struct Public(pub [u8; 32]); /// A key pair. @@ -83,6 +84,14 @@ impl AsMut<[u8]> for Public { } } +impl Deref for Public { + type Target = [u8]; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + impl rstd::convert::TryFrom<&[u8]> for Public { type Error = (); @@ -116,6 +125,15 @@ impl From for H256 { } } +#[cfg(feature = "std")] +impl std::str::FromStr for Public { + type Err = crate::crypto::PublicError; + + fn from_str(s: &str) -> Result { + Self::from_ss58check(s) + } +} + impl UncheckedFrom<[u8; 32]> for Public { fn unchecked_from(x: [u8; 32]) -> Self { Public::from_raw(x) diff --git a/core/primitives/src/sr25519.rs b/core/primitives/src/sr25519.rs index eed2b70830889..668bf1f0b42ee 100644 --- a/core/primitives/src/sr25519.rs +++ b/core/primitives/src/sr25519.rs @@ -40,6 +40,7 @@ use crate::crypto::Ss58Codec; use crate::{crypto::{Public as TraitPublic, UncheckedFrom, CryptoType, Derive}}; use crate::hash::{H256, H512}; use codec::{Encode, Decode}; +use rstd::ops::Deref; #[cfg(feature = "std")] use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; @@ -53,7 +54,7 @@ const SIGNING_CTX: &[u8] = b"substrate"; /// An Schnorrkel/Ristretto x25519 ("sr25519") public key. #[cfg_attr(feature = "full_crypto", derive(Hash))] -#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Encode, Decode, Default, PassByInner)] +#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Encode, Decode, Default, PassByInner)] pub struct Public(pub [u8; 32]); /// An Schnorrkel/Ristretto x25519 ("sr25519") key pair. @@ -89,6 +90,14 @@ impl AsMut<[u8]> for Public { } } +impl Deref for Public { + type Target = [u8]; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + impl From for [u8; 32] { fn from(x: Public) -> [u8; 32] { x.0 @@ -101,6 +110,15 @@ impl From for H256 { } } +#[cfg(feature = "std")] +impl std::str::FromStr for Public { + type Err = crate::crypto::PublicError; + + fn from_str(s: &str) -> Result { + Self::from_ss58check(s) + } +} + impl rstd::convert::TryFrom<&[u8]> for Public { type Error = ();