Refactor crypto-related traits implementations in term of Public/Signature Bytes (#3806)

Another simple refactory to prune some duplicate code

Follow up of: https://github.com/paritytech/polkadot-sdk/pull/3684
This commit is contained in:
Davide Galassi
2024-03-25 15:26:38 +00:00
committed by GitHub
parent ce7613a49f
commit 9d122401f1
11 changed files with 254 additions and 658 deletions
+31 -84
View File
@@ -485,8 +485,11 @@ pub trait ByteArray: AsRef<[u8]> + AsMut<[u8]> + for<'a> TryFrom<&'a [u8], Error
}
}
/// Trait suitable for typical cryptographic key public type.
pub trait Public: CryptoType + ByteArray + Derive + PartialEq + Eq + Clone + Send + Sync {}
/// Trait suitable for cryptographic public keys.
pub trait Public: CryptoType + ByteArray + PartialEq + Eq + Clone + Send + Sync + Derive {}
/// Trait suitable for cryptographic signatures.
pub trait Signature: CryptoType + ByteArray + PartialEq + Eq + Clone + Send + Sync {}
/// An opaque 32-byte cryptographic identifier.
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Encode, Decode, MaxEncodedLen, TypeInfo)]
@@ -648,32 +651,11 @@ pub use self::dummy::*;
mod dummy {
use super::*;
#[doc(hidden)]
pub struct DummyTag;
/// Dummy cryptography. Doesn't do anything.
#[derive(Clone, Hash, Default, Eq, PartialEq)]
pub struct Dummy;
impl AsRef<[u8]> for Dummy {
fn as_ref(&self) -> &[u8] {
&b""[..]
}
}
impl AsMut<[u8]> for Dummy {
fn as_mut(&mut self) -> &mut [u8] {
unsafe {
#[allow(mutable_transmutes)]
sp_std::mem::transmute::<_, &'static mut [u8]>(&b""[..])
}
}
}
impl<'a> TryFrom<&'a [u8]> for Dummy {
type Error = ();
fn try_from(_: &'a [u8]) -> Result<Self, ()> {
Ok(Self)
}
}
pub type Dummy = CryptoBytes<0, DummyTag>;
impl CryptoType for Dummy {
type Pair = Dummy;
@@ -681,21 +663,10 @@ mod dummy {
impl Derive for Dummy {}
impl ByteArray for Dummy {
const LEN: usize = 0;
fn from_slice(_: &[u8]) -> Result<Self, ()> {
Ok(Self)
}
#[cfg(feature = "std")]
fn to_raw_vec(&self) -> Vec<u8> {
vec![]
}
fn as_slice(&self) -> &[u8] {
b""
}
}
impl Public for Dummy {}
impl Signature for Dummy {}
impl Pair for Dummy {
type Public = Dummy;
type Seed = Dummy;
@@ -716,15 +687,15 @@ mod dummy {
_: Iter,
_: Option<Dummy>,
) -> Result<(Self, Option<Dummy>), DeriveError> {
Ok((Self, None))
Ok((Self::default(), None))
}
fn from_seed_slice(_: &[u8]) -> Result<Self, SecretStringError> {
Ok(Self)
Ok(Self::default())
}
fn sign(&self, _: &[u8]) -> Self::Signature {
Self
Self::default()
}
fn verify<M: AsRef<[u8]>>(_: &Self::Signature, _: M, _: &Self::Public) -> bool {
@@ -732,11 +703,11 @@ mod dummy {
}
fn public(&self) -> Self::Public {
Self
Self::default()
}
fn to_raw_vec(&self) -> Vec<u8> {
vec![]
Default::default()
}
}
}
@@ -845,7 +816,7 @@ pub trait Pair: CryptoType + Sized {
/// The type used to represent a signature. Can be created from a key pair and a message
/// and verified with the message and a public key.
type Signature: AsRef<[u8]>;
type Signature: Signature;
/// Generate new secure (random) key pair.
///
@@ -1218,6 +1189,8 @@ mod tests {
use super::*;
use crate::DeriveJunction;
struct TestCryptoTag;
#[derive(Clone, Eq, PartialEq, Debug)]
enum TestPair {
Generated,
@@ -1226,59 +1199,33 @@ mod tests {
Standard { phrase: String, password: Option<String>, path: Vec<DeriveJunction> },
Seed(Vec<u8>),
}
impl Default for TestPair {
fn default() -> Self {
TestPair::Generated
}
}
impl CryptoType for TestPair {
type Pair = Self;
}
#[derive(Clone, PartialEq, Eq, Hash, Default)]
struct TestPublic;
impl AsRef<[u8]> for TestPublic {
fn as_ref(&self) -> &[u8] {
&[]
}
}
impl AsMut<[u8]> for TestPublic {
fn as_mut(&mut self) -> &mut [u8] {
&mut []
}
}
impl<'a> TryFrom<&'a [u8]> for TestPublic {
type Error = ();
type TestPublic = PublicBytes<0, TestCryptoTag>;
fn try_from(data: &'a [u8]) -> Result<Self, ()> {
Self::from_slice(data)
}
}
impl CryptoType for TestPublic {
type Pair = TestPair;
}
impl Derive for TestPublic {}
impl ByteArray for TestPublic {
const LEN: usize = 0;
fn from_slice(bytes: &[u8]) -> Result<Self, ()> {
if bytes.is_empty() {
Ok(Self)
} else {
Err(())
}
}
fn as_slice(&self) -> &[u8] {
&[]
}
fn to_raw_vec(&self) -> Vec<u8> {
vec![]
}
type TestSignature = SignatureBytes<0, TestCryptoTag>;
impl CryptoType for TestSignature {
type Pair = TestPair;
}
impl Public for TestPublic {}
impl Pair for TestPair {
type Public = TestPublic;
type Seed = [u8; 8];
type Signature = [u8; 0];
type Signature = TestSignature;
fn generate() -> (Self, <Self as Pair>::Seed) {
(TestPair::Generated, [0u8; 8])
@@ -1327,7 +1274,7 @@ mod tests {
}
fn sign(&self, _message: &[u8]) -> Self::Signature {
[]
TestSignature::default()
}
fn verify<M: AsRef<[u8]>>(_: &Self::Signature, _: M, _: &Self::Public) -> bool {
@@ -1335,7 +1282,7 @@ mod tests {
}
fn public(&self) -> Self::Public {
TestPublic
TestPublic::default()
}
fn from_seed_slice(seed: &[u8]) -> Result<Self, SecretStringError> {