Skip to content

Commit

Permalink
feat(crypto): remove SignatureError, use secp256k1::Error instead
Browse files Browse the repository at this point in the history
  • Loading branch information
tmpolaczyk committed Nov 30, 2021
1 parent ac6ea99 commit 282b142
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 24 deletions.
30 changes: 9 additions & 21 deletions crypto/src/signature.rs
Original file line number Diff line number Diff line change
@@ -1,45 +1,33 @@
//! Signature module

use crate::key::CryptoEngine;
use failure::Fail;
use secp256k1::{Message, SecretKey};
use secp256k1::{Error, Message, SecretKey};

/// Signature
pub type Signature = secp256k1::Signature;

/// PublicKey
pub type PublicKey = secp256k1::PublicKey;

/// The error type for operations with signatures
#[derive(Debug, PartialEq, Fail)]
pub enum SignatureError {
#[fail(display = "Fail in verify process")]
/// Fail in verify process
VerifyError,
}

/// Sign data with provided secret key
/// - Returns an Error if data is all zeros or is not a 32-byte array
pub fn sign(
secp: &CryptoEngine,
secret_key: SecretKey,
data: &[u8],
) -> Result<Signature, failure::Error> {
/// Sign `data` with provided secret key. `data` must be the 32-byte output of a cryptographically
/// secure hash function, otherwise this function is not secure.
/// - Returns an Error if data is not a 32-byte array
pub fn sign(secp: &CryptoEngine, secret_key: SecretKey, data: &[u8]) -> Result<Signature, Error> {
let msg = Message::from_slice(data)?;

Ok(secp.sign(&msg, &secret_key))
}
/// Verify signature with a provided public key
/// Verify signature with a provided public key.
/// - Returns an Error if data is not a 32-byte array
pub fn verify(
secp: &CryptoEngine,
public_key: &PublicKey,
data: &[u8],
sig: &Signature,
) -> Result<(), failure::Error> {
let msg = Message::from_slice(data).unwrap();
) -> Result<(), Error> {
let msg = Message::from_slice(data)?;

secp.verify(&msg, sig, public_key)
.map_err(|_| SignatureError::VerifyError.into())
}

#[cfg(test)]
Expand Down
6 changes: 3 additions & 3 deletions validations/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ where
x.unwrap_err().downcast::<TransactionError>().unwrap(),
TransactionError::VerifyTransactionSignatureFail {
hash,
msg: "Fail in verify process".to_string(),
msg: "secp: signature failed verification".to_string(),
},
);

Expand All @@ -495,7 +495,7 @@ where
x.unwrap_err().downcast::<TransactionError>().unwrap(),
TransactionError::VerifyTransactionSignatureFail {
hash,
// A "Fail in verify process" msg would also be correct here
// A "secp: signature failed verification" msg would also be correct here
msg: TransactionError::PublicKeyHashMismatch {
expected_pkh: MY_PKH_1.parse().unwrap(),
signature_pkh,
Expand Down Expand Up @@ -2705,7 +2705,7 @@ fn commitment_signatures() {
x.unwrap_err().downcast::<TransactionError>().unwrap(),
TransactionError::VerifyTransactionSignatureFail {
hash,
msg: "Fail in verify process".to_string(),
msg: "secp: signature failed verification".to_string(),
},
);

Expand Down
8 changes: 8 additions & 0 deletions wallet/src/repository/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ pub enum Error {
UnknownFeeType,
#[fail(display = "Wallet not found")]
WalletNotFound,
#[fail(display = "Secp256k1 error: {}", _0)]
Secp256k1(#[cause] witnet_crypto::secp256k1::Error),
}

impl From<failure::Error> for Error {
Expand Down Expand Up @@ -147,3 +149,9 @@ impl From<TransactionError> for Error {
}
}
}

impl From<witnet_crypto::secp256k1::Error> for Error {
fn from(err: witnet_crypto::secp256k1::Error) -> Self {
Error::Secp256k1(err)
}
}

0 comments on commit 282b142

Please sign in to comment.