forked from trustwallet/wallet-core
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTransaction.cpp
More file actions
202 lines (173 loc) · 7.06 KB
/
Copy pathTransaction.cpp
File metadata and controls
202 lines (173 loc) · 7.06 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
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
// 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 "Transaction.h"
#include "BinaryCoding.h"
#include "../Base58.h"
#include "../BinaryCoding.h"
#include "../HexCoding.h"
using namespace TW;
using namespace TW::Waves;
using json = nlohmann::json;
const std::string Transaction::WAVES = "WAVES";
Data serializeTransfer(int64_t amount, std::string asset, int64_t fee, std::string fee_asset, Address to, Data attachment, int64_t timestamp, Data pub_key) {
auto data = Data();
if (asset.empty()) {
asset = Transaction::WAVES;
}
if (fee_asset.empty()) {
fee_asset = Transaction::WAVES;
}
data.resize(2);
data[0] = static_cast<byte>(TransactionType::transfer);
data[1] = static_cast<byte>(TransactionVersion::V2);
append(data, pub_key);
if (asset == Transaction::WAVES) {
data.push_back(static_cast<uint8_t>(0));
} else {
data.push_back(static_cast<uint8_t>(1));
append(data, Base58::bitcoin.decode(asset));
}
if (fee_asset == Transaction::WAVES) {
data.push_back(static_cast<uint8_t>(0));
} else {
data.push_back(static_cast<uint8_t>(1));
append(data, Base58::bitcoin.decode(fee_asset));
}
encode64BE(timestamp, data);
encode64BE(amount, data);
encode64BE(fee, data);
append(data, Data(std::begin(to.bytes), std::end(to.bytes)));
encodeDynamicLengthBytes(attachment, data);
return data;
}
Data serializeLease(int64_t amount, int64_t fee, Address to, int64_t timestamp, Data pub_key) {
auto data = Data();
data.resize(2);
data[0] = static_cast<byte>(TransactionType::lease);
data[1] = static_cast<byte>(TransactionVersion::V2);
data.push_back(static_cast<uint8_t>(0));
append(data, pub_key);
append(data, Data(std::begin(to.bytes), std::end(to.bytes)));
encode64BE(amount, data);
encode64BE(fee, data);
encode64BE(timestamp, data);
return data;
}
Data serializeCancelLease(Data leaseId, int64_t fee, int64_t timestamp, Data pub_key) {
auto data = Data();
data.resize(2);
data[0] = static_cast<byte>(TransactionType::cancelLease);
data[1] = static_cast<byte>(TransactionVersion::V2);
data.push_back(static_cast<uint8_t>(87));
append(data, pub_key);
encode64BE(fee, data);
encode64BE(timestamp, data);
append(data, leaseId);
return data;
}
json jsonTransfer(Data signature, int64_t amount, const std::string &asset, int64_t fee, const std::string &fee_asset, Address to, Data attachment, int64_t timestamp, Data pub_key) {
json jsonTx;
jsonTx["type"] = TransactionType::transfer;
jsonTx["version"] = TransactionVersion::V2;
jsonTx["fee"] = fee;
jsonTx["senderPublicKey"] = Base58::bitcoin.encode(pub_key);
jsonTx["timestamp"] = timestamp;
jsonTx["proofs"] = json::array({Base58::bitcoin.encode(signature)});
jsonTx["recipient"] = Address(to).string();
if (asset != Transaction::WAVES) {
jsonTx["assetId"] = asset;
}
if (fee_asset != Transaction::WAVES) {
jsonTx["feeAssetId"] = fee_asset;
}
jsonTx["amount"] = amount;
jsonTx["attachment"] = Base58::bitcoin.encode(attachment);
return jsonTx;
}
json jsonLease(Data signature, int64_t amount, int64_t fee, Address to, int64_t timestamp, Data pub_key) {
json jsonTx;
jsonTx["type"] = TransactionType::lease;
jsonTx["version"] = TransactionVersion::V2;
jsonTx["fee"] = fee;
jsonTx["senderPublicKey"] = Base58::bitcoin.encode(pub_key);
jsonTx["timestamp"] = timestamp;
jsonTx["proofs"] = json::array({Base58::bitcoin.encode(signature)});
jsonTx["recipient"] = Address(to).string();
jsonTx["amount"] = amount;
return jsonTx;
}
json jsonCancelLease(Data signature, Data leaseId, int64_t fee, int64_t timestamp, Data pub_key) {
json jsonTx;
jsonTx["type"] = TransactionType::cancelLease;
jsonTx["version"] = TransactionVersion::V2;
jsonTx["fee"] = fee;
jsonTx["senderPublicKey"] = Base58::bitcoin.encode(pub_key);
jsonTx["leaseId"] = Base58::bitcoin.encode(leaseId);
jsonTx["chainId"] = 87; // mainnet
jsonTx["timestamp"] = timestamp;
jsonTx["proofs"] = json::array({Base58::bitcoin.encode(signature)});
return jsonTx;
}
Data Transaction::serializeToSign() const {
if (pub_key.empty()) {
throw std::invalid_argument("Public key can't be empty");
}
if (input.has_transfer_message()) {
auto message = input.transfer_message();
auto attachment =
Data(message.attachment().begin(), message.attachment().end());
if (attachment.size() > 140) {
throw std::invalid_argument("Maximum attachment size is 140 bytes");
}
return serializeTransfer(message.amount(), message.asset(),
message.fee(), message.fee_asset(),
Address(message.to()), attachment,
input.timestamp(), pub_key);
} else if (input.has_lease_message()) {
auto message = input.lease_message();
return serializeLease(message.amount(), message.fee(), Address(message.to()), input.timestamp(), pub_key);
} else if (input.has_cancel_lease_message()) {
auto message = input.cancel_lease_message();
auto leaseId = Base58::bitcoin.decode(message.lease_id());
return serializeCancelLease(leaseId, message.fee(), input.timestamp(), pub_key);
}
return Data();
}
json Transaction::buildJson(Data signature) const {
if (input.has_transfer_message()) {
auto message = input.transfer_message();
auto attachment = Data(message.attachment().begin(), message.attachment().end());
return jsonTransfer(
signature,
message.amount(),
message.asset(),
message.fee(),
message.fee_asset(),
Address(message.to()),
attachment,
input.timestamp(),
pub_key);
} else if (input.has_lease_message()) {
auto message = input.lease_message();
return jsonLease(
signature,
message.amount(),
message.fee(),
Address(message.to()),
input.timestamp(),
pub_key);
} else if (input.has_cancel_lease_message()) {
auto message = input.cancel_lease_message();
auto leaseId = Base58::bitcoin.decode(message.lease_id());
return jsonCancelLease(
signature,
leaseId,
message.fee(),
input.timestamp(),
pub_key);
}
return nullptr;
}