use crate::consts::appconsts;
pub type Result<T, E = Error> = std::result::Result<T, E>;
#[cfg(feature = "uniffi")]
pub type UniffiResult<T> = std::result::Result<T, UniffiError>;
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("Unsupported namespace version: {0}")]
UnsupportedNamespaceVersion(u8),
#[error("Invalid namespace size")]
InvalidNamespaceSize,
#[error(transparent)]
Tendermint(#[from] tendermint::Error),
#[error("Length ({0}) different than expected ({1})")]
InvalidLength(usize, usize),
#[error(transparent)]
Protobuf(#[from] tendermint_proto::Error),
#[error(transparent)]
LeopardCodec(#[from] leopard_codec::LeopardError),
#[error("Missing header")]
MissingHeader,
#[error("Missing commit")]
MissingCommit,
#[error("Missing validator set")]
MissingValidatorSet,
#[error("Missing data availability header")]
MissingDataAvailabilityHeader,
#[error("Missing proof")]
MissingProof,
#[error("Missing shares")]
MissingShares,
#[error("Missing fee field")]
MissingFee,
#[error("Missing sum field")]
MissingSum,
#[error("Missing mode info field")]
MissingModeInfo,
#[error("Missing bitarray field")]
MissingBitarray,
#[error("Bit array to large")]
BitarrayTooLarge,
#[error("CompactBitArray malformed")]
MalformedCompactBitArray,
#[error("Wrong proof type")]
WrongProofType,
#[error("Unsupported share version: {0}")]
UnsupportedShareVersion(u8),
#[error("Invalid share size: {0}")]
InvalidShareSize(usize),
#[error("Invalid nmt leaf size: {0}")]
InvalidNmtLeafSize(usize),
#[error("Invalid nmt node order")]
InvalidNmtNodeOrder,
#[error(
"Sequence len must fit into {len} bytes, got value {0}",
len = appconsts::SEQUENCE_LEN_BYTES
)]
ShareSequenceLenExceeded(usize),
#[error("Invalid namespace v0")]
InvalidNamespaceV0,
#[error("Invalid namespace v255")]
InvalidNamespaceV255,
#[error(transparent)]
InvalidNamespacedHash(#[from] nmt_rs::InvalidNamespacedHash),
#[error("Invalid index of signature in commit {0}, height {1}")]
InvalidSignatureIndex(usize, u64),
#[error("Invalid axis type: {0}")]
InvalidAxis(i32),
#[error("Invalid proof type: {0}")]
InvalidShwapProofType(i32),
#[error("Could not deserialize public key")]
InvalidPublicKey,
#[error("Range proof verification failed: {0:?}")]
RangeProofError(nmt_rs::simple_merkle::error::RangeProofError),
#[error("Computed root doesn't match received one")]
RootMismatch,
#[error("Unexpected absent commit signature")]
UnexpectedAbsentSignature,
#[error("Validation error: {0}")]
Validation(#[from] ValidationError),
#[error("Verification error: {0}")]
Verification(#[from] VerificationError),
#[error(
"Share version has to be at most {ver}, got {0}",
ver = appconsts::MAX_SHARE_VERSION
)]
MaxShareVersionExceeded(u8),
#[error("Nmt error: {0}")]
Nmt(&'static str),
#[error("Invalid address prefix: {0}")]
InvalidAddressPrefix(String),
#[error("Invalid address size: {0}")]
InvalidAddressSize(usize),
#[error("Invalid address: {0}")]
InvalidAddress(String),
#[error("Invalid coin amount: {0}")]
InvalidCoinAmount(String),
#[error("Invalid coin denomination: {0}")]
InvalidCoinDenomination(String),
#[error("Invalid balance: {0:?}")]
InvalidBalance(String),
#[error("Invalid Public Key")]
InvalidPublicKeyType(String),
#[error("Unsupported fraud proof type: {0}")]
UnsupportedFraudProofType(String),
#[error("Index ({0}) out of range ({1})")]
IndexOutOfRange(usize, usize),
#[error("Data square index out of range. row: {0}, column: {1}")]
EdsIndexOutOfRange(u16, u16),
#[error("Invalid dimensions of EDS")]
EdsInvalidDimentions,
#[error("Invalid zero block height")]
ZeroBlockHeight,
#[error("Expected first share of a blob")]
ExpectedShareWithSequenceStart,
#[error("Unexpected share from reserved namespace")]
UnexpectedReservedNamespace,
#[error("Unexpected start of a new blob")]
UnexpectedSequenceStart,
#[error("Metadata mismatch between shares in blob: {0}")]
BlobSharesMetadataMismatch(String),
#[error("Blob too large")]
BlobTooLarge,
#[error("NamespaceData too large")]
NamespaceDataTooLarge,
#[error("Invalid committment length")]
InvalidComittmentLength,
#[error("Missing signer field in blob")]
MissingSigner,
#[error("Signer is not supported in share version 0")]
SignerNotSupported,
#[error("Empty blob list")]
EmptyBlobList,
#[error("Missing belegation response")]
MissingDelegationResponse,
#[error("Missing delegation")]
MissingDelegation,
#[error("Missing balance")]
MissingBalance,
#[error("Missing unbond")]
MissingUnbond,
#[error("Missing completion time")]
MissingCompletionTime,
#[error("Missing redelegation")]
MissingRedelegation,
#[error("Missing redelegation entry")]
MissingRedelegationEntry,
#[error("Invalid Cosmos decimal: {0}")]
InvalidCosmosDecimal(String),
}
impl From<prost::DecodeError> for Error {
fn from(value: prost::DecodeError) -> Self {
Error::Protobuf(tendermint_proto::Error::decode_message(value))
}
}
#[cfg(all(feature = "wasm-bindgen", target_arch = "wasm32"))]
impl From<Error> for wasm_bindgen::JsValue {
fn from(value: Error) -> Self {
js_sys::Error::new(&value.to_string()).into()
}
}
#[derive(Debug, thiserror::Error)]
pub enum ValidationError {
#[error("Not enought voting power (got {0}, needed {1})")]
NotEnoughVotingPower(u64, u64),
#[error("{0}")]
Other(String),
}
#[derive(Debug, thiserror::Error)]
pub enum VerificationError {
#[error("Not enought voting power (got {0}, needed {1})")]
NotEnoughVotingPower(u64, u64),
#[error("{0}")]
Other(String),
}
macro_rules! validation_error {
($fmt:literal $(,)?) => {
$crate::ValidationError::Other(std::format!($fmt))
};
($fmt:literal, $($arg:tt)*) => {
$crate::ValidationError::Other(std::format!($fmt, $($arg)*))
};
}
macro_rules! bail_validation {
($($arg:tt)*) => {
return Err($crate::validation_error!($($arg)*).into())
};
}
macro_rules! verification_error {
($fmt:literal $(,)?) => {
$crate::VerificationError::Other(std::format!($fmt))
};
($fmt:literal, $($arg:tt)*) => {
$crate::VerificationError::Other(std::format!($fmt, $($arg)*))
};
}
macro_rules! bail_verification {
($($arg:tt)*) => {
return Err($crate::verification_error!($($arg)*).into())
};
}
pub(crate) use bail_validation;
pub(crate) use bail_verification;
pub(crate) use validation_error;
pub(crate) use verification_error;
#[cfg(feature = "uniffi")]
#[derive(Debug, uniffi::Error, thiserror::Error)]
pub enum UniffiConversionError {
#[error("Invalid namespace length")]
InvalidNamespaceLength,
#[error("Invalid commitment hash length")]
InvalidCommitmentLength,
#[error("Invalid account id length")]
InvalidAccountIdLength,
#[error("Invalid hash length")]
InvalidHashLength,
#[error("Invalid chain id length")]
InvalidChainIdLength,
#[error("Invalid public key")]
InvalidPublicKey,
#[error("Invalid parts header {msg}")]
InvalidPartsHeader {
msg: String,
},
#[error("Timestamp out of range")]
TimestampOutOfRange,
#[error("Header heigth out of range")]
HeaderHeightOutOfRange,
#[error("Invalid signature length")]
InvalidSignatureLength,
#[error("Invalid round index")]
InvalidRoundIndex,
#[error("Invalid validator index")]
InvalidValidatorIndex,
#[error("Voting power out of range")]
InvalidVotingPower,
#[error("Invalid signed header")]
InvalidSignedHeader,
#[error("Could not generate commitment")]
CouldNotGenerateCommitment {
msg: String,
},
#[error("Invalid address")]
InvalidAddress {
msg: String,
},
}
#[cfg(feature = "uniffi")]
#[derive(Debug, uniffi::Error, thiserror::Error)]
pub enum UniffiError {
#[error("Unsupported namespace version: {0}")]
UnsupportedNamespaceVersion(u8),
#[error("Invalid namespace size")]
InvalidNamespaceSize,
#[error("Tendermint error: {0}")]
Tendermint(String),
#[error("tendermint_proto error: {0}")]
Protobuf(String),
#[error("Length ({0}) different than expected ({1})")]
InvalidLength(u64, u64),
#[error("Leopard codec error: {0}")]
LeopardCodec(String),
#[error("Missing header")]
MissingHeader,
#[error("Missing commit")]
MissingCommit,
#[error("Missing validator set")]
MissingValidatorSet,
#[error("Missing data availability header")]
MissingDataAvailabilityHeader,
#[error("Missing proof")]
MissingProof,
#[error("Missing shares")]
MissingShares,
#[error("Missing fee field")]
MissingFee,
#[error("Missing sum field")]
MissingSum,
#[error("Missing mode info field")]
MissingModeInfo,
#[error("Missing bitarray field")]
MissingBitarray,
#[error("Bit array to large")]
BitarrayTooLarge,
#[error("CompactBitArray malformed")]
MalformedCompactBitArray,
#[error("Wrong proof type")]
WrongProofType,
#[error("Unsupported share version: {0}")]
UnsupportedShareVersion(u8),
#[error("Invalid share size: {0}")]
InvalidShareSize(u64),
#[error("Invalid nmt leaf size: {0}")]
InvalidNmtLeafSize(u64),
#[error("Invalid nmt node order")]
InvalidNmtNodeOrder,
#[error(
"Sequence len must fit into {len} bytes, got value {0}",
len = appconsts::SEQUENCE_LEN_BYTES
)]
ShareSequenceLenExceeded(u64),
#[error("Invalid namespace v0")]
InvalidNamespaceV0,
#[error("Invalid namespace v255")]
InvalidNamespaceV255,
#[error("Invalid namespace hash: {0}")]
InvalidNamespacedHash(String),
#[error("Invalid index of signature in commit {0}, height {1}")]
InvalidSignatureIndex(u64, u64),
#[error("Invalid axis type: {0}")]
InvalidAxis(i32),
#[error("Invalid proof type: {0}")]
InvalidShwapProofType(i32),
#[error("Could not deserialize public key")]
InvalidPublicKey,
#[error("Range proof verification failed: {0:?}")]
RangeProofError(String),
#[error("Computed root doesn't match received one")]
RootMismatch,
#[error("Unexpected absent commit signature")]
UnexpectedAbsentSignature,
#[error("Validation error: {0}")]
Validation(String),
#[error("Verification error: {0}")]
Verification(String),
#[error(
"Share version has to be at most {ver}, got {0}",
ver = appconsts::MAX_SHARE_VERSION
)]
MaxShareVersionExceeded(u8),
#[error("Nmt error: {0}")]
Nmt(String),
#[error("Invalid address prefix: {0}")]
InvalidAddressPrefix(String),
#[error("Invalid address size: {0}")]
InvalidAddressSize(u64),
#[error("Invalid address: {0}")]
InvalidAddress(String),
#[error("Invalid coin amount: {0}")]
InvalidCoinAmount(String),
#[error("Invalid coin denomination: {0}")]
InvalidCoinDenomination(String),
#[error("Invalid balance")]
InvalidBalance(String),
#[error("Invalid Public Key")]
InvalidPublicKeyType(String),
#[error("Unsupported fraud proof type: {0}")]
UnsupportedFraudProofType(String),
#[error("Index ({0}) out of range ({1})")]
IndexOutOfRange(u64, u64),
#[error("Data square index out of range. row: {0}, column: {1}")]
EdsIndexOutOfRange(u16, u16),
#[error("Invalid dimensions of EDS")]
EdsInvalidDimentions,
#[error("Invalid zero block height")]
ZeroBlockHeight,
#[error("Expected first share of a blob")]
ExpectedShareWithSequenceStart,
#[error("Unexpected share from reserved namespace")]
UnexpectedReservedNamespace,
#[error("Unexpected start of a new blob")]
UnexpectedSequenceStart,
#[error("Metadata mismatch between shares in blob: {0}")]
BlobSharesMetadataMismatch(String),
#[error("Blob too large")]
BlobTooLarge,
#[error("NamespaceData too large")]
NamespaceDataTooLarge,
#[error("Invalid committment length")]
InvalidComittmentLength,
#[error("Missing signer field in blob")]
MissingSigner,
#[error("Signer is not supported in share version 0")]
SignerNotSupported,
#[error("Empty blob list")]
EmptyBlobList,
#[error("Missing DelegationResponse")]
MissingDelegationResponse,
#[error("Missing Delegation")]
MissingDelegation,
#[error("Missing Balance")]
MissingBalance,
#[error("Missing unbond")]
MissingUnbond,
#[error("Missing completion time")]
MissingCompletionTime,
#[error("Missing redelegation")]
MissingRedelegation,
#[error("Missing redelegation entry")]
MissingRedelegationEntry,
#[error("Invalid Cosmos decimal: {0}")]
InvalidCosmosDecimal(String),
}
#[cfg(feature = "uniffi")]
impl From<Error> for UniffiError {
fn from(value: Error) -> Self {
match value {
Error::UnsupportedNamespaceVersion(v) => UniffiError::UnsupportedNamespaceVersion(v),
Error::InvalidNamespaceSize => UniffiError::InvalidNamespaceSize,
Error::Tendermint(e) => UniffiError::Tendermint(e.to_string()),
Error::Protobuf(e) => UniffiError::Protobuf(e.to_string()),
Error::InvalidLength(recv, expected) => {
UniffiError::InvalidLength(recv as u64, expected as u64)
}
Error::LeopardCodec(e) => UniffiError::LeopardCodec(e.to_string()),
Error::MissingHeader => UniffiError::MissingHeader,
Error::MissingCommit => UniffiError::MissingCommit,
Error::MissingValidatorSet => UniffiError::MissingValidatorSet,
Error::MissingDataAvailabilityHeader => UniffiError::MissingDataAvailabilityHeader,
Error::MissingProof => UniffiError::MissingProof,
Error::MissingShares => UniffiError::MissingShares,
Error::MissingFee => UniffiError::MissingFee,
Error::MissingSum => UniffiError::MissingSum,
Error::MissingModeInfo => UniffiError::MissingModeInfo,
Error::MissingBitarray => UniffiError::MissingBitarray,
Error::BitarrayTooLarge => UniffiError::BitarrayTooLarge,
Error::MalformedCompactBitArray => UniffiError::MalformedCompactBitArray,
Error::WrongProofType => UniffiError::WrongProofType,
Error::UnsupportedShareVersion(v) => UniffiError::UnsupportedShareVersion(v),
Error::InvalidShareSize(s) => {
UniffiError::InvalidShareSize(s.try_into().expect("size to fit"))
}
Error::InvalidNmtLeafSize(s) => {
UniffiError::InvalidNmtLeafSize(s.try_into().expect("size to fit"))
}
Error::InvalidNmtNodeOrder => UniffiError::InvalidNmtNodeOrder,
Error::ShareSequenceLenExceeded(l) => {
UniffiError::ShareSequenceLenExceeded(l.try_into().expect("length to fit"))
}
Error::InvalidNamespaceV0 => UniffiError::InvalidNamespaceV0,
Error::InvalidNamespaceV255 => UniffiError::InvalidNamespaceV255,
Error::InvalidNamespacedHash(h) => UniffiError::InvalidNamespacedHash(h.to_string()),
Error::InvalidSignatureIndex(i, h) => {
UniffiError::InvalidSignatureIndex(i.try_into().expect("index to fit"), h)
}
Error::InvalidAxis(a) => UniffiError::InvalidAxis(a),
Error::InvalidShwapProofType(t) => UniffiError::InvalidShwapProofType(t),
Error::InvalidPublicKey => UniffiError::InvalidPublicKey,
Error::RangeProofError(e) => UniffiError::RangeProofError(format!("{e:?}")),
Error::RootMismatch => UniffiError::RootMismatch,
Error::UnexpectedAbsentSignature => UniffiError::UnexpectedAbsentSignature,
Error::Validation(e) => UniffiError::Validation(e.to_string()),
Error::Verification(e) => UniffiError::Verification(e.to_string()),
Error::MaxShareVersionExceeded(v) => UniffiError::MaxShareVersionExceeded(v),
Error::Nmt(e) => UniffiError::Nmt(e.to_string()),
Error::InvalidAddressPrefix(p) => UniffiError::InvalidAddressPrefix(p),
Error::InvalidAddressSize(s) => {
UniffiError::InvalidAddressSize(s.try_into().expect("size to fit"))
}
Error::InvalidAddress(a) => UniffiError::InvalidAddress(a),
Error::InvalidCoinAmount(a) => UniffiError::InvalidCoinAmount(a),
Error::InvalidCoinDenomination(d) => UniffiError::InvalidCoinDenomination(d),
Error::InvalidBalance(b) => UniffiError::InvalidBalance(b),
Error::InvalidPublicKeyType(t) => UniffiError::InvalidPublicKeyType(t),
Error::UnsupportedFraudProofType(t) => UniffiError::UnsupportedFraudProofType(t),
Error::IndexOutOfRange(i, r) => UniffiError::IndexOutOfRange(
i.try_into().expect("index to fit"),
r.try_into().expect("range to fit"),
),
Error::EdsIndexOutOfRange(r, c) => UniffiError::EdsIndexOutOfRange(r, c),
Error::EdsInvalidDimentions => UniffiError::EdsInvalidDimentions,
Error::ZeroBlockHeight => UniffiError::ZeroBlockHeight,
Error::ExpectedShareWithSequenceStart => UniffiError::ExpectedShareWithSequenceStart,
Error::UnexpectedReservedNamespace => UniffiError::UnexpectedReservedNamespace,
Error::UnexpectedSequenceStart => UniffiError::UnexpectedSequenceStart,
Error::BlobSharesMetadataMismatch(s) => UniffiError::BlobSharesMetadataMismatch(s),
Error::BlobTooLarge => UniffiError::BlobTooLarge,
Error::NamespaceDataTooLarge => UniffiError::NamespaceDataTooLarge,
Error::InvalidComittmentLength => UniffiError::InvalidComittmentLength,
Error::MissingSigner => UniffiError::MissingSigner,
Error::SignerNotSupported => UniffiError::SignerNotSupported,
Error::EmptyBlobList => UniffiError::EmptyBlobList,
Error::MissingDelegationResponse => UniffiError::MissingDelegationResponse,
Error::MissingDelegation => UniffiError::MissingDelegation,
Error::MissingBalance => UniffiError::MissingBalance,
Error::MissingUnbond => UniffiError::MissingUnbond,
Error::MissingCompletionTime => UniffiError::MissingCompletionTime,
Error::MissingRedelegation => UniffiError::MissingRedelegation,
Error::MissingRedelegationEntry => UniffiError::MissingRedelegationEntry,
Error::InvalidCosmosDecimal(s) => UniffiError::InvalidCosmosDecimal(s),
}
}
}