tenzro-wallet 0.1.0

MPC wallet for Tenzro Network — FROST-Ed25519 + ML-DSA-65 hybrid threshold wallets, Argon2id keystore, transaction history, contacts
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
//! Wallet service implementation for Tenzro Network.
//!
//! This module provides the main `TenzroWalletService` implementation
//! of the `WalletService` trait.

use crate::asset_manager::AssetManager;
use crate::balance::{Balance, BalanceTracker};
use crate::contacts::{AddressBook, Contact};
use crate::error::{Result, WalletError};
use crate::history::{HistoryFilter, TransactionHistory, TxRecord};
use crate::keystore::Keystore;
use crate::mpc_signing::TransactionSigner;
use crate::nonce::NonceManager;
use crate::provisioning::{ProvisioningConfig, WalletProvisioner};
use crate::signing::HybridSignatureBytes;
use crate::state_sync::{ChainStateProvider, WalletStateSync};
use crate::traits::WalletService;
use crate::validation::{TransactionValidator, ValidationConfig};
use crate::wallet::{MpcWallet, WalletId};
use async_trait::async_trait;
use dashmap::DashMap;
use std::path::PathBuf;
use std::sync::Arc;
use tenzro_types::primitives::{Address, Hash, Nonce};
use tenzro_types::transaction::Transaction;
use tenzro_types::AssetId;
use tracing::{debug, info, warn};

/// Configuration for the wallet service
#[derive(Debug, Clone)]
pub struct WalletServiceConfig {
    /// Path to keystore directory
    pub keystore_path: PathBuf,
    /// Path to contacts file
    pub contacts_path: PathBuf,
    /// Provisioning configuration
    pub provisioning_config: ProvisioningConfig,
    /// Default password for auto-provisioning (for demo/testing only)
    pub default_password: Option<String>,
    /// Transaction validation configuration
    pub validation_config: ValidationConfig,
}

impl Default for WalletServiceConfig {
    fn default() -> Self {
        Self {
            keystore_path: PathBuf::from("./data/wallets"),
            contacts_path: PathBuf::from("./data/contacts.json"),
            provisioning_config: ProvisioningConfig::default(),
            default_password: None,
            validation_config: ValidationConfig::default(),
        }
    }
}

impl WalletServiceConfig {
    /// Create a new configuration
    pub fn new(keystore_path: PathBuf) -> Self {
        Self {
            keystore_path: keystore_path.clone(),
            contacts_path: keystore_path.join("contacts.json"),
            provisioning_config: ProvisioningConfig::default(),
            default_password: None,
            validation_config: ValidationConfig::default(),
        }
    }

    /// Set the provisioning configuration
    pub fn with_provisioning_config(mut self, config: ProvisioningConfig) -> Self {
        self.provisioning_config = config;
        self
    }

    /// Set default password (for testing only)
    pub fn with_default_password(mut self, password: String) -> Self {
        self.default_password = Some(password);
        self
    }

    /// Set validation configuration
    pub fn with_validation_config(mut self, config: ValidationConfig) -> Self {
        self.validation_config = config;
        self
    }
}

/// Main wallet service implementation for Tenzro Network.
///
/// This service manages:
/// - Auto-provisioning of MPC wallets
/// - In-memory wallet cache with persistent keystore
/// - Balance tracking (local + on-chain sync)
/// - Transaction validation, signing, and verification
/// - Nonce management for replay protection
/// - Transaction history tracking
/// - Address book / contact management
/// - Asset management
pub struct TenzroWalletService {
    /// Configuration
    config: WalletServiceConfig,
    /// Wallet provisioner
    provisioner: WalletProvisioner,
    /// In-memory wallet store
    wallets: Arc<DashMap<WalletId, MpcWallet>>,
    /// Balance tracker
    balances: Arc<BalanceTracker>,
    /// Asset manager
    assets: Arc<AssetManager>,
    /// Keystore for persistent storage
    keystore: Arc<tokio::sync::Mutex<Keystore>>,
    /// Transaction validator
    validator: TransactionValidator,
    /// Nonce manager
    nonces: Arc<NonceManager>,
    /// Transaction history
    history: Arc<TransactionHistory>,
    /// Address book
    contacts: Arc<AddressBook>,
    /// State sync manager
    state_sync: Arc<WalletStateSync>,
}

