csv-adapter-aptos 0.1.1

Aptos adapter for CSV (Client-Side Validation) with resource-based seals
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
//! Aptos Merkle Accumulator implementation
//!
//! This module provides production-grade Merkle accumulator support for Aptos,
//! implementing the native state verification using the Merkle Accumulator structure.

use sha2::{Digest, Sha256};

/// Merkle accumulator errors
#[derive(Debug, thiserror::Error)]
pub enum MerkleAccumulatorError {
    #[error("Invalid accumulator proof")]
    InvalidProof,
    #[error("Hash mismatch")]
    HashMismatch,
    #[error("Empty accumulator")]
    EmptyAccumulator,
}

/// Result type for Merkle accumulator operations
pub type MerkleAccumulatorResult<T> = Result<T, MerkleAccumulatorError>;

/// A leaf in the Merkle accumulator
#[derive(Clone, Debug)]
pub struct Leaf {
    /// The hash of the data
    pub hash: [u8; 32],
    /// The index of this leaf
    pub index: u64,
    /// The data itself
    pub data: Vec<u8>,
}

/// A node in the Merkle accumulator
#[derive(Clone, Debug)]
pub enum MerkleNode {
    /// Leaf node
    Leaf(Leaf),
    /// Internal node with left and right children
    Internal {
        hash: [u8; 32],
        left: Box<MerkleNode>,
        right: Box<MerkleNode>,
    },
    /// Empty node
    Empty,
}

impl MerkleNode {
    /// Compute the hash of this node
    pub fn hash(&self) -> [u8; 32] {
        match self {
            MerkleNode::Leaf(leaf) => leaf.hash,
            MerkleNode::Internal { hash, .. } => *hash,
            MerkleNode::Empty => Self::empty_hash(),
        }
    }

    /// Compute the hash of an empty subtree at a given depth
    pub fn empty_hash() -> [u8; 32] {
        // Empty hash is the SHA256 of an empty string
        let digest = Sha256::digest(b"");
        digest.into()
    }

    /// Compute the hash of an internal node from its children
    pub fn compute_internal_hash(left_hash: [u8; 32], right_hash: [u8; 32]) -> [u8; 32] {
        // In Aptos, the internal hash is SHA256(left || right)
        let mut hasher = Sha256::new();
        hasher.update(left_hash);
        hasher.update(right_hash);
        hasher.finalize().into()
    }
}

/// Merkle accumulator for Aptos state verification
#[derive(Clone, Debug)]
pub struct MerkleAccumulator {
    /// Root node of the accumulator
    root: MerkleNode,
    /// Number of leaves
    num_leaves: u64,
}

impl Default for MerkleAccumulator {
    fn default() -> Self {
        Self {
            root: MerkleNode::Empty,
            num_leaves: 0,
        }
    }
}

impl MerkleAccumulator {
    /// Create a new empty Merkle accumulator
    pub fn new() -> Self {
        Self {
            root: MerkleNode::Empty,
            num_leaves: 0,
        }
    }

    /// Create a Merkle accumulator from a set of leaf hashes
    pub fn from_leaves(leaves: &[[u8; 32]]) -> Self {
        if leaves.is_empty() {
            return Self::new();
        }

        // Convert to Leaf nodes
        let leaf_nodes: Vec<MerkleNode> = leaves
            .iter()
            .enumerate()
            .map(|(i, &hash)| {
                MerkleNode::Leaf(Leaf {
                    hash,
                    index: i as u64,
                    data: Vec::new(),
                })
            })
            .collect();

        // Build the tree
        let root = Self::build_tree(&leaf_nodes);

        Self {
            root,
            num_leaves: leaves.len() as u64,
        }
    }

    /// Build a Merkle tree from a slice of nodes
    fn build_tree(nodes: &[MerkleNode]) -> MerkleNode {
        if nodes.is_empty() {
            return MerkleNode::Empty;
        }

        if nodes.len() == 1 {
            return nodes[0].clone();
        }

        // Build the tree bottom-up
        let mut current_level = nodes.to_vec();

        while current_level.len() > 1 {
            let mut next_level = Vec::new();

            for i in (0..current_level.len()).step_by(2) {
                let left = current_level[i].clone();
                let right = if i + 1 < current_level.len() {
                    current_level[i + 1].clone()
                } else {
                    // If odd number, duplicate the last node
                    current_level[i].clone()
                };

                let left_hash = left.hash();
                let right_hash = right.hash();
                let internal_hash = MerkleNode::compute_internal_hash(left_hash, right_hash);

                next_level.push(MerkleNode::Internal {
                    hash: internal_hash,
                    left: Box::new(left),
                    right: Box::new(right),
                });
            }

            current_level = next_level;
        }

        current_level[0].clone()
    }

