tibet-zip-cli 2.1.1

tibet-zip command-line tool: pack, unpack, verify, inspect
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
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
//! tibet-zip CLI library.
//!
//! The entire CLI implementation lives here as `pub fn run()`, so the
//! canonical `tibet-zip-cli` binary and the short-name `tbz-cli` alias
//! both delegate to a single source of truth — no drift possible.
//!
//! Usage (when installed):
//!   tbz pack <path> -o output.tza    Create a TBZ archive
//!   tbz unpack <archive.tza>         Extract via TIBET Airlock
//!   tbz verify <archive.tza>         Validate without extracting
//!   tbz inspect <archive.tza>        Show manifest and block info
//!   tbz keygen -o <name>             Generate Ed25519 keypair (v2)
//!   tbz init                         Generate .jis.json for current repo
//!
//! Short aliases (because life is too short for tar -xvf):
//!   tbz p  = tbz pack
//!   tbz x  = tbz unpack    (x for eXtract, like tar)
//!   tbz v  = tbz verify
//!   tbz i  = tbz inspect
//!
//! Smart defaults:
//!   tbz archive.tza          → auto-detects: verify + unpack
//!   tbz ./src                → auto-detects: pack

use clap::{Parser, Subcommand};
use std::fs;
use std::io::{BufReader, BufWriter, Read as _};
use std::path::Path;

use sha2::{Digest, Sha256};
use tbz_core::envelope::TibetEnvelope;
use tbz_core::manifest::{BlockEntry, Manifest};
use tbz_core::stream::{TbzReader, TbzWriter};
use tbz_core::v2;
use tbz_core::{signature, BlockType};

pub mod tibet_zip;

// ---------------------------------------------------------------------------
// Format detection: TBZ block-format vs TIBET-ZIP (Desktop ZIP+MANIFEST)
// ---------------------------------------------------------------------------

/// Archive format detected by magic bytes
#[derive(Debug, Clone, Copy, PartialEq)]
enum ArchiveFormat {
    /// CLI block format: magic 0x54425A ("TBZ"), zstd+Ed25519
    TbzBlock,
    /// Desktop ZIP format: magic 0x504B0304 (PK), MANIFEST.json with SHA-256
    TibetZip,
    /// Unknown format
    Unknown,
}

/// Detect archive format by reading the first 4 bytes
fn detect_format(path: &str) -> anyhow::Result<ArchiveFormat> {
    let mut file = fs::File::open(path)?;
    let mut magic = [0u8; 4];
    let n = std::io::Read::read(&mut file, &mut magic)?;
    if n < 3 {
        return Ok(ArchiveFormat::Unknown);
    }
    if magic[0..3] == [0x54, 0x42, 0x5A] {
        Ok(ArchiveFormat::TbzBlock)
    } else if magic == [0x50, 0x4B, 0x03, 0x04] {
        Ok(ArchiveFormat::TibetZip)
    } else {
        Ok(ArchiveFormat::Unknown)
    }
}

// ---------------------------------------------------------------------------
// Transparency Mirror client (best-effort HTTP, never a hard error)
// ---------------------------------------------------------------------------
mod mirror_client {
    use serde::{Deserialize, Serialize};

    const TIMEOUT_SECS: u64 = 5;

    #[derive(Serialize)]
    pub struct RegisterPayload {
        pub content_hash: String,
        pub signing_key: String,
        pub jis_id: Option<String>,
        pub source_repo: Option<String>,
        pub block_count: u32,
        pub total_size: u64,
    }

    #[derive(Deserialize)]
    pub struct RegisterResponse {
        pub status: String, // "registered" | "already_registered"
    }

    #[derive(Deserialize)]
    pub struct LookupEntry {
        pub content_hash: String,
        pub first_seen: String,
        pub attestations: Vec<LookupAttestation>,
    }

    #[derive(Deserialize)]
    pub struct LookupAttestation {
        pub verdict: String,
    }

    pub fn register(base_url: &str, payload: &RegisterPayload) -> Result<RegisterResponse, String> {
        let url = format!("{}/api/tbz-mirror/register", base_url.trim_end_matches('/'));
        let resp = ureq::post(&url)
            .timeout(std::time::Duration::from_secs(TIMEOUT_SECS))
            .send_json(serde_json::json!({
                "content_hash": payload.content_hash,
                "signing_key": payload.signing_key,
                "jis_id": payload.jis_id,
                "source_repo": payload.source_repo,
                "block_count": payload.block_count,
                "total_size": payload.total_size,
            }))
            .map_err(|e| e.to_string())?;

        resp.into_json::<RegisterResponse>().map_err(|e| e.to_string())
    }

    pub fn lookup(base_url: &str, hash: &str) -> Result<Option<LookupEntry>, String> {
        let url = format!(
            "{}/api/tbz-mirror/lookup/{}",
            base_url.trim_end_matches('/'),
            hash,
        );
        let resp = ureq::get(&url)
            .timeout(std::time::Duration::from_secs(TIMEOUT_SECS))
            .call();

        match resp {
            Ok(r) => {
                let entry = r.into_json::<LookupEntry>().map_err(|e| e.to_string())?;
                Ok(Some(entry))
            }
            Err(ureq::Error::Status(404, _)) => Ok(None),
            Err(e) => Err(e.to_string()),
        }
    }
}

/// Compute SHA-256 of a file on disk (streaming, 8 KB chunks).
fn hash_file(path: &Path) -> anyhow::Result<String> {
    let file = fs::File::open(path)?;
    let mut reader = BufReader::new(file);
    let mut hasher = Sha256::new();
    let mut buf = [0u8; 8192];
    loop {
        let n = reader.read(&mut buf)?;
        if n == 0 {
            break;
        }
        hasher.update(&buf[..n]);
    }
    Ok(format!("sha256:{:x}", hasher.finalize()))
}

