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 "Transaction.h"
8

9
#include "../Ethereum/RLP.h"
10

11
using namespace TW;
12
using namespace TW::Theta;
13
using RLP = Ethereum::RLP;
14

15 1
Data encode(const Coins& coins) noexcept {
16 1
    auto encoded = Data();
17 1
    append(encoded, RLP::encode(coins.thetaWei));
18 1
    append(encoded, RLP::encode(coins.tfuelWei));
19 1
    return RLP::encodeList(encoded);
20
}
21

22 1
Data encode(const TxInput& input) noexcept {
23 1
    auto encoded = Data();
24 1
    append(encoded, RLP::encode(input.address.bytes));
25 1
    append(encoded, encode(input.coins));
26 1
    append(encoded, RLP::encode(input.sequence));
27 1
    append(encoded, RLP::encode(input.signature));
28 1
    return RLP::encodeList(encoded);
29
}
30

31 1
Data encode(const std::vector<TxInput>& inputs) noexcept {
32 1
    auto encoded = Data();
33 1
    for (const auto& input : inputs) {
34 1
        append(encoded, encode(input));
35
    }
36 1
    return RLP::encodeList(encoded);
37
}
38

39 1
Data encode(const TxOutput& output) noexcept {
40 1
    auto encoded = Data();
41 1
    append(encoded, RLP::encode(output.address.bytes));
42 1
    append(encoded, encode(output.coins));
43 1
    return RLP::encodeList(encoded);
44
}
45

46 1
Data encode(const std::vector<TxOutput>& outputs) noexcept {
47 1
    auto encoded = Data();
48 1
    for (const auto& output : outputs) {
49 1
        append(encoded, encode(output));
50
    }
51 1
    return RLP::encodeList(encoded);
52
}
53

54 1
Transaction::Transaction(Ethereum::Address from, Ethereum::Address to,
55
                         uint256_t thetaAmount, uint256_t tfuelAmount,
56
                         uint64_t sequence, uint256_t feeAmount /* = 1000000000000*/) {
57 1
    auto fee = Coins(0, feeAmount);
58 1
    auto coinsInput = Coins(thetaAmount, tfuelAmount + feeAmount);
59 1
    auto coinsOutput = Coins(thetaAmount, tfuelAmount);
60 1
    auto input = TxInput(std::move(from), coinsInput, sequence);
61 1
    auto output = TxOutput(std::move(to), coinsOutput);
62

63 1
    this->fee = fee;
64 1
    this->inputs.push_back(input);
65 1
    this->outputs.push_back(output);
66
}
67

68 1
Data Transaction::encode() const noexcept {
69 1
    auto encoded = Data();
70 1
    uint16_t txType = 2; // TxSend
71 1
    append(encoded, RLP::encode(txType));
72 1
    auto encodedData = Data();
73 1
    append(encodedData, ::encode(fee));
74 1
    append(encodedData, ::encode(inputs));
75 1
    append(encodedData, ::encode(outputs));
76 1
    append(encoded, RLP::encodeList(encodedData));
77 1
    return encoded;
78
}
79

80 1
bool Transaction::setSignature(const Ethereum::Address& address, const Data& signature) noexcept {
81 1
    for (auto& input : inputs) {
82 1
        if (input.address == address) {
83 1
            input.signature = signature;
84 1
            return true;
85
        }
86
    }
87 0
    return false;
88
}

Read our documentation on viewing source code .

Loading