anya-core 1.2.0

Enterprise-grade Bitcoin Infrastructure Platform
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
use std::error::Error;
// Web5 Identity Implementation
// Provides DID (Decentralized Identity) functionality
// as part of the Web5 integration - [AIR-012] Operational Reliability

use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use std::time::{SystemTime, UNIX_EPOCH};

// Define Result type for Web5
pub type Web5Result<T> = Result<T, Web5Error>;

// Define Error enum for Web5
#[derive(Debug, thiserror::Error)]
pub enum Web5Error {
    #[error("Identity error: {0}")]
    Identity(String),

    #[error("Protocol error: {0}")]
    Protocol(String),

    #[error("Communication error: {0}")]
    Communication(String),

    #[error("Storage error: {0}")]
    Storage(String),

    #[error("Credential error: {0}")]
    Credential(String),

    #[error("Not found: {0}")]
    NotFound(String),

    #[error("DWN error: {0}")]
    DWNError(String),

    #[error("Serialization error: {0}")]
    SerializationError(String),
}

// [AIS-3] Implementation for From<Box<dyn std::error::Error>> for Web5Error
// This allows the ? operator to work correctly when converting from Box<dyn Error> to Web5Error
impl From<Box<dyn std::error::Error>> for Web5Error {
    fn from(err: Box<dyn std::error::Error>) -> Self {
        Web5Error::Protocol(err.to_string())
    }
}

// [AIS-3] Implementation for From<String> for Web5Error
// This allows the ? operator to work correctly when converting from String to Web5Error
impl From<String> for Web5Error {
    fn from(err: String) -> Self {
        Web5Error::Protocol(err)
    }
}

// [AIS-3] Implementation for From<&str> for Web5Error
// This allows the ? operator to work correctly when converting from &str to Web5Error
impl From<&str> for Web5Error {
    fn from(err: &str) -> Self {
        Web5Error::Protocol(err.to_string())
    }
}

/// DID Manager
///
/// Core component responsible for decentralized identity management.
/// Implements the ports and adapters pattern for extensibility.
#[derive(Clone, Debug)]
pub struct DIDManager {
    /// DIDs managed by this instance
    dids: Arc<Mutex<HashMap<String, DID>>>,
    /// Default DID to use
    default_did: Option<String>,
    /// DID method to use
    method: String,
}

/// Decentralized Identifier
///
/// Represents a DID with its document and private keys.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct DID {
    /// DID URI (e.g., "did:ion:123...")
    pub id: String,
    /// DID Document
    pub document: DIDDocument,
    /// Private keys associated with this DID
    #[serde(skip_serializing)]
    pub private_keys: HashMap<String, Vec<u8>>,
}

/// DID Document
///
/// The public representation of a DID, containing verification methods
/// and service endpoints as defined in the DID Core specification.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct DIDDocument {
    /// DID context
    #[serde(rename = "@context")]
    pub context: Vec<String>,
    /// DID URI
    pub id: String,
    /// Verification methods
    #[serde(default)]
    pub verification_method: Vec<VerificationMethod>,
    /// Authentication methods
    #[serde(default)]
    pub authentication: Vec<String>,
    /// Assertion methods
    #[serde(default)]
    pub assertion_method: Vec<String>,
    /// Service endpoints
    #[serde(default)]
    pub service: Vec<Service>,
}

/// Verification Method
///
/// A cryptographic mechanism used for authentication and
/// digital signatures within a DID.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct VerificationMethod {
    /// ID of the verification method
    pub id: String,
    /// Type of the verification method
    #[serde(rename = "type")]
    pub vm_type: String,
    /// Controller of the verification method
    pub controller: String,
    /// Public key in JWK format
    #[serde(skip_serializing_if = "Option::is_none")]
    pub public_key_jwk: Option<JWK>,
}