#[derive(Parser)]
#[command(name = "tbz")]
#[command(about = "TBZ (TIBET-zip) — Block-level authenticated compression")]
#[command(version)]
struct Cli {
    #[command(subcommand)]
    command: Option<Commands>,

    /// Smart mode: pass a .tza file to verify+unpack, or a directory to pack
    #[arg(global = false)]
    path: Option<String>,

    /// Enable Transparency Mirror registration/lookups (opt-in)
    #[arg(long, global = true, default_value_t = false)]
    mirror: bool,

    /// Transparency Mirror base URL (also via TBZ_MIRROR_URL env)
    #[arg(long, global = true, env = "TBZ_MIRROR_URL",
          default_value = "https://brein.jaspervandemeent.nl")]
    mirror_url: String,
}

#[derive(Subcommand)]
enum Commands {
    /// Create a TBZ archive from a file or directory
    ///
    /// Default = v1 transparent archive. Pass --seal --to <pubkey-hex> to
    /// produce a v2 sealed envelope (AES-256-GCM, identity-bound).
    #[command(alias = "p")]
    Pack {
        /// Path to file or directory to archive
        path: String,
        /// Output file path
        #[arg(short, long, default_value = "output.tza")]
        output: String,
        /// JIS authorization level for all blocks (default: 0)
        #[arg(long, default_value = "0")]
        jis_level: u8,
        /// Seal the archive (= v2): wrap in an AES-256-GCM envelope.
        #[arg(long)]
        seal: bool,
        /// Receiver's Ed25519 public key (hex, 64 chars). Required with --seal.
        #[arg(long, value_name = "PUBKEY_HEX")]
        to: Option<String>,
        /// Sender's Ed25519 private key file (hex). Optional; ephemeral if absent.
        #[arg(long, value_name = "PRIVKEY_PATH")]
        from: Option<String>,
    },

    /// Extract a TBZ archive via the TIBET Airlock
    ///
    /// Auto-detects v1 vs v2 from magic bytes. For v2 sealed archives,
    /// pass --as <privkey-path> to decrypt as the named receiver.
    #[command(alias = "x")]
    Unpack {
        /// Path to the TBZ archive
        archive: String,
        /// Output directory
        #[arg(short, long, default_value = ".")]
        output: String,
        /// Receiver's Ed25519 private key file (hex). Required for v2 sealed archives.
        #[arg(long = "as", value_name = "PRIVKEY_PATH")]
        as_key: Option<String>,
    },

    /// Validate a TBZ archive without extracting
    #[command(alias = "v")]
    Verify {
        /// Path to the TBZ archive
        archive: String,
    },

    /// Show manifest and block information
    #[command(alias = "i")]
    Inspect {
        /// Path to the TBZ archive
        archive: String,
    },

    /// Generate .jis.json for the current repository
    Init {
        /// Platform (github, gitlab, etc.)
        #[arg(long, default_value = "github")]
        platform: String,
        /// Account name
        #[arg(long)]
        account: Option<String>,
        /// Repository name
        #[arg(long)]
        repo: Option<String>,
    },

    /// Generate an Ed25519 keypair for v2 sealed archives
    ///
    /// Writes <output>.priv (32-byte hex, mode 0600) and <output>.pub (32-byte hex).
    Keygen {
        /// Output basename — produces <output>.priv and <output>.pub
        #[arg(short, long, default_value = "tbz-key")]
        output: String,
    },
}

