block_chain/
wallet.rs

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
//!
//! This is the Wallet module.<br>
//! This contains the Wallet and Transaction Struct.
//!

use serde::{Deserialize, Serialize};
use std::collections::VecDeque;

///
/// Struct Wallet.<br>
/// It includes address, keys, <br>
/// balance and transaction history.
///
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Wallet {
    pub address: String,
    pub private_key: String,
    pub public_key: String,
    pub balance: f64,
    pub transaction_history: VecDeque<Transaction>,
}

///
/// Struct Transaction.<br>
/// It includes tx_id, sender, <br>
/// receiver, amount and time stamp.
///
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Transaction {
    pub tx_id: String,
    pub sender: String,
    pub receiver: String,
    pub amount: f64,
    pub timestamp: u64,
}

///
/// Functions for Struct Wallet.
///
impl Wallet {
    pub fn new(address: String, private_key: String, public_key: String) -> Self {
        Wallet {
            address,
            private_key,
            public_key,
            balance: 0.0,
            transaction_history: VecDeque::new(),
        }
    }

    ///
    /// Process a new transaction.
    ///
    pub fn add_transaction(&mut self, tx: Transaction) {
        self.transaction_history.push_back(tx);
    }

    ///
    /// Update wallet balance.
    ///
    pub fn update_balance(&mut self, amount: f64) {
        self.balance += amount;
    }

    ///
    /// Get wallet balance.
    ///
    pub fn get_balance(&self) -> f64 {
        self.balance
    }

    ///
    /// Get the transaction history of the wallet.
    ///
    pub fn get_transaction_history(&self) -> &VecDeque<Transaction> {
        &self.transaction_history
    }

    ///
    /// Load wallet data from database.
    ///
    pub fn load_from_file(filename: &str) -> Wallet {
        let data = std::fs::read_to_string(filename).expect("Unable to read file");
        serde_json::from_str(&data).expect("Unable to parse JSON")
    }
}