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
//! Transaction history tracking for Tenzro Network wallets.
//!
//! Tracks all transactions sent and received by wallet addresses,
//! their confirmation status, and provides query/filter capabilities.

use crate::error::{Result, WalletError};
use dashmap::DashMap;
use serde::{Deserialize, Serialize};
use std::sync::atomic::{AtomicU64, Ordering};
use tenzro_types::primitives::{Address, Hash, Nonce, Timestamp};
use tenzro_types::transaction::TransactionType;

/// Transaction status in the lifecycle.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum TxStatus {
    /// Transaction created but not yet submitted
    Created,
    /// Transaction submitted to mempool
    Pending,
    /// Transaction included in a block
    Confirmed,
    /// Transaction reached finality
    Finalized,
    /// Transaction failed during execution
    Failed,
    /// Transaction was replaced (by higher gas price)
    Replaced,
    /// Transaction was dropped from mempool
    Dropped,
}

impl std::fmt::Display for TxStatus {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            TxStatus::Created => write!(f, "created"),
            TxStatus::Pending => write!(f, "pending"),
            TxStatus::Confirmed => write!(f, "confirmed"),
            TxStatus::Finalized => write!(f, "finalized"),
            TxStatus::Failed => write!(f, "failed"),
            TxStatus::Replaced => write!(f, "replaced"),
            TxStatus::Dropped => write!(f, "dropped"),
        }
    }
}

/// Direction of a transaction relative to a wallet.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum TxDirection {
    /// Transaction sent from this wallet
    Outgoing,
    /// Transaction received by this wallet
    Incoming,
    /// Self-transaction (e.g., contract interaction from own wallet)
    Internal,
}

/// A recorded transaction in the history.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TxRecord {
    /// Transaction hash
    pub tx_hash: Hash,
    /// Sender address
    pub from: Address,
    /// Recipient address
    pub to: Address,
    /// Transaction nonce
    pub nonce: Nonce,
    /// Transaction type
    pub tx_type: TransactionType,
    /// Gas limit set
    pub gas_limit: u64,
    /// Gas price set
    pub gas_price: u64,
    /// Gas actually used (set after confirmation)
    pub gas_used: Option<u64>,
    /// Current status
    pub status: TxStatus,
    /// Direction relative to the tracking wallet
    pub direction: TxDirection,
    /// Block height where included (set after confirmation)
    pub block_height: Option<u64>,
    /// Timestamp when the transaction was created
    pub created_at: Timestamp,
    /// Timestamp when confirmed (set after confirmation)
    pub confirmed_at: Option<Timestamp>,
    /// Optional error message (set on failure)
    pub error: Option<String>,
    /// Optional memo from the transaction
    pub memo: Option<String>,
}

impl TxRecord {
    /// Create a new outgoing transaction record.
    pub fn new_outgoing(
        tx_hash: Hash,
        from: Address,
        to: Address,
        nonce: Nonce,
        tx_type: TransactionType,
        gas_limit: u64,
        gas_price: u64,
        memo: Option<String>,
    ) -> Self {
        Self {
            tx_hash,
            from,
            to,
            nonce,
            tx_type,
            gas_limit,
            gas_price,
            gas_used: None,
            status: TxStatus::Created,
            direction: TxDirection::Outgoing,
            block_height: None,
            created_at: Timestamp::now(),
            confirmed_at: None,
            error: None,
            memo,
        }
    }

    /// Create a new incoming transaction record.
    pub fn new_incoming(
        tx_hash: Hash,
        from: Address,
        to: Address,
        nonce: Nonce,
        tx_type: TransactionType,
        gas_limit: u64,
        gas_price: u64,
        block_height: u64,
    ) -> Self {
        Self {
            tx_hash,
            from,
            to,
            nonce,
            tx_type,
            gas_limit,
            gas_price,
            gas_used: None,
            status: TxStatus::Confirmed,
            direction: TxDirection::Incoming,
            block_height: Some(block_height),
            created_at: Timestamp::now(),
            confirmed_at: Some(Timestamp::now()),
            error: None,
            memo: None,
        }
    }

    /// Mark the transaction as submitted to mempool.
    pub fn mark_pending(&mut self) {
        self.status = TxStatus::Pending;
    }

