auths-core 0.1.3

Core cryptography and keychain integration for Auths
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
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
//! HTTP witness server using Axum.
//!
//! This module implements a lightweight HTTP server that acts as a KERI witness,
//! receiving events, enforcing first-seen-always-seen, and issuing receipts.
//!
//! # Endpoints
//!
//! - `POST /witness/:prefix/event` - Submit an event for witnessing
//! - `GET /witness/:prefix/head` - Get the latest observed sequence
//! - `GET /witness/:prefix/receipt/:said` - Retrieve an issued receipt
//! - `GET /health` - Health check
//!
//! # Feature Gate
//!
//! This module requires the `witness-server` feature.

use std::net::SocketAddr;
use std::path::PathBuf;
use std::sync::Arc;

use auths_crypto::{CurveType, SecureSeed, TypedSignerKey};
use auths_keri::{Prefix, Said};
use auths_verifier::types::CanonicalDid;
use axum::{
    Json, Router,
    extract::{Path as AxumPath, State},
    http::StatusCode,
    routing::{get, post},
};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::sync::Mutex;

use auths_keri::{KeriSequence, VersionString};

use super::error::{DuplicityEvidence, WitnessError};
use super::receipt::{Receipt, ReceiptTag, SignedReceipt};
use super::storage::WitnessStorage;

/// Shared server state.
#[derive(Clone)]
pub struct WitnessServerState {
    inner: Arc<WitnessServerInner>,
}

#[allow(dead_code)]
struct WitnessServerInner {
    /// Witness identifier (DID)
    witness_did: CanonicalDid,
    /// Curve-tagged signing key (fn-116.1/B1a). Carries curve so sign/DID
    /// paths dispatch correctly; replaces the historical
    /// `{seed: SecureSeed, public_key: [u8; 32]}` pair that was Ed25519-locked.
    signer: TypedSignerKey,
    /// SQLite storage (Mutex for thread safety since Connection is !Sync)
    storage: Mutex<WitnessStorage>,
    /// Clock function for getting current time
    clock: Box<dyn Fn() -> DateTime<Utc> + Send + Sync>,
}

/// Configuration for the witness server.
pub struct WitnessServerConfig {
    /// Witness identifier (DID)
    pub witness_did: CanonicalDid,
    /// Curve-tagged signing key (fn-116.1/B1a).
    pub signer: TypedSignerKey,
    /// Path to SQLite database
    pub db_path: std::path::PathBuf,
    /// Path to TLS certificate (PEM format). Used by `run_server_tls()` when the `tls` feature is enabled.
    pub tls_cert_path: Option<PathBuf>,
    /// Path to TLS private key (PEM format). Used by `run_server_tls()` when the `tls` feature is enabled.
    pub tls_key_path: Option<PathBuf>,
}

impl WitnessServerConfig {
    /// Create a new config with a generated keypair for the given curve.
    ///
    /// accepts `curve: CurveType` (default `CurveType::P256` at
    /// the CLI layer). DID derivation dispatches on curve via `DecodedDidKey`.
    pub fn with_generated_keypair(
        db_path: std::path::PathBuf,
        curve: CurveType,
    ) -> Result<Self, WitnessError> {
        let (seed_bytes, pubkey_bytes) = generate_keypair_for_curve(curve)?;
        let typed_seed = match curve {
            CurveType::Ed25519 => auths_crypto::TypedSeed::Ed25519(seed_bytes),
            CurveType::P256 => auths_crypto::TypedSeed::P256(seed_bytes),
        };
        let signer = TypedSignerKey::from_parts(typed_seed, pubkey_bytes)
            .map_err(|e| WitnessError::Network(format!("invalid witness signer: {e}")))?;
        Self::from_signer(db_path, signer)
    }

    /// Create a config from an already-constructed signer (e.g. a persisted
    /// witness identity loaded from a keystore).
    ///
    /// The advertised `/health` AID derives from the signer's curve-tagged public
    /// key, so a loaded key and its published identity cannot diverge.
    ///
    /// Args:
    /// * `db_path`: Path to the SQLite database for witness storage.
    /// * `signer`: The witness's persisted signing key.
    ///
    /// Usage:
    /// ```ignore
    /// let cfg = WitnessServerConfig::from_signer(db_path, signer)?;
    /// ```
    pub fn from_signer(
        db_path: std::path::PathBuf,
        signer: TypedSignerKey,
    ) -> Result<Self, WitnessError> {
        let witness_did = derive_witness_did(signer.curve(), signer.public_key())?;
        Ok(Self {
            witness_did,
            signer,
            db_path,
            tls_cert_path: None,
            tls_key_path: None,
        })
    }
}

/// Generate a keypair for the given curve; returns (seed_bytes, pubkey_bytes).
///
/// Ed25519: 32-byte seed + 32-byte pubkey. P-256: 32-byte scalar + 33-byte
/// compressed SEC1 pubkey.
pub(crate) fn generate_keypair_for_curve(
    curve: CurveType,
) -> Result<([u8; 32], Vec<u8>), WitnessError> {
    match curve {
        CurveType::Ed25519 => {
            use crate::crypto::provider_bridge;
            let (seed, pubkey) = provider_bridge::generate_ed25519_keypair_sync()
                .map_err(|e| WitnessError::Network(format!("Ed25519 keygen: {e}")))?;
            Ok((*seed.as_bytes(), pubkey.to_vec()))
        }
        CurveType::P256 => {
            use p256::ecdsa::{SigningKey, VerifyingKey};
            use p256::elliptic_curve::rand_core::OsRng;
            let sk = SigningKey::random(&mut OsRng);
            let mut scalar = [0u8; 32];
            scalar.copy_from_slice(&sk.to_bytes());
            let vk = VerifyingKey::from(&sk);
            let pubkey = vk.to_encoded_point(true).as_bytes().to_vec();
            Ok((scalar, pubkey))
        }
    }
}

