Skip to main content

tenzro_types/
wallet.rs

1//! Wallet types for Tenzro Network
2//!
3//! This module defines types for wallet management, key storage,
4//! and account access.
5
6use crate::primitives::{Address, Timestamp};
7use serde::{Deserialize, Serialize};
8
9/// Information about a wallet on Tenzro Network
10#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
11pub struct WalletInfo {
12    /// Wallet identifier
13    pub wallet_id: String,
14    /// Wallet type
15    pub wallet_type: WalletType,
16    /// Accounts associated with this wallet
17    pub accounts: Vec<Address>,
18    /// Wallet label/name
19    pub label: Option<String>,
20    /// Wallet creation timestamp
21    pub created_at: Timestamp,
22    /// Last access timestamp
23    pub last_accessed: Option<Timestamp>,
24}
25
26impl WalletInfo {
27    /// Creates a new wallet info
28    pub fn new(wallet_id: String, wallet_type: WalletType) -> Self {
29        Self {
30            wallet_id,
31            wallet_type,
32            accounts: Vec::new(),
33            label: None,
34            created_at: Timestamp::now(),
35            last_accessed: None,
36        }
37    }
38
39    /// Adds a label to the wallet
40    pub fn with_label(mut self, label: String) -> Self {
41        self.label = Some(label);
42        self
43    }
44
45    /// Adds an account to the wallet
46    pub fn add_account(&mut self, address: Address) {
47        if !self.accounts.contains(&address) {
48            self.accounts.push(address);
49        }
50    }
51
52    /// Updates the last accessed timestamp
53    pub fn update_last_accessed(&mut self) {
54        self.last_accessed = Some(Timestamp::now());
55    }
56
57    /// Returns the number of accounts in the wallet
58    pub fn account_count(&self) -> usize {
59        self.accounts.len()
60    }
61}
62
63/// Type of wallet
64#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
65pub enum WalletType {
66    /// HD (Hierarchical Deterministic) wallet
67    HD,
68    /// Single-key wallet
69    SingleKey,
70    /// Multi-signature wallet
71    MultiSig,
72    /// Hardware wallet
73    Hardware,
74    /// Smart contract wallet
75    SmartContract,
76    /// Custodial wallet
77    Custodial,
78}
79
80impl WalletType {
81    /// Returns the wallet type name
82    pub fn as_str(&self) -> &str {
83        match self {
84            Self::HD => "HD",
85            Self::SingleKey => "Single Key",
86            Self::MultiSig => "Multi-Signature",
87            Self::Hardware => "Hardware",
88            Self::SmartContract => "Smart Contract",
89            Self::Custodial => "Custodial",
90        }
91    }
92
93    /// Checks if this wallet type supports multiple accounts
94    pub fn supports_multiple_accounts(&self) -> bool {
95        matches!(self, Self::HD | Self::MultiSig | Self::SmartContract | Self::Custodial)
96    }
97}
98
99/// Multi-signature wallet configuration
100#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
101pub struct MultiSigConfig {
102    /// Required number of signatures
103    pub threshold: u32,
104    /// Total number of signers
105    pub total_signers: u32,
106    /// Signer addresses
107    pub signers: Vec<Address>,
108}
109
110impl MultiSigConfig {
111    /// Creates a new multi-sig configuration
112    pub fn new(threshold: u32, signers: Vec<Address>) -> Self {
113        Self {
114            threshold,
115            total_signers: signers.len() as u32,
116            signers,
117        }
118    }
119
120    /// Validates the configuration
121    pub fn is_valid(&self) -> bool {
122        self.threshold > 0
123            && self.threshold <= self.total_signers
124            && self.signers.len() == self.total_signers as usize
125    }
126
127    /// Checks if an address is a signer
128    pub fn is_signer(&self, address: &Address) -> bool {
129        self.signers.contains(address)
130    }
131}