1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
use bytes::{Buf, Bytes};
use client_contracts::beacon_light_client_types::HeaderMessage as ContractHeaderMessage;
use client_contracts::beacon_light_client_types::SyncAggregate as ContractSyncAggregate;
use client_contracts::beacon_light_client_types::SyncCommittee as ContractSyncCommittee;
use serde::{Deserialize, Serialize};
use std::fmt::Display;
use std::str::FromStr;
use web3::{
    contract::tokens::{Tokenizable, Tokenize},
    ethabi::{ethereum_types::H32, Token},
    types::{Bytes as Web3Bytes, H256},
};

use serde::de::{self, Deserializer};

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct ResponseWrapper<T> {
    pub data: T,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct GetHeaderResponse {
    pub root: String,
    pub canonical: bool,
    pub header: Header,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Header {
    pub message: HeaderMessage,
    pub signature: String,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct HeaderMessage {
    #[serde(deserialize_with = "from_str")]
    pub slot: u64,
    #[serde(deserialize_with = "from_str")]
    pub proposer_index: u64,
    pub parent_root: String,
    pub state_root: String,
    pub body_root: String,
}

impl HeaderMessage {
    pub fn to_contract_type(&self) -> color_eyre::Result<ContractHeaderMessage> {
        Ok(ContractHeaderMessage {
            slot: self.slot,
            proposer_index: self.proposer_index,
            parent_root: H256::from_str(&self.parent_root)?,
            state_root: H256::from_str(&self.state_root)?,
            body_root: H256::from_str(&self.body_root)?,
        })
    }
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Snapshot {
    pub header: HeaderMessage,
    pub current_sync_committee: SyncCommittee,
    pub current_sync_committee_branch: Vec<String>,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct SyncCommittee {
    pub pubkeys: Vec<String>,
    pub aggregate_pubkey: String,
}

impl SyncCommittee {
    pub fn to_contract_type(&self) -> color_eyre::Result<ContractSyncCommittee> {
        Ok(ContractSyncCommittee {
            pubkeys: self
                .pubkeys
                .iter()
                .map(|x| hex::decode(&x.clone()[2..]))
                .collect::<Result<Vec<Vec<u8>>, _>>()?,
            aggregate_pubkey: Web3Bytes(hex::decode(&self.aggregate_pubkey.clone()[2..])?),
        })
    }
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct SyncCommitteePeriodUpdate {
    pub attested_header: HeaderMessage,
    pub next_sync_committee: SyncCommittee,
    pub finalized_header: HeaderMessage,
    pub finality_branch: Vec<String>,
    pub sync_aggregate: SyncAggregate,
    pub fork_version: String,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct GetBlockResponse {
    pub message: BlockMessage,
    pub signature: String,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct BlockMessage {
    pub slot: String,
    pub proposer_index: String,
    pub parent_root: String,
    pub state_root: String,
    pub body: BlockBody,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct BlockBody {
    pub attestations: Vec<Attestation>,
    pub sync_aggregate: SyncAggregate,
    pub execution_payload: ExecutionPayload,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Attestation {
    pub aggregation_bits: String,
    pub data: AttestationData,
    pub signature: String,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct AttestationData {
    pub slot: String,
    pub index: String,
    pub beacon_block_root: String,
    pub source: Checkpoint,
    pub target: Checkpoint,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Checkpoint {
    pub epoch: String,
    pub root: String,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct SyncAggregate {
    pub sync_committee_bits: String,
    pub sync_committee_signature: String,
}

impl SyncAggregate {
    pub fn to_contract_type(&self) -> color_eyre::Result<ContractSyncAggregate> {
        let mut sync_committee_bits: [H256; 2] = [H256::default(); 2];
        sync_committee_bits[0] = H256::from_str(&self.sync_committee_bits[..66])?;
        sync_committee_bits[1] = H256::from_str(&self.sync_committee_bits[66..])?;

        let sync_committee_signature =
            Web3Bytes(hex::decode(&self.sync_committee_signature.clone()[2..])?);
        Ok(ContractSyncAggregate {
            sync_committee_bits,
            sync_committee_signature,
        })
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExecutionPayload {
    pub parent_hash: String,
    pub fee_recipient: String,
    pub state_root: String,
    pub receipts_root: String,
    pub logs_bloom: String,
    pub prev_randao: String,
    pub block_number: String,
    pub gas_limit: String,
    pub gas_used: String,
    pub timestamp: String,
    pub extra_data: String,
    pub base_fee_per_gas: String,
    pub block_hash: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Finality {
    pub previous_justified: Checkpoint,
    pub current_justified: Checkpoint,
    pub finalized: Checkpoint,
}

#[derive(Debug)]
pub enum Proof {
    SingleProof {
        gindex: u16,
        leaf: H256,
        witnesses: Vec<H256>,
    },
    #[allow(dead_code)]
    TreeOffsets {
        offsets: Vec<u16>,
        leaves: Vec<H256>,
    },
}

impl From<Bytes> for Proof {
    fn from(mut x: Bytes) -> Self {
        match x.get_u8() {
            0u8 => Proof::SingleProof {
                gindex: x.get_u16_le(),
                leaf: {
                    let mut leaf = [0u8; 32];
                    x.copy_to_slice(&mut leaf);
                    H256::from(leaf)
                },
                witnesses: {
                    let witcount: usize = x.get_u16_le().into();
                    (0..witcount)
                        .map(|_| {
                            let mut witness = [0u8; 32];
                            x.copy_to_slice(&mut witness);
                            H256::from(witness)
                        })
                        .collect()
                },
            },
            _ => {
                unimplemented!();
            }
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ForkVersion {
    pub previous_version: H32,
    pub current_version: H32,
    pub epoch: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FinalityUpdate {
    pub attested_header: HeaderMessage,
    pub finalized_header: HeaderMessage,
    pub finality_branch: Vec<String>,
    pub sync_aggregate: SyncAggregate,
}

#[derive(Debug, Clone)]
pub struct MessagesProof {
    pub account_proof: web3::types::Bytes,
    pub lane_id_proof: web3::types::Bytes,
    pub lane_nonce_proof: web3::types::Bytes,
    pub lane_messages_proof: Vec<web3::types::Bytes>,
}

impl MessagesProof {
    pub fn get_token(&self) -> color_eyre::Result<Token> {
        Ok(Token::Tuple(
            (
                self.account_proof.clone(),
                self.lane_id_proof.clone(),
                self.lane_nonce_proof.clone(),
                Token::Array(
                    self.lane_messages_proof
                        .iter()
                        .map(|x| x.clone().into_token())
                        .collect::<Vec<Token>>(),
                ),
            )
                .into_tokens(),
        ))
    }
}

#[derive(Debug, Clone)]
pub struct MessagesConfirmationProof {
    pub account_proof: web3::types::Bytes,
    pub lane_nonce_proof: web3::types::Bytes,
    pub lane_relayers_proof: Vec<web3::types::Bytes>,
}

impl MessagesConfirmationProof {
    pub fn get_token(&self) -> color_eyre::Result<Token> {
        Ok(Token::Tuple(
            (
                self.account_proof.clone(),
                self.lane_nonce_proof.clone(),
                Token::Array(
                    self.lane_relayers_proof
                        .iter()
                        .map(|x| x.clone().into_token())
                        .collect::<Vec<Token>>(),
                ),
            )
                .into_tokens(),
        ))
    }
}

fn from_str<'de, T, D>(deserializer: D) -> Result<T, D::Error>
where
    T: FromStr,
    T::Err: Display,
    D: Deserializer<'de>,
{
    let s = String::deserialize(deserializer)?;
    T::from_str(&s).map_err(de::Error::custom)
}