/// Derive a `did:key:` for the witness from a curve-tagged public key.
fn derive_witness_did(curve: CurveType, pubkey_bytes: &[u8]) -> Result<CanonicalDid, WitnessError> {
    Ok(CanonicalDid::from_public_key_did_key(pubkey_bytes, curve))
}

/// Event submission request.
#[derive(Debug, Deserialize)]
pub struct SubmitEventRequest {
    /// Event SAID
    pub d: String,
    /// Event sequence number
    pub s: u64,
    /// Event type
    pub t: String,
    /// Full event JSON (for signature verification)
    #[serde(flatten)]
    pub extra: serde_json::Value,
}

/// Health check response.
#[derive(Debug, Serialize)]
pub struct HealthResponse {
    /// Witness server status string.
    pub status: String,
    /// DID of this witness.
    pub witness_did: CanonicalDid,
    /// Number of first-seen events recorded.
    pub first_seen_count: usize,
    /// Total receipts issued.
    pub receipt_count: usize,
}

/// Head response.
#[derive(Debug, Serialize)]
pub struct HeadResponse {
    /// KERI prefix of the identity.
    pub prefix: Prefix,
    /// Latest observed sequence number.
    pub latest_seq: Option<u64>,
}

/// First-seen SAID at a chosen sequence number.
///
/// Lets a monitor compare witnesses by CONTENT (the SAID each first saw at a
/// sequence), not just by HEAD number — so a same-seq/different-SAID fork is
/// visible where two `/head` calls would return the identical `latest_seq`.
#[derive(Debug, Serialize)]
pub struct SaidAtSeqResponse {
    /// KERI prefix of the identity.
    pub prefix: Prefix,
    /// The queried sequence number.
    pub seq: u64,
    /// SAID of the event this witness first saw at `seq`.
    pub said: Said,
}

/// Error response.
#[derive(Debug, Serialize)]
pub struct ErrorResponse {
    /// Error message.
    pub error: String,
    /// Duplicity evidence, if detected.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub duplicity: Option<DuplicityEvidence>,
}

#[allow(dead_code)]
impl WitnessServerState {
    /// Create a new server state.
    #[allow(clippy::disallowed_methods)] // Server constructor is a clock boundary
    pub fn new(config: WitnessServerConfig) -> Result<Self, WitnessError> {
        let storage = WitnessStorage::open(&config.db_path)?;

        Ok(Self {
            inner: Arc::new(WitnessServerInner {
                witness_did: config.witness_did,
                signer: config.signer,
                storage: Mutex::new(storage),
                clock: Box::new(Utc::now),
            }),
        })
    }

    /// Create a new server state with in-memory storage (for testing).
    #[allow(clippy::disallowed_methods)] // Server constructor is a clock boundary
    pub fn in_memory(
        witness_did: CanonicalDid,
        signer: TypedSignerKey,
    ) -> Result<Self, WitnessError> {
        let storage = WitnessStorage::in_memory()?;

        Ok(Self {
            inner: Arc::new(WitnessServerInner {
                witness_did,
                signer,
                storage: Mutex::new(storage),
                clock: Box::new(Utc::now),
            }),
        })
    }

    /// Legacy helper for tests that have an Ed25519 seed + pubkey.
    #[allow(clippy::disallowed_methods)]
    pub fn in_memory_ed25519(
        witness_did: CanonicalDid,
        seed: SecureSeed,
        public_key: [u8; 32],
    ) -> Result<Self, WitnessError> {
        let typed_seed = auths_crypto::TypedSeed::Ed25519(*seed.as_bytes());
        let signer = TypedSignerKey::from_parts(typed_seed, public_key.to_vec())
            .map_err(|e| WitnessError::Network(format!("invalid witness signer: {e}")))?;
        Self::in_memory(witness_did, signer)
    }

    /// Create a new server state with generated keypair (for testing).
    #[allow(clippy::disallowed_methods)] // Server constructor is a clock boundary
    pub fn in_memory_generated() -> Result<Self, WitnessError> {
        Self::in_memory_generated_for_curve(CurveType::Ed25519)
    }

    /// Create a new server state with a generated keypair of the specified curve.
    #[allow(clippy::disallowed_methods)]
    pub fn in_memory_generated_for_curve(curve: CurveType) -> Result<Self, WitnessError> {
        let (seed_bytes, pubkey_bytes) = generate_keypair_for_curve(curve)?;
        let typed_seed = match curve {
            CurveType::Ed25519 => auths_crypto::TypedSeed::Ed25519(seed_bytes),
            CurveType::P256 => auths_crypto::TypedSeed::P256(seed_bytes),
        };
        let signer = TypedSignerKey::from_parts(typed_seed, pubkey_bytes.clone())
            .map_err(|e| WitnessError::Network(format!("invalid witness signer: {e}")))?;
        let witness_did = derive_witness_did(curve, &pubkey_bytes)?;

        let storage = WitnessStorage::in_memory()?;
        Ok(Self {
            inner: Arc::new(WitnessServerInner {
                witness_did,
                signer,
                storage: Mutex::new(storage),
                clock: Box::new(Utc::now),
            }),
        })
    }

    /// Get the witness DID.
    pub fn witness_did(&self) -> &str {
        &self.inner.witness_did
    }