impl TenzroWalletService {
    /// Create a new wallet service with default configuration
    pub fn new() -> Result<Self> {
        Self::with_config(WalletServiceConfig::default())
    }

    /// Create a new wallet service with custom configuration
    pub fn with_config(config: WalletServiceConfig) -> Result<Self> {
        let provisioner = WalletProvisioner::with_config(config.provisioning_config.clone())?;
        let keystore = Keystore::new(&config.keystore_path)?;
        let validator = TransactionValidator::with_config(config.validation_config.clone());
        let nonces = Arc::new(NonceManager::new());
        let balances = Arc::new(BalanceTracker::new());
        let history = Arc::new(TransactionHistory::new());

        let contacts = Arc::new(
            AddressBook::with_storage(&config.contacts_path).unwrap_or_else(|_| AddressBook::new()),
        );

        let state_sync = Arc::new(WalletStateSync::new(
            balances.clone(),
            nonces.clone(),
            history.clone(),
        ));

        Ok(Self {
            config,
            provisioner,
            wallets: Arc::new(DashMap::new()),
            balances,
            assets: Arc::new(AssetManager::new()),
            keystore: Arc::new(tokio::sync::Mutex::new(keystore)),
            validator,
            nonces,
            history,
            contacts,
            state_sync,
        })
    }

    /// Get the balance tracker
    pub fn balance_tracker(&self) -> Arc<BalanceTracker> {
        self.balances.clone()
    }

    /// Get the asset manager
    pub fn asset_manager(&self) -> Arc<AssetManager> {
        self.assets.clone()
    }

    /// Get the nonce manager
    pub fn nonce_manager(&self) -> Arc<NonceManager> {
        self.nonces.clone()
    }

    /// Get the transaction history
    pub fn transaction_history(&self) -> Arc<TransactionHistory> {
        self.history.clone()
    }

    /// Get the address book
    pub fn address_book(&self) -> Arc<AddressBook> {
        self.contacts.clone()
    }

    /// Get the state sync manager
    pub fn state_sync(&self) -> Arc<WalletStateSync> {
        self.state_sync.clone()
    }

    /// Connect a chain state provider for on-chain synchronization.
    pub fn connect_chain_provider(&mut self, provider: Arc<dyn ChainStateProvider>) {
        self.state_sync = Arc::new(WalletStateSync::new(
            self.balances.clone(),
            self.nonces.clone(),
            self.history.clone(),
        ).with_chain_provider(provider));
    }

    /// Load a wallet from keystore.
    ///
    /// The keystore returns the `(PublicKeyPackage, Vec<KeyShare>)` pair
    /// atomically — the FROST group public key is required for signing and
    /// is not reconstructible from individual shares, so it is sealed in the
    /// same Argon2id-derived bundle.
    async fn load_wallet_from_keystore(
        &self,
        wallet_id: &WalletId,
        password: &str,
    ) -> Result<MpcWallet> {
        let mut keystore = self.keystore.lock().await;
        let (pubkey_package, key_shares) = keystore.load_shares(wallet_id, password)?;

        if key_shares.is_empty() {
            return Err(WalletError::KeystoreError(
                "No key shares found in keystore".to_string(),
            ));
        }

        // Derive address from the FROST group public key.
        let group_pk = pubkey_package.group_public_key.as_public_key();
        let crypto_addr = group_pk.to_address();
        let mut addr_bytes = [0u8; 32];
        addr_bytes[..20].copy_from_slice(crypto_addr.as_bytes());
        let address = Address::new(addr_bytes);

        // Load the ML-DSA-65 + BLS12-381 signing keys persisted alongside
        // the classical bundle. We reuse the existing keystore lock guard so
        // we never double-acquire `self.keystore`.
        let pq_signing_key = keystore.load_pq_seed(wallet_id, password)?;
        let bls_signing_key = keystore.load_bls_seed(wallet_id, password)?;
        drop(keystore);

        let wallet = MpcWallet::new(
            wallet_id.clone(),
            address,
            key_shares,
            pubkey_package,
            pq_signing_key,
            bls_signing_key,
        )?;

        warn!(
            "Loaded wallet {} from keystore (metadata reconstructed from shares)",
            wallet_id
        );

        Ok(wallet)
    }