    /// Mark the transaction as confirmed in a block.
    pub fn mark_confirmed(&mut self, block_height: u64, gas_used: u64) {
        self.status = TxStatus::Confirmed;
        self.block_height = Some(block_height);
        self.gas_used = Some(gas_used);
        self.confirmed_at = Some(Timestamp::now());
    }

    /// Mark the transaction as finalized.
    pub fn mark_finalized(&mut self) {
        self.status = TxStatus::Finalized;
    }

    /// Mark the transaction as failed.
    pub fn mark_failed(&mut self, error: String) {
        self.status = TxStatus::Failed;
        self.error = Some(error);
    }

    /// Mark the transaction as dropped from mempool.
    pub fn mark_dropped(&mut self) {
        self.status = TxStatus::Dropped;
    }

    /// Check if the transaction is still pending.
    pub fn is_pending(&self) -> bool {
        matches!(self.status, TxStatus::Created | TxStatus::Pending)
    }

    /// Check if the transaction is confirmed or finalized.
    pub fn is_confirmed(&self) -> bool {
        matches!(self.status, TxStatus::Confirmed | TxStatus::Finalized)
    }

    /// Get the effective gas cost (gas_used * gas_price).
    pub fn gas_cost(&self) -> Option<u128> {
        self.gas_used
            .map(|used| (used as u128) * (self.gas_price as u128))
    }

    /// Get the transfer value if this is a transfer-type transaction.
    pub fn value(&self) -> u128 {
        match &self.tx_type {
            TransactionType::Transfer { amount } => *amount,
            TransactionType::ProviderStake { amount, .. } => *amount,
            TransactionType::ProviderUnstake { amount } => *amount,
            TransactionType::BridgeTransfer { amount, .. } => *amount,
            _ => 0,
        }
    }
}

/// Query filter for transaction history.
#[derive(Debug, Clone, Default)]
pub struct HistoryFilter {
    /// Filter by status
    pub status: Option<TxStatus>,
    /// Filter by direction
    pub direction: Option<TxDirection>,
    /// Filter by minimum timestamp
    pub after: Option<Timestamp>,
    /// Filter by maximum timestamp
    pub before: Option<Timestamp>,
    /// Maximum number of results
    pub limit: Option<usize>,
    /// Offset for pagination
    pub offset: usize,
}

impl HistoryFilter {
    /// Create a new filter for pending transactions.
    pub fn pending() -> Self {
        Self {
            status: Some(TxStatus::Pending),
            ..Default::default()
        }
    }

    /// Create a new filter for confirmed transactions.
    pub fn confirmed() -> Self {
        Self {
            status: Some(TxStatus::Confirmed),
            ..Default::default()
        }
    }

    /// Create a new filter for outgoing transactions.
    pub fn outgoing() -> Self {
        Self {
            direction: Some(TxDirection::Outgoing),
            ..Default::default()
        }
    }

    /// Create a new filter for incoming transactions.
    pub fn incoming() -> Self {
        Self {
            direction: Some(TxDirection::Incoming),
            ..Default::default()
        }
    }

    /// Set the result limit.
    pub fn with_limit(mut self, limit: usize) -> Self {
        self.limit = Some(limit);
        self
    }

    /// Set the offset for pagination.
    pub fn with_offset(mut self, offset: usize) -> Self {
        self.offset = offset;
        self
    }
}

/// Transaction history tracker for wallet addresses.
///
/// Stores transaction records indexed by address and provides
/// query/filter capabilities for retrieving history.
pub struct TransactionHistory {
    /// Records indexed by address → list of records
    records: DashMap<Address, Vec<TxRecord>>,
    /// Index by transaction hash for quick lookups
    hash_index: DashMap<Hash, (Address, usize)>,
    /// Total record count
    total_count: AtomicU64,
}

impl TransactionHistory {
    /// Create a new transaction history tracker.
    pub fn new() -> Self {
        Self {
            records: DashMap::new(),
            hash_index: DashMap::new(),
            total_count: AtomicU64::new(0),
        }
    }

    /// Record a new transaction.
    pub fn record(&self, address: &Address, record: TxRecord) {
        let tx_hash = record.tx_hash;
        let mut entries = self.records.entry(*address).or_default();
        let idx = entries.len();
        entries.push(record);
        self.hash_index.insert(tx_hash, (*address, idx));
        self.total_count.fetch_add(1, Ordering::Relaxed);
    }