    /// Create a receipt for an event.
    fn create_receipt(
        &self,
        prefix: &Prefix,
        seq: u128,
        event_said: &Said,
    ) -> Result<Receipt, WitnessError> {
        let receipt = Receipt {
            v: VersionString::placeholder(),
            t: ReceiptTag,
            d: event_said.clone(),
            i: prefix.clone(),
            s: KeriSequence::new(seq),
        };

        Ok(receipt)
    }

    /// Create a signed receipt for an event.
    fn create_signed_receipt(
        &self,
        prefix: &Prefix,
        seq: u128,
        event_said: &Said,
    ) -> Result<SignedReceipt, WitnessError> {
        let receipt = self.create_receipt(prefix, seq, event_said)?;

        let signing_payload =
            serde_json::to_vec(&receipt).map_err(|e| WitnessError::Serialization(e.to_string()))?;
        let signature = self.sign_payload(&signing_payload)?;

        Ok(SignedReceipt { receipt, signature })
    }

    /// Sign a payload with the witness keypair (curve-dispatched).
    ///
    /// routes through `TypedSignerKey::sign` which dispatches
    /// on the seed's curve. No more hardcoded Ed25519.
    fn sign_payload(&self, payload: &[u8]) -> Result<Vec<u8>, WitnessError> {
        self.inner
            .signer
            .sign(payload)
            .map_err(|e| WitnessError::Serialization(format!("signing failed: {e}")))
    }

    /// Get the witness public key bytes.
    pub fn public_key(&self) -> Vec<u8> {
        self.inner.signer.public_key().to_vec()
    }

    /// Returns the curve of the witness signing key.
    pub fn curve(&self) -> CurveType {
        self.inner.signer.curve()
    }
}

/// Build the Axum router for the witness server.
pub fn router(state: WitnessServerState) -> Router {
    Router::new()
        .route("/witness/{prefix}/event", post(submit_event))
        .route("/witness/{prefix}/head", get(get_head))
        .route("/witness/{prefix}/said/{seq}", get(get_said_at_seq))
        .route("/witness/{prefix}/receipt/{said}", get(get_receipt))
        .route("/health", get(health))
        .with_state(state)
}

/// Run the witness server.
pub async fn run_server(state: WitnessServerState, addr: SocketAddr) -> Result<(), WitnessError> {
    let app = router(state);

    let listener = tokio::net::TcpListener::bind(addr)
        .await
        .map_err(|e| WitnessError::Network(format!("failed to bind: {}", e)))?;

    axum::serve(listener, app)
        .await
        .map_err(|e| WitnessError::Network(format!("server error: {}", e)))?;

    Ok(())
}

/// Run the witness server with TLS (rustls).
///
/// Requires the `tls` feature flag. The certificate and key must be PEM-encoded files.
/// For production deployments behind a reverse proxy, prefer [`run_server`] and terminate
/// TLS at the proxy layer instead.
#[cfg(feature = "tls")]
#[allow(dead_code)] // feature-gated public API — available when tls feature is enabled
pub async fn run_server_tls(
    state: WitnessServerState,
    addr: SocketAddr,
    cert_path: &std::path::Path,
    key_path: &std::path::Path,
) -> Result<(), WitnessError> {
    let app = router(state);

    let tls_config = axum_server::tls_rustls::RustlsConfig::from_pem_file(cert_path, key_path)
        .await
        .map_err(|e| WitnessError::Network(format!("failed to load TLS config: {}", e)))?;

    axum_server::bind_rustls(addr, tls_config)
        .serve(app.into_make_service())
        .await
        .map_err(|e| WitnessError::Network(format!("TLS server error: {}", e)))?;

    Ok(())
}

/// Verify the SAID of a KERI event.
///
/// Zeros the `d` field, serializes to canonical JSON, computes Blake3 SAID,
/// and compares with the claimed `d` value.
fn verify_event_said(event: &serde_json::Value) -> Result<(), String> {
    let claimed_d = event
        .get("d")
        .and_then(|v| v.as_str())
        .ok_or("missing 'd' (SAID) field")?;

    let computed = crate::crypto::said::compute_said(event)
        .map_err(|e| format!("failed to compute SAID: {}", e))?;

    if computed.as_str() != claimed_d {
        return Err(format!(
            "SAID mismatch: claimed {} but computed {}",
            claimed_d, computed
        ));
    }
    Ok(())
}

/// Validate the structural requirements of a KERI event.
fn validate_event_structure(event: &serde_json::Value) -> Result<(), String> {
    let obj = event.as_object().ok_or("event must be a JSON object")?;

    // Required fields for all event types
    for field in &["v", "t", "d", "i", "s"] {
        if !obj.contains_key(*field) {
            return Err(format!("missing required field '{}'", field));
        }
    }

    let event_type = obj
        .get("t")
        .and_then(|v| v.as_str())
        .ok_or("'t' must be a string")?;

    match event_type {
        "icp" => {
            // Inception events require keys
            for field in &["k", "x"] {
                if !obj.contains_key(*field) {
                    return Err(format!(
                        "inception event missing required field '{}'",
                        field
                    ));
                }
            }
        }
        "rot" | "ixn" => {
            // Rotation/interaction events require prior event reference
            if !obj.contains_key("p") {
                return Err(format!(
                    "{} event missing required field 'p' (prior event reference)",
                    event_type
                ));
            }
        }
        _ => {
            // Unknown event types are allowed but logged
        }
    }

    Ok(())
}

/// Typed failure from inbound event signature/key validation.
///
/// Separates **routing** faults — material we cannot even dispatch to a curve
/// (an untagged or unknown-curve `k[0]`) — from **cryptographic** faults — a
/// well-formed, curve-tagged signature that fails to verify. Collapsing the two
/// into one "invalid signature" hides which bug actually occurred and which side
/// (emitter format vs. key mismatch) must change.
#[derive(Debug, thiserror::Error)]
enum EventSignatureError {
    /// The `x` field is present but not a JSON string.
    #[error("'x' (signature) must be a string")]
    SignatureNotString,

