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    types: Vec<String>,
14    abi: Vec<u8>,
15    version: EncodingVersion,
16}
17
18impl AbiEncodeResult {
19    pub fn new(types: Vec<String>, abi: Vec<u8>, version: EncodingVersion) -> Self {
20        Self {
21            types,
22            abi,
23            version,
24        }
25    }
26
27    pub fn types(&self) -> &[String] {
28        &self.types
29    }
30
31    pub fn abi(&self) -> &[u8] {
32        &self.abi
33    }
34
35    pub fn version(&self) -> EncodingVersion {
36        self.version
37    }
38}
39
40pub fn compute_v(signature: &Signature, chain_id: Option<u64>) -> U256 {
41    let parity = signature.v() as u64; // Get y_parity (boolean as 0 or 1)
42
43    match chain_id {
44        Some(id) => U256::from(35 + 2 * id + parity), // Corrected EIP-155 format
45        None => U256::from(27 + parity),              // Legacy format
46    }
47}
48
49pub fn compute_y_parity(signature: &Signature) -> u8 {
50    if signature.v() {
51        1
52    } else {
53        0
54    }
55}
56
57pub fn encode_blob_hashes(blob_hashes: &[FixedBytes<32>]) -> DynSolValue {
58    let mut result = Vec::new();
59    for hash in blob_hashes {
60        result.push(DynSolValue::FixedBytes(*hash, 32));
61    }
62    DynSolValue::Array(result)
63}