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
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
use std::collections::HashMap;
// [AIR-3][AIS-3][BPC-3][RES-3] Import necessary dependencies for HSM simulator
// This follows official Bitcoin Improvement Proposals (BIPs) standards for secure HSM implementation
use async_trait::async_trait;
use base64::Engine;
use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey};
use bitcoin::{psbt::Psbt, Address, CompressedPublicKey, Network};
use chrono::Utc;
use rand::prelude::*;
use rand::rngs::OsRng;
use sha2::{Digest, Sha256};
use std::str::FromStr;
use std::time::Duration;
use tokio::sync::Mutex;
use tokio::time::sleep;
use uuid::Uuid;

use crate::security::hsm::config::SimulatorConfig;
use crate::security::hsm::error::HsmError;
use crate::security::hsm::provider::{
    HsmOperation, HsmProvider, HsmProviderStatus, HsmRequest, HsmResponse, KeyGenParams, KeyInfo,
    KeyPair, KeyType, KeyUsage, SigningAlgorithm,
};

/// Simulator HSM provider for testing and development
/// Simulates real hardware behavior with controlled conditions for Bitcoin testnet
#[derive(Debug)]
pub struct SimulatorHsmProvider {
    config: SimulatorConfig,
    keys: Mutex<HashMap<String, KeyInfo>>,
    key_data: Mutex<HashMap<String, Vec<u8>>>,
    rng: Mutex<OsRng>,
    network: Network,
    secp: Secp256k1<bitcoin::secp256k1::All>,
    // Simulated hardware state
    state: Mutex<HardwareState>,
}

/// Simulated hardware state
#[derive(Debug, Clone)]
struct HardwareState {
    battery_level: u8,
    firmware_version: String,
    is_locked: bool,
    pin_attempts: u8,
    max_pin_attempts: u8,
    session_timeout: Duration,
    last_operation: Option<chrono::DateTime<chrono::Utc>>,
}

impl SimulatorHsmProvider {
    /// Create a new simulator HSM provider
    pub fn new(config: &SimulatorConfig) -> Result<Self, HsmError> {
        // Ensure storage path exists
        std::fs::create_dir_all(&config.storage_path).map_err(|e| {
            HsmError::InitializationError(format!("Failed to create storage path: {}", e))
        })?;

        Ok(Self {
            config: config.clone(),
            keys: Mutex::new(HashMap::new()),
            key_data: Mutex::new(HashMap::new()),
            rng: Mutex::new(OsRng),
            network: Network::Testnet, // Always use testnet
            secp: Secp256k1::new(),
            state: Mutex::new(HardwareState {
                battery_level: 100,
                firmware_version: "1.0.0".to_string(),
                is_locked: true,
                pin_attempts: 0,
                max_pin_attempts: 3,
                session_timeout: Duration::from_secs(300), // 5 minutes
                last_operation: None,
            }),
        })
    }

    /// Generate a random key ID
    fn generate_key_id(&self) -> String {
        Uuid::new_v4().to_string()
    }

    /// Simulate hardware conditions like latency and random failures
    async fn simulate_conditions(&self) -> Result<(), HsmError> {
        // Get mutable state lock
        let mut state = self.state.lock().await;

        // Check for session timeout
        if let Some(last_op) = state.last_operation {
            let now = Utc::now();
            let elapsed = now.signed_duration_since(last_op);

            if elapsed.to_std().unwrap_or_default() > state.session_timeout {
                state.is_locked = true;
                return Err(HsmError::AuthenticationError(
                    "Session timed out".to_string(),
                ));
            }
        }

        // Update the last operation timestamp
        state.last_operation = Some(Utc::now());

        // Check if the device is locked
        if state.is_locked {
            return Err(HsmError::DeviceLocked("Device is locked".to_string()));
        }

        // Simulate battery drain
        if state.battery_level > 0 {
            state.battery_level = state.battery_level.saturating_sub(1);
        } else {
            return Err(HsmError::HardwareFailure("Battery depleted".to_string()));
        }

        // Release the mutex lock before simulating latency
        drop(state);

        // Simulate latency if enabled
        if self.config.simulate_latency {
            sleep(Duration::from_millis(self.config.latency_ms)).await;
        }

        // Simulate random failures if enabled
        if self.config.simulate_failures {
            let mut rng = self.rng.lock().await;
            let failure_roll: f64 = rng.gen();
            if failure_roll < self.config.failure_rate {
                return Err(HsmError::HardwareFailure(
                    "Random hardware failure".to_string(),
                ));
            }
        }

        Ok(())
    }

