Add `Display` impl to `Error`; cleanup `Result` mess
This commit is contained in:
parent
ff29303da1
commit
f6585616b1
19
src/key.rs
19
src/key.rs
|
@ -26,8 +26,7 @@ use crypto::hmac::Hmac;
|
|||
use crypto::mac::Mac;
|
||||
|
||||
use super::init;
|
||||
use super::Result;
|
||||
use super::Error::{InvalidNonce, InvalidPublicKey, InvalidSecretKey, Unknown};
|
||||
use super::Error::{self, InvalidNonce, InvalidPublicKey, InvalidSecretKey, Unknown};
|
||||
use constants;
|
||||
use ffi;
|
||||
|
||||
|
@ -81,7 +80,7 @@ impl Nonce {
|
|||
|
||||
/// Converts a `NONCE_SIZE`-byte slice to a nonce
|
||||
#[inline]
|
||||
pub fn from_slice(data: &[u8]) -> Result<Nonce> {
|
||||
pub fn from_slice(data: &[u8]) -> Result<Nonce, Error> {
|
||||
match data.len() {
|
||||
constants::NONCE_SIZE => {
|
||||
let mut ret = [0; constants::NONCE_SIZE];
|
||||
|
@ -171,7 +170,7 @@ impl SecretKey {
|
|||
|
||||
/// Converts a `SECRET_KEY_SIZE`-byte slice to a secret key
|
||||
#[inline]
|
||||
pub fn from_slice(data: &[u8]) -> Result<SecretKey> {
|
||||
pub fn from_slice(data: &[u8]) -> Result<SecretKey, Error> {
|
||||
init();
|
||||
match data.len() {
|
||||
constants::SECRET_KEY_SIZE => {
|
||||
|
@ -195,7 +194,7 @@ impl SecretKey {
|
|||
/// Marked `unsafe` since you must
|
||||
/// call `init()` (or construct a `Secp256k1`, which does this for you) before
|
||||
/// using this function
|
||||
pub fn add_assign(&mut self, other: &SecretKey) -> Result<()> {
|
||||
pub fn add_assign(&mut self, other: &SecretKey) -> Result<(), Error> {
|
||||
init();
|
||||
unsafe {
|
||||
if ffi::secp256k1_ec_privkey_tweak_add(self.as_mut_ptr(), other.as_ptr()) != 1 {
|
||||
|
@ -265,7 +264,7 @@ impl PublicKey {
|
|||
|
||||
/// Creates a public key directly from a slice
|
||||
#[inline]
|
||||
pub fn from_slice(data: &[u8]) -> Result<PublicKey> {
|
||||
pub fn from_slice(data: &[u8]) -> Result<PublicKey, Error> {
|
||||
match data.len() {
|
||||
constants::COMPRESSED_PUBLIC_KEY_SIZE => {
|
||||
let mut ret = [0; constants::COMPRESSED_PUBLIC_KEY_SIZE];
|
||||
|
@ -337,7 +336,7 @@ impl PublicKey {
|
|||
|
||||
#[inline]
|
||||
/// Adds the pk corresponding to `other` to the pk `self` in place
|
||||
pub fn add_exp_assign(&mut self, other: &SecretKey) -> Result<()> {
|
||||
pub fn add_exp_assign(&mut self, other: &SecretKey) -> Result<(), Error> {
|
||||
init();
|
||||
unsafe {
|
||||
if ffi::secp256k1_ec_pubkey_tweak_add(self.as_mut_ptr(),
|
||||
|
@ -487,7 +486,7 @@ impl ops::Index<ops::RangeFull> for PublicKey {
|
|||
}
|
||||
|
||||
impl Decodable for PublicKey {
|
||||
fn decode<D: Decoder>(d: &mut D) -> ::std::result::Result<PublicKey, D::Error> {
|
||||
fn decode<D: Decoder>(d: &mut D) -> Result<PublicKey, D::Error> {
|
||||
d.read_seq(|d, len| {
|
||||
if len == constants::UNCOMPRESSED_PUBLIC_KEY_SIZE {
|
||||
unsafe {
|
||||
|
@ -515,13 +514,13 @@ impl Decodable for PublicKey {
|
|||
}
|
||||
|
||||
impl Encodable for PublicKey {
|
||||
fn encode<S: Encoder>(&self, s: &mut S) -> ::std::result::Result<(), S::Error> {
|
||||
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
|
||||
(&self[..]).encode(s)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for SecretKey {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
||||
(&self[..]).fmt(f)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -113,7 +113,7 @@ macro_rules! impl_array_newtype {
|
|||
}
|
||||
|
||||
impl ::serialize::Decodable for $thing {
|
||||
fn decode<D: ::serialize::Decoder>(d: &mut D) -> ::std::result::Result<$thing, D::Error> {
|
||||
fn decode<D: ::serialize::Decoder>(d: &mut D) -> Result<$thing, D::Error> {
|
||||
use serialize::Decodable;
|
||||
|
||||
::assert_type_is_copy::<$ty>();
|
||||
|
@ -137,7 +137,7 @@ macro_rules! impl_array_newtype {
|
|||
|
||||
impl ::serialize::Encodable for $thing {
|
||||
fn encode<S: ::serialize::Encoder>(&self, s: &mut S)
|
||||
-> ::std::result::Result<(), S::Error> {
|
||||
-> Result<(), S::Error> {
|
||||
self[..].encode(s)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -43,7 +43,7 @@ extern crate libc;
|
|||
extern crate rand;
|
||||
|
||||
use std::intrinsics::copy_nonoverlapping;
|
||||
use std::{io, ops};
|
||||
use std::{fmt, io, ops};
|
||||
use std::sync::{Once, ONCE_INIT};
|
||||
use libc::c_int;
|
||||
use rand::{OsRng, Rng, SeedableRng};
|
||||
|
@ -93,7 +93,7 @@ impl Signature {
|
|||
|
||||
/// Converts a byte slice to a signature
|
||||
#[inline]
|
||||
pub fn from_slice(data: &[u8]) -> Result<Signature> {
|
||||
pub fn from_slice(data: &[u8]) -> Result<Signature, Error> {
|
||||
if data.len() <= constants::MAX_SIGNATURE_SIZE {
|
||||
let mut ret = [0; constants::MAX_SIGNATURE_SIZE];
|
||||
unsafe {
|
||||
|
@ -179,8 +179,12 @@ pub enum Error {
|
|||
Unknown
|
||||
}
|
||||
|
||||
/// Result type
|
||||
pub type Result<T> = ::std::result::Result<T, Error>;
|
||||
// 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)
|
||||
}
|
||||
}
|
||||
|
||||
static mut Secp256k1_init: Once = ONCE_INIT;
|
||||
|
||||
|
@ -233,7 +237,7 @@ impl Secp256k1 {
|
|||
|
||||
/// Constructs a signature for `msg` using the secret key `sk` and nonce `nonce`
|
||||
pub fn sign(&self, msg: &[u8], sk: &key::SecretKey, nonce: &key::Nonce)
|
||||
-> Result<Signature> {
|
||||
-> Result<Signature, Error> {
|
||||
let mut sig = [0; constants::MAX_SIGNATURE_SIZE];
|
||||
let mut len = constants::MAX_SIGNATURE_SIZE as c_int;
|
||||
unsafe {
|
||||
|
@ -250,7 +254,7 @@ impl Secp256k1 {
|
|||
|
||||
/// Constructs a compact signature for `msg` using the secret key `sk`
|
||||
pub fn sign_compact(&self, msg: &[u8], sk: &key::SecretKey, nonce: &key::Nonce)
|
||||
-> Result<(Signature, RecoveryId)> {
|
||||
-> Result<(Signature, RecoveryId), Error> {
|
||||
let mut sig = [0; constants::MAX_SIGNATURE_SIZE];
|
||||
let mut recid = 0;
|
||||
unsafe {
|
||||
|
@ -267,7 +271,7 @@ impl Secp256k1 {
|
|||
/// `msg`. Returns through the out-pointer `pubkey`.
|
||||
pub fn recover_compact(&self, msg: &[u8], sig: &[u8],
|
||||
compressed: bool, recid: RecoveryId)
|
||||
-> Result<key::PublicKey> {
|
||||
-> Result<key::PublicKey, Error> {
|
||||
let mut pk = key::PublicKey::new(compressed);
|
||||
let RecoveryId(recid) = recid;
|
||||
|
||||
|
@ -290,14 +294,14 @@ impl Secp256k1 {
|
|||
/// there with zero-padded signatures that don't fit in the `Signature` type.
|
||||
/// Use `verify_raw` instead.
|
||||
#[inline]
|
||||
pub fn verify(msg: &[u8], sig: &Signature, pk: &key::PublicKey) -> Result<()> {
|
||||
pub fn verify(msg: &[u8], sig: &Signature, pk: &key::PublicKey) -> Result<(), Error> {
|
||||
Secp256k1::verify_raw(msg, &sig[..], pk)
|
||||
}
|
||||
|
||||
/// Checks that `sig` is a valid ECDSA signature for `msg` using the public
|
||||
/// key `pubkey`. Returns `Ok(true)` on success.
|
||||
#[inline]
|
||||
pub fn verify_raw(msg: &[u8], sig: &[u8], pk: &key::PublicKey) -> Result<()> {
|
||||
pub fn verify_raw(msg: &[u8], sig: &[u8], pk: &key::PublicKey) -> Result<(), Error> {
|
||||
init(); // This is a static function, so we have to init
|
||||
let res = unsafe {
|
||||
ffi::secp256k1_ecdsa_verify(msg.as_ptr(), msg.len() as c_int,
|
||||
|
|
Loading…
Reference in New Issue