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 balance denomination: {0}")]
199 InvalidBalanceDenomination(String),
200
201 #[error("Invalid balance amount: {0}")]
203 InvalidBalanceAmount(String),
204
205 #[error("Invalid coin amount: {0}")]
207 InvalidCoinAmount(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
270impl From<prost::DecodeError> for Error {
271 fn from(value: prost::DecodeError) -> Self {
272 Error::Protobuf(tendermint_proto::Error::decode_message(value))
273 }
274}
275
276#[cfg(all(feature = "wasm-bindgen", target_arch = "wasm32"))]
277impl From<Error> for wasm_bindgen::JsValue {
278 fn from(value: Error) -> Self {
279 js_sys::Error::new(&value.to_string()).into()
280 }
281}
282
283#[derive(Debug, thiserror::Error)]
289pub enum ValidationError {
290 #[error("Not enought voting power (got {0}, needed {1})")]
292 NotEnoughVotingPower(u64, u64),
293
294 #[error("{0}")]
296 Other(String),
297}
298
299#[derive(Debug, thiserror::Error)]
305pub enum VerificationError {
306 #[error("Not enought voting power (got {0}, needed {1})")]
308 NotEnoughVotingPower(u64, u64),
309
310 #[error("{0}")]
312 Other(String),
313}
314
315macro_rules! validation_error {
316 ($fmt:literal $(,)?) => {
317 $crate::ValidationError::Other(std::format!($fmt))
318 };
319 ($fmt:literal, $($arg:tt)*) => {
320 $crate::ValidationError::Other(std::format!($fmt, $($arg)*))
321 };
322}
323
324macro_rules! bail_validation {
325 ($($arg:tt)*) => {
326 return Err($crate::validation_error!($($arg)*).into())
327 };
328}
329
330macro_rules! verification_error {
331 ($fmt:literal $(,)?) => {
332 $crate::VerificationError::Other(std::format!($fmt))
333 };
334 ($fmt:literal, $($arg:tt)*) => {
335 $crate::VerificationError::Other(std::format!($fmt, $($arg)*))
336 };
337}
338
339macro_rules! bail_verification {
340 ($($arg:tt)*) => {
341 return Err($crate::verification_error!($($arg)*).into())
342 };
343}
344
345pub(crate) use bail_validation;
347pub(crate) use bail_verification;
348pub(crate) use validation_error;
349pub(crate) use verification_error;
350
351#[cfg(feature = "uniffi")]
353#[derive(Debug, uniffi::Error, thiserror::Error)]
354pub enum UniffiConversionError {
355 #[error("Invalid namespace length")]
357 InvalidNamespaceLength,
358
359 #[error("Invalid commitment hash length")]
361 InvalidCommitmentLength,
362
363 #[error("Invalid account id length")]
365 InvalidAccountIdLength,
366
367 #[error("Invalid hash length")]
369 InvalidHashLength,
370
371 #[error("Invalid chain id length")]
373 InvalidChainIdLength,
374
375 #[error("Invalid public key")]
377 InvalidPublicKey,
378
379 #[error("Invalid parts header {msg}")]
381 InvalidPartsHeader {
382 msg: String,
384 },
385
386 #[error("Timestamp out of range")]
388 TimestampOutOfRange,
389
390 #[error("Header heigth out of range")]
392 HeaderHeightOutOfRange,
393
394 #[error("Invalid signature length")]
396 InvalidSignatureLength,
397
398 #[error("Invalid round index")]
400 InvalidRoundIndex,
401
402 #[error("Invalid validator index")]
404 InvalidValidatorIndex,
405
406 #[error("Voting power out of range")]
408 InvalidVotingPower,
409
410 #[error("Invalid signed header")]
412 InvalidSignedHeader,
413
414 #[error("Could not generate commitment")]
416 CouldNotGenerateCommitment {
417 msg: String,
419 },
420
421 #[error("Invalid address")]
423 InvalidAddress {
424 msg: String,
426 },
427}
428
429#[cfg(feature = "uniffi")]
433#[derive(Debug, uniffi::Error, thiserror::Error)]
434pub enum UniffiError {
435 #[error("Unsupported namespace version: {0}")]
437 UnsupportedNamespaceVersion(u8),
438
439 #[error("Unsupported app version: {0}")]
441 UnsupportedAppVersion(u64),
442
443 #[error("Invalid namespace size")]
445 InvalidNamespaceSize,
446
447 #[error("Tendermint error: {0}")]
449 Tendermint(String),
450
451 #[error("tendermint_proto error: {0}")]
453 Protobuf(String),
454
455 #[error("Multihash error: {0}")]
457 Multihash(String),
458
459 #[error("Error creating or parsing CID: {0}")]
461 CidError(String),
462
463 #[error("Leopard codec error: {0}")]
465 LeopardCodec(String),
466
467 #[error("Missing header")]
469 MissingHeader,
470
471 #[error("Missing commit")]
473 MissingCommit,
474
475 #[error("Missing validator set")]
477 MissingValidatorSet,
478
479 #[error("Missing data availability header")]
481 MissingDataAvailabilityHeader,
482
483 #[error("Missing proof")]
485 MissingProof,
486
487 #[error("Missing shares")]
489 MissingShares,
490
491 #[error("Missing fee field")]
493 MissingFee,
494
495 #[error("Missing sum field")]
497 MissingSum,
498
499 #[error("Missing mode info field")]
501 MissingModeInfo,
502
503 #[error("Missing bitarray field")]
505 MissingBitarray,
506
507 #[error("Bit array to large")]
509 BitarrayTooLarge,
510
511 #[error("CompactBitArray malformed")]
513 MalformedCompactBitArray,
514
515 #[error("Wrong proof type")]
517 WrongProofType,
518
519 #[error("Unsupported share version: {0}")]
521 UnsupportedShareVersion(u8),
522
523 #[error("Invalid share size: {0}")]
525 InvalidShareSize(u64),
526
527 #[error("Invalid nmt leaf size: {0}")]
529 InvalidNmtLeafSize(u64),
530
531 #[error("Invalid nmt node order")]
533 InvalidNmtNodeOrder,
534
535 #[error(
537 "Sequence len must fit into {} bytes, got value {0}",
538 appconsts::SEQUENCE_LEN_BYTES
539 )]
540 ShareSequenceLenExceeded(u64),
541
542 #[error("Invalid namespace v0")]
544 InvalidNamespaceV0,
545
546 #[error("Invalid namespace v255")]
548 InvalidNamespaceV255,
549
550 #[error("Invalid namespace hash: {0}")]
552 InvalidNamespacedHash(String),
553
554 #[error("Invalid index of signature in commit {0}, height {1}")]
556 InvalidSignatureIndex(u64, u64),
557
558 #[error("Invalid axis type: {0}")]
560 InvalidAxis(i32),
561
562 #[error("Invalid proof type: {0}")]
564 InvalidShwapProofType(i32),
565
566 #[error("Could not deserialize public key")]
568 InvalidPublicKey,
569
570 #[error("Range proof verification failed: {0:?}")]
572 RangeProofError(String),
573
574 #[error("Computed root doesn't match received one")]
576 RootMismatch,
577
578 #[error("Unexpected absent commit signature")]
580 UnexpectedAbsentSignature,
581
582 #[error("Validation error: {0}")]
584 Validation(String),
585
586 #[error("Verification error: {0}")]
588 Verification(String),
589
590 #[error(
592 "Share version has to be at most {}, got {0}",
593 appconsts::MAX_SHARE_VERSION
594 )]
595 MaxShareVersionExceeded(u8),
596
597 #[error("Nmt error: {0}")]
599 Nmt(String),
600
601 #[error("Invalid address prefix: {0}")]
603 InvalidAddressPrefix(String),
604
605 #[error("Invalid address size: {0}")]
607 InvalidAddressSize(u64),
608
609 #[error("Invalid address: {0}")]
611 InvalidAddress(String),
612
613 #[error("Invalid balance denomination: {0}")]
615 InvalidBalanceDenomination(String),
616
617 #[error("Invalid balance amount: {0}")]
619 InvalidBalanceAmount(String),
620
621 #[error("Invalid coin amount: {0}")]
623 InvalidCoinAmount(String),
624
625 #[error("Invalid Public Key")]
627 InvalidPublicKeyType(String),
628
629 #[error("Unsupported fraud proof type: {0}")]
631 UnsupportedFraudProofType(String),
632
633 #[error("Index ({0}) out of range ({1})")]
635 IndexOutOfRange(u64, u64),
636
637 #[error("Data square index out of range. row: {0}, column: {1}")]
639 EdsIndexOutOfRange(u16, u16),
640
641 #[error("Invalid dimensions of EDS")]
643 EdsInvalidDimentions,
644
645 #[error("Invalid zero block height")]
647 ZeroBlockHeight,
648
649 #[error("Expected first share of a blob")]
651 ExpectedShareWithSequenceStart,
652
653 #[error("Unexpected share from reserved namespace")]
655 UnexpectedReservedNamespace,
656
657 #[error("Unexpected start of a new blob")]
659 UnexpectedSequenceStart,
660
661 #[error("Metadata mismatch between shares in blob: {0}")]
663 BlobSharesMetadataMismatch(String),
664
665 #[error("Blob too large")]
667 BlobTooLarge,
668
669 #[error("Invalid committment length")]
671 InvalidComittmentLength,
672
673 #[error("Missing signer field in blob")]
675 MissingSigner,
676
677 #[error("Signer is not supported in share version 0")]
679 SignerNotSupported,
680
681 #[error("Empty blob list")]
683 EmptyBlobList,
684}
685
686#[cfg(feature = "uniffi")]
687impl From<Error> for UniffiError {
688 fn from(value: Error) -> Self {
689 match value {
690 Error::UnsupportedNamespaceVersion(v) => UniffiError::UnsupportedNamespaceVersion(v),
691 Error::UnsupportedAppVersion(v) => UniffiError::UnsupportedAppVersion(v),
692 Error::InvalidNamespaceSize => UniffiError::InvalidNamespaceSize,
693 Error::Tendermint(e) => UniffiError::Tendermint(e.to_string()),
694 Error::Protobuf(e) => UniffiError::Protobuf(e.to_string()),
695 Error::Multihash(e) => UniffiError::Multihash(e.to_string()),
696 Error::CidError(e) => UniffiError::CidError(e.to_string()),
697 Error::LeopardCodec(e) => UniffiError::LeopardCodec(e.to_string()),
698 Error::MissingHeader => UniffiError::MissingHeader,
699 Error::MissingCommit => UniffiError::MissingCommit,
700 Error::MissingValidatorSet => UniffiError::MissingValidatorSet,
701 Error::MissingDataAvailabilityHeader => UniffiError::MissingDataAvailabilityHeader,
702 Error::MissingProof => UniffiError::MissingProof,
703 Error::MissingShares => UniffiError::MissingShares,
704 Error::MissingFee => UniffiError::MissingFee,
705 Error::MissingSum => UniffiError::MissingSum,
706 Error::MissingModeInfo => UniffiError::MissingModeInfo,
707 Error::MissingBitarray => UniffiError::MissingBitarray,
708 Error::BitarrayTooLarge => UniffiError::BitarrayTooLarge,
709 Error::MalformedCompactBitArray => UniffiError::MalformedCompactBitArray,
710 Error::WrongProofType => UniffiError::WrongProofType,
711 Error::UnsupportedShareVersion(v) => UniffiError::UnsupportedShareVersion(v),
712 Error::InvalidShareSize(s) => {
713 UniffiError::InvalidShareSize(s.try_into().expect("size to fit"))
714 }
715 Error::InvalidNmtLeafSize(s) => {
716 UniffiError::InvalidNmtLeafSize(s.try_into().expect("size to fit"))
717 }
718 Error::InvalidNmtNodeOrder => UniffiError::InvalidNmtNodeOrder,
719 Error::ShareSequenceLenExceeded(l) => {
720 UniffiError::ShareSequenceLenExceeded(l.try_into().expect("length to fit"))
721 }
722 Error::InvalidNamespaceV0 => UniffiError::InvalidNamespaceV0,
723 Error::InvalidNamespaceV255 => UniffiError::InvalidNamespaceV255,
724 Error::InvalidNamespacedHash(h) => UniffiError::InvalidNamespacedHash(h.to_string()),
725 Error::InvalidSignatureIndex(i, h) => {
726 UniffiError::InvalidSignatureIndex(i.try_into().expect("index to fit"), h)
727 }
728 Error::InvalidAxis(a) => UniffiError::InvalidAxis(a),
729 Error::InvalidShwapProofType(t) => UniffiError::InvalidShwapProofType(t),
730 Error::InvalidPublicKey => UniffiError::InvalidPublicKey,
731 Error::RangeProofError(e) => UniffiError::RangeProofError(format!("{e:?}")),
732 Error::RootMismatch => UniffiError::RootMismatch,
733 Error::UnexpectedAbsentSignature => UniffiError::UnexpectedAbsentSignature,
734 Error::Validation(e) => UniffiError::Validation(e.to_string()),
735 Error::Verification(e) => UniffiError::Verification(e.to_string()),
736 Error::MaxShareVersionExceeded(v) => UniffiError::MaxShareVersionExceeded(v),
737 Error::Nmt(e) => UniffiError::Nmt(e.to_string()),
738 Error::InvalidAddressPrefix(p) => UniffiError::InvalidAddressPrefix(p),
739 Error::InvalidAddressSize(s) => {
740 UniffiError::InvalidAddressSize(s.try_into().expect("size to fit"))
741 }
742 Error::InvalidAddress(a) => UniffiError::InvalidAddress(a),
743 Error::InvalidBalanceDenomination(d) => UniffiError::InvalidBalanceDenomination(d),
744 Error::InvalidBalanceAmount(a) => UniffiError::InvalidBalanceAmount(a),
745 Error::InvalidCoinAmount(a) => UniffiError::InvalidCoinAmount(a),
746 Error::InvalidPublicKeyType(t) => UniffiError::InvalidPublicKeyType(t),
747 Error::UnsupportedFraudProofType(t) => UniffiError::UnsupportedFraudProofType(t),
748 Error::IndexOutOfRange(i, r) => UniffiError::IndexOutOfRange(
749 i.try_into().expect("index to fit"),
750 r.try_into().expect("range to fit"),
751 ),
752 Error::EdsIndexOutOfRange(r, c) => UniffiError::EdsIndexOutOfRange(r, c),
753 Error::EdsInvalidDimentions => UniffiError::EdsInvalidDimentions,
754 Error::ZeroBlockHeight => UniffiError::ZeroBlockHeight,
755 Error::ExpectedShareWithSequenceStart => UniffiError::ExpectedShareWithSequenceStart,
756 Error::UnexpectedReservedNamespace => UniffiError::UnexpectedReservedNamespace,
757 Error::UnexpectedSequenceStart => UniffiError::UnexpectedSequenceStart,
758 Error::BlobSharesMetadataMismatch(s) => UniffiError::BlobSharesMetadataMismatch(s),
759 Error::BlobTooLarge => UniffiError::BlobTooLarge,
760 Error::InvalidComittmentLength => UniffiError::InvalidComittmentLength,
761 Error::MissingSigner => UniffiError::MissingSigner,
762 Error::SignerNotSupported => UniffiError::SignerNotSupported,
763 Error::EmptyBlobList => UniffiError::EmptyBlobList,
764 }
765 }
766}