Migrate custom error trait impls to thiserror (#1856)

* Migrate to thiserror

* missing bits

* review comment

* Apply suggestions from code review

Co-authored-by: Niklas Adolfsson <niklasadolfsson1@gmail.com>

* From<scale_decode::visitor::Error> to remove Into::intos

* scale crates for core::error::Error

* bump msrv 1.81

* make signer crate compile

---------

Co-authored-by: Niklas Adolfsson <niklasadolfsson1@gmail.com>
Co-authored-by: James Wilson <james.wilson@parity.io>
This commit is contained in:
Pavlo Khrystenko
2024-11-18 10:39:14 +01:00
committed by GitHub
parent 137701757e
commit 7d1002192e
17 changed files with 329 additions and 477 deletions
Generated
+180 -135
View File
File diff suppressed because it is too large Load Diff
+8 -8
View File
@@ -35,7 +35,7 @@ resolver = "2"
authors = ["Parity Technologies <admin@parity.io>"] authors = ["Parity Technologies <admin@parity.io>"]
edition = "2021" edition = "2021"
version = "0.38.0" version = "0.38.0"
rust-version = "1.74.0" rust-version = "1.81.0"
license = "Apache-2.0 OR GPL-3.0" license = "Apache-2.0 OR GPL-3.0"
repository = "https://github.com/paritytech/subxt" repository = "https://github.com/paritytech/subxt"
documentation = "https://docs.rs/subxt" documentation = "https://docs.rs/subxt"
@@ -94,16 +94,16 @@ proc-macro2 = "1.0.86"
quote = "1.0.37" quote = "1.0.37"
regex = { version = "1.11.0", default-features = false } regex = { version = "1.11.0", default-features = false }
scale-info = { version = "2.11.4", default-features = false } scale-info = { version = "2.11.4", default-features = false }
scale-value = { version = "0.17.0", default-features = false } scale-value = { version = "0.18.0", default-features = false }
scale-bits = { version = "0.6.0", default-features = false } scale-bits = { version = "0.7.0", default-features = false }
scale-decode = { version = "0.14.0", default-features = false } scale-decode = { version = "0.16.0", default-features = false }
scale-encode = { version = "0.8.0", default-features = false } scale-encode = { version = "0.10.0", default-features = false }
scale-typegen = "0.9.0" scale-typegen = "0.10.0"
scale-typegen-description = "0.9.0" scale-typegen-description = "0.10.0"
serde = { version = "1.0.210", default-features = false, features = ["derive"] } serde = { version = "1.0.210", default-features = false, features = ["derive"] }
serde_json = { version = "1.0.128", default-features = false } serde_json = { version = "1.0.128", default-features = false }
syn = { version = "2.0.77", features = ["full", "extra-traits"] } syn = { version = "2.0.77", features = ["full", "extra-traits"] }
thiserror = "1.0.64" thiserror = { version = "2.0.0", default-features = false }
tokio = { version = "1.40", default-features = false } tokio = { version = "1.40", default-features = false }
tracing = { version = "0.1.40", default-features = false } tracing = { version = "0.1.40", default-features = false }
tracing-wasm = "0.2.1" tracing-wasm = "0.2.1"
+2 -4
View File
@@ -19,10 +19,6 @@ default = ["std"]
std = [ std = [
"codec/std", "codec/std",
"scale-info/std", "scale-info/std",
"scale-value/std",
"scale-bits/std",
"scale-decode/std",
"scale-encode/std",
"frame-metadata/std", "frame-metadata/std",
"subxt-metadata/std", "subxt-metadata/std",
"hex/std", "hex/std",
@@ -49,6 +45,8 @@ hex = { workspace = true, default-features = false, features = ["alloc"] }
serde = { workspace = true, default-features = false, features = ["derive"] } serde = { workspace = true, default-features = false, features = ["derive"] }
serde_json = { workspace = true, default-features = false, features = ["raw_value", "alloc"] } serde_json = { workspace = true, default-features = false, features = ["raw_value", "alloc"] }
hashbrown = { workspace = true } hashbrown = { workspace = true }
thiserror = { workspace = true, default-features = false }
# For ss58 encoding AccountId32 to serialize them properly: # For ss58 encoding AccountId32 to serialize them properly:
base58 = { workspace = true } base58 = { workspace = true }
+55 -154
View File
@@ -4,61 +4,56 @@
//! The errors that can be emitted in this crate. //! The errors that can be emitted in this crate.
use core::fmt::Display;
use alloc::boxed::Box; use alloc::boxed::Box;
use alloc::string::String; use alloc::string::String;
use subxt_metadata::StorageHasher; use subxt_metadata::StorageHasher;
use thiserror::Error as DeriveError;
/// The error emitted when something goes wrong. /// The error emitted when something goes wrong.
#[derive(Debug)] #[derive(Debug, DeriveError)]
pub enum Error { pub enum Error {
/// Codec error. /// Codec error.
#[error("Codec error: {0}")]
Codec(codec::Error), Codec(codec::Error),
/// Metadata error. /// Metadata error.
Metadata(MetadataError), #[error(transparent)]
Metadata(#[from] MetadataError),
/// Storage address error. /// Storage address error.
StorageAddress(StorageAddressError), #[error(transparent)]
StorageAddress(#[from] StorageAddressError),
/// Error decoding to a [`crate::dynamic::Value`]. /// Error decoding to a [`crate::dynamic::Value`].
Decode(scale_decode::Error), #[error("Error decoding into dynamic value: {0}")]
Decode(#[from] scale_decode::Error),
/// Error encoding from a [`crate::dynamic::Value`]. /// Error encoding from a [`crate::dynamic::Value`].
Encode(scale_encode::Error), #[error("Error encoding from dynamic value: {0}")]
Encode(#[from] scale_encode::Error),
/// Error constructing the appropriate extrinsic params. /// Error constructing the appropriate extrinsic params.
ExtrinsicParams(ExtrinsicParamsError), #[error(transparent)]
ExtrinsicParams(#[from] ExtrinsicParamsError),
/// Block body error. /// Block body error.
Block(BlockError), #[error("Error working with block_body: {0}")]
Block(#[from] BlockError),
} }
impl core::fmt::Display for Error { impl From<scale_decode::visitor::DecodeError> for Error {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { fn from(err: scale_decode::visitor::DecodeError) -> Error {
match self { Error::Decode(err.into())
Error::Codec(e) => write!(f, "Scale codec error: {e}"),
Error::Metadata(e) => write!(f, "Metadata Error: {e}"),
Error::StorageAddress(e) => write!(f, "Storage Error: {e}"),
Error::Decode(e) => write!(f, "Error decoding into dynamic value: {e}"),
Error::Encode(e) => write!(f, "Error encoding from dynamic value: {e}"),
Error::ExtrinsicParams(e) => write!(f, "Extrinsic params error: {e}"),
Error::Block(e) => write!(f, "Error working with block_body: {}", e),
}
} }
} }
#[cfg(feature = "std")] // TODO: when `codec::Error` implements `core::Error`
impl std::error::Error for Error {} // remove this impl and replace it by thiserror #[from]
impl From<codec::Error> for Error {
impl_from!(ExtrinsicParamsError => Error::ExtrinsicParams); fn from(err: codec::Error) -> Error {
impl_from!(BlockError => Error::Block); Error::Codec(err)
impl_from!(MetadataError => Error::Metadata); }
impl_from!(scale_decode::Error => Error::Decode); }
impl_from!(scale_decode::visitor::DecodeError => Error::Decode);
impl_from!(scale_encode::Error => Error::Encode);
impl_from!(StorageAddressError => Error::StorageAddress);
impl_from!(codec::Error => Error::Codec);
/// Block error /// Block error
#[derive(Debug)] #[derive(Debug, DeriveError)]
pub enum BlockError { pub enum BlockError {
/// Leftover bytes found after decoding the extrinsic. /// Leftover bytes found after decoding the extrinsic.
#[error("After decoding the extrinsic at index {extrinsic_index}, {num_leftover_bytes} bytes were left, suggesting that decoding may have failed")]
LeftoverBytes { LeftoverBytes {
/// Index of the extrinsic that failed to decode. /// Index of the extrinsic that failed to decode.
extrinsic_index: usize, extrinsic_index: usize,
@@ -66,6 +61,7 @@ pub enum BlockError {
num_leftover_bytes: usize, num_leftover_bytes: usize,
}, },
/// Something went wrong decoding the extrinsic. /// Something went wrong decoding the extrinsic.
#[error("Failed to decode extrinsic at index {extrinsic_index}: {error}")]
ExtrinsicDecodeError { ExtrinsicDecodeError {
/// Index of the extrinsic that failed to decode. /// Index of the extrinsic that failed to decode.
extrinsic_index: usize, extrinsic_index: usize,
@@ -73,121 +69,74 @@ pub enum BlockError {
error: ExtrinsicDecodeError, error: ExtrinsicDecodeError,
}, },
} }
impl Display for BlockError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
BlockError::LeftoverBytes {
extrinsic_index,
num_leftover_bytes,
} => {
write!(
f,
"After decoding the extrinsic at index {extrinsic_index}, {num_leftover_bytes} bytes were left, suggesting that decoding may have failed"
)
}
BlockError::ExtrinsicDecodeError {
extrinsic_index,
error,
} => {
write!(
f,
"Failed to decode extrinsic at index {extrinsic_index}: {error}"
)
}
}
}
}
/// An alias for [`frame_decode::extrinsics::ExtrinsicDecodeError`]. /// An alias for [`frame_decode::extrinsics::ExtrinsicDecodeError`].
/// ///
pub type ExtrinsicDecodeError = frame_decode::extrinsics::ExtrinsicDecodeError; pub type ExtrinsicDecodeError = frame_decode::extrinsics::ExtrinsicDecodeError;
/// Something went wrong trying to access details in the metadata. /// Something went wrong trying to access details in the metadata.
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq, DeriveError)]
#[non_exhaustive] #[non_exhaustive]
pub enum MetadataError { pub enum MetadataError {
/// The DispatchError type isn't available in the metadata /// The DispatchError type isn't available in the metadata
#[error("The DispatchError type isn't available")]
DispatchErrorNotFound, DispatchErrorNotFound,
/// Type not found in metadata. /// Type not found in metadata.
#[error("Type with ID {0} not found")]
TypeNotFound(u32), TypeNotFound(u32),
/// Pallet not found (index). /// Pallet not found (index).
#[error("Pallet with index {0} not found")]
PalletIndexNotFound(u8), PalletIndexNotFound(u8),
/// Pallet not found (name). /// Pallet not found (name).
#[error("Pallet with name {0} not found")]
PalletNameNotFound(String), PalletNameNotFound(String),
/// Variant not found. /// Variant not found.
#[error("Variant with index {0} not found")]
VariantIndexNotFound(u8), VariantIndexNotFound(u8),
/// Constant not found. /// Constant not found.
#[error("Constant with name {0} not found")]
ConstantNameNotFound(String), ConstantNameNotFound(String),
/// Call not found. /// Call not found.
#[error("Call with name {0} not found")]
CallNameNotFound(String), CallNameNotFound(String),
/// Runtime trait not found. /// Runtime trait not found.
#[error("Runtime trait with name {0} not found")]
RuntimeTraitNotFound(String), RuntimeTraitNotFound(String),
/// Runtime method not found. /// Runtime method not found.
#[error("Runtime method with name {0} not found")]
RuntimeMethodNotFound(String), RuntimeMethodNotFound(String),
/// Call type not found in metadata. /// Call type not found in metadata.
#[error("Call type not found in pallet with index {0}")]
CallTypeNotFoundInPallet(u8), CallTypeNotFoundInPallet(u8),
/// Event type not found in metadata. /// Event type not found in metadata.
#[error("Event type not found in pallet with index {0}")]
EventTypeNotFoundInPallet(u8), EventTypeNotFoundInPallet(u8),
/// Storage details not found in metadata. /// Storage details not found in metadata.
#[error("Storage details not found in pallet with name {0}")]
StorageNotFoundInPallet(String), StorageNotFoundInPallet(String),
/// Storage entry not found. /// Storage entry not found.
#[error("Storage entry {0} not found")]
StorageEntryNotFound(String), StorageEntryNotFound(String),
/// The generated interface used is not compatible with the node. /// The generated interface used is not compatible with the node.
#[error("The generated code is not compatible with the node")]
IncompatibleCodegen, IncompatibleCodegen,
/// Custom value not found. /// Custom value not found.
#[error("Custom value with name {0} not found")]
CustomValueNameNotFound(String), CustomValueNameNotFound(String),
} }
impl Display for MetadataError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
MetadataError::DispatchErrorNotFound => {
write!(f, "The DispatchError type isn't available")
}
MetadataError::TypeNotFound(e) => write!(f, "Type with ID {e} not found"),
MetadataError::PalletIndexNotFound(e) => write!(f, "Pallet with index {e} not found"),
MetadataError::PalletNameNotFound(e) => write!(f, "Pallet with name {e} not found"),
MetadataError::VariantIndexNotFound(e) => write!(f, "Variant with index {e} not found"),
MetadataError::ConstantNameNotFound(e) => write!(f, "Constant with name {e} not found"),
MetadataError::CallNameNotFound(e) => write!(f, "Call with name {e} not found"),
MetadataError::RuntimeTraitNotFound(e) => {
write!(f, "Runtime trait with name {e} not found")
}
MetadataError::RuntimeMethodNotFound(e) => {
write!(f, "Runtime method with name {e} not found")
}
MetadataError::CallTypeNotFoundInPallet(e) => {
write!(f, "Call type not found in pallet with index {e}")
}
MetadataError::EventTypeNotFoundInPallet(e) => {
write!(f, "Event type not found in pallet with index {e}")
}
MetadataError::StorageNotFoundInPallet(e) => {
write!(f, "Storage details not found in pallet with name {e}")
}
MetadataError::StorageEntryNotFound(e) => write!(f, "Storage entry {e} not found"),
MetadataError::IncompatibleCodegen => {
write!(f, "The generated code is not compatible with the node")
}
MetadataError::CustomValueNameNotFound(e) => {
write!(f, "Custom value with name {e} not found")
}
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for MetadataError {}
/// Something went wrong trying to encode or decode a storage address. /// Something went wrong trying to encode or decode a storage address.
#[derive(Clone, Debug)] #[derive(Clone, Debug, DeriveError)]
#[non_exhaustive] #[non_exhaustive]
pub enum StorageAddressError { pub enum StorageAddressError {
/// Storage lookup does not have the expected number of keys. /// Storage lookup does not have the expected number of keys.
#[error("Storage lookup requires {expected} keys but more keys have been provided.")]
TooManyKeys { TooManyKeys {
/// The number of keys provided in the storage address. /// The number of keys provided in the storage address.
expected: usize, expected: usize,
}, },
/// This storage entry in the metadata does not have the correct number of hashers to fields. /// This storage entry in the metadata does not have the correct number of hashers to fields.
#[error("Storage entry in metadata does not have the correct number of hashers to fields")]
WrongNumberOfHashers { WrongNumberOfHashers {
/// The number of hashers in the metadata for this storage entry. /// The number of hashers in the metadata for this storage entry.
hashers: usize, hashers: usize,
@@ -195,12 +144,16 @@ pub enum StorageAddressError {
fields: usize, fields: usize,
}, },
/// We weren't given enough bytes to decode the storage address/key. /// We weren't given enough bytes to decode the storage address/key.
#[error("Not enough remaining bytes to decode the storage address/key")]
NotEnoughBytes, NotEnoughBytes,
/// We have leftover bytes after decoding the storage address. /// We have leftover bytes after decoding the storage address.
#[error("We have leftover bytes after decoding the storage address")]
TooManyBytes, TooManyBytes,
/// The bytes of a storage address are not the expected address for decoding the storage keys of the address. /// The bytes of a storage address are not the expected address for decoding the storage keys of the address.
#[error("Storage address bytes are not the expected format. Addresses need to be at least 16 bytes (pallet ++ entry) and follow a structure given by the hashers defined in the metadata")]
UnexpectedAddressBytes, UnexpectedAddressBytes,
/// An invalid hasher was used to reconstruct a value from a chunk of bytes that is part of a storage address. Hashers where the hash does not contain the original value are invalid for this purpose. /// An invalid hasher was used to reconstruct a value from a chunk of bytes that is part of a storage address. Hashers where the hash does not contain the original value are invalid for this purpose.
#[error("An invalid hasher was used to reconstruct a value with type ID {ty_id} from a hash formed by a {hasher:?} hasher. This is only possible for concat-style hashers or the identity hasher")]
HasherCannotReconstructKey { HasherCannotReconstructKey {
/// Type id of the key's type. /// Type id of the key's type.
ty_id: u32, ty_id: u32,
@@ -209,47 +162,14 @@ pub enum StorageAddressError {
}, },
} }
impl Display for StorageAddressError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
StorageAddressError::TooManyKeys { expected } => write!(
f,
"Storage lookup requires {expected} keys but more keys have been provided."
),
StorageAddressError::WrongNumberOfHashers { .. } => write!(
f,
"Storage entry in metadata does not have the correct number of hashers to fields"
),
StorageAddressError::NotEnoughBytes => write!(
f,
"Not enough remaining bytes to decode the storage address/key"
),
StorageAddressError::TooManyBytes => write!(
f,
"We have leftover bytes after decoding the storage address"
),
StorageAddressError::UnexpectedAddressBytes => write!(
f,
"Storage address bytes are not the expected format. Addresses need to be at least 16 bytes (pallet ++ entry) and follow a structure given by the hashers defined in the metadata"
),
StorageAddressError::HasherCannotReconstructKey { ty_id, hasher } => write!(
f,
"An invalid hasher was used to reconstruct a value with type ID {ty_id} from a hash formed by a {hasher:?} hasher. This is only possible for concat-style hashers or the identity hasher"
),
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for StorageAddressError {}
/// An error that can be emitted when trying to construct an instance of [`crate::config::ExtrinsicParams`], /// An error that can be emitted when trying to construct an instance of [`crate::config::ExtrinsicParams`],
/// encode data from the instance, or match on signed extensions. /// encode data from the instance, or match on signed extensions.
#[derive(Debug)] #[derive(Debug, DeriveError)]
#[non_exhaustive] #[non_exhaustive]
pub enum ExtrinsicParamsError { pub enum ExtrinsicParamsError {
/// Cannot find a type id in the metadata. The context provides some additional /// Cannot find a type id in the metadata. The context provides some additional
/// information about the source of the error (eg the signed extension name). /// information about the source of the error (eg the signed extension name).
#[error("Cannot find type id '{type_id} in the metadata (context: {context})")]
MissingTypeId { MissingTypeId {
/// Type ID. /// Type ID.
type_id: u32, type_id: u32,
@@ -257,29 +177,13 @@ pub enum ExtrinsicParamsError {
context: &'static str, context: &'static str,
}, },
/// A signed extension in use on some chain was not provided. /// A signed extension in use on some chain was not provided.
#[error("The chain expects a signed extension with the name {0}, but we did not provide one")]
UnknownSignedExtension(String), UnknownSignedExtension(String),
/// Some custom error. /// Some custom error.
#[error("Error constructing extrinsic parameters: {0}")]
Custom(Box<dyn CustomError>), Custom(Box<dyn CustomError>),
} }
impl Display for ExtrinsicParamsError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
ExtrinsicParamsError::MissingTypeId { type_id, context } => write!(
f,
"Cannot find type id '{type_id} in the metadata (context: {context})"
),
ExtrinsicParamsError::UnknownSignedExtension(e) => write!(
f,
"The chain expects a signed extension with the name {e}, but we did not provide one"
),
ExtrinsicParamsError::Custom(e) => {
write!(f, "Error constructing extrinsic parameters: {e}")
}
}
}
}
/// Anything implementing this trait can be used in [`ExtrinsicParamsError::Custom`]. /// Anything implementing this trait can be used in [`ExtrinsicParamsError::Custom`].
#[cfg(feature = "std")] #[cfg(feature = "std")]
pub trait CustomError: std::error::Error + Send + Sync + 'static {} pub trait CustomError: std::error::Error + Send + Sync + 'static {}
@@ -292,9 +196,6 @@ pub trait CustomError: core::fmt::Debug + core::fmt::Display + Send + Sync + 'st
#[cfg(not(feature = "std"))] #[cfg(not(feature = "std"))]
impl<T: core::fmt::Debug + core::fmt::Display + Send + Sync + 'static> CustomError for T {} impl<T: core::fmt::Debug + core::fmt::Display + Send + Sync + 'static> CustomError for T {}
#[cfg(feature = "std")]
impl std::error::Error for ExtrinsicParamsError {}
impl From<core::convert::Infallible> for ExtrinsicParamsError { impl From<core::convert::Infallible> for ExtrinsicParamsError {
fn from(value: core::convert::Infallible) -> Self { fn from(value: core::convert::Infallible) -> Self {
match value {} match value {}
-10
View File
@@ -18,14 +18,4 @@ macro_rules! cfg_substrate_compat {
}; };
} }
macro_rules! impl_from {
($module_path:path => $delegate_ty:ident :: $variant:ident) => {
impl From<$module_path> for $delegate_ty {
fn from(val: $module_path) -> Self {
$delegate_ty::$variant(val.into())
}
}
};
}
pub(crate) use {cfg_feature, cfg_substrate_compat}; pub(crate) use {cfg_feature, cfg_substrate_compat};
+1
View File
@@ -265,6 +265,7 @@ impl StorageKey for Vec<scale_value::Value> {
Some(value_bytes) => { Some(value_bytes) => {
let value = let value =
scale_value::scale::decode_as_type(&mut &*value_bytes, ty_id, types)?; scale_value::scale::decode_as_type(&mut &*value_bytes, ty_id, types)?;
result.push(value.remove_context()); result.push(value.remove_context());
} }
None => { None => {
+6 -17
View File
@@ -6,14 +6,13 @@
//! This doesn't contain much functionality itself, but is easy to convert to/from an `sp_core::AccountId32` //! This doesn't contain much functionality itself, but is easy to convert to/from an `sp_core::AccountId32`
//! for instance, to gain functionality without forcing a dependency on Substrate crates here. //! for instance, to gain functionality without forcing a dependency on Substrate crates here.
use core::fmt::Display;
use alloc::format; use alloc::format;
use alloc::string::String; use alloc::string::String;
use alloc::vec; use alloc::vec;
use alloc::vec::Vec; use alloc::vec::Vec;
use codec::{Decode, Encode}; use codec::{Decode, Encode};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use thiserror::Error as DeriveError;
/// A 32-byte cryptographic identifier. This is a simplified version of Substrate's /// A 32-byte cryptographic identifier. This is a simplified version of Substrate's
/// `sp_core::crypto::AccountId32`. To obtain more functionality, convert this into /// `sp_core::crypto::AccountId32`. To obtain more functionality, convert this into
@@ -106,29 +105,19 @@ impl AccountId32 {
} }
/// An error obtained from trying to interpret an SS58 encoded string into an AccountId32 /// An error obtained from trying to interpret an SS58 encoded string into an AccountId32
#[derive(Clone, Copy, Eq, PartialEq, Debug)] #[derive(Clone, Copy, Eq, PartialEq, Debug, DeriveError)]
#[allow(missing_docs)] #[allow(missing_docs)]
pub enum FromSs58Error { pub enum FromSs58Error {
#[error("Base 58 requirement is violated")]
BadBase58, BadBase58,
#[error("Length is bad")]
BadLength, BadLength,
#[error("Invalid checksum")]
InvalidChecksum, InvalidChecksum,
#[error("Invalid SS58 prefix byte.")]
InvalidPrefix, InvalidPrefix,
} }
impl Display for FromSs58Error {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
FromSs58Error::BadBase58 => write!(f, "Base 58 requirement is violated"),
FromSs58Error::BadLength => write!(f, "Length is bad"),
FromSs58Error::InvalidChecksum => write!(f, "Invalid checksum"),
FromSs58Error::InvalidPrefix => write!(f, "Invalid SS58 prefix byte."),
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for FromSs58Error {}
// We do this just to get a checksum to help verify the validity of the address in to_ss58check // We do this just to get a checksum to help verify the validity of the address in to_ss58check
fn ss58hash(data: &[u8]) -> Vec<u8> { fn ss58hash(data: &[u8]) -> Vec<u8> {
use blake2::{Blake2b512, Digest}; use blake2::{Blake2b512, Digest};
+5 -16
View File
@@ -4,13 +4,12 @@
//! `AccountId20` is a representation of Ethereum address derived from hashing the public key. //! `AccountId20` is a representation of Ethereum address derived from hashing the public key.
use core::fmt::Display;
use alloc::format; use alloc::format;
use alloc::string::String; use alloc::string::String;
use codec::{Decode, Encode}; use codec::{Decode, Encode};
use keccak_hash::keccak; use keccak_hash::keccak;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use thiserror::Error as DeriveError;
#[derive( #[derive(
Copy, Copy,
@@ -72,27 +71,17 @@ impl AccountId20 {
} }
/// An error obtained from trying to interpret a hex encoded string into an AccountId20 /// An error obtained from trying to interpret a hex encoded string into an AccountId20
#[derive(Clone, Copy, Eq, PartialEq, Debug)] #[derive(Clone, Copy, Eq, PartialEq, Debug, DeriveError)]
#[allow(missing_docs)] #[allow(missing_docs)]
pub enum FromChecksumError { pub enum FromChecksumError {
#[error("Length is bad")]
BadLength, BadLength,
#[error("Invalid checksum")]
InvalidChecksum, InvalidChecksum,
#[error("Invalid checksum prefix byte.")]
InvalidPrefix, InvalidPrefix,
} }
impl Display for FromChecksumError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
FromChecksumError::BadLength => write!(f, "Length is bad"),
FromChecksumError::InvalidChecksum => write!(f, "Invalid checksum"),
FromChecksumError::InvalidPrefix => write!(f, "Invalid checksum prefix byte."),
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for FromChecksumError {}
impl Serialize for AccountId20 { impl Serialize for AccountId20 {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where where
+1
View File
@@ -24,6 +24,7 @@ frame-metadata = { workspace = true, default-features = false, features = ["curr
codec = { package = "parity-scale-codec", workspace = true, default-features = false, features = ["derive"] } codec = { package = "parity-scale-codec", workspace = true, default-features = false, features = ["derive"] }
polkadot-sdk = { workspace = true, features = ["sp-crypto-hashing"] } polkadot-sdk = { workspace = true, features = ["sp-crypto-hashing"] }
hashbrown = { workspace = true } hashbrown = { workspace = true }
thiserror = { workspace = true, default-features = false }
[dev-dependencies] [dev-dependencies]
bitvec = { workspace = true, features = ["alloc"] } bitvec = { workspace = true, features = ["alloc"] }
+7 -30
View File
@@ -2,56 +2,33 @@
// This file is dual-licensed as Apache-2.0 or GPL-3.0. // This file is dual-licensed as Apache-2.0 or GPL-3.0.
// see LICENSE for license details. // see LICENSE for license details.
use core::fmt::Display;
use alloc::string::String; use alloc::string::String;
use thiserror::Error as DeriveError;
mod v14; mod v14;
mod v15; mod v15;
/// An error emitted if something goes wrong converting [`frame_metadata`] /// An error emitted if something goes wrong converting [`frame_metadata`]
/// types into [`crate::Metadata`]. /// types into [`crate::Metadata`].
#[derive(Debug, PartialEq, Eq)] #[derive(Debug, PartialEq, Eq, DeriveError)]
#[non_exhaustive] #[non_exhaustive]
pub enum TryFromError { pub enum TryFromError {
/// Type missing from type registry /// Type missing from type registry
#[error("Type id {0} is expected but not found in the type registry")]
TypeNotFound(u32), TypeNotFound(u32),
/// Type was not a variant/enum type /// Type was not a variant/enum type
#[error("Type {0} was not a variant/enum type, but is expected to be one")]
VariantExpected(u32), VariantExpected(u32),
/// An unsupported metadata version was provided. /// An unsupported metadata version was provided.
#[error("Cannot convert v{0} metadata into Metadata type")]
UnsupportedMetadataVersion(u32), UnsupportedMetadataVersion(u32),
/// Type name missing from type registry /// Type name missing from type registry
#[error("Type name {0} is expected but not found in the type registry")]
TypeNameNotFound(String), TypeNameNotFound(String),
/// Invalid type path. /// Invalid type path.
#[error("Type has an invalid path {0}")]
InvalidTypePath(String), InvalidTypePath(String),
} }
impl Display for TryFromError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
TryFromError::TypeNotFound(e) => write!(
f,
"Type id {e} is expected but not found in the type registry"
),
TryFromError::VariantExpected(e) => write!(
f,
"Type {e} was not a variant/enum type, but is expected to be one"
),
TryFromError::UnsupportedMetadataVersion(e) => {
write!(f, "Cannot convert v{e} metadata into Metadata type")
}
TryFromError::TypeNameNotFound(e) => write!(
f,
"Type name {e} is expected but not found in the type registry"
),
TryFromError::InvalidTypePath(e) => write!(f, "Type has an invalid path {e}"),
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for TryFromError {}
impl From<crate::Metadata> for frame_metadata::RuntimeMetadataPrefixed { impl From<crate::Metadata> for frame_metadata::RuntimeMetadataPrefixed {
fn from(value: crate::Metadata) -> Self { fn from(value: crate::Metadata) -> Self {
let m: frame_metadata::v15::RuntimeMetadataV15 = value.into(); let m: frame_metadata::v15::RuntimeMetadataV15 = value.into();
+1
View File
@@ -72,6 +72,7 @@ secp256k1 = { workspace = true, optional = true, features = [
"recovery", "recovery",
] } ] }
keccak-hash = { workspace = true, optional = true } keccak-hash = { workspace = true, optional = true }
thiserror = { workspace = true, default-features = false }
# These are used if the polkadot-js-compat feature is enabled # These are used if the polkadot-js-compat feature is enabled
serde = { workspace = true, optional = true } serde = { workspace = true, optional = true }
+4 -14
View File
@@ -2,13 +2,13 @@
// This file is dual-licensed as Apache-2.0 or GPL-3.0. // This file is dual-licensed as Apache-2.0 or GPL-3.0.
// see LICENSE for license details. // see LICENSE for license details.
use core::fmt::Display;
use super::DeriveJunction; use super::DeriveJunction;
use alloc::{string::ToString, vec::Vec}; use alloc::{string::ToString, vec::Vec};
use regex::Regex; use regex::Regex;
use secrecy::SecretString; use secrecy::SecretString;
use thiserror::Error as DeriveError;
// This code is taken from sp_core::crypto::DeriveJunction. The logic should be identical, // This code is taken from sp_core::crypto::DeriveJunction. The logic should be identical,
// though the code is tweaked a touch! // though the code is tweaked a touch!
@@ -116,23 +116,13 @@ impl core::str::FromStr for SecretUri {
} }
/// This is returned if `FromStr` cannot parse a string into a `SecretUri`. /// This is returned if `FromStr` cannot parse a string into a `SecretUri`.
#[derive(Debug, Copy, Clone, PartialEq)] #[derive(Debug, Copy, Clone, PartialEq, DeriveError)]
pub enum SecretUriError { pub enum SecretUriError {
/// Parsing the secret URI from a string failed; wrong format. /// Parsing the secret URI from a string failed; wrong format.
#[error("Invalid secret phrase format")]
InvalidFormat, InvalidFormat,
} }
impl Display for SecretUriError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
SecretUriError::InvalidFormat => write!(f, "Invalid secret phrase format"),
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for SecretUriError {}
once_static_cloned! { once_static_cloned! {
/// Interpret a phrase like: /// Interpret a phrase like:
/// ///
+17 -15
View File
@@ -7,11 +7,13 @@ use codec::Encode;
use polkadot_sdk::sp_crypto_hashing; use polkadot_sdk::sp_crypto_hashing;
use crate::crypto::{seed_from_entropy, DeriveJunction, SecretUri}; use crate::crypto::{seed_from_entropy, DeriveJunction, SecretUri};
use core::{fmt::Display, str::FromStr}; use core::str::FromStr;
use hex::FromHex; use hex::FromHex;
use secp256k1::{ecdsa::RecoverableSignature, Message, Secp256k1, SecretKey}; use secp256k1::{ecdsa::RecoverableSignature, Message, Secp256k1, SecretKey};
use secrecy::ExposeSecret; use secrecy::ExposeSecret;
use thiserror::Error as DeriveError;
const SECRET_KEY_LENGTH: usize = 32; const SECRET_KEY_LENGTH: usize = 32;
/// Seed bytes used to generate a key pair. /// Seed bytes used to generate a key pair.
@@ -222,33 +224,33 @@ pub(crate) mod internal {
} }
/// An error handed back if creating a keypair fails. /// An error handed back if creating a keypair fails.
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq, DeriveError)]
pub enum Error { pub enum Error {
/// Invalid seed. /// Invalid seed.
#[error("Invalid seed (was it the wrong length?)")]
InvalidSeed, InvalidSeed,
/// Invalid seed. /// Invalid seed.
#[error("Invalid seed for ECDSA, contained soft junction")]
SoftJunction, SoftJunction,
/// Invalid phrase. /// Invalid phrase.
#[error("Cannot parse phrase: {0}")]
Phrase(bip39::Error), Phrase(bip39::Error),
/// Invalid hex. /// Invalid hex.
#[error("Cannot parse hex string: {0}")]
Hex(hex::FromHexError), Hex(hex::FromHexError),
} }
impl Display for Error {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { impl From<hex::FromHexError> for Error {
match self { fn from(err: hex::FromHexError) -> Self {
Error::InvalidSeed => write!(f, "Invalid seed (was it the wrong length?)"), Error::Hex(err)
Error::SoftJunction => write!(f, "Invalid seed for ECDSA, contained soft junction"),
Error::Phrase(e) => write!(f, "Cannot parse phrase: {e}"),
Error::Hex(e) => write!(f, "Cannot parse hex string: {e}"),
}
} }
} }
impl_from!(bip39::Error => Error::Phrase); impl From<bip39::Error> for Error {
impl_from!(hex::FromHexError => Error::Hex); fn from(err: bip39::Error) -> Self {
Error::Phrase(err)
#[cfg(feature = "std")] }
impl std::error::Error for Error {} }
/// Dev accounts, helpful for testing but not to be used in production, /// Dev accounts, helpful for testing but not to be used in production,
/// since the secret keys are known. /// since the secret keys are known.
+5 -17
View File
@@ -6,11 +6,12 @@
use crate::ecdsa; use crate::ecdsa;
use alloc::format; use alloc::format;
use core::fmt::{Display, Formatter};
use core::str::FromStr; use core::str::FromStr;
use keccak_hash::keccak; use keccak_hash::keccak;
use secp256k1::Message; use secp256k1::Message;
use thiserror::Error as DeriveError;
const SECRET_KEY_LENGTH: usize = 32; const SECRET_KEY_LENGTH: usize = 32;
/// Bytes representing a private key. /// Bytes representing a private key.
@@ -201,29 +202,16 @@ pub fn verify<M: AsRef<[u8]>>(sig: &Signature, message: M, pubkey: &PublicKey) -
} }
/// An error handed back if creating a keypair fails. /// An error handed back if creating a keypair fails.
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq, DeriveError)]
pub enum Error { pub enum Error {
/// Invalid seed. /// Invalid seed.
#[error("Invalid seed (was it the wrong length?)")]
InvalidSeed, InvalidSeed,
/// Invalid derivation path. /// Invalid derivation path.
#[error("Could not derive from path; some values in the path may have been >= 2^31?")]
DeriveFromPath, DeriveFromPath,
} }
impl Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
match self {
Error::InvalidSeed => write!(f, "Invalid seed (was it the wrong length?)"),
Error::DeriveFromPath => write!(
f,
"Could not derive from path; some values in the path may have been >= 2^31?"
),
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for Error {}
/// Dev accounts, helpful for testing but not to be used in production, /// Dev accounts, helpful for testing but not to be used in production,
/// since the secret keys are known. /// since the secret keys are known.
pub mod dev { pub mod dev {
+14 -30
View File
@@ -5,7 +5,6 @@
//! A Polkadot-JS account loader. //! A Polkadot-JS account loader.
use base64::Engine; use base64::Engine;
use core::fmt::Display;
use crypto_secretbox::{ use crypto_secretbox::{
aead::{Aead, KeyInit}, aead::{Aead, KeyInit},
Key, Nonce, XSalsa20Poly1305, Key, Nonce, XSalsa20Poly1305,
@@ -13,6 +12,8 @@ use crypto_secretbox::{
use serde::Deserialize; use serde::Deserialize;
use subxt_core::utils::AccountId32; use subxt_core::utils::AccountId32;
use thiserror::Error as DeriveError;
use crate::sr25519; use crate::sr25519;
/// Given a JSON keypair as exported from Polkadot-JS, this returns an [`sr25519::Keypair`] /// Given a JSON keypair as exported from Polkadot-JS, this returns an [`sr25519::Keypair`]
@@ -22,15 +23,19 @@ pub fn decrypt_json(json: &str, password: &str) -> Result<sr25519::Keypair, Erro
} }
/// Error /// Error
#[derive(Debug)] #[derive(Debug, DeriveError)]
pub enum Error { pub enum Error {
/// Error decoding JSON. /// Error decoding JSON.
Json(serde_json::Error), #[error("Invalid JSON: {0}")]
Json(#[from] serde_json::Error),
/// The keypair has an unsupported encoding. /// The keypair has an unsupported encoding.
#[error("Unsupported encoding.")]
UnsupportedEncoding, UnsupportedEncoding,
/// Base64 decoding error. /// Base64 decoding error.
Base64(base64::DecodeError), #[error("Base64 decoding error: {0}")]
Base64(#[from] base64::DecodeError),
/// Wrong Scrypt parameters /// Wrong Scrypt parameters
#[error("Unsupported Scrypt parameters: N: {n}, p: {p}, r: {r}")]
UnsupportedScryptParameters { UnsupportedScryptParameters {
/// N /// N
n: u32, n: u32,
@@ -40,37 +45,16 @@ pub enum Error {
r: u32, r: u32,
}, },
/// Decryption error. /// Decryption error.
Secretbox(crypto_secretbox::Error), #[error("Decryption error: {0}")]
Secretbox(#[from] crypto_secretbox::Error),
/// sr25519 keypair error. /// sr25519 keypair error.
Sr25519(sr25519::Error), #[error(transparent)]
Sr25519(#[from] sr25519::Error),
/// The decrypted keys are not valid. /// The decrypted keys are not valid.
#[error("The decrypted keys are not valid.")]
InvalidKeys, InvalidKeys,
} }
impl_from!(serde_json::Error => Error::Json);
impl_from!(base64::DecodeError => Error::Base64);
impl_from!(crypto_secretbox::Error => Error::Secretbox);
impl_from!(sr25519::Error => Error::Sr25519);
impl Display for Error {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Error::Json(e) => write!(f, "Invalid JSON: {e}"),
Error::UnsupportedEncoding => write!(f, "Unsupported encoding."),
Error::Base64(e) => write!(f, "Base64 decoding error: {e}"),
Error::UnsupportedScryptParameters { n, p, r } => {
write!(f, "Unsupported Scrypt parameters: N: {n}, p: {p}, r: {r}")
}
Error::Secretbox(e) => write!(f, "Decryption error: {e}"),
Error::Sr25519(e) => write!(f, "{e}"),
Error::InvalidKeys => write!(f, "The decrypted keys are not valid."),
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for Error {}
#[derive(Deserialize)] #[derive(Deserialize)]
struct EncryptionMetadata { struct EncryptionMetadata {
/// Descriptor for the content /// Descriptor for the content
+22 -16
View File
@@ -4,7 +4,7 @@
//! An sr25519 keypair implementation. //! An sr25519 keypair implementation.
use core::{fmt::Display, str::FromStr}; use core::str::FromStr;
use crate::crypto::{seed_from_entropy, DeriveJunction, SecretUri}; use crate::crypto::{seed_from_entropy, DeriveJunction, SecretUri};
@@ -15,6 +15,8 @@ use schnorrkel::{
}; };
use secrecy::ExposeSecret; use secrecy::ExposeSecret;
use thiserror::Error as DeriveError;
const SECRET_KEY_LENGTH: usize = schnorrkel::keys::MINI_SECRET_KEY_LENGTH; const SECRET_KEY_LENGTH: usize = schnorrkel::keys::MINI_SECRET_KEY_LENGTH;
const SIGNING_CTX: &[u8] = b"substrate"; const SIGNING_CTX: &[u8] = b"substrate";
@@ -203,35 +205,39 @@ pub fn verify<M: AsRef<[u8]>>(sig: &Signature, message: M, pubkey: &PublicKey) -
} }
/// An error handed back if creating a keypair fails. /// An error handed back if creating a keypair fails.
#[derive(Debug)] #[derive(Debug, DeriveError)]
pub enum Error { pub enum Error {
/// Invalid seed. /// Invalid seed.
#[error("Invalid seed (was it the wrong length?)")]
InvalidSeed, InvalidSeed,
/// Invalid phrase. /// Invalid phrase.
#[error("Cannot parse phrase: {0}")]
Phrase(bip39::Error), Phrase(bip39::Error),
/// Invalid hex. /// Invalid hex.
#[error("Cannot parse hex string: {0}")]
Hex(hex::FromHexError), Hex(hex::FromHexError),
/// Signature error. /// Signature error.
#[error("Signature error: {0}")]
Signature(schnorrkel::SignatureError), Signature(schnorrkel::SignatureError),
} }
impl_from!(bip39::Error => Error::Phrase); impl From<schnorrkel::SignatureError> for Error {
impl_from!(hex::FromHexError => Error::Hex); fn from(value: schnorrkel::SignatureError) -> Self {
impl_from!(schnorrkel::SignatureError => Error::Signature); Error::Signature(value)
impl Display for Error {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Error::InvalidSeed => write!(f, "Invalid seed (was it the wrong length?)"),
Error::Phrase(e) => write!(f, "Cannot parse phrase: {e}"),
Error::Hex(e) => write!(f, "Cannot parse hex string: {e}"),
Error::Signature(e) => write!(f, "Signature error: {e}"),
}
} }
} }
#[cfg(feature = "std")] impl From<hex::FromHexError> for Error {
impl std::error::Error for Error {} fn from(err: hex::FromHexError) -> Self {
Error::Hex(err)
}
}
impl From<bip39::Error> for Error {
fn from(err: bip39::Error) -> Self {
Error::Phrase(err)
}
}
/// Dev accounts, helpful for testing but not to be used in production, /// Dev accounts, helpful for testing but not to be used in production,
/// since the secret keys are known. /// since the secret keys are known.
-10
View File
@@ -35,13 +35,3 @@ macro_rules! once_static_cloned {
)+ )+
}; };
} }
macro_rules! impl_from {
($module_path:path => $delegate_ty:ident :: $variant:ident) => {
impl From<$module_path> for $delegate_ty {
fn from(val: $module_path) -> Self {
$delegate_ty::$variant(val.into())
}
}
};
}