    /// Store a wallet to keystore (FROST bundle + PQ signing seed + BLS
    /// signing seed).
    async fn store_wallet_to_keystore(
        &self,
        wallet: &MpcWallet,
        password: &str,
    ) -> Result<()> {
        let mut keystore = self.keystore.lock().await;
        let pubkey_package = wallet.frost_pubkey_package()?;
        keystore.store_shares(
            &wallet.wallet_id,
            pubkey_package,
            &wallet.key_shares,
            password,
        )?;
        // Mandatory PQ leg — persist the 32-byte canonical FIPS 204 seed
        // sealed with the same Argon2id-derived key. `pq_signing_key()`
        // returns an error only when the wallet was never decrypted, which
        // cannot happen on the provisioning / re-encryption path.
        let pq_key = wallet.pq_signing_key()?;
        keystore.store_pq_seed(&wallet.wallet_id, pq_key, password)?;
        // Mandatory BLS leg — persist the 32-byte BLS12-381 secret key bytes
        // sealed under the same key.
        let bls_key = wallet.bls_signing_key()?;
        keystore.store_bls_seed(&wallet.wallet_id, bls_key, password)?;
        Ok(())
    }

    /// Read the three encrypted keystore files for `wallet_id` as raw bytes
    /// (no decryption). Used by the identity CAR export flow (C.6) to ship a
    /// wallet across machines without ever exposing the password in transit.
    pub async fn export_encrypted_wallet_files(
        &self,
        wallet_id: &WalletId,
    ) -> Result<std::collections::HashMap<String, Vec<u8>>> {
        let keystore = self.keystore.lock().await;
        keystore.export_encrypted_wallet_files(wallet_id)
    }

    /// Write encrypted keystore files received from another node (C.6 import).
    /// Refuses to overwrite an existing wallet of the same ID — callers must
    /// delete first if they really mean to clobber.
    pub async fn import_encrypted_wallet_files(
        &self,
        wallet_id: &WalletId,
        files: &std::collections::HashMap<String, Vec<u8>>,
    ) -> Result<()> {
        let mut keystore = self.keystore.lock().await;
        keystore.import_encrypted_wallet_files(wallet_id, files)
    }

    /// Get wallet by ID (from cache or keystore)
    async fn get_wallet_internal(&self, wallet_id: &WalletId) -> Result<Option<MpcWallet>> {
        // Check cache first
        if let Some(wallet) = self.wallets.get(wallet_id) {
            return Ok(Some(wallet.clone()));
        }

        // Try to load from keystore with default password
        if let Some(password) = &self.config.default_password {
            let keystore = self.keystore.lock().await;
            if keystore.has_wallet(wallet_id) {
                drop(keystore); // Release lock before loading
                let wallet = self.load_wallet_from_keystore(wallet_id, password).await?;
                self.wallets.insert(wallet_id.clone(), wallet.clone());
                return Ok(Some(wallet));
            }
        }

        Ok(None)
    }
}

impl Default for TenzroWalletService {
    fn default() -> Self {
        Self::new().expect("Failed to create default wallet service")
    }
}

#[async_trait]
impl WalletService for TenzroWalletService {
    // === Wallet Lifecycle ===

