Convert Public to CryptoTypePublicPair (#6014)

* Tabify code

* Implement CryptoTypePublicPair in app_crypto

* Cleanup redundancy

* Introduce to_public_crypto_pair to Public trait

* Implement method for test
This commit is contained in:
Rakan Alhneiti
2020-05-13 19:44:54 +02:00
committed by GitHub
parent daf8cf0600
commit 6bdfae2bcb
8 changed files with 52 additions and 50 deletions
@@ -23,27 +23,13 @@ use sp_std::vec::Vec;
pub use sp_core::ed25519::*;
mod app {
use sp_core::crypto::{CryptoTypePublicPair, Public as TraitPublic};
use sp_core::testing::ED25519;
use sp_core::ed25519::CRYPTO_ID;
crate::app_crypto!(super, ED25519);
impl crate::traits::BoundToRuntimeAppPublic for Public {
type Public = Self;
}
impl From<Public> for CryptoTypePublicPair {
fn from(key: Public) -> Self {
(&key).into()
}
}
impl From<&Public> for CryptoTypePublicPair {
fn from(key: &Public) -> Self {
CryptoTypePublicPair(CRYPTO_ID, key.to_raw_vec())
}
}
}
pub use app::{Public as AppPublic, Signature as AppSignature};
@@ -262,6 +262,10 @@ macro_rules! app_crypto_public_common {
impl $crate::Public for Public {
fn from_slice(x: &[u8]) -> Self { Self(<$public>::from_slice(x)) }
fn to_public_crypto_pair(&self) -> $crate::CryptoTypePublicPair {
$crate::CryptoTypePublicPair($crypto_type, self.to_raw_vec())
}
}
impl $crate::AppPublic for Public {
@@ -298,6 +302,21 @@ macro_rules! app_crypto_public_common {
<$public as $crate::RuntimePublic>::to_raw_vec(&self.0)
}
}
impl From<Public> for $crate::CryptoTypePublicPair {
fn from(key: Public) -> Self {
(&key).into()
}
}
impl From<&Public> for $crate::CryptoTypePublicPair {
fn from(key: &Public) -> Self {
$crate::CryptoTypePublicPair(
$crypto_type,
$crate::Public::to_raw_vec(key),
)
}
}
}
}
@@ -23,27 +23,13 @@ use sp_std::vec::Vec;
pub use sp_core::sr25519::*;
mod app {
use sp_core::crypto::{CryptoTypePublicPair, Public as TraitPublic};
use sp_core::testing::SR25519;
use sp_core::sr25519::CRYPTO_ID;
crate::app_crypto!(super, SR25519);
impl crate::traits::BoundToRuntimeAppPublic for Public {
type Public = Self;
}
impl From<Public> for CryptoTypePublicPair {
fn from(key: Public) -> Self {
(&key).into()
}
}
impl From<&Public> for CryptoTypePublicPair {
fn from(key: &Public) -> Self {
CryptoTypePublicPair(CRYPTO_ID, key.to_raw_vec())
}
}
}
pub use app::{Public as AppPublic, Signature as AppSignature};
@@ -22,24 +22,11 @@ use sp_std::vec::Vec;
mod app {
use sp_application_crypto::{
CryptoTypePublicPair,
key_types::AUTHORITY_DISCOVERY,
Public as _,
app_crypto,
sr25519};
sr25519,
};
app_crypto!(sr25519, AUTHORITY_DISCOVERY);
impl From<Public> for CryptoTypePublicPair {
fn from(key: Public) -> Self {
(&key).into()
}
}
impl From<&Public> for CryptoTypePublicPair {
fn from(key: &Public) -> Self {
CryptoTypePublicPair(sr25519::CRYPTO_ID, key.to_raw_vec())
}
}
}
sp_application_crypto::with_pair! {
+12
View File
@@ -559,6 +559,8 @@ pub trait Public:
/// Return a slice filled with raw data.
fn as_slice(&self) -> &[u8] { self.as_ref() }
/// Return `CryptoTypePublicPair` from public key.
fn to_public_crypto_pair(&self) -> CryptoTypePublicPair;
}
/// An opaque 32-byte cryptographic identifier.
@@ -706,6 +708,11 @@ mod dummy {
#[cfg(feature = "std")]
fn to_raw_vec(&self) -> Vec<u8> { vec![] }
fn as_slice(&self) -> &[u8] { b"" }
fn to_public_crypto_pair(&self) -> CryptoTypePublicPair {
CryptoTypePublicPair(
CryptoTypeId(*b"dumm"), Public::to_raw_vec(self)
)
}
}
impl Pair for Dummy {
@@ -1061,6 +1068,11 @@ mod tests {
fn to_raw_vec(&self) -> Vec<u8> {
vec![]
}
fn to_public_crypto_pair(&self) -> CryptoTypePublicPair {
CryptoTypePublicPair(
CryptoTypeId(*b"dumm"), self.to_raw_vec(),
)
}
}
impl Pair for TestPair {
type Public = TestPublic;
+5 -1
View File
@@ -36,7 +36,7 @@ use crate::{hashing::blake2_256, crypto::{Pair as TraitPair, DeriveJunction, Sec
use crate::crypto::Ss58Codec;
#[cfg(feature = "std")]
use serde::{de, Serializer, Serialize, Deserializer, Deserialize};
use crate::crypto::{Public as TraitPublic, UncheckedFrom, CryptoType, Derive, CryptoTypeId};
use crate::crypto::{Public as TraitPublic, CryptoTypePublicPair, UncheckedFrom, CryptoType, Derive, CryptoTypeId};
#[cfg(feature = "full_crypto")]
use secp256k1::{PublicKey, SecretKey};
@@ -118,6 +118,10 @@ impl TraitPublic for Public {
r.copy_from_slice(data);
Self(r)
}
fn to_public_crypto_pair(&self) -> CryptoTypePublicPair {
CryptoTypePublicPair(CRYPTO_ID, self.to_raw_vec())
}
}
impl Derive for Public {}
+10 -6
View File
@@ -377,20 +377,24 @@ impl TraitPublic for Public {
r.copy_from_slice(data);
Public(r)
}
fn to_public_crypto_pair(&self) -> CryptoTypePublicPair {
CryptoTypePublicPair(CRYPTO_ID, self.to_raw_vec())
}
}
impl Derive for Public {}
impl From<Public> for CryptoTypePublicPair {
fn from(key: Public) -> Self {
(&key).into()
}
fn from(key: Public) -> Self {
(&key).into()
}
}
impl From<&Public> for CryptoTypePublicPair {
fn from(key: &Public) -> Self {
CryptoTypePublicPair(CRYPTO_ID, key.to_raw_vec())
}
fn from(key: &Public) -> Self {
CryptoTypePublicPair(CRYPTO_ID, key.to_raw_vec())
}
}
/// Derive a single hard junction.
+4
View File
@@ -391,6 +391,10 @@ impl TraitPublic for Public {
r.copy_from_slice(data);
Public(r)
}
fn to_public_crypto_pair(&self) -> CryptoTypePublicPair {
CryptoTypePublicPair(CRYPTO_ID, self.to_raw_vec())
}
}
impl From<Public> for CryptoTypePublicPair {