    /// The `x` field is not valid hex.
    #[error("'x' (signature) is not valid hex: {0}")]
    SignatureNotHex(String),

    /// An inception event has no `x` signature.
    #[error("inception event missing 'x' (signature)")]
    SignatureMissing,

    /// `k` is absent or empty on an inception event.
    #[error("inception event 'k' must be a non-empty array")]
    MissingKeys,

    /// `k[0]` is present but not a JSON string.
    #[error("'k[0]' must be a string")]
    KeyNotString,

    /// Routing fault: `k[0]` is not a curve-tagged CESR verkey, so verification
    /// cannot dispatch to a curve. Byte-length guessing is deliberately refused.
    #[error(
        "'k[0]' is not a curve-tagged CESR verkey (expected `D…` Ed25519 or \
         `1AAI…`/`1AAJ…` P-256); untagged or unknown-curve keys are rejected: {0}"
    )]
    UnroutableKey(String),

    /// The event JSON is not an object (cannot build the signing payload).
    #[error("event is not a JSON object")]
    NotAnObject,

    /// The signing payload could not be serialized.
    #[error("failed to serialize signing payload: {0}")]
    PayloadSerialization(String),

    /// Cryptographic fault: a curve-tagged signature that does not verify against
    /// `k[0]` (wrong key, wrong curve, or malformed for that curve).
    #[error("inception self-signature did not verify against k[0]: {0}")]
    SelfSignatureInvalid(String),
}

/// Validate that the `x` signature field, if present, is well-formed hex.
///
/// Curve-specific length/format is **not** asserted here — the in-band curve tag
/// on `k[0]` is authoritative, and the per-curve verifier (in
/// [`verify_inception_self_signature`]) rejects a wrong-length signature. Guessing
/// the curve from `x`'s byte length is exactly the silent-correctness hazard the
/// wire-format tagging rule forbids.
fn validate_signature_format(event: &serde_json::Value) -> Result<(), EventSignatureError> {
    if let Some(sig_val) = event.get("x") {
        let sig_hex = sig_val
            .as_str()
            .ok_or(EventSignatureError::SignatureNotString)?;
        hex::decode(sig_hex).map_err(|e| EventSignatureError::SignatureNotHex(e.to_string()))?;
    }
    Ok(())
}

/// For inception events, verify the self-signature (signature over the event by `k[0]`).
///
/// Parses `k[0]` via `KeriPublicKey::parse` (CESR-aware — the workspace emits
/// curve-tagged pubkeys `D…` Ed25519 / `1AAI…`/`1AAJ…` P-256) and dispatches
/// signature verification on the parsed curve. The in-band tag is authoritative:
/// there is no byte-length fallback and no legacy raw-hex path — an untagged or
/// unknown-curve `k[0]` is a typed [`EventSignatureError::UnroutableKey`], never a
/// silently-misrouted crypto check.
fn verify_inception_self_signature(event: &serde_json::Value) -> Result<(), EventSignatureError> {
    let event_type = event.get("t").and_then(|v| v.as_str()).unwrap_or("");
    if event_type != "icp" {
        return Ok(());
    }

    let k = event
        .get("k")
        .and_then(|v| v.as_array())
        .filter(|a| !a.is_empty())
        .ok_or(EventSignatureError::MissingKeys)?;

    let k0_str = k[0].as_str().ok_or(EventSignatureError::KeyNotString)?;

    // In-band curve tag is authoritative — no byte-length dispatch, no hex fallback.
    let keri_key = auths_keri::KeriPublicKey::parse(k0_str)
        .map_err(|e| EventSignatureError::UnroutableKey(e.to_string()))?;

    let sig_hex = event
        .get("x")
        .and_then(|v| v.as_str())
        .ok_or(EventSignatureError::SignatureMissing)?;
    let sig_bytes =
        hex::decode(sig_hex).map_err(|e| EventSignatureError::SignatureNotHex(e.to_string()))?;

    // Build the signing payload: canonical JSON with empty 'd' and 'x' fields
    let mut payload_event = event.clone();
    let obj = payload_event
        .as_object_mut()
        .ok_or(EventSignatureError::NotAnObject)?;
    obj.insert("d".to_string(), serde_json::Value::String(String::new()));
    obj.insert("x".to_string(), serde_json::Value::String(String::new()));
    let payload = serde_json::to_vec(&payload_event)
        .map_err(|e| EventSignatureError::PayloadSerialization(e.to_string()))?;

    keri_key
        .verify_signature(&payload, &sig_bytes)
        .map_err(EventSignatureError::SelfSignatureInvalid)
}

