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

use blockchain::blockchain::Blockchain;
use tx::tx::Transaction;
use std::collections::HashMap;
use hex::encode;

impl Blockchain {
    pub fn sign_transaction(self, _tx: Transaction, _priv_key: Vec<u8>) {
        println!("sign transaction....");
        let mut _prev_txs:HashMap<String, Transaction> = HashMap::new();
        for _vin in _tx.vin.to_owned() {
            let _prev_tx = self.to_owned().find_transaction(_vin.txid);
            _prev_txs.insert(encode(_prev_tx.to_owned().id), _prev_tx);
        }

        _tx.sign(_prev_txs, _priv_key);
    }

    pub fn verify_transaction(self, _tx: Transaction) -> bool {
        println!("verify transaction....");
        let mut _prev_txs:HashMap<String, Transaction> = HashMap::new();
        for _vin in _tx.vin.to_owned() {
            let _prev_tx = self.to_owned().find_transaction(_vin.txid);
            _prev_txs.insert(encode(_prev_tx.to_owned().id), _prev_tx);
        }
        return _tx.verify(_prev_txs);
    }
}