atlas-transparency-log 0.2.1

A cryptographically secure transparency log service for C2PA manifests with Merkle tree proofs
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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
use actix_web::{http::header, web, App, HttpRequest, HttpResponse, HttpServer};
use base64::{engine::general_purpose::STANDARD, Engine as _};
use bytes::Bytes;
use chrono::{DateTime, Utc};
use log::{debug, error, info};
use mongodb::{Client, Database};
use ring::signature::Ed25519KeyPair;
use serde::{Deserialize, Serialize};
use std::sync::Arc;

use atlas_transparency_log::{
    detect_content_type, hash_binary, is_valid_manifest_id,
    merkle_tree::{ConsistencyProof, InclusionProof, LogLeaf, MerkleProof, MerkleTree},
    sign_data, ContentFormat,
};

#[derive(Clone)]
struct AppState {
    db: Arc<Database>,
    key_pair: Arc<Ed25519KeyPair>,
    merkle_tree: Arc<parking_lot::RwLock<MerkleTree>>,
}

#[derive(Debug, Serialize, Deserialize)]
struct ManifestEntry {
    #[serde(rename = "_id", skip_serializing_if = "Option::is_none")]
    pub id: Option<mongodb::bson::oid::ObjectId>,
    pub manifest_id: String,
    pub manifest_type: String,
    #[serde(skip_serializing_if = "should_skip_metadata", default)]
    pub content_format: ContentFormat,
    #[serde(rename = "manifest", skip_serializing_if = "Option::is_none")]
    pub manifest_json: Option<serde_json::Value>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub manifest_cbor: Option<String>, // Base64 encoded CBOR
    #[serde(skip_serializing_if = "Option::is_none")]
    pub manifest_binary: Option<String>, // Base64 encoded binary
    pub created_at: DateTime<Utc>,
    #[serde(skip_serializing_if = "should_skip_metadata", default)]
    pub sequence_number: u64,
    #[serde(skip_serializing_if = "should_skip_metadata", default)]
    pub hash: String,
    #[serde(skip_serializing_if = "should_skip_metadata", default)]
    pub signature: String,
}

// Thread-local flag for controlling metadata serialization
thread_local! {
    static INCLUDE_TLOG_METADATA: std::cell::Cell<bool> = std::cell::Cell::new(false);
}

fn should_skip_metadata<T>(_: &T) -> bool {
    !INCLUDE_TLOG_METADATA.with(|f| f.get())
}

fn set_include_tlog_metadata(include: bool) {
    INCLUDE_TLOG_METADATA.with(|f| f.set(include));
}

#[derive(Debug, Deserialize)]
struct GetManifestQuery {
    include_tlog_metadata: Option<bool>,
}