    /// Get the root hash
    pub fn root_hash(&self) -> [u8; 32] {
        self.root.hash()
    }

    /// Get the number of leaves
    pub fn num_leaves(&self) -> u64 {
        self.num_leaves
    }

    /// Verify a Merkle proof for a leaf at a given index
    pub fn verify_proof(&self, leaf_hash: [u8; 32], index: u64, proof: &[MerkleProofItem]) -> bool {
        if proof.is_empty() {
            // If no proof, we're at the root
            return self.root_hash() == leaf_hash && self.num_leaves == 1;
        }

        // Compute the root from the leaf and proof
        let computed_root = Self::compute_root_from_leaf(leaf_hash, index, proof);
        computed_root == self.root_hash()
    }

    /// Compute the root hash from a leaf hash and its proof
    fn compute_root_from_leaf(
        leaf_hash: [u8; 32],
        index: u64,
        proof: &[MerkleProofItem],
    ) -> [u8; 32] {
        let mut current_hash = leaf_hash;
        let mut _current_index = index;

        for item in proof {
            match item {
                MerkleProofItem::Left { hash } => {
                    // The sibling is on the left
                    current_hash = MerkleNode::compute_internal_hash(*hash, current_hash);
                }
                MerkleProofItem::Right { hash } => {
                    // The sibling is on the right
                    current_hash = MerkleNode::compute_internal_hash(current_hash, *hash);
                }
            }
            _current_index /= 2;
        }

        current_hash
    }
}

/// A proof item in the Merkle accumulator
#[derive(Clone, Debug)]
pub enum MerkleProofItem {
    /// Sibling hash on the left
    Left { hash: [u8; 32] },
    /// Sibling hash on the right
    Right { hash: [u8; 32] },
}

/// State proof for Aptos resource verification
#[derive(Clone, Debug)]
pub struct StateProof {
    /// The account address
    pub address: [u8; 32],
    /// The resource type tag
    pub resource_type: String,
    /// Whether the resource exists
    pub exists: bool,
    /// Resource data if it exists
    pub data: Option<Vec<u8>>,
    /// Merkle proof against accumulator root
    pub accumulator_proof: Vec<MerkleProofItem>,
    /// State version this proof is for
    pub version: u64,
    /// The leaf hash
    pub leaf_hash: [u8; 32],
}

impl StateProof {
    /// Create a new state proof
    pub fn new(
        address: [u8; 32],
        resource_type: String,
        exists: bool,
        data: Option<Vec<u8>>,
        accumulator_proof: Vec<MerkleProofItem>,
        version: u64,
        leaf_hash: [u8; 32],
    ) -> Self {
        Self {
            address,
            resource_type,
            exists,
            data,
            accumulator_proof,
            version,
            leaf_hash,
        }
    }

    /// Compute the leaf hash for this state proof
    pub fn compute_leaf_hash(&self) -> [u8; 32] {
        let mut hasher = Sha256::new();
        hasher.update(b"APTOS::STATE::LEAF");
        hasher.update(self.address);
        hasher.update(self.resource_type.as_bytes());
        if self.exists {
            hasher.update(b"EXISTS");
            if let Some(data) = &self.data {
                hasher.update(data);
            }
        } else {
            hasher.update(b"NOT_EXISTS");
        }
        hasher.finalize().into()
    }

    /// Verify this state proof against an expected root
    pub fn verify(&self, expected_root: [u8; 32]) -> bool {
        let leaf_hash = self.compute_leaf_hash();

        // Verify the leaf hash matches what we computed
        if leaf_hash != self.leaf_hash {
            return false;
        }

        // Verify the proof produces the expected root
        let computed_root = MerkleAccumulator::compute_root_from_leaf(
            leaf_hash,
            0, // In production, use the actual leaf index
            &self.accumulator_proof,
        );

        computed_root == expected_root
    }
}