    async fn provision_wallet(&self) -> Result<MpcWallet> {
        info!("Provisioning new wallet");

        let wallet = self.provisioner.provision_wallet()?;

        debug!(
            "Provisioned wallet {} at address {}",
            wallet.wallet_id, wallet.address
        );

        // Store in cache
        self.wallets.insert(wallet.wallet_id.clone(), wallet.clone());

        // Store to keystore if default password is set
        if let Some(password) = &self.config.default_password {
            self.store_wallet_to_keystore(&wallet, password).await?;
        }

        // Initialize balances for supported assets
        for asset_id in &wallet.supported_assets {
            self.balances.set_balance(
                &wallet.address,
                asset_id,
                Balance::zero(),
            );
        }

        info!(
            "Successfully provisioned wallet {} with {} shares",
            wallet.wallet_id,
            wallet.share_count()
        );

        Ok(wallet)
    }

    async fn get_wallet(&self, wallet_id: &WalletId) -> Result<Option<MpcWallet>> {
        self.get_wallet_internal(wallet_id).await
    }

    async fn list_wallets(&self) -> Result<Vec<WalletId>> {
        let mut wallet_ids: Vec<_> = self.wallets.iter().map(|e| e.key().clone()).collect();

        let keystore = self.keystore.lock().await;
        let keystore_ids = keystore.list_wallets()?;
        for id in keystore_ids {
            if !wallet_ids.contains(&id) {
                wallet_ids.push(id);
            }
        }

        Ok(wallet_ids)
    }

    async fn delete_wallet(&self, wallet_id: &WalletId) -> Result<()> {
        self.wallets.remove(wallet_id);

        let mut keystore = self.keystore.lock().await;
        keystore.delete_wallet(wallet_id)?;

        info!("Deleted wallet {}", wallet_id);
        Ok(())
    }

    async fn set_wallet_label(&self, wallet_id: &WalletId, label: String) -> Result<()> {
        let mut wallet = self
            .get_wallet_internal(wallet_id)
            .await?
            .ok_or_else(|| WalletError::WalletNotFound(wallet_id.to_string()))?;

        wallet.set_label(label);
        self.wallets.insert(wallet_id.clone(), wallet);
        Ok(())
    }

    // === Balance ===

    async fn get_balance(&self, address: &Address, asset: &AssetId) -> Result<Balance> {
        if !self.assets.is_supported(asset) {
            return Err(WalletError::AssetNotSupported(asset.as_str().to_string()));
        }

        // Try on-chain sync first, fall back to local
        self.state_sync.get_balance(address, asset).await
    }

    async fn available_balance(&self, address: &Address, asset: &AssetId) -> Result<u128> {
        let balance = self.get_balance(address, asset).await?;
        Ok(balance.spendable())
    }

    // === Signing ===

    async fn sign_transaction(
        &self,
        wallet_id: &WalletId,
        tx: &Transaction,
    ) -> Result<HybridSignatureBytes> {
        debug!("Signing transaction with wallet {}", wallet_id);

        // Validate the transaction before signing
        self.validator.validate(tx)?;

        // Get wallet
        let wallet = self
            .get_wallet_internal(wallet_id)
            .await?
            .ok_or_else(|| WalletError::WalletNotFound(wallet_id.to_string()))?;

        // Verify sender matches wallet address or public key (hex canonical address)
        let pubkey_matches = Address::from_bytes(wallet.public_key.as_bytes())
            .map(|a| tx.from == a)
            .unwrap_or(false);
        if tx.from != wallet.address && !pubkey_matches {
            return Err(WalletError::TransactionValidationFailed(format!(
                "transaction sender {} does not match wallet address {} or public key",
                tx.from, wallet.address
            )));
        }

        // The transaction must commit to the wallet's PQ verifying key — this
        // closes the door on key-substitution attacks that the `Transaction::hash()`
        // PQ-binding already covers, but enforced here so callers get an early
        // error rather than a silent admission failure.
        if tx.pq_public_key != wallet.pq_verifying_key {
            return Err(WalletError::TransactionValidationFailed(
                "transaction pq_public_key does not match wallet ML-DSA-65 verifying key"
                    .to_string(),
            ));
        }

        // Hash the transaction for signing
        let tx_hash = tx.hash();

        // Classical leg — threshold MPC signature with post-signing verification.
        let classical = TransactionSigner::sign_transaction(&wallet, tx_hash.as_bytes())?;

        // Post-quantum leg — ML-DSA-65 signature over the same hash.
        let pq = wallet.pq_signing_key()?.sign(tx_hash.as_bytes());

        // Record in history
        let record = TxRecord::new_outgoing(
            tx_hash,
            tx.from,
            tx.to,
            tx.nonce,
            tx.tx_type.clone(),
            tx.gas_limit,
            tx.gas_price,
            tx.memo.clone(),
        );
        self.history.record(&wallet.address, record);

        info!(
            "Successfully signed and recorded transaction with wallet {}",
            wallet_id
        );

        Ok(HybridSignatureBytes::new(classical, pq))
    }

