1
// Copyright © 2017-2020 Trust Wallet.
2
//
3
// This file is part of Trust. The full Trust copyright notice, including
4
// terms governing use, modification, and redistribution, is contained in the
5
// file LICENSE at the root of the source code distribution tree.
6

7
#include <ctype.h>
8

9
#include "../uint256.h"
10
#include "../Data.h"
11
#include "../Hash.h"
12
#include "Transaction.h"
13
#include "MinerTransaction.h"
14

15
using namespace std;
16

17
using namespace TW;
18
using namespace TW::NEO;
19

20 1
int64_t Transaction::size() const {
21 1
    return serialize().size();
22
}
23

24 1
void Transaction::deserialize(const Data& data, int initial_pos) {
25 1
    type = (TransactionType) data[initial_pos++];
26 1
    version = data[initial_pos++];
27 1
    initial_pos = deserializeExclusiveData(data, initial_pos);
28 1
    attributes.clear();
29 1
    initial_pos = Serializable::deserialize<TransactionAttribute>(attributes, data, initial_pos);
30 1
    inInputs.clear();
31 1
    initial_pos = Serializable::deserialize<CoinReference>(inInputs, data, initial_pos);
32 1
    outputs.clear();
33 1
    Serializable::deserialize<TransactionOutput>(outputs, data, initial_pos);
34
}
35

36 1
Transaction * Transaction::deserializeFrom(const Data& data, int initial_pos) {
37 1
    Transaction * resp = nullptr;
38 1
    switch ((TransactionType) data[initial_pos]) {
39
        case TransactionType::TT_MinerTransaction:
40 1
            resp = new MinerTransaction();
41 1
            break;
42
        default:
43 1
            throw std::invalid_argument("Transaction::deserializeFrom Invalid transaction type");
44
            break;
45
    }
46 1
    resp->deserialize(data, initial_pos);
47 1
    return resp;
48
}
49

50 1
Data Transaction::serialize() const {
51 1
    Data resp;
52 1
    resp.push_back((byte) type);
53 1
    resp.push_back(version);
54 1
    append(resp, serializeExclusiveData());
55

56 1
    append(resp, Serializable::serialize(attributes));
57 1
    append(resp, Serializable::serialize(inInputs));
58 1
    append(resp, Serializable::serialize(outputs));
59 1
    if(witnesses.size())
60
	 {
61 1
		resp.push_back((byte) witnesses.size());
62 1
		for (int i = 0; i < witnesses.size(); i++)
63 1
		  append(resp, witnesses[i].serialize());
64
	 }
65

66 1
    return resp;
67
}
68

69 1
bool Transaction::operator==(const Transaction &other) const {
70 1
    if (this == &other) {
71 1
        return true;
72
    }
73 1
    return this->type == other.type
74 1
        && this->version == other.version
75 1
        && this->attributes.size() == other.attributes.size()
76 1
        && this->inInputs.size() == other.inInputs.size()
77 1
        && this->outputs.size() == other.outputs.size()
78 1
        && this->attributes == other.attributes
79 1
        && this->inInputs == other.inInputs
80 1
        && this->outputs == other.outputs;
81
}
82

83 1
Data Transaction::getHash() const {
84 1
    return Hash::sha256(Hash::sha256(serialize()));
85
}
86

87 0
uint256_t Transaction::getHashUInt256() const {
88 0
    return load(getHash());
89
}

Read our documentation on viewing source code .

Loading