pub fn run() -> anyhow::Result<()> {
    let _ = tracing_subscriber::fmt::try_init();

    let cli = Cli::parse();

    // Resolve mirror URL once (None = disabled, opt-in only)
    let mirror_url: Option<&str> = if cli.mirror {
        Some(&cli.mirror_url)
    } else {
        None
    };

    // If a subcommand was given, use it directly
    if let Some(command) = cli.command {
        return match command {
            Commands::Pack { path, output, jis_level, seal, to, from } => {
                if seal {
                    let to_hex = to.ok_or_else(|| anyhow::anyhow!(
                        "--seal requires --to <pubkey-hex> (64 hex chars)"))?;
                    cmd_pack_sealed(&path, &output, jis_level, mirror_url, &to_hex, from.as_deref())
                } else {
                    cmd_pack(&path, &output, jis_level, mirror_url)
                }
            }
            Commands::Unpack { archive, output, as_key } => {
                cmd_unpack_dispatch(&archive, &output, as_key.as_deref())
            }
            Commands::Verify { archive } => cmd_verify(&archive, mirror_url),
            Commands::Inspect { archive } => cmd_inspect(&archive),
            Commands::Init { platform, account, repo } => cmd_init(&platform, account, repo),
            Commands::Keygen { output } => cmd_keygen(&output),
        };
    }

    // Smart auto-detection: tbz <path>
    //
    // v1.0.2: magic-bytes-FIRST. We read the first 4 bytes and check
    // for the TBZ magic (0x54 0x42 0x5A 0x01 / TBZ\x01) BEFORE looking
    // at the file extension. This prevents accidental double-wrap when
    // a sealed envelope was renamed for human navigation
    // (e.g. `vergadering-dinsdag.pdf`) — `tbz <file>` will now correctly
    // route to unpack instead of re-packing the sealed bundle inside a
    // new TBZ container.
    //
    // Bug reported by Jasper in cross-host vloedtest 12 mei 2026:
    //   tbz superbelangrijk-doc-LEES-DIT-EERST.pdf
    //   → Auto-detected: file → pack to ...tza    (= WRONG: re-wrapping a TBZ)
    if let Some(path) = cli.path {
        let p = Path::new(&path);

        // Magic-bytes precheck (= content is truth, name is hint)
        let is_tbz_by_magic = if p.is_file() {
            match std::fs::File::open(p) {
                Ok(mut f) => {
                    use std::io::Read;
                    let mut buf = [0u8; 4];
                    matches!(f.read(&mut buf), Ok(n) if n == 4)
                        && buf == [0x54, 0x42, 0x5A, 0x01]
                }
                Err(_) => false,
            }
        } else {
            false
        };

        if is_tbz_by_magic {
            // Sealed envelope identified by magic bytes — route to unpack
            // regardless of extension. Plus warn the operator if the
            // filename doesn't carry the typical .tza/.tbz suffix, so
            // they know we detected a rename-recovered bundle.
            let extension_matches =
                path.ends_with(".tza") || path.ends_with(".tbz");
            if !extension_matches {
                println!(
                    "✓ TBZ magic bytes detected — treating as sealed bundle"
                );
                println!(
                    "  (filename does not carry .tza/.tbz suffix; this may be"
                );
                println!(
                    "   an operator-renamed bundle. Content is truth, name is hint.)"
                );
            }
            println!("Auto-detected: TBZ envelope → unpack (with airlock verification)\n");
            let out_dir = p.file_stem()
                .map(|s| s.to_string_lossy().to_string())
                .unwrap_or_else(|| "tbz_out".to_string());
            cmd_unpack(&path, &out_dir)?;
            return Ok(());
        }

        if (path.ends_with(".tza") || path.ends_with(".tbz")) && p.is_file() {
            // Has TBZ-style extension but NO magic match. Could be a
            // truncated/corrupt bundle, or a non-TBZ file with a
            // misleading extension. Fail loudly.
            anyhow::bail!(
                "File '{}' has .tza/.tbz extension but does NOT carry the TBZ magic bytes. \n  Refusing to treat as a sealed archive. Use `tbz inspect {}` for details.",
                path, path
            );
        } else if p.is_dir() {
            // Directory → pack
            let dir_name = p.file_name()
                .map(|s| s.to_string_lossy().to_string())
                .unwrap_or_else(|| "output".to_string());
            let output = format!("{}.tza", dir_name);
            println!("Auto-detected: directory → pack to {}\n", output);
            cmd_pack(&path, &output, 0, mirror_url)?;
            return Ok(());
        } else if p.is_file() {
            // Single file → pack
            let file_name = p.file_stem()
                .map(|s| s.to_string_lossy().to_string())
                .unwrap_or_else(|| "output".to_string());
            let output = format!("{}.tza", file_name);
            println!("Auto-detected: file → pack to {}\n", output);
            cmd_pack(&path, &output, 0, mirror_url)?;
            return Ok(());
        } else {
            anyhow::bail!("Path not found: {}", path);
        }
    }

    // No subcommand and no path — show help
    Cli::parse_from(["tbz", "--help"]);
    Ok(())
}

/// Pack files into a TBZ archive
fn cmd_pack(path: &str, output: &str, default_jis_level: u8, mirror_url: Option<&str>) -> anyhow::Result<()> {
    let source = Path::new(path);
    if !source.exists() {
        anyhow::bail!("Source path does not exist: {}", path);
    }

    // Collect files to pack
    let files = collect_files(source)?;
    println!("TBZ pack: {} file(s) from {}", files.len(), path);

    // Check for .jis.json
    let jis_manifest = tbz_jis::JisManifest::load(Path::new(".")).ok();
    if let Some(ref jis) = jis_manifest {
        println!("  .jis.json found: {}", jis.repo_identifier());
    }

    // Generate signing keypair for this archive
    let (signing_key, verifying_key) = signature::generate_keypair();

    // Build manifest
    let mut manifest = Manifest::new();
    for (i, (file_path, data)) in files.iter().enumerate() {
        let jis_level = jis_manifest
            .as_ref()
            .map(|j| j.jis_level_for_path(file_path))
            .unwrap_or(default_jis_level);

        manifest.add_block(BlockEntry {
            index: (i + 1) as u32,
            block_type: "data".to_string(),
            compressed_size: 0, // filled after compression
            uncompressed_size: data.len() as u64,
            jis_level,
            description: file_path.clone(),
            path: Some(file_path.clone()),
        });
    }

    // Embed verifying key in manifest
    manifest.set_signing_key(&verifying_key);

    // Write TBZ archive
    let out_file = fs::File::create(output)?;
    let mut writer = TbzWriter::new(BufWriter::new(out_file), signing_key);

    // Block 0: manifest
    writer.write_manifest(&manifest)?;
    println!("  [0] manifest ({} block entries)", manifest.blocks.len());

    // Block 1..N: data
    for (file_path, data) in &files {
        let jis_level = jis_manifest
            .as_ref()
            .map(|j| j.jis_level_for_path(file_path))
            .unwrap_or(default_jis_level);

        let envelope = TibetEnvelope::new(
            signature::sha256_hash(data),
            "data",
            mime_for_path(file_path),
            "tbz-cli",
            &format!("Pack file: {}", file_path),
            vec!["block:0".to_string()],
        );

        let envelope = if let Some(ref jis) = jis_manifest {
            envelope.with_source_repo(&jis.repo_identifier())
        } else {
            envelope
        };

        writer.write_data_block(data, jis_level, &envelope)?;
        println!(
            "  [{}] {} ({} bytes, JIS level {})",
            writer.block_count() - 1,
            file_path,
            data.len(),
            jis_level,
        );
    }

    let total_blocks = writer.block_count();
    writer.finish();

    // Show public key (for verification)
    let vk_hex = hex_encode(&verifying_key.to_bytes());

    println!("\nArchive written: {}", output);
    println!("  Blocks: {}", total_blocks);
    println!("  Signing key (Ed25519 public): {}", vk_hex);
    println!("  Format: TBZ v{}", tbz_core::VERSION);

    // --- Transparency Mirror registration (best-effort) ---
    if let Some(url) = mirror_url {
        let archive_hash = hash_file(Path::new(output))?;
        println!("\n  Mirror: registering {} ...", archive_hash);

        let jis_id = jis_manifest.as_ref().map(|_| {
            format!("jis:ed25519:{}", &vk_hex[..16])
        });
        let source_repo = jis_manifest.as_ref().map(|j| j.repo_identifier());

        let payload = mirror_client::RegisterPayload {
            content_hash: archive_hash,
            signing_key: vk_hex.clone(),
            jis_id,
            source_repo,
            block_count: total_blocks as u32,
            total_size: fs::metadata(output).map(|m| m.len()).unwrap_or(0),
        };

        match mirror_client::register(url, &payload) {
            Ok(resp) => println!("  Mirror: {} ({})", resp.status, url),
            Err(e) => println!("  Mirror: WARNING — {}", e),
        }
    }

    Ok(())
}