    /// Unlock the device with PIN
    pub async fn unlock(&self, pin: &str) -> Result<(), HsmError> {
        let mut state = self.state.lock().await;

        // Simulate fixed PIN for testnet simulation (1234)
        const CORRECT_PIN: &str = "1234";

        if state.pin_attempts >= state.max_pin_attempts {
            return Err(HsmError::PinLocked(
                "Too many incorrect attempts, device is locked".to_string(),
            ));
        }

        if pin != CORRECT_PIN {
            state.pin_attempts += 1;
            return Err(HsmError::AuthenticationError(format!(
                "Incorrect PIN, {} attempts remaining",
                state.max_pin_attempts - state.pin_attempts
            )));
        }

        // Correct PIN
        state.is_locked = false;
        state.pin_attempts = 0;
        state.last_operation = Some(chrono::Utc::now());

        Ok(())
    }

    /// Generate testnet Bitcoin key
    async fn generate_bitcoin_key(
        &self,
        _params: &KeyGenParams,
    ) -> Result<(Vec<u8>, Vec<u8>), HsmError> {
        self.simulate_conditions().await?;

        let mut rng = self.rng.lock().await;
        let secret_key = SecretKey::new(&mut *rng);
        let public_key = PublicKey::from_secret_key(&self.secp, &secret_key);

        // Generate testnet address
        let compressed_key = CompressedPublicKey::from_slice(&public_key.serialize()).unwrap();
        let address = Address::p2wpkh(&compressed_key, self.network);

        tracing::info!("Generated new testnet address: {}", address);

        Ok((
            public_key.serialize().to_vec(),
            secret_key.secret_bytes().to_vec(),
        ))
    }

    /// Sign Bitcoin transaction for testnet
    async fn sign_bitcoin_transaction(&self, key_id: &str, tx: &mut Psbt) -> Result<(), HsmError> {
        self.simulate_conditions().await?;

        let key_data = self.key_data.lock().await;
        let secret_data = key_data
            .get(key_id)
            .ok_or_else(|| HsmError::KeyNotFound(key_id.to_string()))?;

        let secret_key = SecretKey::from_slice(secret_data)
            .map_err(|e| HsmError::SigningError(format!("Invalid key data: {}", e)))?;

        // Generate public key from secret key
        let public_key = PublicKey::from_secret_key(&self.secp, &secret_key);

        // Sign the transaction for testnet
        let mut signing_keys = std::collections::BTreeMap::new();
        let bitcoin_public_key = bitcoin::PublicKey::new(public_key);
        signing_keys.insert(
            bitcoin_public_key,
            bitcoin::PrivateKey::new(secret_key, self.network),
        );

        let result = tx.sign(&signing_keys, &self.secp);
        match result {
            Ok(_) => {
                tracing::info!("Successfully signed testnet transaction");
                Ok(())
            }
            Err((_, errors)) => {
                tracing::error!("Failed to sign transaction: {:?}", errors);
                Err(HsmError::SigningError(
                    "Failed to sign transaction".to_string(),
                ))
            }
        }
    }

    /// Get device diagnostics
    pub async fn get_diagnostics(&self) -> Result<DeviceDiagnostics, HsmError> {
        let state = self.state.lock().await;

        let diagnostics = DeviceDiagnostics {
            battery_level: state.battery_level,
            firmware_version: state.firmware_version.clone(),
            is_locked: state.is_locked,
            last_operation: state.last_operation,
            active_keys: self.keys.lock().await.len(),
            network: "testnet".to_string(),
        };

        Ok(diagnostics)
    }
}

