Skip to content

Commit

Permalink
Flatten Base58Error to allow trivial serialization (#76)
Browse files Browse the repository at this point in the history
* inc major semver since Base58Error changed

* not wrapping bs58 errors anymore so dont need imports or exports

* dont need a fallible error now that we aren't wrapping bs58 errors

* rename module is spewing unused import errors even though it did not change, so disable that check in clippy

* only allow unused imports for _rename module
  • Loading branch information
xoloki authored Jan 25, 2024
1 parent 32ba1c9 commit b471b7d
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 25 deletions.
2 changes: 1 addition & 1 deletion p256k1/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "p256k1"
version = "6.0.0"
version = "7.0.0"
edition = "2021"
authors = ["Joey Yandle <[email protected]>"]
license = "Apache-2.0"
Expand Down
25 changes: 15 additions & 10 deletions p256k1/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
use bs58::{decode::Error as DecodeError, encode::Error as EncodeError};
use core::{
cmp::PartialEq,
fmt::{Debug, Display, Formatter, Result as FmtResult},
};
use serde::{Deserialize, Serialize};

/// Re-export of crate `bs58`'s decode error
pub type Base58DecodeError = DecodeError;
/// Re-export of crate `bs58`'s encode error
pub type Base58EncodeError = EncodeError;

#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
/// Base58-related errors
pub enum Base58Error {
/// Error decoding
Decode(Base58DecodeError),
Decode,
/// Error encoding
Encode(Base58EncodeError),
Encode,
}

impl Display for Base58Error {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
write!(f, "{:?}", self)
}
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
/// Errors when performing conversion operations
pub enum ConversionError {
/// Error decompressing a point into a field element
Expand Down
4 changes: 2 additions & 2 deletions p256k1/src/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,8 @@ impl TryFrom<&str> for Element {
fn try_from(s: &str) -> Result<Self, Error> {
match bs58::decode(s).into_vec() {
Ok(bytes) => Element::try_from(&bytes[..]),
Err(e) => Err(Error::Conversion(ConversionError::Base58(
Base58Error::Decode(e),
Err(_e) => Err(Error::Conversion(ConversionError::Base58(
Base58Error::Decode, //(e),
))),
}
}
Expand Down
8 changes: 4 additions & 4 deletions p256k1/src/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ impl TryFrom<&str> for PublicKey {
fn try_from(s: &str) -> Result<Self, self::Error> {
match bs58::decode(s).into_vec() {
Ok(bytes) => PublicKey::try_from(&bytes[..]),
Err(e) => Err(Error::Conversion(ConversionError::Base58(
Base58Error::Decode(e),
Err(_e) => Err(Error::Conversion(ConversionError::Base58(
Base58Error::Decode, //(e),
))),
}
}
Expand Down Expand Up @@ -292,8 +292,8 @@ impl TryFrom<&str> for XOnlyPublicKey {
fn try_from(s: &str) -> Result<Self, self::Error> {
match bs58::decode(s).into_vec() {
Ok(bytes) => XOnlyPublicKey::try_from(&bytes[..]),
Err(e) => Err(Error::Conversion(ConversionError::Base58(
Base58Error::Decode(e),
Err(_e) => Err(Error::Conversion(ConversionError::Base58(
Base58Error::Decode, //(e),
))),
}
}
Expand Down
1 change: 1 addition & 0 deletions p256k1/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ mod bindings {
#[cfg(not(feature = "with_bindgen"))]
mod bindings;

#[allow(unused_imports)]
mod _rename;

/// secp256k1 context operations
Expand Down
10 changes: 4 additions & 6 deletions p256k1/src/point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ pub const N: [u8; 32] = [
0xBA, 0xAE, 0xDC, 0xE6, 0xAF, 0x48, 0xA0, 0x3B, 0xBF, 0xD2, 0x5E, 0x8C, 0xD0, 0x36, 0x41, 0x41,
];

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
/// Errors in point operations
pub enum Error {
/// Error doing multi-exponentiation
Expand Down Expand Up @@ -482,9 +482,7 @@ impl TryFrom<&Compressed> for Point {
let ry = secp256k1_ge_set_xo_var(
&mut y,
&x,
(c.data[0] as u32 == SECP256K1_TAG_PUBKEY_ODD)
.try_into()
.unwrap(),
(c.data[0] as u32 == SECP256K1_TAG_PUBKEY_ODD).into(),
);
if ry == 0 {
return Err(Error::Conversion(ConversionError::BadGroupElement));
Expand Down Expand Up @@ -711,8 +709,8 @@ impl TryFrom<&str> for Compressed {
fn try_from(s: &str) -> Result<Self, Error> {
match bs58::decode(s).into_vec() {
Ok(bytes) => Compressed::try_from(&bytes[..]),
Err(e) => Err(Error::Conversion(ConversionError::Base58(
Base58Error::Decode(e),
Err(_e) => Err(Error::Conversion(ConversionError::Base58(
Base58Error::Decode, //(e),
))),
}
}
Expand Down
4 changes: 2 additions & 2 deletions p256k1/src/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,8 @@ impl TryFrom<&str> for Scalar {
fn try_from(s: &str) -> Result<Self, Error> {
match bs58::decode(s).into_vec() {
Ok(bytes) => Scalar::try_from(&bytes[..]),
Err(e) => Err(Error::Conversion(ConversionError::Base58(
Base58Error::Decode(e),
Err(_e) => Err(Error::Conversion(ConversionError::Base58(
Base58Error::Decode, //(e),
))),
}
}
Expand Down

0 comments on commit b471b7d

Please sign in to comment.