/// Inspect a TBZ archive
fn cmd_inspect(archive: &str) -> anyhow::Result<()> {
    // Format detection: route to TIBET-ZIP handler if Desktop format
    match detect_format(archive)? {
        ArchiveFormat::TibetZip => return tibet_zip::inspect(archive),
        ArchiveFormat::Unknown => anyhow::bail!("Not a TBZ archive: unrecognized format"),
        ArchiveFormat::TbzBlock => {} // continue with block format below
    }

    let file = fs::File::open(archive)?;
    let mut reader = TbzReader::new(std::io::BufReader::new(file));

    println!("TBZ inspect: {}\n", archive);
    println!("  Magic: 0x54425A (TBZ)");
    println!("  Format: v{}\n", tbz_core::VERSION);

    let mut block_idx = 0;
    while let Some(block) = reader.read_block()? {
        let type_str = match block.header.block_type {
            BlockType::Manifest => "MANIFEST",
            BlockType::Data => "DATA",
            BlockType::Nested => "NESTED",
        };

        println!("  Block {} [{}]", block.header.block_index, type_str);
        println!("    JIS level:         {}", block.header.jis_level);
        println!("    Compressed:        {} bytes", block.header.compressed_size);
        println!("    Uncompressed:      {} bytes", block.header.uncompressed_size);
        println!("    TIBET ERIN hash:   {}", block.envelope.erin.content_hash);
        println!("    TIBET ERACHTER:    {}", block.envelope.erachter);

        if let Some(ref repo) = block.envelope.eromheen.source_repo {
            println!("    Source repo:       {}", repo);
        }

        // For manifest block, show the parsed manifest
        if block.header.block_type == BlockType::Manifest {
            if let Ok(decompressed) = block.decompress() {
                if let Ok(manifest) = serde_json::from_slice::<Manifest>(&decompressed) {
                    println!("    --- Manifest ---");
                    println!("    Total blocks:      {}", manifest.block_count);
                    println!("    Total uncompressed: {} bytes", manifest.total_uncompressed_size);
                    println!("    Max JIS level:     {}", manifest.max_jis_level());
                    for entry in &manifest.blocks {
                        println!(
                            "      [{:>3}] {}{} bytes, JIS {}",
                            entry.index,
                            entry.path.as_deref().unwrap_or(&entry.description),
                            entry.uncompressed_size,
                            entry.jis_level,
                        );
                    }
                }
            }
        }

        // Signature present?
        let sig_nonzero = block.signature.iter().any(|&b| b != 0);
        println!("    Signature:         {}", if sig_nonzero { "Ed25519 (present)" } else { "none" });
        println!();

        block_idx += 1;
    }

    println!("  Total: {} blocks", block_idx);
    Ok(())
}

