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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
//! Cross-chain bridge types for Tenzro Network
//!
//! This module defines types for bridging assets between Tenzro Network
//! and other blockchains.
use crate::primitives::{Address, Hash, Timestamp};
use serde::{Deserialize, Serialize};
/// A message sent through the bridge
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct BridgeMessage {
/// Message ID
pub message_id: String,
/// Source chain
pub source_chain: String,
/// Destination chain
pub destination_chain: String,
/// Sender address on source chain
pub sender: String,
/// Recipient address on destination chain
pub recipient: String,
/// Message payload
pub payload: Vec<u8>,
/// Message nonce
pub nonce: u64,
/// Message timestamp
pub timestamp: Timestamp,
/// Bridge protocol used
pub protocol: BridgeProtocol,
/// Message status
pub status: BridgeMessageStatus,
}
impl BridgeMessage {
/// Creates a new bridge message
pub fn new(
source_chain: String,
destination_chain: String,
sender: String,
recipient: String,
payload: Vec<u8>,
nonce: u64,
protocol: BridgeProtocol,
) -> Self {
Self {
message_id: uuid::Uuid::new_v4().to_string(),
source_chain,
destination_chain,
sender,
recipient,
payload,
nonce,
timestamp: Timestamp::now(),
protocol,
status: BridgeMessageStatus::Pending,
}
}
/// Computes the message hash
pub fn hash(&self) -> Hash {
// In production, this would use a proper hashing algorithm
let json = serde_json::to_string(self).unwrap_or_default();
let mut hasher = [0u8; 32];
let bytes = json.as_bytes();
for (i, byte) in bytes.iter().enumerate().take(32) {
hasher[i % 32] ^= byte;
}
Hash::new(hasher)
}
}
/// Status of a bridge message
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum BridgeMessageStatus {
/// Message is pending
Pending,
/// Message is being processed
Processing,
/// Message has been confirmed on source chain
SourceConfirmed,
/// Message is being relayed
Relaying,
/// Message has been delivered to destination
Delivered,
/// Message failed
Failed,
/// Message was refunded
Refunded,
}
/// Bridge protocol types
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum BridgeProtocol {
/// Lock and mint protocol
LockAndMint,
/// Burn and mint protocol
BurnAndMint,
/// Liquidity pool protocol
LiquidityPool,
/// Atomic swap protocol
AtomicSwap,
/// Optimistic bridge protocol
Optimistic,
/// ZK proof bridge protocol
ZeroKnowledge,
}
impl BridgeProtocol {
/// Returns the protocol name
pub fn as_str(&self) -> &str {
match self {
Self::LockAndMint => "Lock and Mint",
Self::BurnAndMint => "Burn and Mint",
Self::LiquidityPool => "Liquidity Pool",
Self::AtomicSwap => "Atomic Swap",
Self::Optimistic => "Optimistic",
Self::ZeroKnowledge => "Zero Knowledge",
}
}
}
/// A cross-chain transfer via bridge
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct BridgeTransfer {
/// Transfer ID
pub transfer_id: String,
/// Bridge message
pub message: BridgeMessage,
/// Asset being transferred
pub asset_id: String,
/// Amount being transferred
pub amount: u64,
/// Bridge fee (in source chain units)
pub bridge_fee: u64,
/// Relayer fee (in destination chain units)
pub relayer_fee: u64,
/// Source transaction hash
pub source_tx_hash: Option<Hash>,
/// Destination transaction hash
pub destination_tx_hash: Option<Hash>,
/// Transfer proof
pub proof: Option<BridgeProof>,
/// Transfer metadata
pub metadata: BridgeTransferMetadata,
}
impl BridgeTransfer {
/// Creates a new bridge transfer
pub fn new(
message: BridgeMessage,
asset_id: String,
amount: u64,
bridge_fee: u64,
) -> Self {
Self {
transfer_id: uuid::Uuid::new_v4().to_string(),
message,
asset_id,
amount,
bridge_fee,
relayer_fee: 0,
source_tx_hash: None,
destination_tx_hash: None,
proof: None,
metadata: BridgeTransferMetadata::default(),
}
}
/// Sets the source transaction hash
pub fn with_source_tx(mut self, tx_hash: Hash) -> Self {
self.source_tx_hash = Some(tx_hash);
self
}
/// Sets the destination transaction hash
pub fn with_destination_tx(mut self, tx_hash: Hash) -> Self {
self.destination_tx_hash = Some(tx_hash);
self
}
/// Sets the transfer proof
pub fn with_proof(mut self, proof: BridgeProof) -> Self {
self.proof = Some(proof);
self
}
}
/// Metadata for bridge transfers
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
pub struct BridgeTransferMetadata {
/// Number of confirmations on source chain
pub source_confirmations: u32,
/// Required confirmations
pub required_confirmations: u32,
/// Relayer address
pub relayer: Option<String>,
/// Challenge period end (for optimistic bridges)
pub challenge_period_end: Option<Timestamp>,
/// Additional metadata
pub extra: Option<Vec<u8>>,
}
/// Proof for a bridge transfer
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct BridgeProof {
/// Proof type
pub proof_type: BridgeProofType,
/// Proof data
pub proof_data: Vec<u8>,
/// Validator signatures (if applicable)
pub signatures: Vec<ValidatorSignature>,
}
impl BridgeProof {
/// Creates a new bridge proof
pub fn new(proof_type: BridgeProofType, proof_data: Vec<u8>) -> Self {
Self {
proof_type,
proof_data,
signatures: Vec::new(),
}
}
/// Adds a validator signature
pub fn add_signature(&mut self, signature: ValidatorSignature) {
self.signatures.push(signature);
}
}
/// Type of bridge proof
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum BridgeProofType {
/// Merkle proof
Merkle,
/// Multi-signature proof
MultiSig,
/// ZK proof
ZeroKnowledge,
/// Light client proof
LightClient,
/// Optimistic proof (challenge-based)
Optimistic,
}
/// A validator signature for bridge operations
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ValidatorSignature {
/// Validator address
pub validator: Address,
/// Signature bytes
pub signature: Vec<u8>,
/// Validator's voting power
pub voting_power: u128,
/// Signature timestamp
pub signed_at: Timestamp,
}
/// Bridge relay configuration
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct BridgeRelayConfig {
/// Relayer address
pub relayer: Address,
/// Supported source chains
pub supported_source_chains: Vec<String>,
/// Supported destination chains
pub supported_destination_chains: Vec<String>,
/// Relayer fee (basis points)
pub relayer_fee_bps: u32,
/// Minimum transfer amount
pub min_transfer_amount: u64,
/// Maximum transfer amount
pub max_transfer_amount: u64,
/// Relayer status
pub status: RelayerStatus,
}
impl BridgeRelayConfig {
/// Creates a new bridge relay configuration
pub fn new(relayer: Address) -> Self {
Self {
relayer,
supported_source_chains: Vec::new(),
supported_destination_chains: Vec::new(),
relayer_fee_bps: 10, // 0.1%
min_transfer_amount: 1000,
max_transfer_amount: u64::MAX,
status: RelayerStatus::Active,
}
}
/// Adds a supported source chain
pub fn add_source_chain(&mut self, chain: String) {
if !self.supported_source_chains.contains(&chain) {
self.supported_source_chains.push(chain);
}
}
/// Adds a supported destination chain
pub fn add_destination_chain(&mut self, chain: String) {
if !self.supported_destination_chains.contains(&chain) {
self.supported_destination_chains.push(chain);
}
}
/// Checks if a route is supported
pub fn supports_route(&self, source: &str, destination: &str) -> bool {
self.supported_source_chains.iter().any(|c| c == source)
&& self.supported_destination_chains.iter().any(|c| c == destination)
}
}
/// Relayer operational status
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum RelayerStatus {
/// Relayer is active
Active,
/// Relayer is paused
Paused,
/// Relayer is suspended
Suspended,
}