-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathTransaction.cpp
291 lines (244 loc) · 8.59 KB
/
Transaction.cpp
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// SPDX-License-Identifier: Apache-2.0
//
// Copyright © 2017 Trust Wallet.
#include "BinaryCoding.h"
#include "Transaction.h"
#include "../BinaryCoding.h"
#include <algorithm>
#include <cmath>
#include <sstream>
#include <string>
namespace TW::Ripple {
const int NETWORK_PREFIX = 0x53545800;
Data Transaction::serialize() const {
// See https://xrpl.org/serialization.html
auto data = Data();
/// fields must be sorted by field type code then by field code (key)
// https://xrpl.org/serialization.html#canonical-field-order
/// "type"
encodeType(FieldType::int16, 2, data);
encode16BE(uint16_t(transaction_type), 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 (((transaction_type == TransactionType::payment) ||
(transaction_type == TransactionType::EscrowCreate)) && encode_tag) {
encodeType(FieldType::int32, 14, data);
encode32BE(static_cast<uint32_t>(destination_tag), data);
}
/// "OfferSequence"
if ((transaction_type == TransactionType::EscrowCancel) ||
(transaction_type == TransactionType::EscrowFinish)) {
encodeType(FieldType::int32, 25, data);
encode32BE(offer_sequence, data);
}
/// "lastLedgerSequence"
if (last_ledger_sequence > 0) {
encodeType(FieldType::int32, 27, data);
encode32BE(last_ledger_sequence, data);
}
/// "CancelAfter"
if ((transaction_type == TransactionType::EscrowCreate) && cancel_after > 0) {
encodeType(FieldType::int32, 36, data);
encode32BE(static_cast<uint32_t>(cancel_after), data);
}
/// "FinishAfter"
if ((transaction_type == TransactionType::EscrowCreate) && finish_after > 0) {
encodeType(FieldType::int32, 37, data);
encode32BE(static_cast<uint32_t>(finish_after), data);
}
/// "NFTokenId"
if ((transaction_type == TransactionType::NFTokenCreateOffer) ||
(transaction_type == TransactionType::NFTokenBurn)) {
encodeType(FieldType::hash256, 10, data);
data.insert(data.end(), nftoken_id.begin(), nftoken_id.end());
}
/// "NFTokenAcceptOffer"
if (transaction_type == TransactionType::NFTokenAcceptOffer) {
encodeType(FieldType::hash256, 29, data);
data.insert(data.end(), sell_offer.begin(), sell_offer.end());
}
/// "amount"
if ((transaction_type == TransactionType::payment) ||
(transaction_type == TransactionType::NFTokenCreateOffer)) {
encodeType(FieldType::amount, 1, data);
append(data,
(!currency_amount.currency.empty()) ?
serializeCurrencyAmount(currency_amount) :
serializeAmount(amount));
} else if (transaction_type == TransactionType::TrustSet) {
encodeType(FieldType::amount, 3, data);
append(data, serializeCurrencyAmount(limit_amount));
} else if (transaction_type == TransactionType::EscrowCreate) {
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);
}
/// "Fulfillment"
if ((transaction_type == TransactionType::EscrowFinish) && !fulfillment.empty()) {
encodeType(FieldType::vl, 16, data);
encodeBytes(fulfillment, data);
}
/// "Condition"
if (((transaction_type == TransactionType::EscrowCreate) ||
(transaction_type == TransactionType::EscrowFinish)) && !condition.empty()) {
encodeType(FieldType::vl, 17, data);
encodeBytes(condition, data);
}
/// "account"
encodeType(FieldType::account, 1, data);
encodeBytes(serializeAddress(account), data);
/// "destination"
if ((transaction_type == TransactionType::payment) ||
(transaction_type == TransactionType::NFTokenCreateOffer) ||
(transaction_type == TransactionType::EscrowCreate)) {
encodeType(FieldType::account, 3, data);
encodeBytes(destination, data);
}
/// "Owner"
if ((transaction_type == TransactionType::EscrowCancel) ||
(transaction_type == TransactionType::EscrowFinish)) {
encodeType(FieldType::account, 2, data);
encodeBytes(owner, data);
}
/// "NFTokenOffers"
if (transaction_type == TransactionType::NFTokenCancelOffer) {
// only support one offer
encodeType(FieldType::vector256, 4, data);
encodeBytes(token_offers, 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::serializeCurrencyAmount(const CurrencyAmount& currency_amount) {
// Calculate value
// https://xrpl.org/serialization.html#token-amount-format
int64_t sign = 0;
int64_t mantissa = 0;
int32_t exp = 0;
try {
int32_t num_after_dot = 0;
bool after_dot = false;
bool after_e = false;
bool has_exp = false;
std::ostringstream mantissa_oss, exp_oss;
for (auto i : currency_amount.value) {
if (i == '.') {
after_dot = true;
} else if (i == 'e') {
after_dot = false;
after_e = true;
} else if (after_e) {
has_exp = true;
exp_oss << i;
} else {
mantissa_oss << i;
if (after_dot) {
num_after_dot++;
}
}
}
mantissa = std::stoll(mantissa_oss.str());
sign = (mantissa >= 0) ? 1 : 0;
mantissa = (mantissa < 0) ? (mantissa * -1) : mantissa;
exp = has_exp ? std::stoi(exp_oss.str()) : 0;
exp -= num_after_dot;
} catch (const std::exception& e) {
return Data();
}
int64_t min_mantissa = (uint64_t)std::pow(10, 15);
int64_t max_mantissa = (uint64_t)std::pow(10, 16) - 1;
int32_t min_exp = -96;
int32_t max_exp = 80;
while ((mantissa < min_mantissa) && (exp > min_exp)) {
mantissa *= 10;
exp--;
}
while (mantissa > max_mantissa) {
if (exp >= max_exp) {
return Data();
}
mantissa /= 10;
exp++;
}
if (((exp < min_exp) || (mantissa < min_mantissa)) ||
((exp > max_exp) || (mantissa > max_mantissa))) {
return Data();
}
typedef union {
uint64_t value;
struct {
uint64_t mantissa : 54;
uint64_t exp : 8;
uint64_t sign : 1;
uint64_t not_xrp : 1;
} parts;
} AmountCast;
AmountCast amount_cast;
amount_cast.parts.mantissa = mantissa;
amount_cast.parts.exp = exp + 97;
amount_cast.parts.sign = sign;
amount_cast.parts.not_xrp = 1;
// Serialize fields
// https://xrpl.org/serialization.html#amount-fields
auto data = Data();
encode64BE(amount_cast.value, data);
serializeCurrencyCode(data, currency_amount.currency);
data.insert(data.end(), currency_amount.issuer.begin(), currency_amount.issuer.end());
return data;
}
Data Transaction::serializeAddress(Address address) {
auto data = Data(20);
std::copy(&address.bytes[0] + 1, &address.bytes[0] + std::min(address.bytes.size(), size_t(21)), &data[0]);
return data;
}
void Transaction::serializeCurrencyCode(Data& out, const std::string& currency_code) {
if (currency_code.size() == 40) {
auto code_bytes = parse_hex(currency_code);
out.insert(out.end(), code_bytes.begin(), code_bytes.end());
return;
}
// Standard ISO-4217 currency code
encodeZeros(1, out); // type code (0x00)
encodeZeros(11, out); // reserved
if (currency_code.size() == 3) {
out.insert(out.end(), currency_code.begin(), currency_code.end());
} else {
encodeZeros(3, out); // none
}
encodeZeros(5, out); // reserved
}
} // namespace TW::Ripple