/// Unpack a TBZ archive through the Airlock
///
/// AIRLOCK GATE: Runs full verification BEFORE extraction.
/// Corrupt or tampered archives are BLOCKED.
fn cmd_unpack(archive: &str, output_dir: &str) -> anyhow::Result<()> {
    // Format detection: route to TIBET-ZIP handler if Desktop format
    match detect_format(archive)? {
        ArchiveFormat::TibetZip => return tibet_zip::unpack(archive, output_dir),
        ArchiveFormat::Unknown => anyhow::bail!("Not a TBZ archive: unrecognized format"),
        ArchiveFormat::TbzBlock => {} // continue with block format below
    }

    // =========================================================================
    // AIRLOCK GATE — Verify BEFORE extraction. No exceptions.
    // =========================================================================
    println!("TBZ unpack: {}{}\n", archive, output_dir);
    println!("  Airlock pre-check: verifying archive integrity...\n");

    {
        let vfile = fs::File::open(archive)?;
        let mut vreader = TbzReader::new(std::io::BufReader::new(vfile));
        let mut errors = 0u32;
        let mut block_count = 0u32;
        let mut verifying_key: Option<tbz_core::VerifyingKey> = None;

        while let Some(block) = vreader.read_block()? {
            if let Err(_) = block.validate() {
                errors += 1;
                block_count += 1;
                continue;
            }

            if block.header.block_type == BlockType::Manifest {
                if let Ok(decompressed) = block.decompress() {
                    if let Ok(manifest) = serde_json::from_slice::<Manifest>(&decompressed) {
                        verifying_key = manifest.get_verifying_key();
                    }
                }
            }

            // Verify signature
            if let Some(ref vk) = verifying_key {
                if block.verify_signature(vk).is_err() {
                    errors += 1;
                }
            }

            // Verify content hash
            match block.decompress() {
                Ok(decompressed) => {
                    let actual_hash = signature::sha256_hash(&decompressed);
                    if actual_hash != block.envelope.erin.content_hash {
                        errors += 1;
                    }
                }
                Err(_) => { errors += 1; }
            }

            block_count += 1;
        }

        if errors > 0 {
            anyhow::bail!(
                "AIRLOCK BREACH BLOCKED — archive corrupt: {} ({} block errors in {} blocks). \
                 Use `tbz verify` to inspect, or fix the archive.",
                archive, errors, block_count
            );
        }

        println!("  Airlock pre-check: {} blocks verified ✓\n", block_count);
    }

    // =========================================================================
    // Extraction — only reached if all blocks verified
    // =========================================================================
    let file = fs::File::open(archive)?;
    let mut reader = TbzReader::new(std::io::BufReader::new(file));

    // Create Airlock
    let mut airlock = tbz_airlock::Airlock::new(256 * 1024 * 1024, 30);
    println!("  Airlock mode: {:?}\n", airlock.mode());

    fs::create_dir_all(output_dir)?;

    let mut block_idx = 0;
    let mut manifest: Option<Manifest> = None;

    while let Some(block) = reader.read_block()? {
        match block.header.block_type {
            BlockType::Manifest => {
                let decompressed = block.decompress()?;
                manifest = Some(serde_json::from_slice(&decompressed)
                    .map_err(|e| anyhow::anyhow!("Invalid manifest: {}", e))?);
                println!("  [0] Manifest parsed ({} entries)", manifest.as_ref().unwrap().blocks.len());
            }
            BlockType::Data => {
                // Decompress into Airlock
                let decompressed = block.decompress()?;
                airlock.allocate(decompressed.len() as u64)?;
                airlock.receive(&decompressed)?;

                // Determine output path from manifest
                let file_path = manifest
                    .as_ref()
                    .and_then(|m| {
                        m.blocks.iter()
                            .find(|e| e.index == block.header.block_index)
                            .and_then(|e| e.path.clone())
                    })
                    .unwrap_or_else(|| format!("block_{}", block.header.block_index));

                // Write from Airlock to filesystem
                let out_path = Path::new(output_dir).join(&file_path);
                if let Some(parent) = out_path.parent() {
                    fs::create_dir_all(parent)?;
                }

                let data = airlock.release(); // returns data + wipes buffer
                fs::write(&out_path, &data)?;

                println!(
                    "  [{}] {} ({} bytes) ✓",
                    block.header.block_index,
                    file_path,
                    data.len(),
                );
            }
            BlockType::Nested => {
                println!("  [{}] Nested TBZ (not yet supported)", block.header.block_index);
            }
        }
        block_idx += 1;
    }

    println!("\n  Extracted {} blocks via Airlock", block_idx);
    println!("  Airlock buffer: wiped (0x00)");
    Ok(())
}

