diff --git a/crypto/src/signature.rs b/crypto/src/signature.rs index 1c58c03600..d387da1840 100644 --- a/crypto/src/signature.rs +++ b/crypto/src/signature.rs @@ -1,8 +1,7 @@ //! Signature module use crate::key::CryptoEngine; -use failure::Fail; -use secp256k1::{Message, SecretKey}; +use secp256k1::{Error, Message, SecretKey}; /// Signature pub type Signature = secp256k1::Signature; @@ -10,36 +9,25 @@ 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 { +/// 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 { 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)] diff --git a/validations/src/tests/mod.rs b/validations/src/tests/mod.rs index 3508f7fd80..b97575d530 100644 --- a/validations/src/tests/mod.rs +++ b/validations/src/tests/mod.rs @@ -482,7 +482,7 @@ where x.unwrap_err().downcast::().unwrap(), TransactionError::VerifyTransactionSignatureFail { hash, - msg: "Fail in verify process".to_string(), + msg: "secp: signature failed verification".to_string(), }, ); @@ -495,7 +495,7 @@ where x.unwrap_err().downcast::().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, @@ -2705,7 +2705,7 @@ fn commitment_signatures() { x.unwrap_err().downcast::().unwrap(), TransactionError::VerifyTransactionSignatureFail { hash, - msg: "Fail in verify process".to_string(), + msg: "secp: signature failed verification".to_string(), }, ); diff --git a/wallet/src/repository/error.rs b/wallet/src/repository/error.rs index 85681261d7..6d75c3ca29 100644 --- a/wallet/src/repository/error.rs +++ b/wallet/src/repository/error.rs @@ -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 for Error { @@ -147,3 +149,9 @@ impl From for Error { } } } + +impl From for Error { + fn from(err: witnet_crypto::secp256k1::Error) -> Self { + Error::Secp256k1(err) + } +}