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

Commit 03f744d

Browse files
committed
Implement Copy and Deref to (ed25519|sr25519)::Public
1 parent d8a3640 commit 03f744d

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

core/primitives/src/ed25519.rs

+10-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::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

core/primitives/src/sr25519.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ use crate::crypto::{
3636
use crate::{crypto::{Public as TraitPublic, UncheckedFrom, CryptoType, Derive}};
3737
use crate::hash::{H256, H512};
3838
use codec::{Encode, Decode};
39+
use core::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

0 commit comments

Comments
 (0)