    async fn sign_data(
        &self,
        wallet_id: &WalletId,
        data: &[u8],
    ) -> Result<HybridSignatureBytes> {
        let wallet = self
            .get_wallet_internal(wallet_id)
            .await?
            .ok_or_else(|| WalletError::WalletNotFound(wallet_id.to_string()))?;

        let classical = TransactionSigner::sign_raw(&wallet, data)?;
        let pq = wallet.pq_signing_key()?.sign(data);
        Ok(HybridSignatureBytes::new(classical, pq))
    }

    // === Nonce ===

    fn next_nonce(&self, address: &Address) -> Nonce {
        self.nonces.next_nonce(address)
    }

    fn peek_nonce(&self, address: &Address) -> Nonce {
        self.nonces.peek_nonce(address)
    }

    // === History ===

    async fn get_history(&self, address: &Address) -> Result<Vec<TxRecord>> {
        Ok(self.history.get_history(address))
    }

    async fn get_filtered_history(
        &self,
        address: &Address,
        filter: &HistoryFilter,
    ) -> Result<Vec<TxRecord>> {
        Ok(self.history.get_filtered(address, filter))
    }

    async fn get_transaction(&self, tx_hash: &Hash) -> Result<Option<TxRecord>> {
        Ok(self.history.get_by_hash(tx_hash))
    }

    // === Contacts ===

    async fn add_contact(&self, contact: Contact) -> Result<()> {
        self.contacts.add_contact(contact)
    }

    async fn get_contact(&self, address: &Address) -> Result<Option<Contact>> {
        Ok(self.contacts.get_by_address(address))
    }

    async fn resolve_contact(&self, name: &str) -> Result<Option<Address>> {
        Ok(self.contacts.resolve_name(name))
    }

    // === Assets ===

    async fn supported_assets(&self) -> Vec<AssetId> {
        self.assets
            .list_assets()
            .iter()
            .map(|a| a.asset_id.clone())
            .collect()
    }

    async fn add_asset_support(&self, wallet_id: &WalletId, asset_id: AssetId) -> Result<()> {
        if !self.assets.is_supported(&asset_id) {
            return Err(WalletError::AssetNotSupported(asset_id.as_str().to_string()));
        }

        let mut wallet = self
            .get_wallet_internal(wallet_id)
            .await?
            .ok_or_else(|| WalletError::WalletNotFound(wallet_id.to_string()))?;

        wallet.add_supported_asset(asset_id.clone());

        self.balances.set_balance(
            &wallet.address,
            &asset_id,
            Balance::zero(),
        );

        self.wallets.insert(wallet_id.clone(), wallet);
        Ok(())
    }

    async fn remove_asset_support(&self, wallet_id: &WalletId, asset_id: &AssetId) -> Result<()> {
        let mut wallet = self
            .get_wallet_internal(wallet_id)
            .await?
            .ok_or_else(|| WalletError::WalletNotFound(wallet_id.to_string()))?;

        wallet.remove_supported_asset(asset_id);
        self.wallets.insert(wallet_id.clone(), wallet);
        Ok(())
    }

    // === Export/Import ===

