From e99d7faf0029eb27e36569b68e0fb67662a12a5c Mon Sep 17 00:00:00 2001
From: Pavlo Khrystenko
Date: Wed, 22 May 2024 18:11:01 +0200
Subject: [PATCH] use snafu in signer
---
signer/Cargo.toml | 2 +-
signer/src/crypto/secret_uri.rs | 6 +++---
signer/src/ecdsa.rs | 29 ++++++++++++++++++-----------
signer/src/eth.rs | 10 ++++++----
signer/src/sr25519.rs | 26 +++++++++++++++++---------
signer/src/utils.rs | 25 +++++++++++++++++++++++++
6 files changed, 70 insertions(+), 28 deletions(-)
diff --git a/signer/Cargo.toml b/signer/Cargo.toml
index ddd6517d9d..e85fcaa8e4 100644
--- a/signer/Cargo.toml
+++ b/signer/Cargo.toml
@@ -52,7 +52,7 @@ hex = { workspace = true, features = ["alloc"] }
cfg-if = { workspace = true }
codec = { package = "parity-scale-codec", workspace = true, features = ["derive"] }
sp-crypto-hashing = { workspace = true }
-derive_more = { workspace = true }
+snafu = { workspace = true }
pbkdf2 = { workspace = true }
sha2 = { workspace = true }
hmac = { workspace = true }
diff --git a/signer/src/crypto/secret_uri.rs b/signer/src/crypto/secret_uri.rs
index 5148311795..6ee344a3a0 100644
--- a/signer/src/crypto/secret_uri.rs
+++ b/signer/src/crypto/secret_uri.rs
@@ -4,9 +4,9 @@
use super::DeriveJunction;
use alloc::vec::Vec;
-use derive_more::Display;
use regex::Regex;
use secrecy::SecretString;
+use snafu::Snafu;
// This code is taken from sp_core::crypto::DeriveJunction. The logic should be identical,
// though the code is tweaked a touch!
@@ -117,10 +117,10 @@ impl core::str::FromStr for SecretUri {
}
/// This is returned if `FromStr` cannot parse a string into a `SecretUri`.
-#[derive(Debug, Copy, Clone, PartialEq, Display)]
+#[derive(Debug, Copy, Clone, PartialEq, Snafu)]
pub enum SecretUriError {
/// Parsing the secret URI from a string failed; wrong format.
- #[display(fmt = "Invalid secret phrase format")]
+ #[snafu(display("Invalid secret phrase format"))]
InvalidFormat,
}
diff --git a/signer/src/ecdsa.rs b/signer/src/ecdsa.rs
index 8531d4b51c..4ccead970f 100644
--- a/signer/src/ecdsa.rs
+++ b/signer/src/ecdsa.rs
@@ -5,12 +5,15 @@
//! An ecdsa keypair implementation.
use codec::Encode;
-use crate::crypto::{seed_from_entropy, DeriveJunction, SecretUri};
+use crate::{
+ crypto::{seed_from_entropy, DeriveJunction, SecretUri},
+ utils::DisplayError,
+};
use core::str::FromStr;
-use derive_more::{Display, From};
use hex::FromHex;
use secp256k1::{ecdsa::RecoverableSignature, Message, Secp256k1, SecretKey};
use secrecy::ExposeSecret;
+use snafu::Snafu;
const SECRET_KEY_LENGTH: usize = 32;
@@ -213,22 +216,26 @@ pub(crate) mod internal {
}
/// An error handed back if creating a keypair fails.
-#[derive(Debug, PartialEq, Display, From)]
+#[derive(Debug, PartialEq, Snafu)]
pub enum Error {
/// Invalid seed.
- #[display(fmt = "Invalid seed (was it the wrong length?)")]
- #[from(ignore)]
+ #[snafu(display("Invalid seed (was it the wrong length?)"))]
InvalidSeed,
/// Invalid seed.
- #[display(fmt = "Invalid seed for ECDSA, contained soft junction")]
- #[from(ignore)]
+ #[snafu(display("Invalid seed for ECDSA, contained soft junction"))]
SoftJunction,
/// Invalid phrase.
- #[display(fmt = "Cannot parse phrase: {_0}")]
- Phrase(bip39::Error),
+ #[snafu(display("Cannot parse phrase: {source}"), context(false))]
+ Phrase {
+ #[snafu(source(from(bip39::Error, DisplayError)))]
+ source: DisplayError,
+ },
/// Invalid hex.
- #[display(fmt = "Cannot parse hex string: {_0}")]
- Hex(hex::FromHexError),
+ #[snafu(display("Cannot parse hex string: {source}"), context(false))]
+ Hex {
+ #[snafu(source(from(hex::FromHexError, DisplayError)))]
+ source: DisplayError,
+ },
}
#[cfg(feature = "std")]
diff --git a/signer/src/eth.rs b/signer/src/eth.rs
index 63d23bf384..9c920cb84d 100644
--- a/signer/src/eth.rs
+++ b/signer/src/eth.rs
@@ -9,9 +9,9 @@ use alloc::format;
use alloc::string::String;
use core::fmt::{Display, Formatter};
use core::str::FromStr;
-use derive_more::Display;
use keccak_hash::keccak;
use secp256k1::Message;
+use snafu::Snafu;
const SECRET_KEY_LENGTH: usize = 32;
@@ -215,13 +215,15 @@ pub fn verify>(sig: &Signature, message: M, pubkey: &ecdsa::Publi
}
/// An error handed back if creating a keypair fails.
-#[derive(Debug, PartialEq, Display)]
+#[derive(Debug, PartialEq, Snafu)]
pub enum Error {
/// Invalid seed.
- #[display(fmt = "Invalid seed (was it the wrong length?)")]
+ #[snafu(display("Invalid seed (was it the wrong length?)"))]
InvalidSeed,
/// Invalid derivation path.
- #[display(fmt = "Could not derive from path; some valeus in the path may have been >= 2^31?")]
+ #[snafu(display(
+ "Could not derive from path; some valeus in the path may have been >= 2^31?"
+ ))]
DeriveFromPath,
}
diff --git a/signer/src/sr25519.rs b/signer/src/sr25519.rs
index 9848cbdd68..86573ae98d 100644
--- a/signer/src/sr25519.rs
+++ b/signer/src/sr25519.rs
@@ -6,15 +6,18 @@
use core::str::FromStr;
-use crate::crypto::{seed_from_entropy, DeriveJunction, SecretUri};
+use crate::{
+ crypto::{seed_from_entropy, DeriveJunction, SecretUri},
+ utils::DisplayError,
+};
-use derive_more::{Display, From};
use hex::FromHex;
use schnorrkel::{
derive::{ChainCode, Derivation},
ExpansionMode, MiniSecretKey,
};
use secrecy::ExposeSecret;
+use snafu::Snafu;
const SECRET_KEY_LENGTH: usize = schnorrkel::keys::MINI_SECRET_KEY_LENGTH;
const SIGNING_CTX: &[u8] = b"substrate";
@@ -192,18 +195,23 @@ pub fn verify>(sig: &Signature, message: M, pubkey: &PublicKey) -
}
/// An error handed back if creating a keypair fails.
-#[derive(Debug, Display, From)]
+#[derive(Debug, Snafu)]
pub enum Error {
/// Invalid seed.
- #[display(fmt = "Invalid seed (was it the wrong length?)")]
- #[from(ignore)]
+ #[snafu(display("Invalid seed (was it the wrong length?)"))]
InvalidSeed,
/// Invalid phrase.
- #[display(fmt = "Cannot parse phrase: {_0}")]
- Phrase(bip39::Error),
+ #[snafu(display("Cannot parse phrase: {source}"), context(false))]
+ Phrase {
+ #[snafu(source(from(bip39::Error, DisplayError)))]
+ source: DisplayError,
+ },
/// Invalid hex.
- #[display(fmt = "Cannot parse hex string: {_0}")]
- Hex(hex::FromHexError),
+ #[snafu(display("Cannot parse hex string: {source}"), context(false))]
+ Hex {
+ #[snafu(source(from(hex::FromHexError, DisplayError)))]
+ source: DisplayError,
+ },
}
#[cfg(feature = "std")]
diff --git a/signer/src/utils.rs b/signer/src/utils.rs
index e3cea3cfc8..532b208cf3 100644
--- a/signer/src/utils.rs
+++ b/signer/src/utils.rs
@@ -34,3 +34,28 @@ macro_rules! once_static_cloned {
)+
};
}
+
+use core::fmt::{self, Debug, Display};
+
+#[derive(PartialEq)]
+pub struct DisplayError(pub T);
+
+impl Debug for DisplayError
+where
+ T: Debug,
+{
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ self.0.fmt(f)
+ }
+}
+
+impl Display for DisplayError
+where
+ T: Display,
+{
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ self.0.fmt(f)
+ }
+}
+
+impl snafu::Error for DisplayError where T: Display + Debug {}