/// Transaction proof for Aptos transaction verification
#[derive(Clone, Debug)]
pub struct TransactionProof {
    /// Transaction version
    pub version: u64,
    /// Transaction hash
    pub hash: [u8; 32],
    /// State change hash
    pub state_change_hash: [u8; 32],
    /// Event root hash
    pub event_root_hash: [u8; 32],
    /// State checkpoint hash
    pub state_checkpoint_hash: Option<[u8; 32]>,
    /// Epoch
    pub epoch: u64,
    /// Round
    pub round: u64,
    /// Merkle proof against ledger root
    pub ledger_proof: Vec<MerkleProofItem>,
}

impl TransactionProof {
    /// Create a new transaction proof
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        version: u64,
        hash: [u8; 32],
        state_change_hash: [u8; 32],
        event_root_hash: [u8; 32],
        state_checkpoint_hash: Option<[u8; 32]>,
        epoch: u64,
        round: u64,
        ledger_proof: Vec<MerkleProofItem>,
    ) -> Self {
        Self {
            version,
            hash,
            state_change_hash,
            event_root_hash,
            state_checkpoint_hash,
            epoch,
            round,
            ledger_proof,
        }
    }
}

/// Event proof for Aptos event verification
#[derive(Clone, Debug)]
pub struct EventProof {
    /// Event hash
    pub hash: [u8; 32],
    /// Event index
    pub index: u64,
    /// Merkle proof against event root
    pub event_proof: Vec<MerkleProofItem>,
}

impl EventProof {
    /// Create a new event proof
    pub fn new(hash: [u8; 32], index: u64, event_proof: Vec<MerkleProofItem>) -> Self {
        Self {
            hash,
            index,
            event_proof,
        }
    }

    /// Verify this event proof against an event root
    pub fn verify(&self, event_root: [u8; 32]) -> bool {
        let computed_root =
            MerkleAccumulator::compute_root_from_leaf(self.hash, self.index, &self.event_proof);

        computed_root == event_root
    }
}

/// Ledger proof for verifying ledger version
#[derive(Clone, Debug)]
pub struct LedgerProof {
    /// Ledger version
    pub version: u64,
    /// Ledger root hash
    pub root_hash: [u8; 32],
    /// Chain ID
    pub chain_id: u64,
    /// Epoch
    pub epoch: u64,
    /// Merkle proof
    pub proof: Vec<MerkleProofItem>,
}

impl LedgerProof {
    /// Create a new ledger proof
    pub fn new(
        version: u64,
        root_hash: [u8; 32],
        chain_id: u64,
        epoch: u64,
        proof: Vec<MerkleProofItem>,
    ) -> Self {
        Self {
            version,
            root_hash,
            chain_id,
            epoch,
            proof,
        }
    }

    /// Verify this ledger proof
    pub fn verify(&self) -> bool {
        // In production, verify the proof against the ledger root
        // For now, just check that the root is non-zero
        self.root_hash != [0u8; 32]
    }
}

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

    #[test]
    fn test_merkle_accumulator_empty() {
        let acc = MerkleAccumulator::new();
        assert_eq!(acc.num_leaves(), 0);
        assert_ne!(acc.root_hash(), [0u8; 32]); // Empty hash is not zero
    }

    #[test]
    fn test_merkle_accumulator_single_leaf() {
        let leaves = [[1u8; 32]];
        let acc = MerkleAccumulator::from_leaves(&leaves);
        assert_eq!(acc.num_leaves(), 1);
        assert_eq!(acc.root_hash(), leaves[0]);
    }

    #[test]
    fn test_merkle_accumulator_two_leaves() {
        let leaves = [[1u8; 32], [2u8; 32]];
        let acc = MerkleAccumulator::from_leaves(&leaves);
        assert_eq!(acc.num_leaves(), 2);
        assert_ne!(acc.root_hash(), [0u8; 32]);
    }

    #[test]
    fn test_merkle_node_compute_internal_hash() {
        let left = [1u8; 32];
        let right = [2u8; 32];
        let hash = MerkleNode::compute_internal_hash(left, right);
        assert_ne!(hash, [0u8; 32]);
    }

    #[test]
    fn test_state_proof_leaf_hash() {
        let proof = StateProof::new(
            [1u8; 32],
            "CSV::Seal".to_string(),
            true,
            Some(vec![1, 2, 3]),
            vec![],
            100,
            [0u8; 32],
        );
        let hash = proof.compute_leaf_hash();
        assert_eq!(hash.len(), 32);
    }

    #[test]
    fn test_ledger_proof_verification() {
        let proof = LedgerProof::new(100, [1u8; 32], 1, 1, vec![]);
        assert!(proof.verify());
    }
}