// Store manifest with content type support
async fn store_manifest(
    state: web::Data<AppState>,
    req: HttpRequest,
    bytes: Bytes,
    path: web::Path<String>,
    query: web::Query<ManifestQuery>,
) -> HttpResponse {
    // Validate input size
    const MAX_MANIFEST_SIZE: usize = 10 * 1024 * 1024; // 10MB
    if bytes.len() > MAX_MANIFEST_SIZE {
        return HttpResponse::BadRequest().json(serde_json::json!({
            "error": "Manifest too large",
            "max_size": MAX_MANIFEST_SIZE
        }));
    }

    let manifest_id = path.to_string();
    if !is_valid_manifest_id(&manifest_id) {
        return HttpResponse::BadRequest().json(serde_json::json!({
            "error": "Invalid manifest ID format",
            "details": "Must be a valid C2PA URN, UUID, or alphanumeric string"
        }));
    }

    let collection = state.db.collection::<ManifestEntry>("manifests");
    let manifest_type_param = &query.manifest_type;

    debug!(
        "Received manifest with ID: {}, manifest_type param: {:?}",
        &manifest_id, manifest_type_param
    );

    // Detect content format
    let content_format = detect_content_type(&req);

    let content_hash = hash_binary(&bytes);
    let signature = sign_data(&state.key_pair, &content_hash.as_bytes());

    // Get next sequence number
    let sequence_count = collection
        .count_documents(mongodb::bson::doc! {})
        .await
        .unwrap_or(0);
    let sequence_number = sequence_count + 1;

    let now = Utc::now();

    // Default manifest type from query parameter or "unknown"
    let manifest_type = manifest_type_param
        .as_ref()
        .map(|s| s.clone())
        .unwrap_or_else(|| "unknown".to_string());

    // Build the manifest entry based on content type
    let mut entry = ManifestEntry {
        id: None,
        manifest_id: manifest_id.clone(),
        manifest_type,
        content_format: content_format.clone(),
        manifest_json: None,
        manifest_cbor: None,
        manifest_binary: None,
        created_at: now,
        sequence_number: sequence_number as u64,
        hash: content_hash.clone(),
        signature,
    };

    match content_format {
        ContentFormat::JSON => {
            match serde_json::from_slice::<serde_json::Value>(&bytes) {
                Ok(json_value) => {
                    // Extract manifest_type from JSON
                    let json_manifest_type = json_value
                        .get("manifest")
                        .and_then(|m| m.get("manifest_type"))
                        .or_else(|| json_value.get("manifest_type"))
                        .and_then(|v| v.as_str())
                        .map(|s| s.to_string());

                    if let Some(mt) = json_manifest_type {
                        if manifest_type_param.is_none() {
                            entry.manifest_type = mt;
                        }
                    }

                    debug!("Using manifest_type: {}", entry.manifest_type);
                    entry.manifest_json = Some(json_value);
                }
                Err(e) => {
                    error!("Failed to parse JSON: {:?}", e);
                    return HttpResponse::BadRequest().body(format!("Invalid JSON format: {}", e));
                }
            }
        }
        ContentFormat::CBOR => {
            let encoded = STANDARD.encode(&bytes);
            entry.manifest_cbor = Some(encoded);

            match serde_cbor::from_slice::<serde_json::Value>(&bytes) {
                Ok(cbor_value) => {
                    let cbor_manifest_type = cbor_value
                        .get("manifest_type")
                        .and_then(|v| v.as_str())
                        .map(|s| s.to_string());

                    if let Some(mt) = cbor_manifest_type {
                        if manifest_type_param.is_none() {
                            entry.manifest_type = mt;
                        }
                    } else if manifest_type_param.is_none() {
                        entry.manifest_type = "cbor_manifest".to_string();
                    }
                }
                Err(e) => {
                    debug!("Could not extract manifest_type from CBOR: {:?}", e);
                    if manifest_type_param.is_none() {
                        entry.manifest_type = "cbor_manifest".to_string();
                    }
                }
            }
        }
        ContentFormat::Binary => {
            let encoded = STANDARD.encode(&bytes);
            entry.manifest_binary = Some(encoded);

            if manifest_type_param.is_none() {
                entry.manifest_type = "binary_manifest".to_string();
            }
        }
    }

    match collection.insert_one(&entry).await {
        Ok(result) => {
            info!(
                "Successfully stored manifest with ID: {}",
                result.inserted_id
            );

            // Create a LogLeaf with all necessary data
            let leaf = LogLeaf::new(
                content_hash,
                manifest_id.clone(),
                sequence_number as u64,
                now,
            );

            // Update the Merkle tree
            {
                let mut tree = state.merkle_tree.write();
                tree.add_leaf(leaf);

                // Persist the updated Merkle tree to the database
                if let Err(e) = persist_merkle_tree(&state.db, &tree).await {
                    error!("Failed to persist Merkle tree: {:?}", e);
                }
            }

            HttpResponse::Created().json(serde_json::json!({
                "id": result.inserted_id,
                "manifest_id": manifest_id,
                "sequence_number": sequence_number,
                "hash": entry.hash,
                "signature": entry.signature,
            }))
        }
        Err(e) => {
            error!("Failed to store manifest: {:?}", e);
            HttpResponse::InternalServerError().json(serde_json::json!({
                "error": "Failed to store manifest",
                "details": e.to_string()
            }))
        }
    }
}

async fn persist_merkle_tree(
    db: &Database,
    tree: &MerkleTree,
) -> Result<(), mongodb::error::Error> {
    let collection = db.collection::<serde_json::Value>("merkle_tree_state");

    // Clear existing tree state
    collection.delete_many(mongodb::bson::doc! {}).await?;

    // Store the current tree state (leaves and metadata)
    // Note: Root hash is recomputed from leaves during load for integrity
    let tree_state = serde_json::json!({
        "leaves": tree.leaves(),
        "tree_size": tree.size(),
        "root_hash": tree.root_hash(),
        "updated_at": Utc::now(),
    });

    collection.insert_one(tree_state).await?;
    Ok(())
}