    async fn export_wallet_metadata(&self, wallet_id: &WalletId) -> Result<String> {
        let wallet = self
            .get_wallet_internal(wallet_id)
            .await?
            .ok_or_else(|| WalletError::WalletNotFound(wallet_id.to_string()))?;

        wallet.to_metadata_json()
    }

    async fn import_wallet_metadata(&self, metadata: &str) -> Result<WalletId> {
        let wallet = MpcWallet::from_metadata_json(metadata)?;
        let wallet_id = wallet.wallet_id.clone();
        self.wallets.insert(wallet_id.clone(), wallet);
        Ok(wallet_id)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::history::TxStatus;
    use tempfile::TempDir;
    use tenzro_crypto::pq::MlDsaSigningKey;
    use tenzro_types::primitives::ChainId;
    use tenzro_types::transaction::TransactionType;

    fn pq_pk() -> Vec<u8> {
        MlDsaSigningKey::generate().verifying_key_bytes().to_vec()
    }

    fn test_config() -> (TempDir, WalletServiceConfig) {
        let temp_dir = TempDir::new().unwrap();
        let config = WalletServiceConfig::new(temp_dir.path().to_path_buf())
            .with_default_password("test-password".to_string());
        (temp_dir, config)
    }

    #[tokio::test]
    async fn test_provision_wallet() {
        let (_dir, config) = test_config();
        let service = TenzroWalletService::with_config(config).unwrap();
        let wallet = service.provision_wallet().await.unwrap();

        assert!(wallet.can_sign());
        assert!(!wallet.supported_assets.is_empty());
    }

    #[tokio::test]
    async fn test_get_wallet() {
        let (_dir, config) = test_config();
        let service = TenzroWalletService::with_config(config).unwrap();
        let wallet = service.provision_wallet().await.unwrap();

        let retrieved = service.get_wallet(&wallet.wallet_id).await.unwrap();
        assert!(retrieved.is_some());
        assert_eq!(retrieved.unwrap().wallet_id, wallet.wallet_id);
    }

    #[tokio::test]
    async fn test_balance_query() {
        let (_dir, config) = test_config();
        let service = TenzroWalletService::with_config(config).unwrap();
        let wallet = service.provision_wallet().await.unwrap();

        let balance = service
            .get_balance(&wallet.address, &AssetId::tnzo())
            .await
            .unwrap();

        assert_eq!(balance.available, 0);
        assert_eq!(balance.spendable(), 0);
    }

    #[tokio::test]
    async fn test_sign_validated_transaction() {
        let (_dir, config) = test_config();
        let service = TenzroWalletService::with_config(config).unwrap();
        let wallet = service.provision_wallet().await.unwrap();

        let tx = Transaction::new(
            ChainId(1337),
            wallet.address,
            Address::new([2u8; 32]),
            Nonce(0),
            TransactionType::Transfer { amount: 1000 },
            21_000,
            1_000_000_000,
            wallet.pq_verifying_key_bytes(),
        );

        let signature = service
            .sign_transaction(&wallet.wallet_id, &tx)
            .await
            .unwrap();

        assert!(!signature.classical.is_empty());
        assert_eq!(signature.pq.len(), 3309);

        // Verify history was recorded
        let history = service.get_history(&wallet.address).await.unwrap();
        assert_eq!(history.len(), 1);
        assert_eq!(history[0].status, TxStatus::Created);
    }

    #[tokio::test]
    async fn test_sign_invalid_transaction_fails() {
        let (_dir, config) = test_config();
        let service = TenzroWalletService::with_config(config).unwrap();
        let wallet = service.provision_wallet().await.unwrap();

        // Wrong chain ID
        let tx = Transaction::new(
            ChainId(999),
            wallet.address,
            Address::new([2u8; 32]),
            Nonce(0),
            TransactionType::Transfer { amount: 1000 },
            21_000,
            1_000_000_000,
            pq_pk(),
        );

        let result = service.sign_transaction(&wallet.wallet_id, &tx).await;
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("chain ID"));
    }

