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

Commit

Permalink
Implement Copy, Deref and FromStr to (ed25519|sr25519)::Public
Browse files Browse the repository at this point in the history
  • Loading branch information
c410-f3r committed Nov 1, 2019
1 parent d8a3640 commit dee188a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
20 changes: 19 additions & 1 deletion core/primitives/src/ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use crate::crypto::{Pair as TraitPair, DeriveJunction, SecretStringError, Ss58Co
#[cfg(feature = "std")]
use serde::{de, Serializer, Serialize, Deserializer, Deserialize};
use crate::{crypto::{Public as TraitPublic, UncheckedFrom, CryptoType, Derive}};
use rstd::{str::FromStr, 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
Expand All @@ -42,7 +43,7 @@ type Seed = [u8; 32];

/// A public key.
#[cfg_attr(feature = "std", derive(Hash))]
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Encode, Decode, Default)]
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Encode, Decode, Default)]
pub struct Public(pub [u8; 32]);

impl AsRef<[u8; 32]> for Public {
Expand All @@ -63,6 +64,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 = ();

Expand Down Expand Up @@ -96,6 +105,15 @@ impl From<Public> for H256 {
}
}

#[cfg(feature = "std")]
impl FromStr for Public {
type Err = crate::crypto::PublicError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Self::from_ss58check(s)
}
}

impl UncheckedFrom<[u8; 32]> for Public {
fn unchecked_from(x: [u8; 32]) -> Self {
Public::from_raw(x)
Expand Down
22 changes: 20 additions & 2 deletions core/primitives/src/sr25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ use substrate_bip39::mini_secret_from_entropy;
use bip39::{Mnemonic, Language, MnemonicType};
#[cfg(feature = "std")]
use crate::crypto::{
Pair as TraitPair, DeriveJunction, Infallible, SecretStringError, Ss58Codec
Pair as TraitPair, DeriveJunction, Infallible, SecretStringError, Ss58Codec, PublicError
};
use crate::{crypto::{Public as TraitPublic, UncheckedFrom, CryptoType, Derive}};
use crate::hash::{H256, H512};
use codec::{Encode, Decode};
use rstd::{str::FromStr, ops::Deref};

#[cfg(feature = "std")]
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
Expand All @@ -48,7 +49,7 @@ const SIGNING_CTX: &[u8] = b"substrate";

/// An Schnorrkel/Ristretto x25519 ("sr25519") public key.
#[cfg_attr(feature = "std", derive(Hash))]
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Encode, Decode, Default)]
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Encode, Decode, Default)]
pub struct Public(pub [u8; 32]);

impl AsRef<[u8; 32]> for Public {
Expand All @@ -69,6 +70,14 @@ impl AsMut<[u8]> for Public {
}
}

impl Deref for Public {
type Target = [u8];

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl From<Public> for [u8; 32] {
fn from(x: Public) -> [u8; 32] {
x.0
Expand All @@ -81,6 +90,15 @@ impl From<Public> for H256 {
}
}

#[cfg(feature = "std")]
impl FromStr for Public {
type Err = PublicError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Self::from_ss58check(s)
}
}

impl rstd::convert::TryFrom<&[u8]> for Public {
type Error = ();

Expand Down

0 comments on commit dee188a

Please sign in to comment.