Skip to main content

ckb_transaction_firewall_sdk/
errors.rs

1use std::fmt;
2
3/// Errors returned by the CKB Transaction Firewall SDK.
4///
5/// Each variant maps to a specific on-chain error code. Codes 8–12 and 17
6/// can be surfaced by SDK preflight; codes 5–7 and 13–16 are enforced
7/// exclusively by the on-chain contract.
8#[derive(Debug, Clone, PartialEq, Eq)]
9pub enum FirewallError {
10    /// No registry cell dep matches the required spec (on-chain code 8).
11    MissingRegistryCellDep,
12    /// Registry cell data is malformed or has an unsupported version (on-chain code 9).
13    InvalidRegistryData,
14    /// Registry entries are not in ascending lexicographic order (on-chain code 10).
15    RegistryNotSorted,
16    /// A transaction output's lock args appear in the active blacklist (on-chain code 11).
17    BlacklistedLockArgs,
18    /// A transaction output's type args appear in the active blacklist (on-chain code 12).
19    BlacklistedTypeArgs,
20    /// More than one cell dep matches the same registry spec (on-chain code 17).
21    AmbiguousRegistryCellDep,
22}
23
24impl FirewallError {
25    /// On-chain error code corresponding to this error variant.
26    pub fn code(&self) -> i8 {
27        match self {
28            FirewallError::MissingRegistryCellDep => error_codes::MISSING_REGISTRY_CELL_DEP,
29            FirewallError::InvalidRegistryData => error_codes::INVALID_REGISTRY_DATA,
30            FirewallError::RegistryNotSorted => error_codes::REGISTRY_NOT_SORTED,
31            FirewallError::BlacklistedLockArgs => error_codes::BLACKLISTED_LOCK_ARGS,
32            FirewallError::BlacklistedTypeArgs => error_codes::BLACKLISTED_TYPE_ARGS,
33            FirewallError::AmbiguousRegistryCellDep => error_codes::AMBIGUOUS_REGISTRY_CELL_DEP,
34        }
35    }
36}
37
38impl fmt::Display for FirewallError {
39    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40        match self {
41            FirewallError::MissingRegistryCellDep => {
42                write!(f, "missing registry cell dep (code {})", self.code())
43            }
44            FirewallError::InvalidRegistryData => {
45                write!(f, "invalid registry data (code {})", self.code())
46            }
47            FirewallError::RegistryNotSorted => {
48                write!(f, "registry entries not sorted (code {})", self.code())
49            }
50            FirewallError::BlacklistedLockArgs => {
51                write!(f, "blacklisted lock args (code {})", self.code())
52            }
53            FirewallError::BlacklistedTypeArgs => {
54                write!(f, "blacklisted type args (code {})", self.code())
55            }
56            FirewallError::AmbiguousRegistryCellDep => {
57                write!(f, "ambiguous registry cell dep (code {})", self.code())
58            }
59        }
60    }
61}
62
63impl std::error::Error for FirewallError {}
64
65/// On-chain error codes for the CKB Transaction Firewall lock script.
66///
67/// These are the frozen v1 codes defined in
68/// `contracts/firewall-lock/src/main.rs`. Codes 8–12 and 17 can be surfaced
69/// by SDK preflight; codes 5–7 and 13–16 are enforced exclusively by the
70/// on-chain contract and are provided here for diagnostic purposes only.
71pub mod error_codes {
72    /// Lock args byte layout is invalid — too short or mismatched inner_args_len (on-chain only).
73    pub const INVALID_ARGS_LAYOUT: i8 = 5;
74    /// Registry data uses an unsupported version (on-chain only).
75    pub const UNSUPPORTED_VERSION: i8 = 6;
76    /// Flags byte has no check bits set, or has reserved bits set (on-chain only).
77    pub const UNSUPPORTED_FLAGS: i8 = 7;
78    /// No registry cell dep matched the required spec.
79    pub const MISSING_REGISTRY_CELL_DEP: i8 = 8;
80    /// Registry cell data is malformed or uses an unsupported version.
81    pub const INVALID_REGISTRY_DATA: i8 = 9;
82    /// Registry entries are not in ascending lexicographic order.
83    pub const REGISTRY_NOT_SORTED: i8 = 10;
84    /// An output's lock args are in the active blacklist.
85    pub const BLACKLISTED_LOCK_ARGS: i8 = 11;
86    /// An output's type args are in the active blacklist.
87    pub const BLACKLISTED_TYPE_ARGS: i8 = 12;
88    /// Inner lock cell dep is missing (on-chain only).
89    pub const MISSING_INNER_LOCK_CELL_DEP: i8 = 13;
90    /// Inner lock script hash type is unrecognised (on-chain only).
91    pub const INVALID_INNER_LOCK_SCRIPT: i8 = 14;
92    /// Inner lock script rejected the spend — bad signature or witness (on-chain only).
93    pub const INNER_LOCK_REJECTED: i8 = 15;
94    /// Failed to load or parse a transaction output script (on-chain only).
95    pub const OUTPUT_SCRIPT_PARSE_FAILED: i8 = 16;
96    /// More than one cell dep matched the same registry spec.
97    pub const AMBIGUOUS_REGISTRY_CELL_DEP: i8 = 17;
98}