diff --git a/Cargo.toml b/Cargo.toml index 36a621c..57af8f9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "secp256k1" -version = "0.3.1" +version = "0.3.2" authors = [ "Dawid Ciężarkiewicz ", "Andrew Poelstra " ] license = "CC0-1.0" diff --git a/src/key.rs b/src/key.rs index a19e9db..4a2eac1 100644 --- a/src/key.rs +++ b/src/key.rs @@ -23,7 +23,7 @@ use serialize::{Decoder, Decodable, Encoder, Encodable}; use serde::{Serialize, Deserialize, Serializer, Deserializer}; use super::{Secp256k1, ContextFlag}; -use super::Error::{self, IncapableContext, InvalidPublicKey, InvalidSecretKey, Unknown}; +use super::Error::{self, IncapableContext, InvalidPublicKey, InvalidSecretKey}; use constants; use ffi; @@ -88,7 +88,7 @@ impl SecretKey { -> Result<(), Error> { unsafe { if ffi::secp256k1_ec_privkey_tweak_add(secp.ctx, self.as_mut_ptr(), other.as_ptr()) != 1 { - Err(Unknown) + Err(InvalidSecretKey) } else { Ok(()) } @@ -180,7 +180,7 @@ impl PublicKey { other.as_ptr()) == 1 { Ok(()) } else { - Err(Unknown) + Err(InvalidSecretKey) } } } diff --git a/src/lib.rs b/src/lib.rs index 94bfdb1..459e3ca 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -44,7 +44,7 @@ extern crate libc; extern crate rand; use libc::size_t; -use std::{fmt, ops, ptr}; +use std::{error, fmt, ops, ptr}; use rand::Rng; #[macro_use] @@ -267,14 +267,28 @@ pub enum Error { InvalidSecretKey, /// Bad recovery id InvalidRecoveryId, - /// Boolean-returning function returned the wrong boolean - Unknown } // Passthrough Debug to Display, since errors should be user-visible impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { - fmt::Debug::fmt(self, f) + f.write_str(error::Error::description(self)) + } +} + +impl error::Error for Error { + fn cause(&self) -> Option<&error::Error> { None } + + fn description(&self) -> &str { + match *self { + Error::IncapableContext => "secp: context does not have sufficient capabilities", + Error::IncorrectSignature => "secp: signature failed verification", + Error::InvalidMessage => "secp: message was not 32 bytes (do you need to hash?)", + Error::InvalidPublicKey => "secp: malformed public key", + Error::InvalidSignature => "secp: malformed signature", + Error::InvalidSecretKey => "secp: malformed or out-of-range secret key", + Error::InvalidRecoveryId => "secp: bad recovery id" + } } }