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