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(transparent)]
37 Protobuf(#[from] tendermint_proto::Error),
38
39 #[error(transparent)]
41 Multihash(#[from] cid::multihash::Error),
42
43 #[error(transparent)]
45 CidError(#[from] blockstore::block::CidError),
46
47 #[error(transparent)]
49 LeopardCodec(#[from] leopard_codec::LeopardError),
50
51 #[error("Missing header")]
53 MissingHeader,
54
55 #[error("Missing commit")]
57 MissingCommit,
58
59 #[error("Missing validator set")]
61 MissingValidatorSet,
62
63 #[error("Missing data availability header")]
65 MissingDataAvailabilityHeader,
66
67 #[error("Missing proof")]
69 MissingProof,
70
71 #[error("Missing shares")]
73 MissingShares,
74
75 #[error("Missing fee field")]
77 MissingFee,
78
79 #[error("Missing sum field")]
81 MissingSum,
82
83 #[error("Missing mode info field")]
85 MissingModeInfo,
86
87 #[error("Missing bitarray field")]
89 MissingBitarray,
90
91 #[error("Bit array to large")]
93 BitarrayTooLarge,
94
95 #[error("CompactBitArray malformed")]
97 MalformedCompactBitArray,
98
99 #[error("Wrong proof type")]
101 WrongProofType,
102
103 #[error("Unsupported share version: {0}")]
105 UnsupportedShareVersion(u8),
106
107 #[error("Invalid share size: {0}")]
109 InvalidShareSize(usize),
110
111 #[error("Invalid nmt leaf size: {0}")]
113 InvalidNmtLeafSize(usize),
114
115 #[error("Invalid nmt node order")]
117 InvalidNmtNodeOrder,
118
119 #[error(
121 "Sequence len must fit into {} bytes, got value {0}",
122 appconsts::SEQUENCE_LEN_BYTES
123 )]
124 ShareSequenceLenExceeded(usize),
125
126 #[error("Invalid namespace v0")]
128 InvalidNamespaceV0,
129
130 #[error("Invalid namespace v255")]
132 InvalidNamespaceV255,
133
134 #[error(transparent)]
136 InvalidNamespacedHash(#[from] nmt_rs::InvalidNamespacedHash),
137
138 #[error("Invalid index of signature in commit {0}, height {1}")]
140 InvalidSignatureIndex(usize, u64),
141
142 #[error("Invalid axis type: {0}")]
144 InvalidAxis(i32),
145
146 #[error("Invalid proof type: {0}")]
148 InvalidShwapProofType(i32),
149
150 #[error("Could not deserialize public key")]
152 InvalidPublicKey,
153
154 #[error("Range proof verification failed: {0:?}")]
156 RangeProofError(nmt_rs::simple_merkle::error::RangeProofError),
157
158 #[error("Computed root doesn't match received one")]
160 RootMismatch,
161
162 #[error("Unexpected absent commit signature")]
164 UnexpectedAbsentSignature,
165
166 #[error("Validation error: {0}")]
168 Validation(#[from] ValidationError),
169
170 #[error("Verification error: {0}")]
172 Verification(#[from] VerificationError),
173
174 #[error(
176 "Share version has to be at most {}, got {0}",
177 appconsts::MAX_SHARE_VERSION
178 )]
179 MaxShareVersionExceeded(u8),
180
181 #[error("Nmt error: {0}")]
183 Nmt(&'static str),
184
185 #[error("Invalid address prefix: {0}")]
187 InvalidAddressPrefix(String),
188
189 #[error("Invalid address size: {0}")]
191 InvalidAddressSize(usize),
192
193 #[error("Invalid address: {0}")]
195 InvalidAddress(String),
196
197 #[error("Invalid coin amount: {0}")]
199 InvalidCoinAmount(String),
200
201 #[error("Invalid coin denomination: {0}")]
203 InvalidCoinDenomination(String),
204
205 #[error("Invalid balance: {0:?}")]
207 InvalidBalance(String),
208
209 #[error("Invalid Public Key")]
211 InvalidPublicKeyType(String),
212
213 #[error("Unsupported fraud proof type: {0}")]
215 UnsupportedFraudProofType(String),
216
217 #[error("Index ({0}) out of range ({1})")]
219 IndexOutOfRange(usize, usize),
220
221 #[error("Data square index out of range. row: {0}, column: {1}")]
223 EdsIndexOutOfRange(u16, u16),
224
225 #[error("Invalid dimensions of EDS")]
227 EdsInvalidDimentions,
228
229 #[error("Invalid zero block height")]
231 ZeroBlockHeight,
232
233 #[error("Expected first share of a blob")]
235 ExpectedShareWithSequenceStart,
236
237 #[error("Unexpected share from reserved namespace")]
239 UnexpectedReservedNamespace,
240
241 #[error("Unexpected start of a new blob")]
243 UnexpectedSequenceStart,
244
245 #[error("Metadata mismatch between shares in blob: {0}")]
247 BlobSharesMetadataMismatch(String),
248
249 #[error("Blob too large")]
251 BlobTooLarge,
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("Multihash error: {0}")]
489 Multihash(String),
490
491 #[error("Error creating or parsing CID: {0}")]
493 CidError(String),
494
495 #[error("Leopard codec error: {0}")]
497 LeopardCodec(String),
498
499 #[error("Missing header")]
501 MissingHeader,
502
503 #[error("Missing commit")]
505 MissingCommit,
506
507 #[error("Missing validator set")]
509 MissingValidatorSet,
510
511 #[error("Missing data availability header")]
513 MissingDataAvailabilityHeader,
514
515 #[error("Missing proof")]
517 MissingProof,
518
519 #[error("Missing shares")]
521 MissingShares,
522
523 #[error("Missing fee field")]
525 MissingFee,
526
527 #[error("Missing sum field")]
529 MissingSum,
530
531 #[error("Missing mode info field")]
533 MissingModeInfo,
534
535 #[error("Missing bitarray field")]
537 MissingBitarray,
538
539 #[error("Bit array to large")]
541 BitarrayTooLarge,
542
543 #[error("CompactBitArray malformed")]
545 MalformedCompactBitArray,
546
547 #[error("Wrong proof type")]
549 WrongProofType,
550
551 #[error("Unsupported share version: {0}")]
553 UnsupportedShareVersion(u8),
554
555 #[error("Invalid share size: {0}")]
557 InvalidShareSize(u64),
558
559 #[error("Invalid nmt leaf size: {0}")]
561 InvalidNmtLeafSize(u64),
562
563 #[error("Invalid nmt node order")]
565 InvalidNmtNodeOrder,
566
567 #[error(
569 "Sequence len must fit into {} bytes, got value {0}",
570 appconsts::SEQUENCE_LEN_BYTES
571 )]
572 ShareSequenceLenExceeded(u64),
573
574 #[error("Invalid namespace v0")]
576 InvalidNamespaceV0,
577
578 #[error("Invalid namespace v255")]
580 InvalidNamespaceV255,
581
582 #[error("Invalid namespace hash: {0}")]
584 InvalidNamespacedHash(String),
585
586 #[error("Invalid index of signature in commit {0}, height {1}")]
588 InvalidSignatureIndex(u64, u64),
589
590 #[error("Invalid axis type: {0}")]
592 InvalidAxis(i32),
593
594 #[error("Invalid proof type: {0}")]
596 InvalidShwapProofType(i32),
597
598 #[error("Could not deserialize public key")]
600 InvalidPublicKey,
601
602 #[error("Range proof verification failed: {0:?}")]
604 RangeProofError(String),
605
606 #[error("Computed root doesn't match received one")]
608 RootMismatch,
609
610 #[error("Unexpected absent commit signature")]
612 UnexpectedAbsentSignature,
613
614 #[error("Validation error: {0}")]
616 Validation(String),
617
618 #[error("Verification error: {0}")]
620 Verification(String),
621
622 #[error(
624 "Share version has to be at most {}, got {0}",
625 appconsts::MAX_SHARE_VERSION
626 )]
627 MaxShareVersionExceeded(u8),
628
629 #[error("Nmt error: {0}")]
631 Nmt(String),
632
633 #[error("Invalid address prefix: {0}")]
635 InvalidAddressPrefix(String),
636
637 #[error("Invalid address size: {0}")]
639 InvalidAddressSize(u64),
640
641 #[error("Invalid address: {0}")]
643 InvalidAddress(String),
644
645 #[error("Invalid coin amount: {0}")]
647 InvalidCoinAmount(String),
648
649 #[error("Invalid coin denomination: {0}")]
651 InvalidCoinDenomination(String),
652
653 #[error("Invalid balance")]
655 InvalidBalance(String),
656
657 #[error("Invalid Public Key")]
659 InvalidPublicKeyType(String),
660
661 #[error("Unsupported fraud proof type: {0}")]
663 UnsupportedFraudProofType(String),
664
665 #[error("Index ({0}) out of range ({1})")]
667 IndexOutOfRange(u64, u64),
668
669 #[error("Data square index out of range. row: {0}, column: {1}")]
671 EdsIndexOutOfRange(u16, u16),
672
673 #[error("Invalid dimensions of EDS")]
675 EdsInvalidDimentions,
676
677 #[error("Invalid zero block height")]
679 ZeroBlockHeight,
680
681 #[error("Expected first share of a blob")]
683 ExpectedShareWithSequenceStart,
684
685 #[error("Unexpected share from reserved namespace")]
687 UnexpectedReservedNamespace,
688
689 #[error("Unexpected start of a new blob")]
691 UnexpectedSequenceStart,
692
693 #[error("Metadata mismatch between shares in blob: {0}")]
695 BlobSharesMetadataMismatch(String),
696
697 #[error("Blob too large")]
699 BlobTooLarge,
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::Multihash(e) => UniffiError::Multihash(e.to_string()),
760 Error::CidError(e) => UniffiError::CidError(e.to_string()),
761 Error::LeopardCodec(e) => UniffiError::LeopardCodec(e.to_string()),
762 Error::MissingHeader => UniffiError::MissingHeader,
763 Error::MissingCommit => UniffiError::MissingCommit,
764 Error::MissingValidatorSet => UniffiError::MissingValidatorSet,
765 Error::MissingDataAvailabilityHeader => UniffiError::MissingDataAvailabilityHeader,
766 Error::MissingProof => UniffiError::MissingProof,
767 Error::MissingShares => UniffiError::MissingShares,
768 Error::MissingFee => UniffiError::MissingFee,
769 Error::MissingSum => UniffiError::MissingSum,
770 Error::MissingModeInfo => UniffiError::MissingModeInfo,
771 Error::MissingBitarray => UniffiError::MissingBitarray,
772 Error::BitarrayTooLarge => UniffiError::BitarrayTooLarge,
773 Error::MalformedCompactBitArray => UniffiError::MalformedCompactBitArray,
774 Error::WrongProofType => UniffiError::WrongProofType,
775 Error::UnsupportedShareVersion(v) => UniffiError::UnsupportedShareVersion(v),
776 Error::InvalidShareSize(s) => {
777 UniffiError::InvalidShareSize(s.try_into().expect("size to fit"))
778 }
779 Error::InvalidNmtLeafSize(s) => {
780 UniffiError::InvalidNmtLeafSize(s.try_into().expect("size to fit"))
781 }
782 Error::InvalidNmtNodeOrder => UniffiError::InvalidNmtNodeOrder,
783 Error::ShareSequenceLenExceeded(l) => {
784 UniffiError::ShareSequenceLenExceeded(l.try_into().expect("length to fit"))
785 }
786 Error::InvalidNamespaceV0 => UniffiError::InvalidNamespaceV0,
787 Error::InvalidNamespaceV255 => UniffiError::InvalidNamespaceV255,
788 Error::InvalidNamespacedHash(h) => UniffiError::InvalidNamespacedHash(h.to_string()),
789 Error::InvalidSignatureIndex(i, h) => {
790 UniffiError::InvalidSignatureIndex(i.try_into().expect("index to fit"), h)
791 }
792 Error::InvalidAxis(a) => UniffiError::InvalidAxis(a),
793 Error::InvalidShwapProofType(t) => UniffiError::InvalidShwapProofType(t),
794 Error::InvalidPublicKey => UniffiError::InvalidPublicKey,
795 Error::RangeProofError(e) => UniffiError::RangeProofError(format!("{e:?}")),
796 Error::RootMismatch => UniffiError::RootMismatch,
797 Error::UnexpectedAbsentSignature => UniffiError::UnexpectedAbsentSignature,
798 Error::Validation(e) => UniffiError::Validation(e.to_string()),
799 Error::Verification(e) => UniffiError::Verification(e.to_string()),
800 Error::MaxShareVersionExceeded(v) => UniffiError::MaxShareVersionExceeded(v),
801 Error::Nmt(e) => UniffiError::Nmt(e.to_string()),
802 Error::InvalidAddressPrefix(p) => UniffiError::InvalidAddressPrefix(p),
803 Error::InvalidAddressSize(s) => {
804 UniffiError::InvalidAddressSize(s.try_into().expect("size to fit"))
805 }
806 Error::InvalidAddress(a) => UniffiError::InvalidAddress(a),
807 Error::InvalidCoinAmount(a) => UniffiError::InvalidCoinAmount(a),
808 Error::InvalidCoinDenomination(d) => UniffiError::InvalidCoinDenomination(d),
809 Error::InvalidBalance(b) => UniffiError::InvalidBalance(b),
810 Error::InvalidPublicKeyType(t) => UniffiError::InvalidPublicKeyType(t),
811 Error::UnsupportedFraudProofType(t) => UniffiError::UnsupportedFraudProofType(t),
812 Error::IndexOutOfRange(i, r) => UniffiError::IndexOutOfRange(
813 i.try_into().expect("index to fit"),
814 r.try_into().expect("range to fit"),
815 ),
816 Error::EdsIndexOutOfRange(r, c) => UniffiError::EdsIndexOutOfRange(r, c),
817 Error::EdsInvalidDimentions => UniffiError::EdsInvalidDimentions,
818 Error::ZeroBlockHeight => UniffiError::ZeroBlockHeight,
819 Error::ExpectedShareWithSequenceStart => UniffiError::ExpectedShareWithSequenceStart,
820 Error::UnexpectedReservedNamespace => UniffiError::UnexpectedReservedNamespace,
821 Error::UnexpectedSequenceStart => UniffiError::UnexpectedSequenceStart,
822 Error::BlobSharesMetadataMismatch(s) => UniffiError::BlobSharesMetadataMismatch(s),
823 Error::BlobTooLarge => UniffiError::BlobTooLarge,
824 Error::InvalidComittmentLength => UniffiError::InvalidComittmentLength,
825 Error::MissingSigner => UniffiError::MissingSigner,
826 Error::SignerNotSupported => UniffiError::SignerNotSupported,
827 Error::EmptyBlobList => UniffiError::EmptyBlobList,
828 Error::MissingDelegationResponse => UniffiError::MissingDelegationResponse,
829 Error::MissingDelegation => UniffiError::MissingDelegation,
830 Error::MissingBalance => UniffiError::MissingBalance,
831 Error::MissingUnbond => UniffiError::MissingUnbond,
832 Error::MissingCompletionTime => UniffiError::MissingCompletionTime,
833 Error::MissingRedelegation => UniffiError::MissingRedelegation,
834 Error::MissingRedelegationEntry => UniffiError::MissingRedelegationEntry,
835 Error::InvalidCosmosDecimal(s) => UniffiError::InvalidCosmosDecimal(s),
836 }
837 }
838}