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
#include "Identifiers.h"
9
#include <Base58.h>
10
#include <Ethereum/RLP.h>
11
#include <Hash.h>
12

13
using namespace TW;
14
using namespace TW::Aeternity;
15

16
/// RLP returns a byte serialized representation
17 1
Data Transaction::encode() {
18 1
    auto encoded = Data();
19 1
    append(encoded, Ethereum::RLP::encode(Identifiers::objectTagSpendTransaction));
20 1
    append(encoded, Ethereum::RLP::encode(Identifiers::rlpMessageVersion));
21 1
    append(encoded, Ethereum::RLP::encode(buildTag(sender_id)));
22 1
    append(encoded, Ethereum::RLP::encode(buildTag(recipient_id)));
23 1
    append(encoded, encodeSafeZero(amount));
24 1
    append(encoded, encodeSafeZero(fee));
25 1
    append(encoded, encodeSafeZero(ttl));
26 1
    append(encoded, encodeSafeZero(nonce));
27 1
    append(encoded, Ethereum::RLP::encode(payload));
28

29 1
    const Data& raw = Ethereum::RLP::encodeList(encoded);
30 1
    return raw;
31
}
32

33 1
TW::Data Transaction::buildTag(const std::string& address) {
34 1
    auto payload = address.substr(Identifiers::prefixTransaction.size(), address.size());
35

36 1
    auto data = Data();
37 1
    append(data, Identifiers::iDTagAccount);
38 1
    append(data, Base58::bitcoin.decodeCheck(payload));
39

40 1
    return data;
41
}
42

43 1
TW::Data Transaction::encodeSafeZero(uint256_t value) {
44 1
    if (value == 0) {
45 1
        return Ethereum::RLP::encode(Data{0});
46
    } else {
47 1
        return Ethereum::RLP::encode(value);
48
    }
49
}

Read our documentation on viewing source code .

Loading