    /// Get a transaction record by hash.
    pub fn get_by_hash(&self, tx_hash: &Hash) -> Option<TxRecord> {
        if let Some(entry) = self.hash_index.get(tx_hash) {
            let (address, idx) = entry.value();
            if let Some(records) = self.records.get(address) {
                return records.get(*idx).cloned();
            }
        }
        None
    }

    /// Update the status of a transaction by hash.
    pub fn update_status(&self, tx_hash: &Hash, status: TxStatus) -> Result<()> {
        if let Some(entry) = self.hash_index.get(tx_hash) {
            let (address, idx) = entry.value();
            if let Some(mut records) = self.records.get_mut(address)
                && let Some(record) = records.get_mut(*idx)
            {
                record.status = status;
                return Ok(());
            }
        }
        Err(WalletError::Other(format!(
            "Transaction {} not found in history",
            tx_hash
        )))
    }

    /// Mark a transaction as confirmed.
    pub fn confirm(&self, tx_hash: &Hash, block_height: u64, gas_used: u64) -> Result<()> {
        if let Some(entry) = self.hash_index.get(tx_hash) {
            let (address, idx) = entry.value();
            if let Some(mut records) = self.records.get_mut(address)
                && let Some(record) = records.get_mut(*idx)
            {
                record.mark_confirmed(block_height, gas_used);
                return Ok(());
            }
        }
        Err(WalletError::Other(format!(
            "Transaction {} not found in history",
            tx_hash
        )))
    }

    /// Get all records for an address.
    pub fn get_history(&self, address: &Address) -> Vec<TxRecord> {
        self.records
            .get(address)
            .map(|records| records.clone())
            .unwrap_or_default()
    }

    /// Get filtered records for an address.
    pub fn get_filtered(&self, address: &Address, filter: &HistoryFilter) -> Vec<TxRecord> {
        let records = self
            .records
            .get(address)
            .map(|r| r.clone())
            .unwrap_or_default();

        let filtered: Vec<TxRecord> = records
            .into_iter()
            .filter(|r| {
                if let Some(status) = &filter.status
                    && r.status != *status
                {
                    return false;
                }
                if let Some(direction) = &filter.direction
                    && r.direction != *direction
                {
                    return false;
                }
                if let Some(after) = &filter.after
                    && r.created_at.0 < after.0
                {
                    return false;
                }
                if let Some(before) = &filter.before
                    && r.created_at.0 > before.0
                {
                    return false;
                }
                true
            })
            .skip(filter.offset)
            .take(filter.limit.unwrap_or(usize::MAX))
            .collect();

        filtered
    }

    /// Get the number of pending transactions for an address.
    pub fn pending_count(&self, address: &Address) -> usize {
        self.records
            .get(address)
            .map(|records| records.iter().filter(|r| r.is_pending()).count())
            .unwrap_or(0)
    }

    /// Get total record count across all addresses.
    pub fn total_count(&self) -> u64 {
        self.total_count.load(Ordering::Relaxed)
    }

    /// Clear all history for an address.
    pub fn clear(&self, address: &Address) {
        if let Some((_, records)) = self.records.remove(address) {
            for record in &records {
                self.hash_index.remove(&record.tx_hash);
            }
            self.total_count
                .fetch_sub(records.len() as u64, Ordering::Relaxed);
        }
    }

    /// Clear all history.
    pub fn clear_all(&self) {
        self.records.clear();
        self.hash_index.clear();
        self.total_count.store(0, Ordering::Relaxed);
    }
}

impl Default for TransactionHistory {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use tenzro_crypto::pq::MlDsaSigningKey;
    use tenzro_types::primitives::ChainId;
    use tenzro_types::transaction::Transaction;

    fn test_tx_record(from: Address, to: Address) -> TxRecord {
        let pq_pk = MlDsaSigningKey::generate().verifying_key_bytes().to_vec();
        let tx = Transaction::new(
            ChainId(1337),
            from,
            to,
            Nonce(0),
            TransactionType::Transfer { amount: 1000 },
            21_000,
            1_000_000_000,
            pq_pk,
        );
        TxRecord::new_outgoing(
            tx.hash(),
            from,
            to,
            Nonce(0),
            TransactionType::Transfer { amount: 1000 },
            21_000,
            1_000_000_000,
            None,
        )
    }