async fn load_merkle_tree(db: &Database) -> MerkleTree {
    let collection = db.collection::<serde_json::Value>("merkle_tree_state");

    match collection.find_one(mongodb::bson::doc! {}).await {
        Ok(Some(state)) => {
            if let Ok(leaves) = serde_json::from_value::<Vec<LogLeaf>>(state["leaves"].clone()) {
                // Recompute root hash from leaves to ensure integrity
                return MerkleTree::from_leaves(leaves);
            }
        }
        _ => {}
    }

    // If no tree exists or error occurs, rebuild from manifests
    let manifests_collection = db.collection::<ManifestEntry>("manifests");
    if let Ok(cursor) = manifests_collection.find(mongodb::bson::doc! {}).await {
        if let Ok(manifests) = futures::stream::TryStreamExt::try_collect::<Vec<_>>(cursor).await {
            let mut tree = MerkleTree::new();

            for manifest in manifests {
                let leaf = LogLeaf::new(
                    manifest.hash,
                    manifest.manifest_id,
                    manifest.sequence_number,
                    manifest.created_at,
                );
                tree.add_leaf(leaf);
            }

            return tree;
        }
    }

    MerkleTree::new()
}

// List manifests with pagination
async fn list_manifests(state: web::Data<AppState>, query: web::Query<ListQuery>) -> HttpResponse {
    let collection = state.db.collection::<ManifestEntry>("manifests");

    let limit = query.limit.unwrap_or(100) as i64;
    let skip = query.skip.unwrap_or(0) as u64;

    // Build filter document based on query parameters
    let mut filter = mongodb::bson::Document::new();

    if let Some(manifest_type) = &query.manifest_type {
        filter.insert("manifest_type", manifest_type);
    }

    if let Some(format) = &query.format {
        let content_format = match format.as_str() {
            "json" => "JSON",
            "cbor" => "CBOR",
            "binary" => "Binary",
            _ => "JSON",
        };
        filter.insert("content_format", content_format);
    }

    let filter_doc = if filter.is_empty() {
        mongodb::bson::doc! {}
    } else {
        filter
    };

    match collection
        .find(filter_doc)
        .sort(mongodb::bson::doc! { "sequence_number": 1 })
        .skip(skip)
        .limit(limit)
        .await
    {
        Ok(cursor) => match futures::stream::TryStreamExt::try_collect::<Vec<_>>(cursor).await {
            Ok(manifests) => HttpResponse::Ok().json(manifests),
            Err(e) => HttpResponse::InternalServerError().body(e.to_string()),
        },
        Err(e) => HttpResponse::InternalServerError().body(e.to_string()),
    }
}

// Query parameters for manifest operations
#[derive(Debug, Deserialize)]
struct ManifestQuery {
    manifest_type: Option<String>,
}

// Enhanced listing query parameters
#[derive(Debug, Deserialize)]
struct ListQuery {
    limit: Option<usize>,
    skip: Option<u64>,
    manifest_type: Option<String>,
    format: Option<String>,
}

// List manifests by type
async fn list_manifests_by_type(
    state: web::Data<AppState>,
    path: web::Path<String>,
    query: web::Query<ListQuery>,
) -> HttpResponse {
    let manifest_type = path.into_inner();
    let collection = state.db.collection::<ManifestEntry>("manifests");

    let limit = query.limit.unwrap_or(100) as i64;
    let skip = query.skip.unwrap_or(0) as u64;

    let filter = mongodb::bson::doc! { "manifest_type": manifest_type };

    match collection
        .find(filter)
        .sort(mongodb::bson::doc! { "sequence_number": 1 })
        .skip(skip)
        .limit(limit)
        .await
    {
        Ok(cursor) => match futures::stream::TryStreamExt::try_collect::<Vec<_>>(cursor).await {
            Ok(manifests) => HttpResponse::Ok().json(manifests),
            Err(e) => HttpResponse::InternalServerError().body(e.to_string()),
        },
        Err(e) => HttpResponse::InternalServerError().body(e.to_string()),
    }
}