/// Verify a TBZ archive without extracting
fn cmd_verify(archive: &str, mirror_url: Option<&str>) -> anyhow::Result<()> {
    // Format detection: route to TIBET-ZIP handler if Desktop format
    match detect_format(archive)? {
        ArchiveFormat::TibetZip => return tibet_zip::verify(archive),
        ArchiveFormat::Unknown => anyhow::bail!("Not a TBZ archive: unrecognized format"),
        ArchiveFormat::TbzBlock => {} // continue with block format below
    }

    let file = fs::File::open(archive)?;
    let mut reader = TbzReader::new(std::io::BufReader::new(file));

    println!("TBZ verify: {}\n", archive);

    let mut errors = 0;
    let mut block_idx = 0;
    let mut verifying_key: Option<tbz_core::VerifyingKey> = None;

    while let Some(block) = reader.read_block()? {
        // Validate header
        if let Err(e) = block.validate() {
            println!("  [{}] FAIL header: {}", block.header.block_index, e);
            errors += 1;
            block_idx += 1;
            continue;
        }

        // Extract verifying key from manifest (block 0)
        if block.header.block_type == BlockType::Manifest {
            if let Ok(decompressed) = block.decompress() {
                if let Ok(manifest) = serde_json::from_slice::<Manifest>(&decompressed) {
                    verifying_key = manifest.get_verifying_key();
                    if let Some(ref vk) = verifying_key {
                        let vk_hex = hex_encode(&vk.to_bytes());
                        println!("  Signing key: Ed25519 {}", &vk_hex[..16]);
                        println!();
                    } else {
                        println!("  WARNING: No signing key in manifest — signature checks skipped\n");
                    }
                }
            }
        }

        // 1. Verify Ed25519 signature (cryptographic proof of block integrity)
        let sig_ok = if let Some(ref vk) = verifying_key {
            match block.verify_signature(vk) {
                Ok(()) => true,
                Err(e) => {
                    println!("  [{}] FAIL signature: {}", block.header.block_index, e);
                    errors += 1;
                    false
                }
            }
        } else {
            true // no key available, skip
        };

        // 2. Verify content hash matches TIBET ERIN
        match block.decompress() {
            Ok(decompressed) => {
                let actual_hash = signature::sha256_hash(&decompressed);
                if actual_hash == block.envelope.erin.content_hash {
                    let sig_status = if verifying_key.is_some() && sig_ok {
                        "hash + signature"
                    } else if verifying_key.is_some() {
                        "hash only (sig FAILED)"
                    } else {
                        "hash only (no key)"
                    };
                    println!("  [{}] OK — {} verified", block.header.block_index, sig_status);
                } else {
                    println!(
                        "  [{}] FAIL — hash mismatch\n    expected: {}\n    actual:   {}",
                        block.header.block_index,
                        block.envelope.erin.content_hash,
                        actual_hash,
                    );
                    errors += 1;
                }
            }
            Err(e) => {
                println!("  [{}] FAIL — decompress error: {}", block.header.block_index, e);
                errors += 1;
            }
        }

        block_idx += 1;
    }

    println!();
    if errors == 0 {
        if verifying_key.is_some() {
            println!("  Result: ALL {} BLOCKS VERIFIED (hash + Ed25519) ✓", block_idx);
        } else {
            println!("  Result: ALL {} BLOCKS VERIFIED (hash only, no signing key) ✓", block_idx);
        }
    } else {
        println!("  Result: {} ERRORS in {} blocks ✗", errors, block_idx);
    }

    // --- Transparency Mirror lookup (best-effort) ---
    if let Some(url) = mirror_url {
        let archive_hash = hash_file(Path::new(archive))?;
        match mirror_client::lookup(url, &archive_hash) {
            Ok(Some(entry)) => {
                let verdicts: Vec<&str> = entry.attestations.iter()
                    .map(|a| a.verdict.as_str())
                    .collect();
                println!("\n  Mirror: KNOWN");
                println!("    Hash:         {}", entry.content_hash);
                println!("    First seen:   {}", entry.first_seen);
                println!(
                    "    Attestations: {} ({})",
                    entry.attestations.len(),
                    if verdicts.is_empty() { "none".to_string() } else { verdicts.join(", ") },
                );
            }
            Ok(None) => {
                println!("\n  Mirror: UNKNOWN — not registered in Transparency Mirror");
            }
            Err(e) => {
                println!("\n  Mirror: WARNING — {}", e);
            }
        }
    }

    Ok(())
}

/// Generate .jis.json and Ed25519 keypair for current repo
fn cmd_init(platform: &str, account: Option<String>, repo: Option<String>) -> anyhow::Result<()> {
    let account = account.unwrap_or_else(|| "<your-account>".to_string());
    let repo = repo.unwrap_or_else(|| {
        std::env::current_dir()
            .ok()
            .and_then(|p| p.file_name().map(|n| n.to_string_lossy().to_string()))
            .unwrap_or_else(|| "<repo>".to_string())
    });

    // Check if .tbz/ already exists
    let tbz_dir = Path::new(".tbz");
    let key_path = tbz_dir.join("signing.key");
    let pub_path = tbz_dir.join("signing.pub");

    let (signing_key, verifying_key) = if key_path.exists() {
        // Load existing keypair
        let sk_hex = fs::read_to_string(&key_path)?;
        let sk_bytes: Vec<u8> = (0..sk_hex.trim().len())
            .step_by(2)
            .filter_map(|i| u8::from_str_radix(&sk_hex.trim()[i..i + 2], 16).ok())
            .collect();
        if sk_bytes.len() != 32 {
            anyhow::bail!("Invalid signing key in .tbz/signing.key");
        }
        let mut key_array = [0u8; 32];
        key_array.copy_from_slice(&sk_bytes);
        let sk = tbz_core::SigningKey::from_bytes(&key_array);
        let vk = sk.verifying_key();
        println!("Using existing keypair from .tbz/");
        (sk, vk)
    } else {
        // Generate new keypair
        let (sk, vk) = signature::generate_keypair();

        fs::create_dir_all(tbz_dir)?;
        fs::write(&key_path, hex_encode(&sk.to_bytes()))?;
        fs::write(&pub_path, hex_encode(&vk.to_bytes()))?;

        println!("Generated Ed25519 keypair:");
        println!("  Private: .tbz/signing.key (KEEP SECRET — add to .gitignore!)");
        println!("  Public:  .tbz/signing.pub");
        (sk, vk)
    };

    let vk_hex = hex_encode(&verifying_key.to_bytes());
    let jis_id = format!("jis:ed25519:{}", &vk_hex[..16]);

    // Sign the JIS identity claim
    let claim_data = format!("{}:{}:{}:{}", platform, account, repo, vk_hex);
    let claim_sig = signature::sign(claim_data.as_bytes(), &signing_key);

    let jis_json = serde_json::json!({
        "tbz": "1.0",
        "jis_id": jis_id,
        "signing_key": vk_hex,
        "claim": {
            "platform": platform,
            "account": account,
            "repo": repo,
            "intent": "official_releases",
            "sectors": {
                "src/*": { "jis_level": 0, "description": "Public source code" },
                "keys/*": { "jis_level": 2, "description": "Signing keys" }
            }
        },
        "tibet": {
            "erin": "Repository identity binding",
            "eraan": [&jis_id],
            "erachter": format!("Provenance root for TBZ packages from {}/{}", account, repo)
        },
        "signature": hex_encode(&claim_sig),
        "timestamp": chrono_now()
    });

    let output = serde_json::to_string_pretty(&jis_json)?;
    fs::write(".jis.json", &output)?;

    // Ensure .tbz/signing.key is in .gitignore
    let gitignore = Path::new(".gitignore");
    if gitignore.exists() {
        let content = fs::read_to_string(gitignore)?;
        if !content.contains(".tbz/signing.key") {
            fs::write(gitignore, format!("{}\n# TBZ signing key (NEVER commit!)\n.tbz/signing.key\n", content.trim_end()))?;
            println!("\n  Added .tbz/signing.key to .gitignore");
        }
    }

    println!("\nGenerated .jis.json:");
    println!("  JIS ID: {}", jis_id);
    println!("  Claim: {}/{}/{}", platform, account, repo);
    println!("  Signature: Ed25519 (signed)");

    Ok(())
}

