Skip to content

Commit

Permalink
Use derive macros for AsRef and AsMut (MystenLabs#9834)
Browse files Browse the repository at this point in the history
## Description 

Use derive macros for AsRef and AsMut for the EdDSA and ECDSA
signatures.

## Test Plan 

Unit tests.
  • Loading branch information
jonas-lj authored Mar 27, 2023
1 parent 837f1fd commit af1fb80
Showing 1 changed file with 10 additions and 40 deletions.
50 changes: 10 additions & 40 deletions crates/sui-types/src/crypto.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0
use anyhow::{anyhow, Error};
use derive_more::From;
use derive_more::{AsMut, AsRef, From};
use eyre::eyre;
use fastcrypto::bls12381::min_sig::{
BLS12381AggregateSignature, BLS12381AggregateSignatureAsBytes, BLS12381KeyPair,
Expand Down Expand Up @@ -785,7 +785,9 @@ impl SuiPublicKey for BLS12381PublicKey {
//

#[serde_as]
#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq, Hash)]
#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq, Hash, AsRef, AsMut)]
#[as_ref(forward)]
#[as_mut(forward)]
pub struct Ed25519SuiSignature(
#[schemars(with = "Base64")]
#[serde_as(as = "Readable<Base64, Bytes>")]
Expand All @@ -810,18 +812,6 @@ impl SuiPublicKey for Ed25519PublicKey {
const SIGNATURE_SCHEME: SignatureScheme = SignatureScheme::ED25519;
}

impl AsRef<[u8]> for Ed25519SuiSignature {
fn as_ref(&self) -> &[u8] {
self.0.as_ref()
}
}

impl AsMut<[u8]> for Ed25519SuiSignature {
fn as_mut(&mut self) -> &mut [u8] {
self.0.as_mut()
}
}

impl ToFromBytes for Ed25519SuiSignature {
fn from_bytes(bytes: &[u8]) -> Result<Self, FastCryptoError> {
if bytes.len() != Self::LENGTH {
Expand All @@ -843,7 +833,9 @@ impl Signer<Signature> for Ed25519KeyPair {
// Secp256k1 Sui Signature port
//
#[serde_as]
#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq, Hash)]
#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq, Hash, AsRef, AsMut)]
#[as_ref(forward)]
#[as_mut(forward)]
pub struct Secp256k1SuiSignature(
#[schemars(with = "Base64")]
#[serde_as(as = "Readable<Base64, Bytes>")]
Expand All @@ -861,18 +853,6 @@ impl SuiPublicKey for Secp256k1PublicKey {
const SIGNATURE_SCHEME: SignatureScheme = SignatureScheme::Secp256k1;
}

impl AsRef<[u8]> for Secp256k1SuiSignature {
fn as_ref(&self) -> &[u8] {
self.0.as_ref()
}
}

impl AsMut<[u8]> for Secp256k1SuiSignature {
fn as_mut(&mut self) -> &mut [u8] {
self.0.as_mut()
}
}

impl ToFromBytes for Secp256k1SuiSignature {
fn from_bytes(bytes: &[u8]) -> Result<Self, FastCryptoError> {
if bytes.len() != Self::LENGTH {
Expand All @@ -894,7 +874,9 @@ impl Signer<Signature> for Secp256k1KeyPair {
// Secp256r1 Sui Signature port
//
#[serde_as]
#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq, Hash)]
#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq, Hash, AsRef, AsMut)]
#[as_ref(forward)]
#[as_mut(forward)]
pub struct Secp256r1SuiSignature(
#[schemars(with = "Base64")]
#[serde_as(as = "Readable<Base64, Bytes>")]
Expand All @@ -912,18 +894,6 @@ impl SuiPublicKey for Secp256r1PublicKey {
const SIGNATURE_SCHEME: SignatureScheme = SignatureScheme::Secp256r1;
}

impl AsRef<[u8]> for Secp256r1SuiSignature {
fn as_ref(&self) -> &[u8] {
self.0.as_ref()
}
}

impl AsMut<[u8]> for Secp256r1SuiSignature {
fn as_mut(&mut self) -> &mut [u8] {
self.0.as_mut()
}
}

impl ToFromBytes for Secp256r1SuiSignature {
fn from_bytes(bytes: &[u8]) -> Result<Self, FastCryptoError> {
if bytes.len() != Self::LENGTH {
Expand Down

0 comments on commit af1fb80

Please sign in to comment.