// Get manifest by ID
async fn get_manifest(
    state: web::Data<AppState>,
    req: HttpRequest,
    path: web::Path<String>,
    query: web::Query<GetManifestQuery>,
) -> HttpResponse {
    let collection = state.db.collection::<ManifestEntry>("manifests");
    debug!("Searching for manifest with ID: {}", &*path);

    // Set metadata inclusion flag based on query parameter
    let include_tlog_metadata = query.include_tlog_metadata.unwrap_or(false);
    set_include_tlog_metadata(include_tlog_metadata);

    match collection
        .find_one(mongodb::bson::doc! { "manifest_id": &*path })
        .await
    {
        Ok(Some(manifest)) => {
            info!("Found manifest for ID: {}", &*path);

            // Check Accept header for content negotiation
            let accept_cbor = req
                .headers()
                .get(header::ACCEPT)
                .and_then(|h| h.to_str().ok())
                .map(|s| s.contains("application/cbor"))
                .unwrap_or(false);

            // Return appropriate format based on what's available and what's requested
            match manifest.content_format {
                ContentFormat::CBOR if accept_cbor => {
                    if let Some(ref cbor_data) = manifest.manifest_cbor {
                        if let Ok(decoded) = STANDARD.decode(cbor_data) {
                            return HttpResponse::Ok()
                                .content_type("application/cbor")
                                .body(decoded);
                        }
                    }
                }
                ContentFormat::Binary => {
                    if let Some(ref binary_data) = manifest.manifest_binary {
                        if let Ok(decoded) = STANDARD.decode(binary_data) {
                            return HttpResponse::Ok()
                                .content_type("application/octet-stream")
                                .body(decoded);
                        }
                    }
                }
                _ => {} // default to JSON response
            }

            // Default: return as JSON
            HttpResponse::Ok().json(manifest)
        }
        Ok(None) => {
            debug!("No manifest found for ID: {}", &*path);
            HttpResponse::NotFound().body(format!("Manifest not found for ID: {}", &*path))
        }
        Err(e) => {
            error!("Error fetching manifest {}: {:?}", &*path, e);
            HttpResponse::InternalServerError().body(format!("Error fetching manifest: {}", e))
        }
    }
}

// Get inclusion proof for a manifest
async fn get_inclusion_proof(state: web::Data<AppState>, path: web::Path<String>) -> HttpResponse {
    let manifest_id = path.into_inner();

    if !is_valid_manifest_id(&manifest_id) {
        return HttpResponse::BadRequest().json(serde_json::json!({
            "error": "Invalid manifest ID format"
        }));
    }

    let tree = state.merkle_tree.read();
    match tree.generate_inclusion_proof(&manifest_id) {
        Some(proof) => HttpResponse::Ok().json(proof),
        None => HttpResponse::NotFound().json(serde_json::json!({
            "error": "No proof available",
            "manifest_id": manifest_id,
            "reason": "Manifest not found in tree"
        })),
    }
}

// Get latest Merkle root
async fn get_merkle_root(state: web::Data<AppState>) -> HttpResponse {
    let tree = state.merkle_tree.read();
    match tree.root_hash() {
        Some(root) => HttpResponse::Ok().json(serde_json::json!({
            "root_hash": root,
            "tree_size": tree.size()
        })),
        None => HttpResponse::NotFound().body("No Merkle root available yet"),
    }
}

// Verify an inclusion proof
async fn verify_proof(
    state: web::Data<AppState>,
    proof: web::Json<InclusionProof>,
) -> HttpResponse {
    let tree = state.merkle_tree.read();
    let is_valid = tree.verify_inclusion_proof(&proof);

    HttpResponse::Ok().json(serde_json::json!({
        "valid": is_valid,
        "manifest_id": proof.manifest_id,
        "proof_description": (&*proof as &dyn MerkleProof).describe()
    }))
}

// Request structure for consistency proof
#[derive(Debug, Deserialize)]
struct ConsistencyProofRequest {
    old_size: usize,
    new_size: usize,
}

// Get consistency proof between two tree sizes
async fn get_consistency_proof(
    state: web::Data<AppState>,
    query: web::Query<ConsistencyProofRequest>,
) -> HttpResponse {
    let tree = state.merkle_tree.read();

    // Validate sizes
    if query.old_size == 0 || query.new_size == 0 {
        return HttpResponse::BadRequest().json(serde_json::json!({
            "error": "Tree sizes must be greater than 0"
        }));
    }

    if query.old_size > query.new_size {
        return HttpResponse::BadRequest().json(serde_json::json!({
            "error": "Old size must be less than or equal to new size"
        }));
    }

    match tree.generate_consistency_proof(query.old_size, query.new_size) {
        Some(proof) => HttpResponse::Ok().json(serde_json::json!({
            "proof": proof,
            "description": (&proof as &dyn MerkleProof).describe()
        })),
        None => HttpResponse::NotFound().json(serde_json::json!({
            "error": "Cannot generate consistency proof",
            "old_size": query.old_size,
            "new_size": query.new_size,
            "current_tree_size": tree.size()
        })),
    }
}