/// POST /witness/:prefix/event - Submit an event for witnessing.
#[allow(clippy::too_many_lines)]
async fn submit_event(
    State(state): State<WitnessServerState>,
    AxumPath(prefix_str): AxumPath<String>,
    Json(event): Json<serde_json::Value>,
) -> Result<Json<SignedReceipt>, (StatusCode, Json<ErrorResponse>)> {
    let prefix = Prefix::new_unchecked(prefix_str);

    // Validate event structure
    if let Err(e) = validate_event_structure(&event) {
        return Err((
            StatusCode::BAD_REQUEST,
            Json(ErrorResponse {
                error: format!("invalid event structure: {}", e),
                duplicity: None,
            }),
        ));
    }

    // Verify SAID
    if let Err(e) = verify_event_said(&event) {
        return Err((
            StatusCode::BAD_REQUEST,
            Json(ErrorResponse {
                error: format!("SAID verification failed: {}", e),
                duplicity: None,
            }),
        ));
    }

    // Validate signature format
    if let Err(e) = validate_signature_format(&event) {
        return Err((
            StatusCode::BAD_REQUEST,
            Json(ErrorResponse {
                error: format!("invalid signature format: {}", e),
                duplicity: None,
            }),
        ));
    }

    // Verify inception self-signature
    if let Err(e) = verify_inception_self_signature(&event) {
        return Err((
            StatusCode::BAD_REQUEST,
            Json(ErrorResponse {
                error: format!("signature verification failed: {}", e),
                duplicity: None,
            }),
        ));
    }

    let event_d = Said::new_unchecked(
        event
            .get("d")
            .and_then(|v| v.as_str())
            .unwrap_or_default()
            .to_string(),
    );
    // KERI sequence numbers are hex strings in the wire format; parse as u128
    // to match `KeyState.sequence` / storage field width.
    let event_s: u128 = event
        .get("s")
        .and_then(|v| v.as_str())
        .and_then(|s| u128::from_str_radix(s, 16).ok())
        .unwrap_or(0);

    let now = (state.inner.clock)();
    let storage = state.inner.storage.lock().map_err(|_| {
        (
            StatusCode::INTERNAL_SERVER_ERROR,
            Json(ErrorResponse {
                error: "internal lock error".to_string(),
                duplicity: None,
            }),
        )
    })?;

    // Check for duplicity
    match storage.check_duplicity(now, &prefix, event_s, &event_d) {
        Ok(None) => {
            // No duplicity - sign and store the receipt. The wire `rct` body
            // stays spec-shaped; the witness signature travels detached in the
            // `SignedReceipt` so the collector can attribute and verify it.
            let signed = state
                .create_signed_receipt(&prefix, event_s, &event_d)
                .map_err(|e| {
                    (
                        StatusCode::INTERNAL_SERVER_ERROR,
                        Json(ErrorResponse {
                            error: format!("failed to create receipt: {e}"),
                            duplicity: None,
                        }),
                    )
                })?;

            if let Err(e) = storage.store_receipt(now, &prefix, &signed.receipt) {
                return Err((
                    StatusCode::INTERNAL_SERVER_ERROR,
                    Json(ErrorResponse {
                        error: format!("failed to store receipt: {}", e),
                        duplicity: None,
                    }),
                ));
            }

            Ok(Json(signed))
        }
        Ok(Some(existing_said)) => {
            // Duplicity detected!
            Err((
                StatusCode::CONFLICT,
                Json(ErrorResponse {
                    error: "duplicity detected".to_string(),
                    duplicity: Some(DuplicityEvidence {
                        prefix,
                        sequence: event_s,
                        event_a_said: existing_said,
                        event_b_said: event_d,
                        witness_reports: vec![],
                    }),
                }),
            ))
        }
        Err(e) => Err((
            StatusCode::INTERNAL_SERVER_ERROR,
            Json(ErrorResponse {
                error: format!("storage error: {}", e),
                duplicity: None,
            }),
        )),
    }
}

/// GET /witness/:prefix/head - Get the latest observed sequence.
async fn get_head(
    State(state): State<WitnessServerState>,
    AxumPath(prefix_str): AxumPath<String>,
) -> Result<Json<HeadResponse>, (StatusCode, Json<ErrorResponse>)> {
    let prefix = Prefix::new_unchecked(prefix_str);
    let storage = state.inner.storage.lock().map_err(|_| {
        (
            StatusCode::INTERNAL_SERVER_ERROR,
            Json(ErrorResponse {
                error: "internal lock error".to_string(),
                duplicity: None,
            }),
        )
    })?;

    match storage.get_latest_seq(&prefix) {
        Ok(latest_seq) => Ok(Json(HeadResponse { prefix, latest_seq })),
        Err(e) => Err((
            StatusCode::INTERNAL_SERVER_ERROR,
            Json(ErrorResponse {
                error: format!("storage error: {}", e),
                duplicity: None,
            }),
        )),
    }
}

/// `GET /witness/{prefix}/said/{seq}` — the SAID this witness first saw at `seq`.
///
/// Returns 404 (a gap, not divergence) when the sequence was never observed, so a
/// monitor can distinguish "not yet seen" from "seen a conflicting SAID".
async fn get_said_at_seq(
    State(state): State<WitnessServerState>,
    AxumPath((prefix_str, seq_str)): AxumPath<(String, String)>,
) -> Result<Json<SaidAtSeqResponse>, (StatusCode, Json<ErrorResponse>)> {
    let prefix = Prefix::new_unchecked(prefix_str);
    let seq: u64 = seq_str.parse().map_err(|_| {
        (
            StatusCode::BAD_REQUEST,
            Json(ErrorResponse {
                error: format!("seq must be a non-negative integer, got '{seq_str}'"),
                duplicity: None,
            }),
        )
    })?;

    let storage = state.inner.storage.lock().map_err(|_| {
        (
            StatusCode::INTERNAL_SERVER_ERROR,
            Json(ErrorResponse {
                error: "internal lock error".to_string(),
                duplicity: None,
            }),
        )
    })?;

    match storage.get_first_seen(&prefix, seq as u128) {
        Ok(Some(said)) => Ok(Json(SaidAtSeqResponse { prefix, seq, said })),
        Ok(None) => Err((
            StatusCode::NOT_FOUND,
            Json(ErrorResponse {
                error: format!(
                    "no event seen for prefix {} at seq {}",
                    prefix.as_str(),
                    seq
                ),
                duplicity: None,
            }),
        )),
        Err(e) => Err((
            StatusCode::INTERNAL_SERVER_ERROR,
            Json(ErrorResponse {
                error: format!("storage error: {}", e),
                duplicity: None,
            }),
        )),
    }
}