    #[test]
    fn test_record_and_retrieve() {
        let history = TransactionHistory::new();
        let addr = Address::new([1u8; 32]);
        let to = Address::new([2u8; 32]);

        let record = test_tx_record(addr, to);
        let tx_hash = record.tx_hash;

        history.record(&addr, record);

        let retrieved = history.get_by_hash(&tx_hash).unwrap();
        assert_eq!(retrieved.tx_hash, tx_hash);
        assert_eq!(retrieved.from, addr);
        assert_eq!(retrieved.status, TxStatus::Created);
    }

    #[test]
    fn test_confirm_transaction() {
        let history = TransactionHistory::new();
        let addr = Address::new([1u8; 32]);
        let to = Address::new([2u8; 32]);

        let record = test_tx_record(addr, to);
        let tx_hash = record.tx_hash;

        history.record(&addr, record);
        history.confirm(&tx_hash, 100, 21_000).unwrap();

        let retrieved = history.get_by_hash(&tx_hash).unwrap();
        assert_eq!(retrieved.status, TxStatus::Confirmed);
        assert_eq!(retrieved.block_height, Some(100));
        assert_eq!(retrieved.gas_used, Some(21_000));
    }

    #[test]
    fn test_filter_by_status() {
        let history = TransactionHistory::new();
        let addr = Address::new([1u8; 32]);
        let to = Address::new([2u8; 32]);

        // Add 3 records
        let r1 = test_tx_record(addr, to);
        let hash1 = r1.tx_hash;
        history.record(&addr, r1);

        let r2 = test_tx_record(addr, to);
        history.record(&addr, r2);

        let r3 = test_tx_record(addr, to);
        history.record(&addr, r3);

        // Confirm first one
        history.confirm(&hash1, 100, 21_000).unwrap();

        let confirmed = history.get_filtered(&addr, &HistoryFilter::confirmed());
        assert_eq!(confirmed.len(), 1);

        // Created records still show as "Created" not "Pending"
        let all = history.get_history(&addr);
        assert_eq!(all.len(), 3);
    }

    #[test]
    fn test_pagination() {
        let history = TransactionHistory::new();
        let addr = Address::new([1u8; 32]);
        let to = Address::new([2u8; 32]);

        for _ in 0..10 {
            history.record(&addr, test_tx_record(addr, to));
        }

        let filter = HistoryFilter::default()
            .with_limit(3)
            .with_offset(2);

        let page = history.get_filtered(&addr, &filter);
        assert_eq!(page.len(), 3);
    }

    #[test]
    fn test_pending_count() {
        let history = TransactionHistory::new();
        let addr = Address::new([1u8; 32]);
        let to = Address::new([2u8; 32]);

        let r1 = test_tx_record(addr, to);
        let hash1 = r1.tx_hash;
        history.record(&addr, r1);
        history.record(&addr, test_tx_record(addr, to));

        assert_eq!(history.pending_count(&addr), 2);

        history.confirm(&hash1, 100, 21_000).unwrap();
        assert_eq!(history.pending_count(&addr), 1);
    }

    #[test]
    fn test_gas_cost() {
        let mut record = test_tx_record(Address::new([1u8; 32]), Address::new([2u8; 32]));
        assert!(record.gas_cost().is_none());

        record.mark_confirmed(100, 21_000);
        // gas_cost = 21_000 * 1_000_000_000 = 21_000_000_000_000
        assert_eq!(record.gas_cost(), Some(21_000_000_000_000u128));
    }

    #[test]
    fn test_clear_history() {
        let history = TransactionHistory::new();
        let addr = Address::new([1u8; 32]);
        let to = Address::new([2u8; 32]);

        history.record(&addr, test_tx_record(addr, to));
        history.record(&addr, test_tx_record(addr, to));
        assert_eq!(history.total_count(), 2);

        history.clear(&addr);
        assert_eq!(history.get_history(&addr).len(), 0);
        assert_eq!(history.total_count(), 0);
    }
}