/// Device diagnostics information
#[derive(Debug, Clone, serde::Serialize)]
pub struct DeviceDiagnostics {
    battery_level: u8,
    firmware_version: String,
    is_locked: bool,
    last_operation: Option<chrono::DateTime<chrono::Utc>>,
    active_keys: usize,
    network: String,
}

#[async_trait]
impl HsmProvider for SimulatorHsmProvider {
    async fn initialize(&self) -> Result<(), HsmError> {
        // Ensure storage path exists
        std::fs::create_dir_all(&self.config.storage_path).map_err(|e| {
            HsmError::InitializationError(format!("Failed to create storage path: {}", e))
        })?;

        tracing::info!("Initialized simulator HSM for Bitcoin testnet operations");
        Ok(())
    }

    async fn generate_key(&self, params: KeyGenParams) -> Result<(KeyPair, KeyInfo), HsmError> {
        self.simulate_conditions().await?;

        let key_id = params.id.clone().unwrap_or_else(|| self.generate_key_id());

        // Generate key pair based on key type with real testnet support
        let (public_key, private_key) = match &params.key_type {
            KeyType::Ec { curve }
                if *curve == crate::security::hsm::provider::EcCurve::Secp256k1 =>
            {
                self.generate_bitcoin_key(&params).await?
            }
            _ => return Err(HsmError::UnsupportedKeyType),
        };

        // Store key information
        let key_info = KeyInfo {
            id: key_id.clone(),
            label: params.label.clone(),
            key_type: params.key_type.clone(),
            extractable: params.extractable,
            usages: params.usages.clone(),
            created_at: Utc::now(),
            expires_at: params.expires_at,
            attributes: params.attributes.clone(),
        };

        let mut keys = self.keys.lock().await;
        let mut key_data = self.key_data.lock().await;

        keys.insert(key_id.clone(), key_info.clone());
        key_data.insert(key_id.clone(), private_key);

        let key_pair = KeyPair {
            id: key_id.clone(),
            key_type: params.key_type,
            public_key,
            private_key_handle: key_id.clone(),
        };

        Ok((key_pair, key_info))
    }

    async fn sign(
        &self,
        key_id: &str,
        algorithm: SigningAlgorithm,
        data: &[u8],
    ) -> Result<Vec<u8>, HsmError> {
        self.simulate_conditions().await?;

        // Check if key exists and has signing capability
        let keys = self.keys.lock().await;
        let key_info = keys
            .get(key_id)
            .ok_or_else(|| HsmError::KeyNotFound(key_id.to_string()))?;

        if !key_info.usages.contains(&KeyUsage::Sign) {
            return Err(HsmError::PermissionDenied(
                "Key does not have signing permission".to_string(),
            ));
        }

        // Get the private key
        let key_data = self.key_data.lock().await;
        let private_key_data = key_data
            .get(key_id)
            .ok_or_else(|| HsmError::KeyNotFound(key_id.to_string()))?;

        // Perform real signing based on algorithm
        match algorithm {
            SigningAlgorithm::EcdsaSha256 => {
                let secret_key = SecretKey::from_slice(private_key_data)
                    .map_err(|e| HsmError::SigningError(format!("Invalid key data: {}", e)))?;

                // Create message hash
                let mut hasher = Sha256::new();
                hasher.update(data);
                let message_hash = hasher.finalize();

                // Sign the hash
                let message = bitcoin::secp256k1::Message::from_digest_slice(&message_hash)
                    .map_err(|e| HsmError::SigningError(format!("Invalid message hash: {}", e)))?;

                let signature = self.secp.sign_ecdsa(&message, &secret_key);
                Ok(signature.serialize_der().to_vec())
            }
            _ => Err(HsmError::UnsupportedOperation(format!(
                "Unsupported signing algorithm: {:?}",
                algorithm
            ))),
        }
    }

