csv-adapter-core 0.1.1

Chain-agnostic core traits and types for CSV (Client-Side Validation) adapters
Documentation
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
//! Cross-Chain Right Transfer
//!
//! Implements the lock-and-prove protocol for transferring Rights between chains:
//! 1. Lock — Source chain consumes seal, emits CrossChainLockEvent
//! 2. Prove — Client generates inclusion proof
//! 3. Verify — Destination chain verifies proof, checks registry, mints new Right
//! 4. Registry — Records transfer, prevents cross-chain double-spend

use alloc::vec::Vec;
use serde::{Deserialize, Serialize};

use crate::hash::Hash;
use crate::right::{OwnershipProof, Right};
use crate::seal::SealRef;

/// Chain identifier for cross-chain transfers.
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[allow(missing_docs)]
pub enum ChainId {
    /// Bitcoin (UTXO seals)
    Bitcoin,
    /// Sui (Object seals)
    Sui,
    /// Aptos (Resource seals)
    Aptos,
    /// Ethereum (Nullifier seals)
    Ethereum,
}

impl ChainId {
    /// Get a numeric identifier for serialization.
    pub fn as_u8(&self) -> u8 {
        match self {
            ChainId::Bitcoin => 0,
            ChainId::Sui => 1,
            ChainId::Aptos => 2,
            ChainId::Ethereum => 3,
        }
    }

    /// Parse a chain ID from a u8.
    pub fn from_u8(id: u8) -> Option<Self> {
        match id {
            0 => Some(ChainId::Bitcoin),
            1 => Some(ChainId::Sui),
            2 => Some(ChainId::Aptos),
            3 => Some(ChainId::Ethereum),
            _ => None,
        }
    }
}

impl core::fmt::Display for ChainId {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            ChainId::Bitcoin => write!(f, "Bitcoin"),
            ChainId::Sui => write!(f, "Sui"),
            ChainId::Aptos => write!(f, "Aptos"),
            ChainId::Ethereum => write!(f, "Ethereum"),
        }
    }
}

/// Event emitted when a Right is locked on the source chain for cross-chain transfer.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct CrossChainLockEvent {
    /// The Right being locked
    pub right_id: Hash,
    /// The commitment hash of the Right
    pub commitment: Hash,
    /// The owner who initiated the lock
    pub owner: OwnershipProof,
    /// Source chain where the Right is being locked
    pub source_chain: ChainId,
    /// Destination chain for the transfer
    pub destination_chain: ChainId,
    /// Destination owner (may differ from source owner)
    pub destination_owner: OwnershipProof,
    /// Source chain's seal reference (consumed during lock)
    pub source_seal: SealRef,
    /// Source transaction hash
    pub source_tx_hash: Hash,
    /// Source block height
    pub source_block_height: u64,
    /// Unix timestamp of the lock event
    pub timestamp: u64,
}

/// Inclusion proof — chain-specific format.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum InclusionProof {
    /// Bitcoin: Merkle branch + block header
    Bitcoin(BitcoinMerkleProof),
    /// Ethereum: MPT receipt proof
    Ethereum(EthereumMPTProof),
    /// Sui: Checkpoint certification
    Sui(SuiCheckpointProof),
    /// Aptos: Ledger info proof
    Aptos(AptosLedgerProof),
}

/// Bitcoin Merkle proof of transaction inclusion in a block.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[allow(missing_docs)]
pub struct BitcoinMerkleProof {
    /// Transaction ID
    pub txid: [u8; 32],
    /// Merkle branch nodes
    pub merkle_branch: Vec<[u8; 32]>,
    /// Serialized block header
    pub block_header: Vec<u8>,
    /// Block height
    pub block_height: u64,
    /// Number of confirmations
    pub confirmations: u64,
}

/// Ethereum MPT proof of receipt inclusion in state.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[allow(missing_docs)]
pub struct EthereumMPTProof {
    /// Transaction hash
    pub tx_hash: [u8; 32],
    /// Receipt root hash
    pub receipt_root: [u8; 32],
    /// RLP-encoded receipt
    pub receipt_rlp: Vec<u8>,
    /// MPT proof nodes
    pub merkle_nodes: Vec<Vec<u8>>,
    /// Serialized block header
    pub block_header: Vec<u8>,
    /// Log index in the receipt
    pub log_index: u64,
    /// Number of confirmations
    pub confirmations: u64,
}