/// GET /witness/:prefix/receipt/:said - Retrieve an issued receipt.
async fn get_receipt(
    State(state): State<WitnessServerState>,
    AxumPath((prefix_str, said_str)): AxumPath<(String, String)>,
) -> Result<Json<Receipt>, StatusCode> {
    let prefix = Prefix::new_unchecked(prefix_str);
    let said = Said::new_unchecked(said_str);
    let storage = state
        .inner
        .storage
        .lock()
        .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;

    match storage.get_receipt(&prefix, &said) {
        Ok(Some(receipt)) => Ok(Json(receipt)),
        Ok(None) => Err(StatusCode::NOT_FOUND),
        Err(_) => Err(StatusCode::INTERNAL_SERVER_ERROR),
    }
}

/// GET /health - Health check.
async fn health(State(state): State<WitnessServerState>) -> Json<HealthResponse> {
    let storage = state
        .inner
        .storage
        .lock()
        .unwrap_or_else(|e| e.into_inner());

    let first_seen_count = storage.count_first_seen().unwrap_or(0);
    let receipt_count = storage.count_receipts().unwrap_or(0);

    Json(HealthResponse {
        status: "ok".to_string(),
        witness_did: state.inner.witness_did.clone(),
        first_seen_count,
        receipt_count,
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use axum::body::Body;
    use axum::http::{Request, StatusCode};
    use ring::signature::{Ed25519KeyPair, KeyPair};
    use tower::ServiceExt;

    fn test_state() -> WitnessServerState {
        WitnessServerState::in_memory_generated().unwrap()
    }

    /// Generate an Ed25519 keypair and return (pkcs8, public_key_hex).
    fn test_keypair() -> (Vec<u8>, String) {
        let rng = ring::rand::SystemRandom::new();
        let pkcs8 = Ed25519KeyPair::generate_pkcs8(&rng).unwrap();
        let kp = Ed25519KeyPair::from_pkcs8(pkcs8.as_ref()).unwrap();
        let pk_hex = hex::encode(kp.public_key().as_ref());
        (pkcs8.as_ref().to_vec(), pk_hex)
    }

    /// Build a valid KERI inception event with proper SAID and self-signature.
    fn make_valid_icp_event(prefix: &str, seq: u128) -> serde_json::Value {
        let (pkcs8, _pk_hex) = test_keypair();
        let kp = Ed25519KeyPair::from_pkcs8(&pkcs8).unwrap();
        // k[0] is the curve-tagged CESR verkey (`D…`), as production emitters use.
        let k0 = auths_keri::KeriPublicKey::ed25519(kp.public_key().as_ref())
            .unwrap()
            .to_qb64()
            .unwrap();

        // Build event with empty d and x for SAID computation
        let mut event = serde_json::json!({
            "v": "KERI10JSON000000_",
            "t": "icp",
            "d": "",
            "i": prefix,
            "s": seq,
            "k": [k0],
            "x": ""
        });

        // Sign the payload (with empty d and x)
        let payload = serde_json::to_vec(&event).unwrap();
        let sig = kp.sign(&payload);
        event["x"] = serde_json::Value::String(hex::encode(sig.as_ref()));

        // Compute SAID (x is already set; compute_said ignores x and injects d placeholder)
        let said = crate::crypto::said::compute_said(&event).unwrap();
        event["d"] = serde_json::Value::String(said.into_inner());

        event
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn health_endpoint() {
        let state = test_state();
        let app = router(state);

        let response = app
            .oneshot(
                Request::builder()
                    .uri("/health")
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();

        assert_eq!(response.status(), StatusCode::OK);
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn submit_valid_icp_event_success() {
        let state = test_state();
        let app = router(state);

        let event = make_valid_icp_event("EPrefix", 0);

        let response = app
            .oneshot(
                Request::builder()
                    .method("POST")
                    .uri("/witness/EPrefix/event")
                    .header("content-type", "application/json")
                    .body(Body::from(serde_json::to_string(&event).unwrap()))
                    .unwrap(),
            )
            .await
            .unwrap();

        assert_eq!(response.status(), StatusCode::OK);
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn server_signs_receipt() {
        let state = test_state();
        let app = router(state.clone());

        let event = make_valid_icp_event("EPrefix", 0);
        let response = app
            .oneshot(
                Request::builder()
                    .method("POST")
                    .uri("/witness/EPrefix/event")
                    .header("content-type", "application/json")
                    .body(Body::from(serde_json::to_string(&event).unwrap()))
                    .unwrap(),
            )
            .await
            .unwrap();

        assert_eq!(response.status(), StatusCode::OK);

        let body = axum::body::to_bytes(response.into_body(), usize::MAX)
            .await
            .unwrap();
        let signed: SignedReceipt =
            serde_json::from_slice(&body).expect("witness response must be a SignedReceipt");

        assert!(
            !signed.signature.is_empty(),
            "witness must attach a signature"
        );

        // The signature must verify against the witness's own key.
        let key = auths_keri::KeriPublicKey::from_verkey_bytes(&state.public_key(), state.curve())
            .unwrap();
        let payload = serde_json::to_vec(&signed.receipt).unwrap();
        assert!(
            key.verify_signature(&payload, &signed.signature).is_ok(),
            "witness signature must verify against its advertised key"
        );
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn submit_event_with_mismatched_said_rejected() {
        let state = test_state();
        let app = router(state);

        let mut event = make_valid_icp_event("EPrefix", 0);
        // Tamper with the SAID
        event["d"] = serde_json::Value::String(
            "EFakeSAID_AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA".to_string(),
        );

        let response = app
            .oneshot(
                Request::builder()
                    .method("POST")
                    .uri("/witness/EPrefix/event")
                    .header("content-type", "application/json")
                    .body(Body::from(serde_json::to_string(&event).unwrap()))
                    .unwrap(),
            )
            .await
            .unwrap();

        assert_eq!(response.status(), StatusCode::BAD_REQUEST);
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn submit_event_missing_required_fields_rejected() {
        let state = test_state();
        let app = router(state);

        // Missing 't', 'd', 'i', 's'
        let body = serde_json::json!({"v": "KERI10JSON000000_"});

        let response = app
            .oneshot(
                Request::builder()
                    .method("POST")
                    .uri("/witness/EPrefix/event")
                    .header("content-type", "application/json")
                    .body(Body::from(serde_json::to_string(&body).unwrap()))
                    .unwrap(),
            )
            .await
            .unwrap();

        assert_eq!(response.status(), StatusCode::BAD_REQUEST);
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn submit_event_invalid_signature_hex_rejected() {
        let state = test_state();
        let app = router(state);

        // Build event with invalid hex in x field
        let mut event = serde_json::json!({
            "v": "KERI10JSON000000_",
            "t": "icp",
            "d": "",
            "i": "EPrefix",
            "s": 0,
            "k": ["0000000000000000000000000000000000000000000000000000000000000000"],
            "x": "not_valid_hex!!!"
        });
        // Set proper SAID for the event as-is
        let said = crate::crypto::said::compute_said(&event).unwrap();
        event["d"] = serde_json::Value::String(said.into_inner());

        let response = app
            .oneshot(
                Request::builder()
                    .method("POST")
                    .uri("/witness/EPrefix/event")
                    .header("content-type", "application/json")
                    .body(Body::from(serde_json::to_string(&event).unwrap()))
                    .unwrap(),
            )
            .await
            .unwrap();

        assert_eq!(response.status(), StatusCode::BAD_REQUEST);
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn submit_icp_event_wrong_self_signature_rejected() {
        let state = test_state();
        let app = router(state);

        let (pkcs8, _) = test_keypair();
        let kp = Ed25519KeyPair::from_pkcs8(&pkcs8).unwrap();
        // Correct, curve-tagged k[0] — so verification reaches the signature check.
        let k0 = auths_keri::KeriPublicKey::ed25519(kp.public_key().as_ref())
            .unwrap()
            .to_qb64()
            .unwrap();
        // Use a different keypair to produce a wrong signature.
        let (wrong_pkcs8, _) = test_keypair();
        let wrong_kp = Ed25519KeyPair::from_pkcs8(&wrong_pkcs8).unwrap();

        let mut event = serde_json::json!({
            "v": "KERI10JSON000000_",
            "t": "icp",
            "d": "",
            "i": "EPrefix",
            "s": 0,
            "k": [k0],
            "x": ""
        });

        // Sign with wrong key
        let payload = serde_json::to_vec(&event).unwrap();
        let sig = wrong_kp.sign(&payload);
        event["x"] = serde_json::Value::String(hex::encode(sig.as_ref()));

        // Compute SAID (x is already set; compute_said ignores x and injects d placeholder)
        let said = crate::crypto::said::compute_said(&event).unwrap();
        event["d"] = serde_json::Value::String(said.into_inner());

        let response = app
            .oneshot(
                Request::builder()
                    .method("POST")
                    .uri("/witness/EPrefix/event")
                    .header("content-type", "application/json")
                    .body(Body::from(serde_json::to_string(&event).unwrap()))
                    .unwrap(),
            )
            .await
            .unwrap();

        assert_eq!(response.status(), StatusCode::BAD_REQUEST);
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn submit_event_duplicity() {
        let state = test_state();

        let event_a = make_valid_icp_event("EPrefix", 0);
        let event_a_said = event_a["d"].as_str().unwrap().to_string();

        // First submission
        {
            let app = router(state.clone());
            let response = app
                .oneshot(
                    Request::builder()
                        .method("POST")
                        .uri("/witness/EPrefix/event")
                        .header("content-type", "application/json")
                        .body(Body::from(serde_json::to_string(&event_a).unwrap()))
                        .unwrap(),
                )
                .await
                .unwrap();

            assert_eq!(response.status(), StatusCode::OK);
        }

        // Second submission with different event at same seq
        {
            let app = router(state.clone());
            let event_b = make_valid_icp_event("EPrefix", 0);
            // event_b will have a different SAID (different key)
            assert_ne!(event_a_said, event_b["d"].as_str().unwrap());

            let response = app
                .oneshot(
                    Request::builder()
                        .method("POST")
                        .uri("/witness/EPrefix/event")
                        .header("content-type", "application/json")
                        .body(Body::from(serde_json::to_string(&event_b).unwrap()))
                        .unwrap(),
                )
                .await
                .unwrap();

            assert_eq!(response.status(), StatusCode::CONFLICT);
        }
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn get_head_empty() {
        let state = test_state();
        let app = router(state);

        let response = app
            .oneshot(
                Request::builder()
                    .uri("/witness/EPrefix/head")
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();

        assert_eq!(response.status(), StatusCode::OK);
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn get_receipt_not_found() {
        let state = test_state();
        let app = router(state);

        let response = app
            .oneshot(
                Request::builder()
                    .uri("/witness/EPrefix/receipt/NONEXISTENT")
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();

        assert_eq!(response.status(), StatusCode::NOT_FOUND);
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn said_at_seq_returns_first_seen() {
        let state = test_state();
        let app = router(state);
        let event = make_valid_icp_event("EPrefix", 0);

        let submit = app
            .clone()
            .oneshot(
                Request::builder()
                    .method("POST")
                    .uri("/witness/EPrefix/event")
                    .header("content-type", "application/json")
                    .body(Body::from(serde_json::to_string(&event).unwrap()))
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(submit.status(), StatusCode::OK);

        let response = app
            .oneshot(
                Request::builder()
                    .uri("/witness/EPrefix/said/0")
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(response.status(), StatusCode::OK);
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn said_at_seq_unseen_is_404() {
        let app = router(test_state());
        let response = app
            .oneshot(
                Request::builder()
                    .uri("/witness/EUnseen/said/0")
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(response.status(), StatusCode::NOT_FOUND);
    }

    #[test]
    fn receipt_d_matches_event_said() {
        let state = test_state();
        let prefix = Prefix::new_unchecked("EPrefix".into());
        let event_said = Said::new_unchecked("ESAID123".into());
        let receipt = state.create_receipt(&prefix, 0, &event_said).unwrap();
        assert_eq!(receipt.d, event_said);
    }

    #[test]
    fn receipt_d_changes_with_event_said() {
        let state = test_state();
        let prefix = Prefix::new_unchecked("EPrefix".into());
        let receipt_a = state
            .create_receipt(&prefix, 0, &Said::new_unchecked("ESAID_A".into()))
            .unwrap();
        let receipt_b = state
            .create_receipt(&prefix, 0, &Said::new_unchecked("ESAID_B".into()))
            .unwrap();
        assert_ne!(receipt_a.d, receipt_b.d);
    }

    #[test]
    fn signed_receipt_signature_verifies() {
        let state = test_state();
        let prefix = Prefix::new_unchecked("EPrefix".into());
        let signed = state
            .create_signed_receipt(&prefix, 0, &Said::new_unchecked("ESAID123".into()))
            .unwrap();
        let public_key = state.public_key();
        let payload = serde_json::to_vec(&signed.receipt).unwrap();

        let pk = ring::signature::UnparsedPublicKey::new(&ring::signature::ED25519, &public_key);
        pk.verify(&payload, &signed.signature)
            .expect("signed receipt signature should verify against serialized receipt");
    }

    #[test]
    fn ed25519_inception_self_signature_accepted() {
        // make_valid_icp_event now emits a curve-tagged `D…` k[0].
        let event = make_valid_icp_event("EPrefix", 0);
        assert!(verify_inception_self_signature(&event).is_ok());
    }

    #[test]
    fn p256_inception_self_signature_accepted() {
        use p256::ecdsa::{Signature, SigningKey, signature::Signer};

        let sk = SigningKey::from_slice(&[7u8; 32]).unwrap();
        let compressed = sk
            .verifying_key()
            .to_encoded_point(true)
            .as_bytes()
            .to_vec();
        let k0 = auths_keri::KeriPublicKey::from_verkey_bytes(&compressed, CurveType::P256)
            .unwrap()
            .to_qb64()
            .unwrap();

        let mut event = serde_json::json!({
            "v": "KERI10JSON000000_",
            "t": "icp",
            "d": "",
            "i": "EPrefix",
            "s": 0,
            "k": [k0],
            "x": ""
        });
        // Sign the canonical payload (d and x already empty), matching the verifier.
        let payload = serde_json::to_vec(&event).unwrap();
        let sig: Signature = sk.sign(&payload);
        event["x"] = serde_json::Value::String(hex::encode(sig.to_bytes()));

        assert!(
            verify_inception_self_signature(&event).is_ok(),
            "P-256 inception with a valid self-signature must be accepted"
        );
    }

    #[test]
    fn untagged_hex_key_rejected_as_unroutable() {
        // A raw 32-byte hex k[0] (no curve tag) must be a routing error, NOT a
        // crypto failure — the legacy length-dispatched fallback is gone.
        let (_, pk_hex) = test_keypair();
        let event = serde_json::json!({
            "v": "KERI10JSON000000_",
            "t": "icp",
            "d": "",
            "i": "EPrefix",
            "s": 0,
            "k": [pk_hex],
            "x": hex::encode([0u8; 64])
        });

        let err = verify_inception_self_signature(&event).unwrap_err();
        assert!(
            matches!(err, EventSignatureError::UnroutableKey(_)),
            "untagged key must surface as a typed routing error, got: {err:?}"
        );
    }

    #[test]
    fn verify_event_said_valid() {
        let event = make_valid_icp_event("EPrefix", 0);
        assert!(verify_event_said(&event).is_ok());
    }

    #[test]
    fn verify_event_said_tampered() {
        let mut event = make_valid_icp_event("EPrefix", 0);
        event["d"] = serde_json::Value::String(
            "EFakeSAID_AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA".to_string(),
        );
        assert!(verify_event_said(&event).is_err());
    }

    #[test]
    fn validate_structure_missing_fields() {
        let event = serde_json::json!({"v": "KERI10JSON000000_"});
        assert!(validate_event_structure(&event).is_err());
    }

    #[test]
    fn validate_structure_icp_missing_k() {
        let event = serde_json::json!({
            "v": "KERI10JSON000000_",
            "t": "icp",
            "d": "E123",
            "i": "EPrefix",
            "s": 0
        });
        assert!(validate_event_structure(&event).is_err());
    }

    #[test]
    fn validate_structure_rot_missing_p() {
        let event = serde_json::json!({
            "v": "KERI10JSON000000_",
            "t": "rot",
            "d": "E123",
            "i": "EPrefix",
            "s": 1
        });
        assert!(validate_event_structure(&event).is_err());
    }
}