// Verify a consistency proof
async fn verify_consistency_proof(
    state: web::Data<AppState>,
    proof: web::Json<ConsistencyProof>,
) -> HttpResponse {
    let tree = state.merkle_tree.read();
    let is_valid = tree.verify_consistency_proof(&proof);

    HttpResponse::Ok().json(serde_json::json!({
        "valid": is_valid,
        "old_size": proof.old_size,
        "new_size": proof.new_size,
        "proof_elements": proof.proof_hashes.len(),
        "description": (&*proof as &dyn MerkleProof).describe()
    }))
}

// Get tree statistics
async fn get_tree_stats(state: web::Data<AppState>) -> HttpResponse {
    let tree = state.merkle_tree.read();

    // Calculate additional statistics
    let total_leaves = tree.size();
    let has_root = tree.root_hash().is_some();

    // Estimate tree depth (log2 of size, rounded up)
    let estimated_depth = if total_leaves > 0 {
        (total_leaves as f64).log2().ceil() as usize
    } else {
        0
    };

    HttpResponse::Ok().json(serde_json::json!({
        "current_size": total_leaves,
        "root_hash": tree.root_hash(),
        "estimated_depth": estimated_depth,
        "has_root": has_root,
        "timestamp": Utc::now(),
        "tree_health": if has_root { "healthy" } else { "empty" }
    }))
}

// Get historical root for specific tree sizes
async fn get_historical_root(state: web::Data<AppState>, path: web::Path<usize>) -> HttpResponse {
    let tree_size = path.into_inner();
    let tree = state.merkle_tree.read();

    if tree_size == 0 || tree_size > tree.size() {
        return HttpResponse::BadRequest().json(serde_json::json!({
            "error": "Invalid tree size",
            "requested_size": tree_size,
            "current_size": tree.size()
        }));
    }

    // Use the tree's method to compute historical root
    let root_hash = tree.compute_root_for_size(tree_size);

    match root_hash {
        Some(root) => HttpResponse::Ok().json(serde_json::json!({
            "tree_size": tree_size,
            "root_hash": root,
            "current_size": tree.size()
        })),
        None => HttpResponse::InternalServerError().json(serde_json::json!({
            "error": "Failed to compute historical root"
        })),
    }
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    env_logger::init();

    // Get MongoDB URI from environment variable or use default
    let mongodb_uri =
        std::env::var("MONGODB_URI").unwrap_or_else(|_| "mongodb://localhost:27017".to_string());

    // Get server host and port from environment variables or use defaults
    let server_host = std::env::var("SERVER_HOST").unwrap_or_else(|_| "0.0.0.0".to_string());
    let server_port = std::env::var("SERVER_PORT").unwrap_or_else(|_| "8080".to_string());

    // Combine host and port
    let server_addr = format!("{}:{}", server_host, server_port);

    // Generate or load keys
    let key_path =
        std::env::var("KEY_PATH").unwrap_or_else(|_| "transparency_log_key.pem".to_string());
    let key_pair = match std::fs::read(&key_path) {
        Ok(pkcs8_bytes) => Ed25519KeyPair::from_pkcs8(&pkcs8_bytes).expect("Failed to parse key"),
        Err(_) => {
            // Generate new key
            let rng = ring::rand::SystemRandom::new();
            let pkcs8_bytes = Ed25519KeyPair::generate_pkcs8(&rng).expect("Failed to generate key");
            std::fs::write(&key_path, pkcs8_bytes.as_ref()).expect("Failed to save key");
            Ed25519KeyPair::from_pkcs8(pkcs8_bytes.as_ref())
                .expect("Failed to parse newly generated key")
        }
    };

    let client = Client::with_uri_str(&mongodb_uri)
        .await
        .expect("Failed to connect to MongoDB");

    // Configurable database name
    let db_name = std::env::var("DB_NAME").unwrap_or_else(|_| "c2pa_manifests".to_string());

    let db = Arc::new(client.database(&db_name));

    // Load Merkle Tree from database or create new one
    let merkle_tree = Arc::new(parking_lot::RwLock::new(load_merkle_tree(&db).await));

    let state = web::Data::new(AppState {
        db: db.clone(),
        key_pair: Arc::new(key_pair),
        merkle_tree,
    });

    println!(
        "Starting transparency log server at http://{}:{}",
        if server_host == "0.0.0.0" {
            "localhost"
        } else {
            &server_host
        },
        server_port
    );

    HttpServer::new(move || {
        App::new()
            .app_data(state.clone())
            .app_data(web::PayloadConfig::new(10 * 1024 * 1024)) // Max 10MB
            // Manifest routes
            .route("/manifests", web::get().to(list_manifests))
            .route("/manifests/{id}", web::post().to(store_manifest))
            .route("/manifests/{id}", web::get().to(get_manifest))
            .route("/manifests/{id}/proof", web::get().to(get_inclusion_proof))
            // Merkle tree routes
            .route("/merkle/root", web::get().to(get_merkle_root))
            .route("/merkle/verify", web::post().to(verify_proof))
            .route("/merkle/stats", web::get().to(get_tree_stats))
            .route("/merkle/consistency", web::get().to(get_consistency_proof))
            .route(
                "/merkle/consistency/verify",
                web::post().to(verify_consistency_proof),
            )
            .route("/merkle/root/{size}", web::get().to(get_historical_root))
            // Type-specific routes
            .route(
                "/types/{manifest_type}/manifests",
                web::get().to(list_manifests_by_type),
            )
    })
    .bind(&server_addr)?
    .run()
    .await
}