/// Sui checkpoint proof of transaction effects certification.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[allow(missing_docs)]
pub struct SuiCheckpointProof {
    /// Transaction digest
    pub tx_digest: [u8; 32],
    /// Checkpoint sequence number
    pub checkpoint_sequence: u64,
    /// Checkpoint contents hash
    pub checkpoint_contents_hash: [u8; 32],
    /// Transaction effects bytes
    pub effects: Vec<u8>,
    /// Event bytes
    pub events: Vec<u8>,
    /// Whether the checkpoint is certified
    pub certified: bool,
}

/// Aptos ledger info proof of transaction execution.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[allow(missing_docs)]
pub struct AptosLedgerProof {
    /// Transaction version
    pub version: u64,
    /// Transaction proof bytes
    pub transaction_proof: Vec<u8>,
    /// Ledger info bytes
    pub ledger_info: Vec<u8>,
    /// Event bytes
    pub events: Vec<u8>,
    /// Whether the transaction succeeded
    pub success: bool,
}

/// Finality proof confirming source transaction is finalized.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct CrossChainFinalityProof {
    /// Source chain identifier
    pub source_chain: ChainId,
    /// Block/checkpoint/ledger height of the transaction
    pub height: u64,
    /// Current height on the source chain
    pub current_height: u64,
    /// Whether finality depth has been achieved
    pub is_finalized: bool,
    /// Required finality depth in blocks
    pub depth: u64,
}

/// Complete proof bundle submitted to the destination chain.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct CrossChainTransferProof {
    /// The lock event data from the source chain
    pub lock_event: CrossChainLockEvent,
    /// Inclusion proof (chain-specific format)
    pub inclusion_proof: InclusionProof,
    /// Finality proof confirming source transaction
    pub finality_proof: CrossChainFinalityProof,
    /// Source chain's state root at the lock block
    pub source_state_root: Hash,
}

/// Entry in the cross-chain seal registry recording a completed transfer.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct CrossChainRegistryEntry {
    /// The Right's unique ID (preserved across chains)
    pub right_id: Hash,
    /// Source chain identifier
    pub source_chain: ChainId,
    /// Source chain's seal reference
    pub source_seal: SealRef,
    /// Destination chain identifier
    pub destination_chain: ChainId,
    /// Destination chain's seal reference
    pub destination_seal: SealRef,
    /// Lock transaction hash on source chain
    pub lock_tx_hash: Hash,
    /// Mint transaction hash on destination chain
    pub mint_tx_hash: Hash,
    /// Unix timestamp of the transfer
    pub timestamp: u64,
}

/// Result of a successful cross-chain transfer.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CrossChainTransferResult {
    /// The new Right created on the destination chain
    pub destination_right: Right,
    /// The destination chain's seal reference
    pub destination_seal: SealRef,
    /// Registry entry recording the transfer
    pub registry_entry: CrossChainRegistryEntry,
}

/// Errors that can occur during cross-chain transfer.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[allow(missing_docs)]
pub enum CrossChainError {
    #[error("Right already locked on source chain")]
    AlreadyLocked,
    #[error("Right already exists on destination chain")]
    AlreadyMinted,
    #[error("Invalid inclusion proof")]
    InvalidInclusionProof,
    #[error("Insufficient finality: {0} confirmations, need {1}")]
    InsufficientFinality(u64, u64),
    #[error("Ownership proof verification failed")]
    InvalidOwnership,
    #[error("Lock event does not match expected data")]
    LockEventMismatch,
    #[error("Cross-chain registry error: {0}")]
    RegistryError(String),
    #[error("Unsupported chain pair: {0} → {1}")]
    UnsupportedChainPair(ChainId, ChainId),
}

/// Trait for locking a Right on a source chain.
///
/// Consumes the Right's seal and returns the lock event data + inclusion proof.
pub trait LockProvider {
    /// Lock a Right for cross-chain transfer.
    ///
    /// # Arguments
    /// * `right_id` — The unique identifier of the Right
    /// * `commitment` — The Right's commitment hash
    /// * `owner` — Current owner's ownership proof
    /// * `destination_chain` — Target chain ID
    /// * `destination_owner` — New owner on destination chain
    ///
    /// # Returns
    /// Lock event data and inclusion proof (chain-specific format)
    fn lock_right(
        &self,
        right_id: Hash,
        commitment: Hash,
        owner: OwnershipProof,
        destination_chain: ChainId,
        destination_owner: OwnershipProof,
    ) -> Result<(CrossChainLockEvent, InclusionProof), CrossChainError>;
}

