1pub mod did;
39pub mod did_web;
40pub mod key_management;
41pub mod kms;
42pub mod proof;
43pub mod rdf_integration;
44pub mod revocation;
45#[cfg(feature = "bbs-plus")]
46pub mod signatures;
47pub mod signed_graph;
48pub mod url;
49pub mod vc;
50#[cfg(feature = "zkp")]
51pub mod zkp;
52
53pub mod document_versioning;
55
56pub mod credential_exchange;
58
59pub mod key_agreement;
61
62pub mod presentation_builder;
64
65pub mod vc_verifier;
67
68pub mod vc_presenter;
70
71pub mod trust_chain;
73
74pub mod authentication;
76
77pub mod presentation_request;
79
80pub mod did_resolver;
82
83pub mod identity_registry;
85
86pub mod credential_schema;
88
89pub mod key_manager;
91
92pub mod proof_purpose;
94pub(crate) mod proof_purpose_registry;
95#[cfg(test)]
96mod proof_purpose_tests;
97pub mod proof_purpose_types;
98pub(crate) mod proof_purpose_verifier;
99
100pub mod access_control;
102
103pub mod credential_store;
105
106pub mod key_derivation;
108
109use chrono::{DateTime, Utc};
110use serde::{Deserialize, Serialize};
111use thiserror::Error;
112
113#[cfg(feature = "did-ethr")]
115pub use did::methods::{DidEthr, DidEthrMethod, EthNetwork};
116#[cfg(feature = "did-ion")]
117pub use did::methods::{
118 DidIon, DidIonMethod, IonCreateOperation, IonDocument, IonKeyDescriptor, IonKeyPurpose,
119 IonOperationType, IonService,
120};
121pub use did::{ChainNamespace, Did, DidDocument, DidPkh, DidPkhMethod, DidResolver};
122pub use key_management::{
123 generate_rotation_key, KeyExpiry, KeyRotation, KeyRotationManager, KeyRotationReason,
124 KeyRotationRecord, KeyRotationRegistry, Keystore, LifecycleKeyRotationRecord,
125 VerificationKey as ManagedVerificationKey,
126};
127pub use kms::{
128 audit::{AuditEvent, AuditEventKind, AuditLog},
129 pkcs11::{KeyHandle, Pkcs11Mechanism, Pkcs11Slot},
130 KeyUsage, KmsAlgorithm, KmsBackend, KmsDidSigner, KmsKeyMetadata,
131};
132pub use proof::{
137 jws::{
138 attach_jws_proof, extract_jws_proof, sign_document, verify_document, CompactJws,
139 JsonWebSignature2020, JwsAlgorithm, JwsHeader, JwsSigner, JwsVerifier,
140 },
141 Proof, ProofPurpose, ProofType,
142};
143pub use revocation::{
144 BloomFilter, CredentialStatus, RevocationEntry, RevocationList2020, RevocationRegistry,
145 RevocationRegistry2020, RevocationStatus, StatusList2021, StatusList2021Inner,
146 StatusListCredential, StatusPurpose, MIN_LIST_SIZE,
147};
148#[cfg(feature = "bbs-plus")]
149pub use signatures::{
150 BbsKeyPair, BbsPlusSignature, BbsProof, BbsProofRequest, EcdsaJwsSigner, EcdsaJwsVerifier,
151 Ed25519JwsSigner, Ed25519JwsVerifier, Es256Signer, Es256Verifier,
152 JwsAlgorithm as SignaturesJwsAlgorithm, JwsHeader as SignaturesJwsHeader, JwsPayload,
153 JwsSignature, JwsSignatureHeader, JwsSigner as SignaturesJwsSigner, JwsSignerTrait,
154 JwsVerifier as SignaturesJwsVerifier, JwsVerifierTrait, MockJwsSigner, MockJwsVerifier,
155 P256KeyPair, Rs256Signer, Rs256Verifier, RsaKeyPair,
156};
157pub use signed_graph::SignedGraph;
158pub use url::{DereferencedResource, DidDereferencer, DidUrl};
159pub use vc::{
160 decode_jwt_vc, encode_vc_as_jwt, CredentialIssuer, CredentialSubject, CredentialVerifier,
161 JwtVc, JwtVcHeader, JwtVcPayload, VerifiableCredential, VerifiablePresentation,
162};
163#[cfg(feature = "zkp")]
164pub use zkp::{
165 prove_selective, verify_selective, AttributeCommitment, CredentialAttribute,
166 DisclosurePresentation, PedersenParams, PedersenSelectiveDisclosureProof, SchnorrProof,
167 SelectiveDisclosureCredential, SelectiveDisclosureProof, SelectiveDisclosureRequest,
168 ZkpProofRequest,
169};
170
171#[derive(Error, Debug)]
173pub enum DidError {
174 #[error("Invalid DID format: {0}")]
175 InvalidFormat(String),
176
177 #[error("Unsupported DID method: {0}")]
178 UnsupportedMethod(String),
179
180 #[error("Resolution failed: {0}")]
181 ResolutionFailed(String),
182
183 #[error("Verification failed: {0}")]
184 VerificationFailed(String),
185
186 #[error("Signing failed: {0}")]
187 SigningFailed(String),
188
189 #[error("Key not found: {0}")]
190 KeyNotFound(String),
191
192 #[error("Invalid key: {0}")]
193 InvalidKey(String),
194
195 #[error("Credential expired")]
196 CredentialExpired,
197
198 #[error("Invalid proof: {0}")]
199 InvalidProof(String),
200
201 #[error("Canonicalization failed: {0}")]
202 CanonicalizationFailed(String),
203
204 #[error("Serialization error: {0}")]
205 SerializationError(String),
206
207 #[error("Network error: {0}")]
208 NetworkError(String),
209
210 #[error("Internal error: {0}")]
211 InternalError(String),
212
213 #[error("Invalid credential: {0}")]
214 InvalidCredential(String),
215}
216
217pub type DidResult<T> = Result<T, DidError>;
218
219#[derive(Debug, Clone, Serialize, Deserialize)]
221#[serde(rename_all = "camelCase")]
222pub struct VerificationMethod {
223 pub id: String,
225 #[serde(rename = "type")]
227 pub method_type: String,
228 pub controller: String,
230 #[serde(skip_serializing_if = "Option::is_none")]
232 pub public_key_multibase: Option<String>,
233 #[serde(skip_serializing_if = "Option::is_none")]
235 pub public_key_jwk: Option<serde_json::Value>,
236 #[serde(skip_serializing_if = "Option::is_none")]
238 pub blockchain_account_id: Option<String>,
239}
240
241impl VerificationMethod {
242 pub fn ed25519(id: &str, controller: &str, public_key: &[u8]) -> Self {
244 let multibase = format!("z{}", bs58::encode(public_key).into_string());
246
247 Self {
248 id: id.to_string(),
249 method_type: "Ed25519VerificationKey2020".to_string(),
250 controller: controller.to_string(),
251 public_key_multibase: Some(multibase),
252 public_key_jwk: None,
253 blockchain_account_id: None,
254 }
255 }
256
257 pub fn blockchain(
261 id: &str,
262 controller: &str,
263 method_type: &str,
264 blockchain_account_id: &str,
265 ) -> Self {
266 Self {
267 id: id.to_string(),
268 method_type: method_type.to_string(),
269 controller: controller.to_string(),
270 public_key_multibase: None,
271 public_key_jwk: None,
272 blockchain_account_id: Some(blockchain_account_id.to_string()),
273 }
274 }
275
276 pub fn jwk(id: &str, controller: &str, method_type: &str, jwk: serde_json::Value) -> Self {
278 Self {
279 id: id.to_string(),
280 method_type: method_type.to_string(),
281 controller: controller.to_string(),
282 public_key_multibase: None,
283 public_key_jwk: Some(jwk),
284 blockchain_account_id: None,
285 }
286 }
287
288 pub fn get_public_key_bytes(&self) -> DidResult<Vec<u8>> {
290 if let Some(ref multibase) = self.public_key_multibase {
291 if let Some(stripped) = multibase.strip_prefix('z') {
293 bs58::decode(stripped)
294 .into_vec()
295 .map_err(|e| DidError::InvalidKey(e.to_string()))
296 } else {
297 Err(DidError::InvalidKey("Unknown multibase prefix".to_string()))
298 }
299 } else if self.blockchain_account_id.is_some() {
300 Err(DidError::InvalidKey(
301 "Blockchain account verification methods do not expose raw public keys".to_string(),
302 ))
303 } else {
304 Err(DidError::InvalidKey("No public key available".to_string()))
305 }
306 }
307}
308
309#[derive(Debug, Clone, Serialize, Deserialize)]
311#[serde(rename_all = "camelCase")]
312pub struct Service {
313 pub id: String,
315 #[serde(rename = "type")]
317 pub service_type: String,
318 pub service_endpoint: String,
320}
321
322#[derive(Debug, Clone, Serialize, Deserialize)]
324pub struct VerificationResult {
325 pub valid: bool,
327 pub issuer: Option<String>,
329 pub verified_at: DateTime<Utc>,
331 pub error: Option<String>,
333 pub checks: Vec<VerificationCheck>,
335}
336
337#[derive(Debug, Clone, Serialize, Deserialize)]
339pub struct VerificationCheck {
340 pub name: String,
342 pub passed: bool,
344 pub details: Option<String>,
346}
347
348impl VerificationResult {
349 pub fn success(issuer: &str) -> Self {
350 Self {
351 valid: true,
352 issuer: Some(issuer.to_string()),
353 verified_at: Utc::now(),
354 error: None,
355 checks: vec![],
356 }
357 }
358
359 pub fn failure(error: &str) -> Self {
360 Self {
361 valid: false,
362 issuer: None,
363 verified_at: Utc::now(),
364 error: Some(error.to_string()),
365 checks: vec![],
366 }
367 }
368
369 pub fn with_check(mut self, name: &str, passed: bool, details: Option<&str>) -> Self {
370 self.checks.push(VerificationCheck {
371 name: name.to_string(),
372 passed,
373 details: details.map(String::from),
374 });
375 self
376 }
377}
378
379#[cfg(test)]
380mod tests {
381 use super::*;
382
383 #[test]
384 fn test_verification_method_ed25519() {
385 let public_key = vec![
386 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
387 25, 26, 27, 28, 29, 30, 31, 32,
388 ];
389
390 let vm = VerificationMethod::ed25519("did:key:z123#key-1", "did:key:z123", &public_key);
391
392 assert_eq!(vm.method_type, "Ed25519VerificationKey2020");
393 assert!(vm.public_key_multibase.is_some());
394
395 let recovered = vm.get_public_key_bytes().unwrap();
396 assert_eq!(recovered, public_key);
397 }
398
399 #[test]
400 fn test_verification_result() {
401 let result = VerificationResult::success("did:key:z123")
402 .with_check("signature", true, None)
403 .with_check("expiration", true, Some("Not expired"));
404
405 assert!(result.valid);
406 assert_eq!(result.checks.len(), 2);
407 }
408}