/// JSON Web Key
///
/// A cryptographic key representation in JSON format.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct JWK {
    /// Key type
    pub kty: String,
    /// Curve (for EC keys)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub crv: Option<String>,
    /// X coordinate (for EC keys)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub x: Option<String>,
    /// Y coordinate (for EC keys)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub y: Option<String>,
    /// Key ID
    #[serde(skip_serializing_if = "Option::is_none")]
    pub kid: Option<String>,
}

/// Service
///
/// A service endpoint for a DID.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Service {
    /// ID of the service
    pub id: String,
    /// Type of the service
    #[serde(rename = "type")]
    pub service_type: String,
    /// Service endpoint URL
    pub service_endpoint: String,
}

impl DIDManager {
    /// Create a new DID manager with the specified method
    pub fn new(method: &str) -> Self {
        Self {
            dids: Arc::new(Mutex::new(HashMap::new())),
            default_did: None,
            method: method.to_string(),
        }
    }

    /// Create a new DID with the configured method
    pub fn create_did(&self) -> Web5Result<DID> {
        // Generate a random ID for the DID
        let id = format!("did:{}:{}", self.method, generate_random_id());

        // Generate a key pair for this DID
        let private_key = generate_private_key();
        let public_key_jwk = generate_public_key_jwk(&private_key);

        // Create verification method
        let verification_method = VerificationMethod {
            id: format!("{id}#key-1"),
            vm_type: "JsonWebKey2020".to_string(),
            controller: id.clone(),
            public_key_jwk: Some(public_key_jwk),
        };

        // Create a basic DID document
        let document = DIDDocument {
            context: vec!["https://www.w3.org/ns/did/v1".to_string()],
            id: id.clone(),
            verification_method: vec![verification_method],
            authentication: vec![format!("{}#key-1", id)],
            assertion_method: vec![format!("{}#key-1", id)],
            service: Vec::new(),
        };

        // Create the DID with private keys
        let mut private_keys = HashMap::new();
        private_keys.insert("key-1".to_string(), private_key);

        let did = DID {
            id: id.clone(),
            document,
            private_keys,
        };

        // Store the DID
        {
            let mut dids = self
                .dids
                .lock()
                .map_err(|e| format!("Mutex lock error: {e}"))?;
            dids.insert(id.clone(), did.clone());
        }

        Ok(did)
    }

    /// Resolve a DID to its document
    pub fn resolve_did(&self, did: &str) -> Result<DIDDocument, Box<dyn Error>> {
        // First, check if we have the DID locally
        let dids = self
            .dids
            .lock()
            .map_err(|e| format!("Mutex lock error: {e}"))?;
        if let Some(did_obj) = dids.get(did) {
            return Ok(did_obj.document.clone());
        }

        // If not found locally, return an error (future: implement remote resolution)
        Err(format!("DID not found: {did}").into())
    }

    /// Set the default DID
    pub fn set_default_did(&mut self, did: &str) -> Result<(), Box<dyn Error>> {
        let dids = self
            .dids
            .lock()
            .map_err(|e| format!("Mutex lock error: {e}"))?;
        if dids.contains_key(did) {
            self.default_did = Some(did.to_string());
            Ok(())
        } else {
            Err(format!("DID {did} not found").into())
        }
    }

    /// Get the default DID
    pub fn get_default_did(&self) -> Result<Option<String>, Box<dyn Error>> {
        Ok(self.default_did.clone())
    }

    /// Sign data with a DID's private key
    pub fn sign(&self, did: &str, data: &[u8]) -> Result<Vec<u8>, Box<dyn Error>> {
        // Get the DID
        let dids = self
            .dids
            .lock()
            .map_err(|e| format!("Mutex lock error: {e}"))?;
        let did_obj = dids
            .get(did)
            .ok_or_else(|| format!("DID not found: {did}"))?;

        // Get the first private key for signing
        if let Some((_, private_key_bytes)) = did_obj.private_keys.iter().next() {
            // Parse the private key
            let private_key = secp256k1::SecretKey::from_slice(private_key_bytes)
                .map_err(|e| format!("Invalid private key: {e}"))?;

            // Create secp256k1 context
            let secp = secp256k1::Secp256k1::signing_only();

            // Hash the data (using SHA256)
            let hash = {
                use sha2::{Digest, Sha256};
                let mut hasher = Sha256::new();
                hasher.update(data);
                hasher.finalize()
            };

            // Create message from hash
            let message = secp256k1::Message::from_digest_slice(&hash)
                .map_err(|e| format!("Failed to create message: {e}"))?;

            // Sign the message
            let signature = secp.sign_ecdsa(&message, &private_key);

            // Return the signature bytes
            Ok(signature.serialize_compact().to_vec())
        } else {
            Err("No private keys found for DID".into())
        }
    }

