forked from amjuarez/bytecoin
-
Notifications
You must be signed in to change notification settings - Fork 102
Expand file tree
/
Copy pathWalletRpcServer.cpp
More file actions
executable file
·295 lines (248 loc) · 11.7 KB
/
WalletRpcServer.cpp
File metadata and controls
executable file
·295 lines (248 loc) · 11.7 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
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
292
293
294
295
// Copyright (c) 2012-2017, The CryptoNote developers, The Bytecoin developers
//
// This file is part of Bytecoin.
//
// Bytecoin is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Bytecoin is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Bytecoin. If not, see <http://www.gnu.org/licenses/>.
#include "WalletRpcServer.h"
#include <fstream>
#include "Common/CommandLine.h"
#include "Common/StringTools.h"
#include "CryptoNoteCore/CryptoNoteFormatUtils.h"
#include "CryptoNoteCore/Account.h"
#include "crypto/hash.h"
#include "WalletLegacy/WalletHelper.h"
// #include "wallet_errors.h"
#include "Rpc/JsonRpc.h"
using namespace Logging;
using namespace CryptoNote;
namespace Tools {
const command_line::arg_descriptor<uint16_t> wallet_rpc_server::arg_rpc_bind_port = { "wallet-rpc-bind-port", "Starts wallet as rpc server for wallet operations, sets bind port for server", 0, true };
const command_line::arg_descriptor<std::string> wallet_rpc_server::arg_rpc_bind_ip = { "wallet-rpc-bind-ip", "Specify ip to bind rpc server", "127.0.0.1" };
void wallet_rpc_server::init_options(boost::program_options::options_description& desc) {
command_line::add_arg(desc, arg_rpc_bind_ip);
command_line::add_arg(desc, arg_rpc_bind_port);
}
//------------------------------------------------------------------------------------------------------------------------------
wallet_rpc_server::wallet_rpc_server(
System::Dispatcher& dispatcher,
Logging::ILogger& log,
CryptoNote::IWalletLegacy&w,
CryptoNote::INode& n,
CryptoNote::Currency& currency,
const std::string& walletFile)
:
HttpServer(dispatcher, log),
logger(log, "WalletRpc"),
m_dispatcher(dispatcher),
m_stopComplete(dispatcher),
m_wallet(w),
m_node(n),
m_currency(currency),
m_walletFilename(walletFile) {
}
//------------------------------------------------------------------------------------------------------------------------------
bool wallet_rpc_server::run() {
start(m_bind_ip, m_port);
m_stopComplete.wait();
return true;
}
void wallet_rpc_server::send_stop_signal() {
m_dispatcher.remoteSpawn([this] {
std::cout << "wallet_rpc_server::send_stop_signal()" << std::endl;
stop();
m_stopComplete.set();
});
}
//------------------------------------------------------------------------------------------------------------------------------
bool wallet_rpc_server::handle_command_line(const boost::program_options::variables_map& vm) {
m_bind_ip = command_line::get_arg(vm, arg_rpc_bind_ip);
m_port = command_line::get_arg(vm, arg_rpc_bind_port);
return true;
}
//------------------------------------------------------------------------------------------------------------------------------
bool wallet_rpc_server::init(const boost::program_options::variables_map& vm) {
if (!handle_command_line(vm)) {
logger(ERROR) << "Failed to process command line in wallet_rpc_server";
return false;
}
return true;
}
void wallet_rpc_server::processRequest(const CryptoNote::HttpRequest& request, CryptoNote::HttpResponse& response) {
using namespace CryptoNote::JsonRpc;
JsonRpcRequest jsonRequest;
JsonRpcResponse jsonResponse;
try {
jsonRequest.parseRequest(request.getBody());
jsonResponse.setId(jsonRequest.getId());
static std::unordered_map<std::string, JsonMemberMethod> s_methods = {
{ "getbalance", makeMemberMethod(&wallet_rpc_server::on_getbalance) },
{ "transfer", makeMemberMethod(&wallet_rpc_server::on_transfer) },
{ "store", makeMemberMethod(&wallet_rpc_server::on_store) },
{ "get_payments", makeMemberMethod(&wallet_rpc_server::on_get_payments) },
{ "get_transfers", makeMemberMethod(&wallet_rpc_server::on_get_transfers) },
{ "get_height", makeMemberMethod(&wallet_rpc_server::on_get_height) },
{ "reset", makeMemberMethod(&wallet_rpc_server::on_reset) }
};
auto it = s_methods.find(jsonRequest.getMethod());
if (it == s_methods.end()) {
throw JsonRpcError(errMethodNotFound);
}
it->second(this, jsonRequest, jsonResponse);
} catch (const JsonRpcError& err) {
jsonResponse.setError(err);
} catch (const std::exception& e) {
jsonResponse.setError(JsonRpcError(WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR, e.what()));
}
response.setBody(jsonResponse.getBody());
}
//------------------------------------------------------------------------------------------------------------------------------
bool wallet_rpc_server::on_getbalance(const wallet_rpc::COMMAND_RPC_GET_BALANCE::request& req, wallet_rpc::COMMAND_RPC_GET_BALANCE::response& res) {
res.locked_amount = m_wallet.pendingBalance();
res.available_balance = m_wallet.actualBalance();
return true;
}
//------------------------------------------------------------------------------------------------------------------------------
bool wallet_rpc_server::on_transfer(const wallet_rpc::COMMAND_RPC_TRANSFER::request& req, wallet_rpc::COMMAND_RPC_TRANSFER::response& res) {
std::vector<CryptoNote::WalletLegacyTransfer> transfers;
for (auto it = req.destinations.begin(); it != req.destinations.end(); it++) {
CryptoNote::WalletLegacyTransfer transfer;
transfer.address = it->address;
transfer.amount = it->amount;
transfers.push_back(transfer);
}
std::vector<uint8_t> extra;
if (!req.payment_id.empty()) {
std::string payment_id_str = req.payment_id;
Crypto::Hash payment_id;
if (!CryptoNote::parsePaymentId(payment_id_str, payment_id)) {
throw JsonRpc::JsonRpcError(WALLET_RPC_ERROR_CODE_WRONG_PAYMENT_ID,
"Payment id has invalid format: \"" + payment_id_str + "\", expected 64-character string");
}
BinaryArray extra_nonce;
CryptoNote::setPaymentIdToTransactionExtraNonce(extra_nonce, payment_id);
if (!CryptoNote::addExtraNonceToTransactionExtra(extra, extra_nonce)) {
throw JsonRpc::JsonRpcError(WALLET_RPC_ERROR_CODE_WRONG_PAYMENT_ID,
"Something went wrong with payment_id. Please check its format: \"" + payment_id_str + "\", expected 64-character string");
}
}
std::string extraString;
std::copy(extra.begin(), extra.end(), std::back_inserter(extraString));
try {
CryptoNote::WalletHelper::SendCompleteResultObserver sent;
WalletHelper::IWalletRemoveObserverGuard removeGuard(m_wallet, sent);
CryptoNote::TransactionId tx = m_wallet.sendTransaction(transfers, req.fee, extraString, req.mixin, req.unlock_time);
if (tx == WALLET_LEGACY_INVALID_TRANSACTION_ID) {
throw std::runtime_error("Couldn't send transaction");
}
std::error_code sendError = sent.wait(tx);
removeGuard.removeObserver();
if (sendError) {
throw std::system_error(sendError);
}
CryptoNote::WalletLegacyTransaction txInfo;
m_wallet.getTransaction(tx, txInfo);
res.tx_hash = Common::podToHex(txInfo.hash);
} catch (const std::exception& e) {
throw JsonRpc::JsonRpcError(WALLET_RPC_ERROR_CODE_GENERIC_TRANSFER_ERROR, e.what());
}
return true;
}
//------------------------------------------------------------------------------------------------------------------------------
bool wallet_rpc_server::on_store(const wallet_rpc::COMMAND_RPC_STORE::request& req, wallet_rpc::COMMAND_RPC_STORE::response& res) {
try {
WalletHelper::storeWallet(m_wallet, m_walletFilename);
} catch (std::exception& e) {
throw JsonRpc::JsonRpcError(WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR, std::string("Couldn't save wallet: ") + e.what());
}
return true;
}
//------------------------------------------------------------------------------------------------------------------------------
bool wallet_rpc_server::on_get_payments(const wallet_rpc::COMMAND_RPC_GET_PAYMENTS::request& req, wallet_rpc::COMMAND_RPC_GET_PAYMENTS::response& res) {
Crypto::Hash expectedPaymentId;
CryptoNote::BinaryArray payment_id_blob;
if (!Common::fromHex(req.payment_id, payment_id_blob)) {
throw JsonRpc::JsonRpcError(WALLET_RPC_ERROR_CODE_WRONG_PAYMENT_ID, "Payment ID has invald format");
}
if (sizeof(expectedPaymentId) != payment_id_blob.size()) {
throw JsonRpc::JsonRpcError(WALLET_RPC_ERROR_CODE_WRONG_PAYMENT_ID, "Payment ID has invalid size");
}
expectedPaymentId = *reinterpret_cast<const Crypto::Hash*>(payment_id_blob.data());
size_t transactionsCount = m_wallet.getTransactionCount();
for (size_t trantransactionNumber = 0; trantransactionNumber < transactionsCount; ++trantransactionNumber) {
WalletLegacyTransaction txInfo;
m_wallet.getTransaction(trantransactionNumber, txInfo);
if (txInfo.state != WalletLegacyTransactionState::Active || txInfo.blockHeight == WALLET_LEGACY_UNCONFIRMED_TRANSACTION_HEIGHT) {
continue;
}
if (txInfo.totalAmount < 0) continue;
std::vector<uint8_t> extraVec;
extraVec.reserve(txInfo.extra.size());
std::for_each(txInfo.extra.begin(), txInfo.extra.end(), [&extraVec](const char el) { extraVec.push_back(el); });
Crypto::Hash paymentId;
if (getPaymentIdFromTxExtra(extraVec, paymentId) && paymentId == expectedPaymentId) {
wallet_rpc::payment_details rpc_payment;
rpc_payment.tx_hash = Common::podToHex(txInfo.hash);
rpc_payment.amount = txInfo.totalAmount;
rpc_payment.block_height = txInfo.blockHeight;
rpc_payment.unlock_time = txInfo.unlockTime;
res.payments.push_back(rpc_payment);
}
}
return true;
}
bool wallet_rpc_server::on_get_transfers(const wallet_rpc::COMMAND_RPC_GET_TRANSFERS::request& req, wallet_rpc::COMMAND_RPC_GET_TRANSFERS::response& res) {
res.transfers.clear();
size_t transactionsCount = m_wallet.getTransactionCount();
for (size_t trantransactionNumber = 0; trantransactionNumber < transactionsCount; ++trantransactionNumber) {
WalletLegacyTransaction txInfo;
m_wallet.getTransaction(trantransactionNumber, txInfo);
if (txInfo.state != WalletLegacyTransactionState::Active || txInfo.blockHeight == WALLET_LEGACY_UNCONFIRMED_TRANSACTION_HEIGHT) {
continue;
}
std::string address = "";
if (txInfo.totalAmount < 0) {
if (txInfo.transferCount > 0) {
WalletLegacyTransfer tr;
m_wallet.getTransfer(txInfo.firstTransferId, tr);
address = tr.address;
}
}
wallet_rpc::Transfer transfer;
transfer.time = txInfo.timestamp;
transfer.output = txInfo.totalAmount < 0;
transfer.transactionHash = Common::podToHex(txInfo.hash);
transfer.amount = std::abs(txInfo.totalAmount);
transfer.fee = txInfo.fee;
transfer.address = address;
transfer.blockIndex = txInfo.blockHeight;
transfer.unlockTime = txInfo.unlockTime;
transfer.paymentId = "";
std::vector<uint8_t> extraVec;
extraVec.reserve(txInfo.extra.size());
std::for_each(txInfo.extra.begin(), txInfo.extra.end(), [&extraVec](const char el) { extraVec.push_back(el); });
Crypto::Hash paymentId;
transfer.paymentId = (getPaymentIdFromTxExtra(extraVec, paymentId) && paymentId != NULL_HASH ? Common::podToHex(paymentId) : "");
res.transfers.push_back(transfer);
}
return true;
}
bool wallet_rpc_server::on_get_height(const wallet_rpc::COMMAND_RPC_GET_HEIGHT::request& req, wallet_rpc::COMMAND_RPC_GET_HEIGHT::response& res) {
res.height = m_node.getLastLocalBlockHeight();
return true;
}
bool wallet_rpc_server::on_reset(const wallet_rpc::COMMAND_RPC_RESET::request& req, wallet_rpc::COMMAND_RPC_RESET::response& res) {
m_wallet.reset();
return true;
}
}