ccnext_abi_encoding/common/
mod.rs

1use alloy::dyn_abi::DynSolValue;
2use alloy::primitives::{FixedBytes, U256};
3use alloy::signers::Signature;
4use serde::{Deserialize, Serialize};
5
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
7pub enum EncodingVersion {
8    V1 = 1,
9}
10
11#[derive(Serialize, Deserialize, Debug)]
12pub struct AbiEncodeResult {
13    abi: Vec<u8>,
14    version: EncodingVersion,
15}
16
17impl AbiEncodeResult {
18    pub fn new(abi: Vec<u8>, version: EncodingVersion) -> Self {
19        Self { abi, version }
20    }
21
22    pub fn abi(&self) -> &[u8] {
23        &self.abi
24    }
25
26    pub fn version(&self) -> EncodingVersion {
27        self.version
28    }
29}
30
31pub fn compute_v(signature: &Signature, chain_id: Option<u64>) -> U256 {
32    let parity = signature.v() as u64; // Get y_parity (boolean as 0 or 1)
33
34    match chain_id {
35        Some(id) => U256::from(35 + 2 * id + parity), // Corrected EIP-155 format
36        None => U256::from(27 + parity),              // Legacy format
37    }
38}
39
40pub fn compute_y_parity(signature: &Signature) -> u8 {
41    if signature.v() {
42        1
43    } else {
44        0
45    }
46}
47
48pub fn encode_blob_hashes(blob_hashes: &[FixedBytes<32>]) -> DynSolValue {
49    let mut result = Vec::new();
50    for hash in blob_hashes {
51        result.push(DynSolValue::FixedBytes(*hash, 32));
52    }
53    DynSolValue::Array(result)
54}