/// Trait for verifying cross-chain transfer proofs.
pub trait TransferVerifier {
    /// Verify a cross-chain transfer proof.
    ///
    /// # Checks
    /// 1. Inclusion proof is valid (source chain finalized)
    /// 2. Seal NOT in CrossChainSealRegistry (no double-spend)
    /// 3. Ownership proof valid (owner signature matches)
    /// 4. Lock event matches expected right_id and commitment
    fn verify_transfer_proof(&self, proof: &CrossChainTransferProof)
        -> Result<(), CrossChainError>;
}

/// Trait for minting a Right on a destination chain.
pub trait MintProvider {
    /// Mint a new Right from a verified cross-chain transfer proof.
    ///
    /// Creates a new Right with the same commitment and state
    /// but a new seal on the destination chain.
    fn mint_right(
        &self,
        proof: &CrossChainTransferProof,
    ) -> Result<CrossChainTransferResult, CrossChainError>;
}

/// Cross-chain transfer orchestrator.
///
/// Coordinates lock → prove → verify → mint across chains.
pub struct CrossChainTransfer {
    /// The cross-chain seal registry
    pub registry: CrossChainRegistry,
}

impl CrossChainTransfer {
    /// Create a new cross-chain transfer orchestrator.
    pub fn new(registry: CrossChainRegistry) -> Self {
        Self { registry }
    }

    /// Execute a full cross-chain transfer.
    ///
    /// 1. Lock the Right on the source chain
    /// 2. Build the transfer proof
    /// 3. Verify on the destination chain
    /// 4. Mint the new Right
    /// 5. Record in the registry
    #[allow(clippy::too_many_arguments)]
    pub fn execute(
        &mut self,
        locker: &dyn LockProvider,
        verifier: &dyn TransferVerifier,
        minter: &dyn MintProvider,
        right_id: Hash,
        commitment: Hash,
        owner: OwnershipProof,
        destination_chain: ChainId,
        destination_owner: OwnershipProof,
        current_block_height: u64,
        finality_depth: u64,
    ) -> Result<CrossChainTransferResult, CrossChainError> {
        // Step 1: Lock on source chain
        let (lock_event, inclusion_proof) = locker.lock_right(
            right_id,
            commitment,
            owner.clone(),
            destination_chain.clone(),
            destination_owner.clone(),
        )?;

        // Step 2: Build transfer proof
        let source_chain = lock_event.source_chain.clone();
        let source_block_height = lock_event.source_block_height;
        let lock_timestamp = lock_event.timestamp;

        let is_finalized = current_block_height >= source_block_height + finality_depth;

        let transfer_proof = CrossChainTransferProof {
            lock_event,
            inclusion_proof,
            finality_proof: CrossChainFinalityProof {
                source_chain: source_chain.clone(),
                height: source_block_height,
                current_height: current_block_height,
                is_finalized,
                depth: finality_depth,
            },
            source_state_root: Hash::new([0u8; 32]),
        };

        // Step 3: Verify on destination
        verifier.verify_transfer_proof(&transfer_proof)?;

        // Step 4: Mint on destination
        let result = minter.mint_right(&transfer_proof)?;

        // Step 5: Record in registry
        let entry = CrossChainRegistryEntry {
            right_id,
            source_chain,
            source_seal: transfer_proof.lock_event.source_seal.clone(),
            destination_chain: transfer_proof.lock_event.destination_chain.clone(),
            destination_seal: result.destination_seal.clone(),
            lock_tx_hash: transfer_proof.lock_event.source_tx_hash,
            mint_tx_hash: Hash::new([0u8; 32]),
            timestamp: lock_timestamp,
        };
        self.registry.record_transfer(entry)?;

        Ok(result)
    }
}

/// Cross-chain seal registry.
///
/// Tracks all cross-chain transfers to prevent double-spends.
#[derive(Default)]
pub struct CrossChainRegistry {
    entries: alloc::collections::BTreeMap<Hash, CrossChainRegistryEntry>,
}

impl CrossChainRegistry {
    /// Create a new empty registry.
    pub fn new() -> Self {
        Self {
            entries: alloc::collections::BTreeMap::new(),
        }
    }

    /// Record a cross-chain transfer.
    pub fn record_transfer(
        &mut self,
        entry: CrossChainRegistryEntry,
    ) -> Result<(), CrossChainError> {
        // Check if this Right has already been transferred
        if self.entries.contains_key(&entry.right_id) {
            return Err(CrossChainError::AlreadyMinted);
        }

        // Check if the source seal has already been consumed
        for existing in self.entries.values() {
            if existing.source_seal == entry.source_seal {
                return Err(CrossChainError::AlreadyLocked);
            }
        }

        self.entries.insert(entry.right_id, entry);
        Ok(())
    }

