Skip to main content

quai_abi/
lib.rs

1//! Bounded Solidity ABI encoding, contract interfaces and EIP-712 hashing.
2//!
3//! Validated against pinned quais.js with explicit stricter input policies.
4//! Signing, contract execution and provider network policy belong to other crates.
5mod abi_type;
6mod artifact;
7pub use artifact::SolidityArtifact;
8mod typed_value;
9pub use typed_value::AbiValue;
10mod codec;
11mod packed;
12pub use packed::{solidity_packed, solidity_packed_keccak256, solidity_packed_sha256};
13mod document;
14mod human;
15mod interface;
16mod parameter;
17pub use parameter::{AbiFormat, AbiParameter};
18mod result;
19pub use result::AbiResult;
20mod workflows;
21pub use workflows::{
22    AbiFilterTopic, AbiFilterValue, MAX_FILTER_ALTERNATIVES, ParsedCall, ParsedLog, ParsedRevert,
23};
24mod schema;
25mod typed_visit;
26pub use typed_visit::TypedValueEncoder;
27mod value;
28
29pub use document::TypedData;
30pub use schema::{TypedDataEncoder, TypedDataField, TypedDataTypes};
31pub use value::{hash_domain, hash_typed_data};
32
33/// Maximum UTF-8 JSON document or combined value string/key bytes.
34pub const MAX_DATA_BYTES: usize = 1 << 20;
35/// Maximum visited JSON values, including array elements and object values.
36pub const MAX_VALUE_NODES: usize = 32_768;
37/// Maximum nested schema dependencies, JSON containers or encoding recursion.
38pub const MAX_DEPTH: usize = 64;
39/// Maximum declared struct types in one schema.
40pub const MAX_TYPES: usize = 128;
41/// Maximum total fields in one schema.
42pub const MAX_FIELDS: usize = 1024;
43/// Maximum combined schema identifier and type-expression bytes.
44pub const MAX_SCHEMA_BYTES: usize = 65_536;
45
46/// Errors never include a message's potentially sensitive value contents.
47#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
48#[non_exhaustive]
49pub enum TypedDataError {
50    /// Input exceeds a fixed resource policy.
51    #[error("typed data exceeds resource limits")]
52    Limit,
53    /// Malformed JSON, duplicate keys or unsupported document shape.
54    #[error("invalid typed-data document")]
55    Document,
56    /// Invalid identifier, primitive, array expression or duplicate field.
57    #[error("invalid typed-data schema")]
58    Schema,
59    /// A field references an undeclared struct.
60    #[error("unknown typed-data type")]
61    UnknownType,
62    /// More than one root, no root, or an unused disconnected struct.
63    #[error("typed-data schema requires one primary type")]
64    PrimaryType,
65    /// Recursive structs are not supported by pinned quais.js.
66    #[error("cyclic typed-data schema")]
67    Cycle,
68    /// Missing/extra fields or a value with an incorrect JSON kind.
69    #[error("typed-data value does not match schema")]
70    Value,
71    /// Integer outside its declared range or not an exact integer.
72    #[error("invalid typed-data integer")]
73    Integer,
74    /// Invalid hex, byte width, address or address checksum.
75    #[error("invalid typed-data bytes or address")]
76    Bytes,
77    /// Fixed array length differs from its declaration.
78    #[error("typed-data array length mismatch")]
79    ArrayLength,
80    /// An unrecognized domain field or inconsistent domain declaration.
81    #[error("invalid typed-data domain")]
82    Domain,
83}
84
85pub(crate) fn keccak(bytes: &[u8]) -> [u8; 32] {
86    use tiny_keccak::{Hasher, Keccak};
87    let mut hasher = Keccak::v256();
88    hasher.update(bytes);
89    let mut result = [0; 32];
90    hasher.finalize(&mut result);
91    result
92}
93
94pub use abi_type::{AbiError, AbiType};
95pub use codec::{AbiCoder, indexed_event_topic};
96pub use interface::{
97    AbiConstructor, AbiCustomError, AbiEvent, AbiEventValue, AbiFunction, AbiInterface,
98    StateMutability, function_selector, signature_hash,
99};