    async fn verify(
        &self,
        key_id: &str,
        algorithm: SigningAlgorithm,
        data: &[u8],
        signature: &[u8],
    ) -> Result<bool, HsmError> {
        self.simulate_conditions().await?;

        // Check if key exists and has verify capability
        let keys = self.keys.lock().await;
        let key_info = keys
            .get(key_id)
            .ok_or_else(|| HsmError::KeyNotFound(key_id.to_string()))?;

        if !key_info.usages.contains(&KeyUsage::Verify) {
            return Err(HsmError::PermissionDenied(
                "Key does not have verify permission".to_string(),
            ));
        }

        // Real verification for testnet
        match algorithm {
            SigningAlgorithm::EcdsaSha256 => {
                // Get the public key
                match key_info.key_type {
                    KeyType::Ec { curve }
                        if curve == crate::security::hsm::provider::EcCurve::Secp256k1 =>
                    {
                        // Create message hash
                        let mut hasher = Sha256::new();
                        hasher.update(data);
                        let message_hash = hasher.finalize();

                        // Get the serialized public key from key storage or regenerate
                        let pubkey_bytes = self.export_public_key(key_id).await?;
                        let public_key = PublicKey::from_slice(&pubkey_bytes).map_err(|e| {
                            HsmError::VerificationError(format!("Invalid public key data: {}", e))
                        })?;

                        // Parse the signature
                        let sig = bitcoin::secp256k1::ecdsa::Signature::from_der(signature)
                            .map_err(|e| {
                                HsmError::VerificationError(format!(
                                    "Invalid signature format: {}",
                                    e
                                ))
                            })?;

                        // Verify
                        let message = bitcoin::secp256k1::Message::from_digest_slice(&message_hash)
                            .map_err(|e| {
                                HsmError::VerificationError(format!("Invalid message hash: {}", e))
                            })?;

                        match self.secp.verify_ecdsa(&message, &sig, &public_key) {
                            Ok(_) => Ok(true),
                            Err(_) => Ok(false),
                        }
                    }
                    _ => Err(HsmError::UnsupportedKeyType),
                }
            }
            _ => Err(HsmError::UnsupportedOperation(format!(
                "Unsupported verification algorithm: {:?}",
                algorithm
            ))),
        }
    }

    async fn export_public_key(&self, key_id: &str) -> Result<Vec<u8>, HsmError> {
        self.simulate_conditions().await?;

        // Check if key exists
        let keys = self.keys.lock().await;
        let key_info = keys
            .get(key_id)
            .ok_or_else(|| HsmError::KeyNotFound(key_id.to_string()))?;

        // Get the private key and derive public key
        let key_data = self.key_data.lock().await;
        let private_key_data = key_data
            .get(key_id)
            .ok_or_else(|| HsmError::KeyNotFound(key_id.to_string()))?;

        match key_info.key_type {
            KeyType::Ec { curve }
                if curve == crate::security::hsm::provider::EcCurve::Secp256k1 =>
            {
                let secret_key = SecretKey::from_slice(private_key_data).map_err(|e| {
                    HsmError::KeyGenerationError(format!("Invalid key data: {}", e))
                })?;
                let public_key = PublicKey::from_secret_key(&self.secp, &secret_key);
                Ok(public_key.serialize().to_vec())
            }
            _ => Err(HsmError::UnsupportedKeyType),
        }
    }

    async fn list_keys(&self) -> Result<Vec<KeyInfo>, HsmError> {
        self.simulate_conditions().await?;

        let keys = self.keys.lock().await;
        Ok(keys.values().cloned().collect())
    }

    async fn delete_key(&self, key_id: &str) -> Result<(), HsmError> {
        self.simulate_conditions().await?;

        let mut keys = self.keys.lock().await;
        let mut key_data = self.key_data.lock().await;

        if keys.remove(key_id).is_none() {
            return Err(HsmError::KeyNotFound(key_id.to_string()));
        }

        key_data.remove(key_id);
        Ok(())
    }

    async fn get_status(&self) -> Result<HsmProviderStatus, HsmError> {
        let state = self.state.lock().await;

        if state.battery_level == 0 {
            return Ok(HsmProviderStatus::Error("Battery depleted".to_string()));
        }

        if state.is_locked {
            return Ok(HsmProviderStatus::Unavailable);
        }

        Ok(HsmProviderStatus::Ready)
    }

    async fn close(&self) -> Result<(), HsmError> {
        let mut state = self.state.lock().await;
        state.is_locked = true;
        Ok(())
    }