    /// Check if a Right has already been transferred.
    pub fn is_right_transferred(&self, right_id: &Hash) -> bool {
        self.entries.contains_key(right_id)
    }

    /// Check if a source seal has already been consumed.
    pub fn is_seal_consumed(&self, seal: &SealRef) -> bool {
        self.entries.values().any(|e| &e.source_seal == seal)
    }

    /// Get the registry entry for a Right.
    pub fn get_entry(&self, right_id: &Hash) -> Option<&CrossChainRegistryEntry> {
        self.entries.get(right_id)
    }

    /// Get the number of recorded transfers.
    pub fn transfer_count(&self) -> usize {
        self.entries.len()
    }

    /// Get all recorded transfers.
    pub fn all_transfers(&self) -> Vec<&CrossChainRegistryEntry> {
        self.entries.values().collect()
    }
}

// Re-export for convenience
pub use crate::seal_registry::SealConsumption;

/// Cross-chain seal registry for tracking transfers across all chains
pub use crate::seal_registry::CrossChainSealRegistry;

#[cfg(test)]
mod tests {
    use super::*;
    use crate::hash::Hash;

    #[test]
    fn test_chain_id_roundtrip() {
        for chain in [
            ChainId::Bitcoin,
            ChainId::Sui,
            ChainId::Aptos,
            ChainId::Ethereum,
        ] {
            let id = chain.as_u8();
            assert_eq!(ChainId::from_u8(id), Some(chain));
        }
        assert_eq!(ChainId::from_u8(99), None);
    }

    #[test]
    fn test_registry_prevents_double_mint() {
        let mut registry = CrossChainRegistry::new();
        let right_id = Hash::new([0xAB; 32]);

        let entry = CrossChainRegistryEntry {
            right_id,
            source_chain: ChainId::Bitcoin,
            source_seal: SealRef::new(vec![0x01], None).unwrap(),
            destination_chain: ChainId::Sui,
            destination_seal: SealRef::new(vec![0x02], None).unwrap(),
            lock_tx_hash: Hash::new([0x03; 32]),
            mint_tx_hash: Hash::new([0x04; 32]),
            timestamp: 1_000_000,
        };

        registry.record_transfer(entry.clone()).unwrap();

        // Second transfer of same Right should fail
        let result = registry.record_transfer(entry);
        assert!(matches!(result, Err(CrossChainError::AlreadyMinted)));
    }

    #[test]
    fn test_registry_prevents_double_lock() {
        let mut registry = CrossChainRegistry::new();
        let seal = SealRef::new(vec![0x01], None).unwrap();

        let entry1 = CrossChainRegistryEntry {
            right_id: Hash::new([0xAB; 32]),
            source_chain: ChainId::Bitcoin,
            source_seal: seal.clone(),
            destination_chain: ChainId::Sui,
            destination_seal: SealRef::new(vec![0x02], None).unwrap(),
            lock_tx_hash: Hash::new([0x03; 32]),
            mint_tx_hash: Hash::new([0x04; 32]),
            timestamp: 1_000_000,
        };

        registry.record_transfer(entry1).unwrap();

        // Second transfer using same source seal should fail
        let entry2 = CrossChainRegistryEntry {
            right_id: Hash::new([0xCD; 32]),
            source_chain: ChainId::Bitcoin,
            source_seal: seal.clone(),
            destination_chain: ChainId::Aptos,
            destination_seal: SealRef::new(vec![0x05], None).unwrap(),
            lock_tx_hash: Hash::new([0x06; 32]),
            mint_tx_hash: Hash::new([0x07; 32]),
            timestamp: 2_000_000,
        };

        let result = registry.record_transfer(entry2);
        assert!(matches!(result, Err(CrossChainError::AlreadyLocked)));
    }

    #[test]
    fn test_registry_tracks_transfers() {
        let mut registry = CrossChainRegistry::new();
        assert_eq!(registry.transfer_count(), 0);

        let entry = CrossChainRegistryEntry {
            right_id: Hash::new([0xAB; 32]),
            source_chain: ChainId::Bitcoin,
            source_seal: SealRef::new(vec![0x01], None).unwrap(),
            destination_chain: ChainId::Sui,
            destination_seal: SealRef::new(vec![0x02], None).unwrap(),
            lock_tx_hash: Hash::new([0x03; 32]),
            mint_tx_hash: Hash::new([0x04; 32]),
            timestamp: 1_000_000,
        };

        registry.record_transfer(entry).unwrap();
        assert_eq!(registry.transfer_count(), 1);
        assert!(registry.is_right_transferred(&Hash::new([0xAB; 32])));
    }
}