Repository navigation
Expand file tree
/
Copy pathTransaction.cpp
More file actions
87 lines (80 loc) · 2.52 KB
/
Copy pathTransaction.cpp
File metadata and controls
87 lines (80 loc) · 2.52 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
// 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 "BinaryCoding.h"
#include "Transaction.h"
#include "../BinaryCoding.h"
#include "../HexCoding.h"
using namespace TW;
using namespace TW::Ripple;
const int NETWORK_PREFIX = 0x53545800;
Data Transaction::serialize() const {
auto data = Data();
/// field must be sorted by field type then by field name
/// "type"
encodeType(FieldType::int16, 2, data);
encode16BE(uint16_t(TransactionType::payment), data);
/// "flags"
encodeType(FieldType::int32, 2, data);
encode32BE(static_cast<uint32_t>(flags), data);
/// "sequence"
encodeType(FieldType::int32, 4, data);
encode32BE(sequence, data);
/// "destinationTag"
if (encode_tag) {
encodeType(FieldType::int32, 14, data);
encode32BE(static_cast<uint32_t>(destination_tag), data);
}
/// "lastLedgerSequence"
if (last_ledger_sequence > 0) {
encodeType(FieldType::int32, 27, data);
encode32BE(last_ledger_sequence, data);
}
/// "amount"
encodeType(FieldType::amount, 1, data);
append(data, serializeAmount(amount));
/// "fee"
encodeType(FieldType::amount, 8, data);
append(data, serializeAmount(fee));
/// "signingPubKey"
if (!pub_key.empty()) {
encodeType(FieldType::vl, 3, data);
encodeBytes(pub_key, data);
}
/// "txnSignature"
if (!signature.empty()) {
encodeType(FieldType::vl, 4, data);
encodeBytes(signature, data);
}
/// "account"
encodeType(FieldType::account, 1, data);
encodeBytes(serializeAddress(account), data);
/// "destination"
encodeType(FieldType::account, 3, data);
encodeBytes(destination, data);
return data;
}
Data Transaction::getPreImage() const {
auto preImage = Data();
encode32BE(NETWORK_PREFIX, preImage);
append(preImage, serialize());
return preImage;
}
Data Transaction::serializeAmount(int64_t amount) {
if (amount < 0) {
return Data();
}
auto data = Data();
encode64BE(uint64_t(amount), data);
/// clear first bit to indicate XRP
data[0] &= 0x7F;
/// set second bit to indicate positive number
data[0] |= 0x40;
return data;
}
Data Transaction::serializeAddress(Address address) {
auto data = Data(&address.bytes[0] + 1, &address.bytes[21]);
return data;
}