// Include the test module
#[cfg(test)]
mod tests {
    use super::*;
    use actix_web;
    use base64::engine::general_purpose::STANDARD;
    use chrono::Utc;
    use ring::signature::Ed25519KeyPair;

    use atlas_common::hash::calculate_hash;

    use atlas_transparency_log::sign_data;

    // Helper function to hash a string using atlas-common
    fn hash_string(data: &str) -> String {
        calculate_hash(data.as_bytes())
    }

    #[actix_web::test]
    async fn test_hashing() {
        // Test hash consistency using atlas-common
        let data = "test data";
        let hash1 = hash_string(data);
        let hash2 = hash_string(data);

        // Same input should produce same hash
        assert_eq!(hash1, hash2);

        // Different inputs should produce different hashes
        let hash3 = hash_string("different data");
        assert_ne!(hash1, hash3);

        // Test that we're using SHA384 (48 bytes = 96 hex chars)
        let raw_hash = calculate_hash(data.as_bytes());
        assert_eq!(raw_hash.len(), 96); // SHA384 produces 96 hex characters
    }

    #[actix_web::test]
    async fn test_signing() {
        // Generate a test key pair
        let rng = ring::rand::SystemRandom::new();
        let pkcs8_bytes = Ed25519KeyPair::generate_pkcs8(&rng).expect("Failed to generate key");
        let key_pair =
            Ed25519KeyPair::from_pkcs8(pkcs8_bytes.as_ref()).expect("Failed to parse key");

        // Sign some data
        let data = "test data";
        let signature = sign_data(&key_pair, data.as_bytes());

        // Signature should not be empty
        assert!(!signature.is_empty());

        // Ed25519 signatures are 64 bytes, which is 88 chars in base64 (including padding)
        let decoded = STANDARD.decode(&signature).unwrap();
        assert_eq!(decoded.len(), 64);
    }

    #[actix_web::test]
    async fn test_manifest_serialization_with_metadata_flag() {
        let now = Utc::now();

        let entry = ManifestEntry {
            id: None,
            manifest_id: "test-id".to_string(),
            manifest_type: "Dataset".to_string(),
            content_format: ContentFormat::JSON,
            manifest_json: Some(serde_json::json!({"test": "data"})),
            manifest_cbor: None,
            manifest_binary: None,
            created_at: now,
            sequence_number: 42,
            hash: "test-hash".to_string(),
            signature: "test-sig".to_string(),
        };

        // Test without metadata (default)
        set_include_tlog_metadata(false);
        let json_without = serde_json::to_value(&entry).unwrap();
        assert!(json_without.get("sequence_number").is_none());
        assert!(json_without.get("hash").is_none());
        assert!(json_without.get("signature").is_none());
        assert!(json_without.get("content_format").is_none());
        assert!(json_without.get("manifest").is_some());

        // Test with metadata
        set_include_tlog_metadata(true);
        let json_with = serde_json::to_value(&entry).unwrap();
        assert_eq!(json_with.get("sequence_number").unwrap(), 42);
        assert_eq!(json_with.get("hash").unwrap(), "test-hash");
        assert_eq!(json_with.get("signature").unwrap(), "test-sig");
        assert!(json_with.get("content_format").is_some());
        assert!(json_with.get("manifest").is_some());
    }
}