1use crate::primitives::{Address, Hash, Timestamp};
7use serde::{Deserialize, Serialize};
8
9#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
11pub struct BridgeMessage {
12 pub message_id: String,
14 pub source_chain: String,
16 pub destination_chain: String,
18 pub sender: String,
20 pub recipient: String,
22 pub payload: Vec<u8>,
24 pub nonce: u64,
26 pub timestamp: Timestamp,
28 pub protocol: BridgeProtocol,
30 pub status: BridgeMessageStatus,
32}
33
34impl BridgeMessage {
35 pub fn new(
37 source_chain: String,
38 destination_chain: String,
39 sender: String,
40 recipient: String,
41 payload: Vec<u8>,
42 nonce: u64,
43 protocol: BridgeProtocol,
44 ) -> Self {
45 Self {
46 message_id: uuid::Uuid::new_v4().to_string(),
47 source_chain,
48 destination_chain,
49 sender,
50 recipient,
51 payload,
52 nonce,
53 timestamp: Timestamp::now(),
54 protocol,
55 status: BridgeMessageStatus::Pending,
56 }
57 }
58
59 pub fn hash(&self) -> Hash {
61 let json = serde_json::to_string(self).unwrap_or_default();
63 let mut hasher = [0u8; 32];
64 let bytes = json.as_bytes();
65 for (i, byte) in bytes.iter().enumerate().take(32) {
66 hasher[i % 32] ^= byte;
67 }
68 Hash::new(hasher)
69 }
70}
71
72#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
74pub enum BridgeMessageStatus {
75 Pending,
77 Processing,
79 SourceConfirmed,
81 Relaying,
83 Delivered,
85 Failed,
87 Refunded,
89}
90
91#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
93pub enum BridgeProtocol {
94 LockAndMint,
96 BurnAndMint,
98 LiquidityPool,
100 AtomicSwap,
102 Optimistic,
104 ZeroKnowledge,
106}
107
108impl BridgeProtocol {
109 pub fn as_str(&self) -> &str {
111 match self {
112 Self::LockAndMint => "Lock and Mint",
113 Self::BurnAndMint => "Burn and Mint",
114 Self::LiquidityPool => "Liquidity Pool",
115 Self::AtomicSwap => "Atomic Swap",
116 Self::Optimistic => "Optimistic",
117 Self::ZeroKnowledge => "Zero Knowledge",
118 }
119 }
120}
121
122#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
124pub struct BridgeTransfer {
125 pub transfer_id: String,
127 pub message: BridgeMessage,
129 pub asset_id: String,
131 pub amount: u64,
133 pub bridge_fee: u64,
135 pub relayer_fee: u64,
137 pub source_tx_hash: Option<Hash>,
139 pub destination_tx_hash: Option<Hash>,
141 pub proof: Option<BridgeProof>,
143 pub metadata: BridgeTransferMetadata,
145}
146
147impl BridgeTransfer {
148 pub fn new(
150 message: BridgeMessage,
151 asset_id: String,
152 amount: u64,
153 bridge_fee: u64,
154 ) -> Self {
155 Self {
156 transfer_id: uuid::Uuid::new_v4().to_string(),
157 message,
158 asset_id,
159 amount,
160 bridge_fee,
161 relayer_fee: 0,
162 source_tx_hash: None,
163 destination_tx_hash: None,
164 proof: None,
165 metadata: BridgeTransferMetadata::default(),
166 }
167 }
168
169 pub fn with_source_tx(mut self, tx_hash: Hash) -> Self {
171 self.source_tx_hash = Some(tx_hash);
172 self
173 }
174
175 pub fn with_destination_tx(mut self, tx_hash: Hash) -> Self {
177 self.destination_tx_hash = Some(tx_hash);
178 self
179 }
180
181 pub fn with_proof(mut self, proof: BridgeProof) -> Self {
183 self.proof = Some(proof);
184 self
185 }
186}
187
188#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
190pub struct BridgeTransferMetadata {
191 pub source_confirmations: u32,
193 pub required_confirmations: u32,
195 pub relayer: Option<String>,
197 pub challenge_period_end: Option<Timestamp>,
199 pub extra: Option<Vec<u8>>,
201}
202
203#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
205pub struct BridgeProof {
206 pub proof_type: BridgeProofType,
208 pub proof_data: Vec<u8>,
210 pub signatures: Vec<ValidatorSignature>,
212}
213
214impl BridgeProof {
215 pub fn new(proof_type: BridgeProofType, proof_data: Vec<u8>) -> Self {
217 Self {
218 proof_type,
219 proof_data,
220 signatures: Vec::new(),
221 }
222 }
223
224 pub fn add_signature(&mut self, signature: ValidatorSignature) {
226 self.signatures.push(signature);
227 }
228}
229
230#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
232pub enum BridgeProofType {
233 Merkle,
235 MultiSig,
237 ZeroKnowledge,
239 LightClient,
241 Optimistic,
243}
244
245#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
247pub struct ValidatorSignature {
248 pub validator: Address,
250 pub signature: Vec<u8>,
252 pub voting_power: u128,
254 pub signed_at: Timestamp,
256}
257
258#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
260pub struct BridgeRelayConfig {
261 pub relayer: Address,
263 pub supported_source_chains: Vec<String>,
265 pub supported_destination_chains: Vec<String>,
267 pub relayer_fee_bps: u32,
269 pub min_transfer_amount: u64,
271 pub max_transfer_amount: u64,
273 pub status: RelayerStatus,
275}
276
277impl BridgeRelayConfig {
278 pub fn new(relayer: Address) -> Self {
280 Self {
281 relayer,
282 supported_source_chains: Vec::new(),
283 supported_destination_chains: Vec::new(),
284 relayer_fee_bps: 10, min_transfer_amount: 1000,
286 max_transfer_amount: u64::MAX,
287 status: RelayerStatus::Active,
288 }
289 }
290
291 pub fn add_source_chain(&mut self, chain: String) {
293 if !self.supported_source_chains.contains(&chain) {
294 self.supported_source_chains.push(chain);
295 }
296 }
297
298 pub fn add_destination_chain(&mut self, chain: String) {
300 if !self.supported_destination_chains.contains(&chain) {
301 self.supported_destination_chains.push(chain);
302 }
303 }
304
305 pub fn supports_route(&self, source: &str, destination: &str) -> bool {
307 self.supported_source_chains.iter().any(|c| c == source)
308 && self.supported_destination_chains.iter().any(|c| c == destination)
309 }
310}
311
312#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
314pub enum RelayerStatus {
315 Active,
317 Paused,
319 Suspended,
321}