    #[tokio::test]
    async fn test_sign_wrong_sender_fails() {
        let (_dir, config) = test_config();
        let service = TenzroWalletService::with_config(config).unwrap();
        let wallet = service.provision_wallet().await.unwrap();

        // Sender doesn't match wallet
        let tx = Transaction::new(
            ChainId(1337),
            Address::new([99u8; 32]),
            Address::new([2u8; 32]),
            Nonce(0),
            TransactionType::Transfer { amount: 1000 },
            21_000,
            1_000_000_000,
            pq_pk(),
        );

        let result = service.sign_transaction(&wallet.wallet_id, &tx).await;
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("does not match"));
    }

    #[tokio::test]
    async fn test_nonce_management() {
        let (_dir, config) = test_config();
        let service = TenzroWalletService::with_config(config).unwrap();
        let wallet = service.provision_wallet().await.unwrap();

        assert_eq!(service.peek_nonce(&wallet.address), Nonce(0));
        assert_eq!(service.next_nonce(&wallet.address), Nonce(0));
        assert_eq!(service.next_nonce(&wallet.address), Nonce(1));
        assert_eq!(service.peek_nonce(&wallet.address), Nonce(2));
    }

    #[tokio::test]
    async fn test_contact_management() {
        let (_dir, config) = test_config();
        let service = TenzroWalletService::with_config(config).unwrap();

        let contact = Contact::new(
            "Alice".to_string(),
            Address::new([1u8; 32]),
        );

        service.add_contact(contact).await.unwrap();

        let resolved = service.resolve_contact("Alice").await.unwrap();
        assert!(resolved.is_some());
        assert_eq!(resolved.unwrap(), Address::new([1u8; 32]));

        let contact = service
            .get_contact(&Address::new([1u8; 32]))
            .await
            .unwrap();
        assert!(contact.is_some());
    }

    #[tokio::test]
    async fn test_list_wallets() {
        let (_dir, config) = test_config();
        let service = TenzroWalletService::with_config(config).unwrap();

        let wallet1 = service.provision_wallet().await.unwrap();
        let wallet2 = service.provision_wallet().await.unwrap();

        let wallets = service.list_wallets().await.unwrap();
        assert_eq!(wallets.len(), 2);
        assert!(wallets.contains(&wallet1.wallet_id));
        assert!(wallets.contains(&wallet2.wallet_id));
    }

    #[tokio::test]
    async fn test_delete_wallet() {
        let (_dir, config) = test_config();
        let service = TenzroWalletService::with_config(config).unwrap();
        let wallet = service.provision_wallet().await.unwrap();

        service.delete_wallet(&wallet.wallet_id).await.unwrap();

        let retrieved = service.get_wallet(&wallet.wallet_id).await.unwrap();
        assert!(retrieved.is_none());
    }

    #[tokio::test]
    async fn test_asset_support() {
        let (_dir, config) = test_config();
        let service = TenzroWalletService::with_config(config).unwrap();
        let wallet = service.provision_wallet().await.unwrap();

        let new_asset = AssetId::from("TEST");
        service
            .asset_manager()
            .register_asset(tenzro_types::AssetInfo::new(
                new_asset.clone(),
                "Test Token".to_string(),
                "TEST".to_string(),
                18,
                1000000,
                tenzro_types::AssetType::Fungible,
            ));

        service
            .add_asset_support(&wallet.wallet_id, new_asset.clone())
            .await
            .unwrap();

        let updated_wallet = service.get_wallet(&wallet.wallet_id).await.unwrap().unwrap();
        assert!(updated_wallet.supports_asset(&new_asset));
    }

    #[tokio::test]
    async fn test_sign_data() {
        let (_dir, config) = test_config();
        let service = TenzroWalletService::with_config(config).unwrap();
        let wallet = service.provision_wallet().await.unwrap();

        let data = b"arbitrary data to sign";
        let signature = service.sign_data(&wallet.wallet_id, data).await.unwrap();
        assert!(!signature.classical.is_empty());
        assert_eq!(signature.pq.len(), 3309);
    }
}