    async fn execute_operation(&self, request: HsmRequest) -> Result<HsmResponse, HsmError> {
        match request.operation {
            HsmOperation::GenerateKey => {
                let params: KeyGenParams = serde_json::from_value(request.parameters.clone())
                    .map_err(|e| {
                        HsmError::InvalidParameters(format!(
                            "Invalid key generation parameters: {}",
                            e
                        ))
                    })?;

                let key_pair = self.generate_key(params).await?;
                let response_data = serde_json::to_value(key_pair)
                    .map_err(|e| HsmError::SerializationError(e.to_string()))?;

                Ok(HsmResponse::success(request.id, Some(response_data)))
            }
            HsmOperation::Sign => {
                let params: SignParams = serde_json::from_value(request.parameters.clone())
                    .map_err(|e| {
                        HsmError::InvalidParameters(format!("Invalid signing parameters: {}", e))
                    })?;

                // [AIR-3][AIS-3][BPC-3][RES-3] Sign data using simulator HSM
                let signature = self
                    .sign(&params.key_id, params.algorithm, &params.data)
                    .await?;
                // [AIR-3][AIS-3][BPC-3][RES-3] Use base64 Engine for encoding
                // This follows official Bitcoin Improvement Proposals (BIPs) standards for secure data handling
                let response_data = serde_json::to_value(Base64SignatureResponse {
                    signature: base64::engine::general_purpose::STANDARD.encode(&signature),
                    algorithm: params.algorithm,
                })
                .map_err(|e| HsmError::SerializationError(e.to_string()))?;

                Ok(HsmResponse::success(request.id, Some(response_data)))
            }
            HsmOperation::Custom(op) if op == "sign_bitcoin_tx" => {
                let params: BitcoinTxSignParams =
                    serde_json::from_value(request.parameters.clone()).map_err(|e| {
                        HsmError::InvalidParameters(format!(
                            "Invalid Bitcoin TX signing parameters: {}",
                            e
                        ))
                    })?;

                // Decode PSBT
                let mut psbt = bitcoin::psbt::Psbt::from_str(&params.psbt)
                    .map_err(|e| HsmError::InvalidParameters(format!("Invalid PSBT: {}", e)))?;

                // Sign the transaction
                self.sign_bitcoin_transaction(&params.key_id, &mut psbt)
                    .await?;

                // Return the signed PSBT
                let response_data = serde_json::json!({
                    "signed_psbt": psbt.to_string(),
                    "network": "testnet",
                });

                Ok(HsmResponse::success(request.id, Some(response_data)))
            }
            HsmOperation::Custom(op) if op == "unlock" => {
                let params: UnlockParams = serde_json::from_value(request.parameters.clone())
                    .map_err(|e| {
                        HsmError::InvalidParameters(format!("Invalid unlock parameters: {}", e))
                    })?;

                // Unlock the device
                self.unlock(&params.pin).await?;

                Ok(HsmResponse::success(request.id, None))
            }
            HsmOperation::Custom(op) if op == "get_diagnostics" => {
                let diagnostics = self.get_diagnostics().await?;
                let response_data = serde_json::to_value(diagnostics)
                    .map_err(|e| HsmError::SerializationError(e.to_string()))?;

                Ok(HsmResponse::success(request.id, Some(response_data)))
            }
            _ => Err(HsmError::UnsupportedOperation(format!(
                "{:?}",
                request.operation
            ))),
        }
    }
}

/// Parameters for signing operation
#[derive(Debug, serde::Deserialize)]
struct SignParams {
    key_id: String,
    algorithm: SigningAlgorithm,
    data: Vec<u8>,
}

/// Parameters for Bitcoin transaction signing
#[derive(Debug, serde::Deserialize)]
struct BitcoinTxSignParams {
    key_id: String,
    psbt: String,
}

/// Parameters for unlocking the device
#[derive(Debug, serde::Deserialize)]
struct UnlockParams {
    pin: String,
}

/// Response for signature in base64 format
#[derive(Debug, serde::Serialize)]
struct Base64SignatureResponse {
    signature: String,
    algorithm: SigningAlgorithm,
}