/// Collect files from a path (file or directory, recursive)
fn collect_files(path: &Path) -> anyhow::Result<Vec<(String, Vec<u8>)>> {
    let mut files = Vec::new();

    if path.is_file() {
        let data = fs::read(path)?;
        let name = path.file_name()
            .map(|n| n.to_string_lossy().to_string())
            .unwrap_or_else(|| "file".to_string());
        files.push((name, data));
    } else if path.is_dir() {
        collect_dir_recursive(path, path, &mut files)?;
    }

    Ok(files)
}

fn collect_dir_recursive(
    base: &Path,
    current: &Path,
    files: &mut Vec<(String, Vec<u8>)>,
) -> anyhow::Result<()> {
    let mut entries: Vec<_> = fs::read_dir(current)?.collect::<Result<_, _>>()?;
    entries.sort_by_key(|e| e.file_name());

    for entry in entries {
        let path = entry.path();
        // Skip hidden files and common non-essential dirs
        let name = entry.file_name().to_string_lossy().to_string();
        if name.starts_with('.') || name == "target" || name == "node_modules" {
            continue;
        }

        if path.is_file() {
            let rel = path.strip_prefix(base)
                .map(|p| p.to_string_lossy().to_string())
                .unwrap_or_else(|_| name);
            let data = fs::read(&path)?;
            files.push((rel, data));
        } else if path.is_dir() {
            collect_dir_recursive(base, &path, files)?;
        }
    }
    Ok(())
}

/// Simple MIME type detection
fn mime_for_path(path: &str) -> &str {
    match path.rsplit('.').next() {
        Some("rs") => "text/x-rust",
        Some("toml") => "application/toml",
        Some("json") => "application/json",
        Some("md") => "text/markdown",
        Some("txt") => "text/plain",
        Some("py") => "text/x-python",
        Some("js") => "text/javascript",
        Some("html") => "text/html",
        Some("css") => "text/css",
        Some("png") => "image/png",
        Some("jpg") | Some("jpeg") => "image/jpeg",
        Some("bin") => "application/octet-stream",
        _ => "application/octet-stream",
    }
}

fn hex_encode(bytes: &[u8]) -> String {
    bytes.iter().map(|b| format!("{:02x}", b)).collect()
}

fn hex_decode_32(s: &str) -> anyhow::Result<[u8; 32]> {
    let s = s.trim();
    if s.len() != 64 {
        anyhow::bail!("expected 64 hex characters, got {}", s.len());
    }
    let mut out = [0u8; 32];
    for (i, chunk) in s.as_bytes().chunks(2).enumerate() {
        let pair = std::str::from_utf8(chunk).map_err(|_| anyhow::anyhow!("invalid utf8"))?;
        out[i] = u8::from_str_radix(pair, 16)
            .map_err(|e| anyhow::anyhow!("invalid hex pair '{}': {}", pair, e))?;
    }
    Ok(out)
}

fn read_signing_key_from_file(path: &str) -> anyhow::Result<ed25519_dalek::SigningKey> {
    let raw = fs::read_to_string(path)
        .map_err(|e| anyhow::anyhow!("cannot read key file {}: {}", path, e))?;
    let bytes = hex_decode_32(&raw)?;
    Ok(ed25519_dalek::SigningKey::from_bytes(&bytes))
}

fn chrono_now() -> String {
    use std::time::SystemTime;
    let duration = SystemTime::now()
        .duration_since(SystemTime::UNIX_EPOCH)
        .unwrap_or_default();
    format!("{}Z", duration.as_secs())
}

// ---------------------------------------------------------------------------
// v2.1.0 NEW SUBCOMMANDS — Keygen, Pack --seal, Unpack --as
// ---------------------------------------------------------------------------

fn cmd_keygen(output: &str) -> anyhow::Result<()> {
    use ed25519_dalek::SigningKey;
    use rand::rngs::OsRng;

    let signing_key = SigningKey::generate(&mut OsRng);
    let verifying_key = signing_key.verifying_key();

    let priv_path = format!("{}.priv", output);
    let pub_path = format!("{}.pub", output);

    // Write private key (hex) with 0600 permissions
    let priv_hex = hex_encode(&signing_key.to_bytes());
    fs::write(&priv_path, &priv_hex)?;
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        let perms = fs::Permissions::from_mode(0o600);
        fs::set_permissions(&priv_path, perms)?;
    }

    let pub_hex = hex_encode(&verifying_key.to_bytes());
    fs::write(&pub_path, &pub_hex)?;

    println!("TBZ keygen: Ed25519 keypair generated\n");
    println!("  Private: {} (mode 0600)", priv_path);
    println!("  Public:  {}", pub_path);
    println!("\n  Pubkey (share this): {}", pub_hex);
    println!("\n  Use with:");
    println!("    tibet-zip pack <dir> -o sealed.tza --seal --to {} --from {}", pub_hex, priv_path);
    println!("    tibet-zip unpack sealed.tza -o out/ --as {}", priv_path);
    Ok(())
}

