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

Commit 8c542ac

Browse files
committed
Implement Copy, Deref and FromStr to (ed25519|sr25519)::Public
1 parent d8a3640 commit 8c542ac

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

core/primitives/src/ed25519.rs

+19-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ use crate::crypto::{Pair as TraitPair, DeriveJunction, SecretStringError, Ss58Co
3333
#[cfg(feature = "std")]
3434
use serde::{de, Serializer, Serialize, Deserializer, Deserialize};
3535
use crate::{crypto::{Public as TraitPublic, UncheckedFrom, CryptoType, Derive}};
36+
use core::{str::FromStr, ops::Deref};
3637

3738
/// A secret seed. It's not called a "secret key" because ring doesn't expose the secret keys
3839
/// of the key pair (yeah, dumb); as such we're forced to remember the seed manually if we
@@ -42,7 +43,7 @@ type Seed = [u8; 32];
4243

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

4849
impl AsRef<[u8; 32]> for Public {
@@ -63,6 +64,14 @@ impl AsMut<[u8]> for Public {
6364
}
6465
}
6566

67+
impl Deref for Public {
68+
type Target = [u8];
69+
70+
fn deref(&self) -> &Self::Target {
71+
&self.0
72+
}
73+
}
74+
6675
impl rstd::convert::TryFrom<&[u8]> for Public {
6776
type Error = ();
6877

@@ -96,6 +105,15 @@ impl From<Public> for H256 {
96105
}
97106
}
98107

108+
#[cfg(feature = "std")]
109+
impl FromStr for Public {
110+
type Err = crate::crypto::PublicError;
111+
112+
fn from_str(s: &str) -> Result<Self, Self::Err> {
113+
Self::from_ss58check(s)
114+
}
115+
}
116+
99117
impl UncheckedFrom<[u8; 32]> for Public {
100118
fn unchecked_from(x: [u8; 32]) -> Self {
101119
Public::from_raw(x)

core/primitives/src/sr25519.rs

+20-2
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,12 @@ use substrate_bip39::mini_secret_from_entropy;
3131
use bip39::{Mnemonic, Language, MnemonicType};
3232
#[cfg(feature = "std")]
3333
use crate::crypto::{
34-
Pair as TraitPair, DeriveJunction, Infallible, SecretStringError, Ss58Codec
34+
Pair as TraitPair, DeriveJunction, Infallible, SecretStringError, Ss58Codec, PublicError
3535
};
3636
use crate::{crypto::{Public as TraitPublic, UncheckedFrom, CryptoType, Derive}};
3737
use crate::hash::{H256, H512};
3838
use codec::{Encode, Decode};
39+
use core::{str::FromStr, ops::Deref};
3940

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

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

5455
impl AsRef<[u8; 32]> for Public {
@@ -69,6 +70,14 @@ impl AsMut<[u8]> for Public {
6970
}
7071
}
7172

73+
impl Deref for Public {
74+
type Target = [u8];
75+
76+
fn deref(&self) -> &Self::Target {
77+
&self.0
78+
}
79+
}
80+
7281
impl From<Public> for [u8; 32] {
7382
fn from(x: Public) -> [u8; 32] {
7483
x.0
@@ -81,6 +90,15 @@ impl From<Public> for H256 {
8190
}
8291
}
8392

93+
#[cfg(feature = "std")]
94+
impl FromStr for Public {
95+
type Err = PublicError;
96+
97+
fn from_str(s: &str) -> Result<Self, Self::Err> {
98+
Self::from_ss58check(s)
99+
}
100+
}
101+
84102
impl rstd::convert::TryFrom<&[u8]> for Public {
85103
type Error = ();
86104

0 commit comments

Comments
 (0)