forked from trustwallet/wallet-core
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTransaction.cpp
More file actions
88 lines (76 loc) · 2.75 KB
/
Copy pathTransaction.cpp
File metadata and controls
88 lines (76 loc) · 2.75 KB
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
// Copyright © 2017-2020 Trust Wallet.
//
// This file is part of Trust. The full Trust copyright notice, including
// terms governing use, modification, and redistribution, is contained in the
// file LICENSE at the root of the source code distribution tree.
#include "Transaction.h"
#include "../Ethereum/RLP.h"
using namespace TW;
using namespace TW::Theta;
using RLP = Ethereum::RLP;
Data encode(const Coins& coins) noexcept {
auto encoded = Data();
append(encoded, RLP::encode(coins.thetaWei));
append(encoded, RLP::encode(coins.tfuelWei));
return RLP::encodeList(encoded);
}
Data encode(const TxInput& input) noexcept {
auto encoded = Data();
append(encoded, RLP::encode(input.address.bytes));
append(encoded, encode(input.coins));
append(encoded, RLP::encode(input.sequence));
append(encoded, RLP::encode(input.signature));
return RLP::encodeList(encoded);
}
Data encode(const std::vector<TxInput>& inputs) noexcept {
auto encoded = Data();
for (const auto& input : inputs) {
append(encoded, encode(input));
}
return RLP::encodeList(encoded);
}
Data encode(const TxOutput& output) noexcept {
auto encoded = Data();
append(encoded, RLP::encode(output.address.bytes));
append(encoded, encode(output.coins));
return RLP::encodeList(encoded);
}
Data encode(const std::vector<TxOutput>& outputs) noexcept {
auto encoded = Data();
for (const auto& output : outputs) {
append(encoded, encode(output));
}
return RLP::encodeList(encoded);
}
Transaction::Transaction(Ethereum::Address from, Ethereum::Address to,
uint256_t thetaAmount, uint256_t tfuelAmount,
uint64_t sequence, uint256_t feeAmount /* = 1000000000000*/) {
auto fee = Coins(0, feeAmount);
auto coinsInput = Coins(thetaAmount, tfuelAmount + feeAmount);
auto coinsOutput = Coins(thetaAmount, tfuelAmount);
auto input = TxInput(std::move(from), coinsInput, sequence);
auto output = TxOutput(std::move(to), coinsOutput);
this->fee = fee;
this->inputs.push_back(input);
this->outputs.push_back(output);
}
Data Transaction::encode() const noexcept {
auto encoded = Data();
uint16_t txType = 2; // TxSend
append(encoded, RLP::encode(txType));
auto encodedData = Data();
append(encodedData, ::encode(fee));
append(encodedData, ::encode(inputs));
append(encodedData, ::encode(outputs));
append(encoded, RLP::encodeList(encodedData));
return encoded;
}
bool Transaction::setSignature(const Ethereum::Address& address, const Data& signature) noexcept {
for (auto& input : inputs) {
if (input.address == address) {
input.signature = signature;
return true;
}
}
return false;
}