1use crate::consts::appconsts;
2
3pub type Result<T, E = Error> = std::result::Result<T, E>;
7
8#[cfg(feature = "uniffi")]
12pub type UniffiResult<T> = std::result::Result<T, UniffiError>;
13
14#[derive(Debug, thiserror::Error)]
18pub enum Error {
19 #[error("Unsupported namespace version: {0}")]
21 UnsupportedNamespaceVersion(u8),
22
23 #[error("Unsupported app version: {0}")]
25 UnsupportedAppVersion(u64),
26
27 #[error("Invalid namespace size")]
29 InvalidNamespaceSize,
30
31 #[error(transparent)]
33 Tendermint(#[from] tendermint::Error),
34
35 #[error("Length ({0}) different than expected ({1})")]
37 InvalidLength(usize, usize),
38
39 #[error(transparent)]
41 Protobuf(#[from] tendermint_proto::Error),
42
43 #[error(transparent)]
45 LeopardCodec(#[from] leopard_codec::LeopardError),
46
47 #[error("Missing header")]
49 MissingHeader,
50
51 #[error("Missing commit")]
53 MissingCommit,
54
55 #[error("Missing validator set")]
57 MissingValidatorSet,
58
59 #[error("Missing data availability header")]
61 MissingDataAvailabilityHeader,
62
63 #[error("Missing proof")]
65 MissingProof,
66
67 #[error("Missing shares")]
69 MissingShares,
70
71 #[error("Missing fee field")]
73 MissingFee,
74
75 #[error("Missing sum field")]
77 MissingSum,
78
79 #[error("Missing mode info field")]
81 MissingModeInfo,
82
83 #[error("Missing bitarray field")]
85 MissingBitarray,
86
87 #[error("Bit array to large")]
89 BitarrayTooLarge,
90
91 #[error("CompactBitArray malformed")]
93 MalformedCompactBitArray,
94
95 #[error("Wrong proof type")]
97 WrongProofType,
98
99 #[error("Unsupported share version: {0}")]
101 UnsupportedShareVersion(u8),
102
103 #[error("Invalid share size: {0}")]
105 InvalidShareSize(usize),
106
107 #[error("Invalid nmt leaf size: {0}")]
109 InvalidNmtLeafSize(usize),
110
111 #[error("Invalid nmt node order")]
113 InvalidNmtNodeOrder,
114
115 #[error(
117 "Sequence len must fit into {len} bytes, got value {0}",
118 len = appconsts::SEQUENCE_LEN_BYTES
119 )]
120 ShareSequenceLenExceeded(usize),
121
122 #[error("Invalid namespace v0")]
124 InvalidNamespaceV0,
125
126 #[error("Invalid namespace v255")]
128 InvalidNamespaceV255,
129
130 #[error(transparent)]
132 InvalidNamespacedHash(#[from] nmt_rs::InvalidNamespacedHash),
133
134 #[error("Invalid index of signature in commit {0}, height {1}")]
136 InvalidSignatureIndex(usize, u64),
137
138 #[error("Invalid axis type: {0}")]
140 InvalidAxis(i32),
141
142 #[error("Invalid proof type: {0}")]
144 InvalidShwapProofType(i32),
145
146 #[error("Could not deserialize public key")]
148 InvalidPublicKey,
149
150 #[error("Range proof verification failed: {0:?}")]
152 RangeProofError(nmt_rs::simple_merkle::error::RangeProofError),
153
154 #[error("Computed root doesn't match received one")]
156 RootMismatch,
157
158 #[error("Unexpected absent commit signature")]
160 UnexpectedAbsentSignature,
161
162 #[error("Validation error: {0}")]
164 Validation(#[from] ValidationError),
165
166 #[error("Verification error: {0}")]
168 Verification(#[from] VerificationError),
169
170 #[error(
172 "Share version has to be at most {ver}, got {0}",
173 ver = appconsts::MAX_SHARE_VERSION
174 )]
175 MaxShareVersionExceeded(u8),
176
177 #[error("Nmt error: {0}")]
179 Nmt(&'static str),
180
181 #[error("Invalid address prefix: {0}")]
183 InvalidAddressPrefix(String),
184
185 #[error("Invalid address size: {0}")]
187 InvalidAddressSize(usize),
188
189 #[error("Invalid address: {0}")]
191 InvalidAddress(String),
192
193 #[error("Invalid coin amount: {0}")]
195 InvalidCoinAmount(String),
196
197 #[error("Invalid coin denomination: {0}")]
199 InvalidCoinDenomination(String),
200
201 #[error("Invalid balance: {0:?}")]
203 InvalidBalance(String),
204
205 #[error("Invalid Public Key")]
207 InvalidPublicKeyType(String),
208
209 #[error("Unsupported fraud proof type: {0}")]
211 UnsupportedFraudProofType(String),
212
213 #[error("Index ({0}) out of range ({1})")]
215 IndexOutOfRange(usize, usize),
216
217 #[error("Data square index out of range. row: {0}, column: {1}")]
219 EdsIndexOutOfRange(u16, u16),
220
221 #[error("Invalid dimensions of EDS")]
223 EdsInvalidDimentions,
224
225 #[error("Invalid zero block height")]
227 ZeroBlockHeight,
228
229 #[error("Expected first share of a blob")]
231 ExpectedShareWithSequenceStart,
232
233 #[error("Unexpected share from reserved namespace")]
235 UnexpectedReservedNamespace,
236
237 #[error("Unexpected start of a new blob")]
239 UnexpectedSequenceStart,
240
241 #[error("Metadata mismatch between shares in blob: {0}")]
243 BlobSharesMetadataMismatch(String),
244
245 #[error("Blob too large")]
247 BlobTooLarge,
248
249 #[error("NamespaceData too large")]
251 NamespaceDataTooLarge,
252
253 #[error("Invalid committment length")]
255 InvalidComittmentLength,
256
257 #[error("Missing signer field in blob")]
259 MissingSigner,
260
261 #[error("Signer is not supported in share version 0")]
263 SignerNotSupported,
264
265 #[error("Empty blob list")]
267 EmptyBlobList,
268
269 #[error("Missing belegation response")]
271 MissingDelegationResponse,
272
273 #[error("Missing delegation")]
275 MissingDelegation,
276
277 #[error("Missing balance")]
279 MissingBalance,
280
281 #[error("Missing unbond")]
283 MissingUnbond,
284
285 #[error("Missing completion time")]
287 MissingCompletionTime,
288
289 #[error("Missing redelegation")]
291 MissingRedelegation,
292
293 #[error("Missing redelegation entry")]
295 MissingRedelegationEntry,
296
297 #[error("Invalid Cosmos decimal: {0}")]
299 InvalidCosmosDecimal(String),
300}
301
302impl From<prost::DecodeError> for Error {
303 fn from(value: prost::DecodeError) -> Self {
304 Error::Protobuf(tendermint_proto::Error::decode_message(value))
305 }
306}
307
308#[cfg(all(feature = "wasm-bindgen", target_arch = "wasm32"))]
309impl From<Error> for wasm_bindgen::JsValue {
310 fn from(value: Error) -> Self {
311 js_sys::Error::new(&value.to_string()).into()
312 }
313}
314
315#[derive(Debug, thiserror::Error)]
321pub enum ValidationError {
322 #[error("Not enought voting power (got {0}, needed {1})")]
324 NotEnoughVotingPower(u64, u64),
325
326 #[error("{0}")]
328 Other(String),
329}
330
331#[derive(Debug, thiserror::Error)]
337pub enum VerificationError {
338 #[error("Not enought voting power (got {0}, needed {1})")]
340 NotEnoughVotingPower(u64, u64),
341
342 #[error("{0}")]
344 Other(String),
345}
346
347macro_rules! validation_error {
348 ($fmt:literal $(,)?) => {
349 $crate::ValidationError::Other(std::format!($fmt))
350 };
351 ($fmt:literal, $($arg:tt)*) => {
352 $crate::ValidationError::Other(std::format!($fmt, $($arg)*))
353 };
354}
355
356macro_rules! bail_validation {
357 ($($arg:tt)*) => {
358 return Err($crate::validation_error!($($arg)*).into())
359 };
360}
361
362macro_rules! verification_error {
363 ($fmt:literal $(,)?) => {
364 $crate::VerificationError::Other(std::format!($fmt))
365 };
366 ($fmt:literal, $($arg:tt)*) => {
367 $crate::VerificationError::Other(std::format!($fmt, $($arg)*))
368 };
369}
370
371macro_rules! bail_verification {
372 ($($arg:tt)*) => {
373 return Err($crate::verification_error!($($arg)*).into())
374 };
375}
376
377pub(crate) use bail_validation;
379pub(crate) use bail_verification;
380pub(crate) use validation_error;
381pub(crate) use verification_error;
382
383#[cfg(feature = "uniffi")]
385#[derive(Debug, uniffi::Error, thiserror::Error)]
386pub enum UniffiConversionError {
387 #[error("Invalid namespace length")]
389 InvalidNamespaceLength,
390
391 #[error("Invalid commitment hash length")]
393 InvalidCommitmentLength,
394
395 #[error("Invalid account id length")]
397 InvalidAccountIdLength,
398
399 #[error("Invalid hash length")]
401 InvalidHashLength,
402
403 #[error("Invalid chain id length")]
405 InvalidChainIdLength,
406
407 #[error("Invalid public key")]
409 InvalidPublicKey,
410
411 #[error("Invalid parts header {msg}")]
413 InvalidPartsHeader {
414 msg: String,
416 },
417
418 #[error("Timestamp out of range")]
420 TimestampOutOfRange,
421
422 #[error("Header heigth out of range")]
424 HeaderHeightOutOfRange,
425
426 #[error("Invalid signature length")]
428 InvalidSignatureLength,
429
430 #[error("Invalid round index")]
432 InvalidRoundIndex,
433
434 #[error("Invalid validator index")]
436 InvalidValidatorIndex,
437
438 #[error("Voting power out of range")]
440 InvalidVotingPower,
441
442 #[error("Invalid signed header")]
444 InvalidSignedHeader,
445
446 #[error("Could not generate commitment")]
448 CouldNotGenerateCommitment {
449 msg: String,
451 },
452
453 #[error("Invalid address")]
455 InvalidAddress {
456 msg: String,
458 },
459}
460
461#[cfg(feature = "uniffi")]
465#[derive(Debug, uniffi::Error, thiserror::Error)]
466pub enum UniffiError {
467 #[error("Unsupported namespace version: {0}")]
469 UnsupportedNamespaceVersion(u8),
470
471 #[error("Unsupported app version: {0}")]
473 UnsupportedAppVersion(u64),
474
475 #[error("Invalid namespace size")]
477 InvalidNamespaceSize,
478
479 #[error("Tendermint error: {0}")]
481 Tendermint(String),
482
483 #[error("tendermint_proto error: {0}")]
485 Protobuf(String),
486
487 #[error("Length ({0}) different than expected ({1})")]
489 InvalidLength(u64, u64),
490
491 #[error("Leopard codec error: {0}")]
493 LeopardCodec(String),
494
495 #[error("Missing header")]
497 MissingHeader,
498
499 #[error("Missing commit")]
501 MissingCommit,
502
503 #[error("Missing validator set")]
505 MissingValidatorSet,
506
507 #[error("Missing data availability header")]
509 MissingDataAvailabilityHeader,
510
511 #[error("Missing proof")]
513 MissingProof,
514
515 #[error("Missing shares")]
517 MissingShares,
518
519 #[error("Missing fee field")]
521 MissingFee,
522
523 #[error("Missing sum field")]
525 MissingSum,
526
527 #[error("Missing mode info field")]
529 MissingModeInfo,
530
531 #[error("Missing bitarray field")]
533 MissingBitarray,
534
535 #[error("Bit array to large")]
537 BitarrayTooLarge,
538
539 #[error("CompactBitArray malformed")]
541 MalformedCompactBitArray,
542
543 #[error("Wrong proof type")]
545 WrongProofType,
546
547 #[error("Unsupported share version: {0}")]
549 UnsupportedShareVersion(u8),
550
551 #[error("Invalid share size: {0}")]
553 InvalidShareSize(u64),
554
555 #[error("Invalid nmt leaf size: {0}")]
557 InvalidNmtLeafSize(u64),
558
559 #[error("Invalid nmt node order")]
561 InvalidNmtNodeOrder,
562
563 #[error(
565 "Sequence len must fit into {len} bytes, got value {0}",
566 len = appconsts::SEQUENCE_LEN_BYTES
567 )]
568 ShareSequenceLenExceeded(u64),
569
570 #[error("Invalid namespace v0")]
572 InvalidNamespaceV0,
573
574 #[error("Invalid namespace v255")]
576 InvalidNamespaceV255,
577
578 #[error("Invalid namespace hash: {0}")]
580 InvalidNamespacedHash(String),
581
582 #[error("Invalid index of signature in commit {0}, height {1}")]
584 InvalidSignatureIndex(u64, u64),
585
586 #[error("Invalid axis type: {0}")]
588 InvalidAxis(i32),
589
590 #[error("Invalid proof type: {0}")]
592 InvalidShwapProofType(i32),
593
594 #[error("Could not deserialize public key")]
596 InvalidPublicKey,
597
598 #[error("Range proof verification failed: {0:?}")]
600 RangeProofError(String),
601
602 #[error("Computed root doesn't match received one")]
604 RootMismatch,
605
606 #[error("Unexpected absent commit signature")]
608 UnexpectedAbsentSignature,
609
610 #[error("Validation error: {0}")]
612 Validation(String),
613
614 #[error("Verification error: {0}")]
616 Verification(String),
617
618 #[error(
620 "Share version has to be at most {ver}, got {0}",
621 ver = appconsts::MAX_SHARE_VERSION
622 )]
623 MaxShareVersionExceeded(u8),
624
625 #[error("Nmt error: {0}")]
627 Nmt(String),
628
629 #[error("Invalid address prefix: {0}")]
631 InvalidAddressPrefix(String),
632
633 #[error("Invalid address size: {0}")]
635 InvalidAddressSize(u64),
636
637 #[error("Invalid address: {0}")]
639 InvalidAddress(String),
640
641 #[error("Invalid coin amount: {0}")]
643 InvalidCoinAmount(String),
644
645 #[error("Invalid coin denomination: {0}")]
647 InvalidCoinDenomination(String),
648
649 #[error("Invalid balance")]
651 InvalidBalance(String),
652
653 #[error("Invalid Public Key")]
655 InvalidPublicKeyType(String),
656
657 #[error("Unsupported fraud proof type: {0}")]
659 UnsupportedFraudProofType(String),
660
661 #[error("Index ({0}) out of range ({1})")]
663 IndexOutOfRange(u64, u64),
664
665 #[error("Data square index out of range. row: {0}, column: {1}")]
667 EdsIndexOutOfRange(u16, u16),
668
669 #[error("Invalid dimensions of EDS")]
671 EdsInvalidDimentions,
672
673 #[error("Invalid zero block height")]
675 ZeroBlockHeight,
676
677 #[error("Expected first share of a blob")]
679 ExpectedShareWithSequenceStart,
680
681 #[error("Unexpected share from reserved namespace")]
683 UnexpectedReservedNamespace,
684
685 #[error("Unexpected start of a new blob")]
687 UnexpectedSequenceStart,
688
689 #[error("Metadata mismatch between shares in blob: {0}")]
691 BlobSharesMetadataMismatch(String),
692
693 #[error("Blob too large")]
695 BlobTooLarge,
696
697 #[error("NamespaceData too large")]
699 NamespaceDataTooLarge,
700
701 #[error("Invalid committment length")]
703 InvalidComittmentLength,
704
705 #[error("Missing signer field in blob")]
707 MissingSigner,
708
709 #[error("Signer is not supported in share version 0")]
711 SignerNotSupported,
712
713 #[error("Empty blob list")]
715 EmptyBlobList,
716
717 #[error("Missing DelegationResponse")]
719 MissingDelegationResponse,
720
721 #[error("Missing Delegation")]
723 MissingDelegation,
724
725 #[error("Missing Balance")]
727 MissingBalance,
728
729 #[error("Missing unbond")]
731 MissingUnbond,
732
733 #[error("Missing completion time")]
735 MissingCompletionTime,
736
737 #[error("Missing redelegation")]
739 MissingRedelegation,
740
741 #[error("Missing redelegation entry")]
743 MissingRedelegationEntry,
744
745 #[error("Invalid Cosmos decimal: {0}")]
747 InvalidCosmosDecimal(String),
748}
749
750#[cfg(feature = "uniffi")]
751impl From<Error> for UniffiError {
752 fn from(value: Error) -> Self {
753 match value {
754 Error::UnsupportedNamespaceVersion(v) => UniffiError::UnsupportedNamespaceVersion(v),
755 Error::UnsupportedAppVersion(v) => UniffiError::UnsupportedAppVersion(v),
756 Error::InvalidNamespaceSize => UniffiError::InvalidNamespaceSize,
757 Error::Tendermint(e) => UniffiError::Tendermint(e.to_string()),
758 Error::Protobuf(e) => UniffiError::Protobuf(e.to_string()),
759 Error::InvalidLength(recv, expected) => {
760 UniffiError::InvalidLength(recv as u64, expected as u64)
761 }
762 Error::LeopardCodec(e) => UniffiError::LeopardCodec(e.to_string()),
763 Error::MissingHeader => UniffiError::MissingHeader,
764 Error::MissingCommit => UniffiError::MissingCommit,
765 Error::MissingValidatorSet => UniffiError::MissingValidatorSet,
766 Error::MissingDataAvailabilityHeader => UniffiError::MissingDataAvailabilityHeader,
767 Error::MissingProof => UniffiError::MissingProof,
768 Error::MissingShares => UniffiError::MissingShares,
769 Error::MissingFee => UniffiError::MissingFee,
770 Error::MissingSum => UniffiError::MissingSum,
771 Error::MissingModeInfo => UniffiError::MissingModeInfo,
772 Error::MissingBitarray => UniffiError::MissingBitarray,
773 Error::BitarrayTooLarge => UniffiError::BitarrayTooLarge,
774 Error::MalformedCompactBitArray => UniffiError::MalformedCompactBitArray,
775 Error::WrongProofType => UniffiError::WrongProofType,
776 Error::UnsupportedShareVersion(v) => UniffiError::UnsupportedShareVersion(v),
777 Error::InvalidShareSize(s) => {
778 UniffiError::InvalidShareSize(s.try_into().expect("size to fit"))
779 }
780 Error::InvalidNmtLeafSize(s) => {
781 UniffiError::InvalidNmtLeafSize(s.try_into().expect("size to fit"))
782 }
783 Error::InvalidNmtNodeOrder => UniffiError::InvalidNmtNodeOrder,
784 Error::ShareSequenceLenExceeded(l) => {
785 UniffiError::ShareSequenceLenExceeded(l.try_into().expect("length to fit"))
786 }
787 Error::InvalidNamespaceV0 => UniffiError::InvalidNamespaceV0,
788 Error::InvalidNamespaceV255 => UniffiError::InvalidNamespaceV255,
789 Error::InvalidNamespacedHash(h) => UniffiError::InvalidNamespacedHash(h.to_string()),
790 Error::InvalidSignatureIndex(i, h) => {
791 UniffiError::InvalidSignatureIndex(i.try_into().expect("index to fit"), h)
792 }
793 Error::InvalidAxis(a) => UniffiError::InvalidAxis(a),
794 Error::InvalidShwapProofType(t) => UniffiError::InvalidShwapProofType(t),
795 Error::InvalidPublicKey => UniffiError::InvalidPublicKey,
796 Error::RangeProofError(e) => UniffiError::RangeProofError(format!("{e:?}")),
797 Error::RootMismatch => UniffiError::RootMismatch,
798 Error::UnexpectedAbsentSignature => UniffiError::UnexpectedAbsentSignature,
799 Error::Validation(e) => UniffiError::Validation(e.to_string()),
800 Error::Verification(e) => UniffiError::Verification(e.to_string()),
801 Error::MaxShareVersionExceeded(v) => UniffiError::MaxShareVersionExceeded(v),
802 Error::Nmt(e) => UniffiError::Nmt(e.to_string()),
803 Error::InvalidAddressPrefix(p) => UniffiError::InvalidAddressPrefix(p),
804 Error::InvalidAddressSize(s) => {
805 UniffiError::InvalidAddressSize(s.try_into().expect("size to fit"))
806 }
807 Error::InvalidAddress(a) => UniffiError::InvalidAddress(a),
808 Error::InvalidCoinAmount(a) => UniffiError::InvalidCoinAmount(a),
809 Error::InvalidCoinDenomination(d) => UniffiError::InvalidCoinDenomination(d),
810 Error::InvalidBalance(b) => UniffiError::InvalidBalance(b),
811 Error::InvalidPublicKeyType(t) => UniffiError::InvalidPublicKeyType(t),
812 Error::UnsupportedFraudProofType(t) => UniffiError::UnsupportedFraudProofType(t),
813 Error::IndexOutOfRange(i, r) => UniffiError::IndexOutOfRange(
814 i.try_into().expect("index to fit"),
815 r.try_into().expect("range to fit"),
816 ),
817 Error::EdsIndexOutOfRange(r, c) => UniffiError::EdsIndexOutOfRange(r, c),
818 Error::EdsInvalidDimentions => UniffiError::EdsInvalidDimentions,
819 Error::ZeroBlockHeight => UniffiError::ZeroBlockHeight,
820 Error::ExpectedShareWithSequenceStart => UniffiError::ExpectedShareWithSequenceStart,
821 Error::UnexpectedReservedNamespace => UniffiError::UnexpectedReservedNamespace,
822 Error::UnexpectedSequenceStart => UniffiError::UnexpectedSequenceStart,
823 Error::BlobSharesMetadataMismatch(s) => UniffiError::BlobSharesMetadataMismatch(s),
824 Error::BlobTooLarge => UniffiError::BlobTooLarge,
825 Error::NamespaceDataTooLarge => UniffiError::NamespaceDataTooLarge,
826 Error::InvalidComittmentLength => UniffiError::InvalidComittmentLength,
827 Error::MissingSigner => UniffiError::MissingSigner,
828 Error::SignerNotSupported => UniffiError::SignerNotSupported,
829 Error::EmptyBlobList => UniffiError::EmptyBlobList,
830 Error::MissingDelegationResponse => UniffiError::MissingDelegationResponse,
831 Error::MissingDelegation => UniffiError::MissingDelegation,
832 Error::MissingBalance => UniffiError::MissingBalance,
833 Error::MissingUnbond => UniffiError::MissingUnbond,
834 Error::MissingCompletionTime => UniffiError::MissingCompletionTime,
835 Error::MissingRedelegation => UniffiError::MissingRedelegation,
836 Error::MissingRedelegationEntry => UniffiError::MissingRedelegationEntry,
837 Error::InvalidCosmosDecimal(s) => UniffiError::InvalidCosmosDecimal(s),
838 }
839 }
840}