fn cmd_pack_sealed(
    path: &str,
    output: &str,
    default_jis_level: u8,
    mirror_url: Option<&str>,
    receiver_hex: &str,
    sender_priv_path: Option<&str>,
) -> anyhow::Result<()> {
    let source = Path::new(path);
    if !source.exists() {
        anyhow::bail!("Source path does not exist: {}", path);
    }
    let receiver_pubkey = hex_decode_32(receiver_hex)
        .map_err(|e| anyhow::anyhow!("--to pubkey: {}", e))?;

    // Sender key: from --from or ephemeral
    let sender_key = match sender_priv_path {
        Some(p) => read_signing_key_from_file(p)?,
        None => {
            use rand::rngs::OsRng;
            ed25519_dalek::SigningKey::generate(&mut OsRng)
        }
    };

    println!("TBZ pack (sealed v2): {}{}", path, output);
    println!("  Sender pubkey:   {}", hex_encode(&sender_key.verifying_key().to_bytes()));
    println!("  Receiver pubkey: {}", receiver_hex);

    // Build the v1 archive in memory (Vec<u8> buffer)
    let v1_bytes = build_v1_archive_bytes(source, default_jis_level, mirror_url)?;
    println!("\n  Inner v1 archive: {} bytes", v1_bytes.len());

    // Wrap in v2 sealed container
    let container = v2::write_sealed_container(&sender_key, &receiver_pubkey, &v1_bytes)
        .map_err(|e| anyhow::anyhow!("v2 seal failed: {}", e))?;

    fs::write(output, &container)?;
    println!("  Outer v2 envelope: {} bytes (overhead: {} bytes)", container.len(), container.len() - v1_bytes.len());
    println!("\n  Sealed: {}", output);
    println!("  Format: TBZ v2 (AES-256-GCM, Ed25519-signed)");
    Ok(())
}

/// Build a v1 archive in memory as Vec<u8>. Refactored from cmd_pack so we
/// can wrap the bytes in a v2 sealed envelope.
fn build_v1_archive_bytes(
    source: &Path,
    default_jis_level: u8,
    _mirror_url: Option<&str>,
) -> anyhow::Result<Vec<u8>> {
    let files = collect_files(source)?;
    let jis_manifest = tbz_jis::JisManifest::load(Path::new(".")).ok();

    let (signing_key, verifying_key) = signature::generate_keypair();

    let mut manifest = Manifest::new();
    for (i, (file_path, data)) in files.iter().enumerate() {
        let jis_level = jis_manifest
            .as_ref()
            .map(|j| j.jis_level_for_path(file_path))
            .unwrap_or(default_jis_level);

        manifest.add_block(BlockEntry {
            index: (i + 1) as u32,
            block_type: "data".to_string(),
            compressed_size: 0,
            uncompressed_size: data.len() as u64,
            jis_level,
            description: file_path.clone(),
            path: Some(file_path.clone()),
        });
    }
    manifest.set_signing_key(&verifying_key);

    let mut buf: Vec<u8> = Vec::new();
    {
        let mut writer = TbzWriter::new(&mut buf, signing_key);
        writer.write_manifest(&manifest)?;
        for (file_path, data) in &files {
            let jis_level = jis_manifest
                .as_ref()
                .map(|j| j.jis_level_for_path(file_path))
                .unwrap_or(default_jis_level);
            let envelope = TibetEnvelope::new(
                signature::sha256_hash(data),
                "data",
                mime_for_path(file_path),
                "tbz-cli",
                &format!("Pack file: {}", file_path),
                vec!["block:0".to_string()],
            );
            let envelope = if let Some(ref jis) = jis_manifest {
                envelope.with_source_repo(&jis.repo_identifier())
            } else {
                envelope
            };
            writer.write_data_block(data, jis_level, &envelope)?;
        }
        writer.finish();
    }
    Ok(buf)
}

/// Dispatch unpack: detect v1 vs v2 and route accordingly.
fn cmd_unpack_dispatch(archive: &str, output_dir: &str, as_key: Option<&str>) -> anyhow::Result<()> {
    // Peek first 32 bytes to detect version
    let mut head = [0u8; 32];
    let n = {
        let mut f = fs::File::open(archive)?;
        std::io::Read::read(&mut f, &mut head)?
    };
    let version = if n >= 7 { v2::detect_version(&head[..n]) } else { 0 };

    if version == 2 {
        let priv_path = as_key.ok_or_else(|| anyhow::anyhow!(
            "{} is a v2 sealed archive — pass --as <privkey-path> to decrypt", archive))?;
        let receiver_key = read_signing_key_from_file(priv_path)?;
        println!("TBZ unpack (v2 sealed): {}{}", archive, output_dir);
        let container = fs::read(archive)?;
        let (env, plain) = v2::read_sealed_container(&container, &receiver_key)
            .map_err(|e| anyhow::anyhow!("v2 unseal failed: {}", e))?;
        println!("  Sender:   {}", hex_encode(&env.sender_pubkey));
        println!("  Receiver: {}", hex_encode(&env.receiver_pubkey));
        println!("  Inner v1 archive: {} bytes\n", plain.len());

        // Write to temp file, then call cmd_unpack with that file
        let tmp = std::env::temp_dir().join(format!("tbz-v2-inner-{}.tza", std::process::id()));
        fs::write(&tmp, &plain)?;
        let result = cmd_unpack(tmp.to_str().unwrap(), output_dir);
        let _ = fs::remove_file(&tmp);
        result
    } else {
        cmd_unpack(archive, output_dir)
    }
}