use snafu in signer

This commit is contained in:
Pavlo Khrystenko
2024-05-22 18:11:01 +02:00
parent f8b8e1b8b2
commit e99d7faf00
6 changed files with 70 additions and 28 deletions
+1 -1
View File
@@ -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 }
+3 -3
View File
@@ -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,
}
+18 -11
View File
@@ -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<bip39::Error>,
},
/// 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<hex::FromHexError>,
},
}
#[cfg(feature = "std")]
+6 -4
View File
@@ -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<M: AsRef<[u8]>>(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,
}
+17 -9
View File
@@ -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<M: AsRef<[u8]>>(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<bip39::Error>,
},
/// 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<hex::FromHexError>,
},
}
#[cfg(feature = "std")]
+25
View File
@@ -34,3 +34,28 @@ macro_rules! once_static_cloned {
)+
};
}
use core::fmt::{self, Debug, Display};
#[derive(PartialEq)]
pub struct DisplayError<T>(pub T);
impl<T> Debug for DisplayError<T>
where
T: Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
impl<T> Display for DisplayError<T>
where
T: Display,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
impl<T> snafu::Error for DisplayError<T> where T: Display + Debug {}