    /// Get a list of all DIDs
    pub fn dids(&self) -> Result<Vec<String>, Box<dyn Error>> {
        let dids = self
            .dids
            .lock()
            .map_err(|e| format!("Mutex lock error: {e}"))?;
        Ok(dids.keys().cloned().collect())
    }

    /// Get a DID by ID
    pub fn get_did(&self, did_id: &str) -> Web5Result<Option<DID>> {
        let dids = self
            .dids
            .lock()
            .map_err(|e| Web5Error::Storage(format!("Mutex lock error: {e}")))?;
        Ok(dids.get(did_id).cloned())
    }

    /// List all DIDs
    pub fn list_dids(&self) -> Vec<DID> {
        let dids = self
            .dids
            .lock()
            .unwrap_or_else(|_| panic!("Failed to lock mutex"));
        dids.values().cloned().collect()
    }
}

/// Identity manager for Web5 DID operations
#[derive(Debug, Clone)]
pub struct IdentityManager {
    did_manager: DIDManager,
}

impl IdentityManager {
    pub fn new(namespace: &str) -> Self {
        Self {
            did_manager: DIDManager::new(namespace),
        }
    }

    pub fn create_identity(&mut self) -> Web5Result<DID> {
        self.did_manager.create_did()
    }

    pub fn get_identity(&self, did_id: &str) -> Web5Result<Option<DID>> {
        self.did_manager.get_did(did_id)
    }

    pub fn list_identities(&self) -> Vec<DID> {
        self.did_manager.list_dids()
    }
}

/// Generate a random ID for a DID
/// [AIS-3] Properly handles errors without using ? operator
fn generate_random_id() -> String {
    let now = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default()
        .as_secs();

    format!("{now:x}")
}

/// Generate a private key for cryptographic operations
fn generate_private_key() -> Vec<u8> {
    // Generate a 32-byte private key (simplified implementation)
    use rand::RngCore;
    let mut key = vec![0u8; 32];
    rand::thread_rng().fill_bytes(&mut key);
    key
}

/// Generate a public key JWK from a private key
fn generate_public_key_jwk(private_key: &[u8]) -> JWK {
    // Simplified implementation - in production this would derive the actual public key
    // from the private key using proper cryptographic operations
    use base64::Engine;

    // For demonstration, we'll create a placeholder JWK
    JWK {
        kty: "EC".to_string(),
        crv: Some("secp256k1".to_string()),
        x: Some(base64::engine::general_purpose::URL_SAFE_NO_PAD.encode(&private_key[..16])),
        y: Some(base64::engine::general_purpose::URL_SAFE_NO_PAD.encode(&private_key[16..])),
        kid: Some("key-1".to_string()),
    }
}

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

    #[test]
    fn test_create_did() -> Result<(), Box<dyn Error>> {
        let manager = DIDManager::new("example");
        let did = manager.create_did()?;
        assert!(!did.id.is_empty());
        assert!(did.id.starts_with("did:example:"));
        assert!(!did.private_keys.is_empty());
        Ok(())
    }

    #[test]
    fn test_default_did() -> Result<(), Box<dyn Error>> {
        let mut manager = DIDManager::new("example");
        let did = manager.create_did()?;

        // Initially no default DID
        assert!(manager.get_default_did()?.is_none());

        // Set and get default DID
        manager.set_default_did(&did.id)?;
        assert_eq!(manager.get_default_did()?.unwrap(), did.id);
        Ok(())
    }
}