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 "BinaryCoding.h"
8
#include "Transaction.h"
9
#include "../BinaryCoding.h"
10
#include "../HexCoding.h"
11

12
using namespace TW;
13
using namespace TW::Ripple;
14

15
const int NETWORK_PREFIX = 0x53545800;
16

17 1
Data Transaction::serialize() const {
18 1
    auto data = Data();
19
    /// field must be sorted by field type then by field name
20
    /// "type"
21 1
    encodeType(FieldType::int16, 2, data);
22 1
    encode16BE(uint16_t(TransactionType::payment), data);
23
    /// "flags"
24 1
    encodeType(FieldType::int32, 2, data);
25 1
    encode32BE(static_cast<uint32_t>(flags), data);
26
    /// "sequence"
27 1
    encodeType(FieldType::int32, 4, data);
28 1
    encode32BE(sequence, data);
29
    /// "destinationTag"
30 1
    if (encode_tag) {
31 1
        encodeType(FieldType::int32, 14, data);
32 1
        encode32BE(static_cast<uint32_t>(destination_tag), data);
33
    }
34
    /// "lastLedgerSequence"
35 1
    if (last_ledger_sequence > 0) {
36 0
        encodeType(FieldType::int32, 27, data);
37 0
        encode32BE(last_ledger_sequence, data);
38
    }
39
    /// "amount"
40 1
    encodeType(FieldType::amount, 1, data);
41 1
    append(data, serializeAmount(amount));
42
    /// "fee"
43 1
    encodeType(FieldType::amount, 8, data);
44 1
    append(data, serializeAmount(fee));
45
    /// "signingPubKey"
46 1
    if (!pub_key.empty()) {
47 1
        encodeType(FieldType::vl, 3, data);
48 1
        encodeBytes(pub_key, data);
49
    }
50
    /// "txnSignature"
51 1
    if (!signature.empty()) {
52 1
        encodeType(FieldType::vl, 4, data);
53 1
        encodeBytes(signature, data);
54
    }
55
    /// "account"
56 1
    encodeType(FieldType::account, 1, data);
57 1
    encodeBytes(serializeAddress(account), data);
58
    /// "destination"
59 1
    encodeType(FieldType::account, 3, data);
60 1
    encodeBytes(destination, data);
61 1
    return data;
62
}
63

64 1
Data Transaction::getPreImage() const {
65 1
    auto preImage = Data();
66 1
    encode32BE(NETWORK_PREFIX, preImage);
67 1
    append(preImage, serialize());
68 1
    return preImage;
69
}
70

71 1
Data Transaction::serializeAmount(int64_t amount) {
72 1
    if (amount < 0) {
73 1
        return Data();
74
    }
75 1
    auto data = Data();
76 1
    encode64BE(uint64_t(amount), data);
77
    /// clear first bit to indicate XRP
78 1
    data[0] &= 0x7F;
79
    /// set second bit to indicate positive number
80 1
    data[0] |= 0x40;
81 1
    return data;
82
}
83

84 1
Data Transaction::serializeAddress(Address address) {
85 1
    auto data = Data(&address.bytes[0] + 1, &address.bytes[21]);
86 1
    return data;
87
}

Read our documentation on viewing source code .

Loading