// Copyright (c) 2011-2016 The Cryptonote developers // Distributed under the MIT/X11 software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include "PaymentServiceJsonRpcServer.h" #include #include "PaymentServiceJsonRpcMessages.h" #include "WalletService.h" #include "Serialization/JsonInputValueSerializer.h" #include "Serialization/JsonOutputStreamSerializer.h" namespace PaymentService { PaymentServiceJsonRpcServer::PaymentServiceJsonRpcServer(System::Dispatcher& sys, System::Event& stopEvent, WalletService& service, Logging::ILogger& loggerGroup) : JsonRpcServer(sys, stopEvent, loggerGroup) , service(service) , logger(loggerGroup, "PaymentServiceJsonRpcServer") { handlers.emplace("reset", jsonHandler(std::bind(&PaymentServiceJsonRpcServer::handleReset, this, std::placeholders::_1, std::placeholders::_2))); handlers.emplace("createAddress", jsonHandler(std::bind(&PaymentServiceJsonRpcServer::handleCreateAddress, this, std::placeholders::_1, std::placeholders::_2))); handlers.emplace("deleteAddress", jsonHandler(std::bind(&PaymentServiceJsonRpcServer::handleDeleteAddress, this, std::placeholders::_1, std::placeholders::_2))); handlers.emplace("getSpendKeys", jsonHandler(std::bind(&PaymentServiceJsonRpcServer::handleGetSpendKeys, this, std::placeholders::_1, std::placeholders::_2))); handlers.emplace("getBalance", jsonHandler(std::bind(&PaymentServiceJsonRpcServer::handleGetBalance, this, std::placeholders::_1, std::placeholders::_2))); handlers.emplace("getBlockHashes", jsonHandler(std::bind(&PaymentServiceJsonRpcServer::handleGetBlockHashes, this, std::placeholders::_1, std::placeholders::_2))); handlers.emplace("getTransactionHashes", jsonHandler(std::bind(&PaymentServiceJsonRpcServer::handleGetTransactionHashes, this, std::placeholders::_1, std::placeholders::_2))); handlers.emplace("getTransactions", jsonHandler(std::bind(&PaymentServiceJsonRpcServer::handleGetTransactions, this, std::placeholders::_1, std::placeholders::_2))); handlers.emplace("getUnconfirmedTransactionHashes", jsonHandler(std::bind(&PaymentServiceJsonRpcServer::handleGetUnconfirmedTransactionHashes, this, std::placeholders::_1, std::placeholders::_2))); handlers.emplace("getTransaction", jsonHandler(std::bind(&PaymentServiceJsonRpcServer::handleGetTransaction, this, std::placeholders::_1, std::placeholders::_2))); handlers.emplace("sendTransaction", jsonHandler(std::bind(&PaymentServiceJsonRpcServer::handleSendTransaction, this, std::placeholders::_1, std::placeholders::_2))); handlers.emplace("createDelayedTransaction", jsonHandler(std::bind(&PaymentServiceJsonRpcServer::handleCreateDelayedTransaction, this, std::placeholders::_1, std::placeholders::_2))); handlers.emplace("getDelayedTransactionHashes", jsonHandler(std::bind(&PaymentServiceJsonRpcServer::handleGetDelayedTransactionHashes, this, std::placeholders::_1, std::placeholders::_2))); handlers.emplace("deleteDelayedTransaction", jsonHandler(std::bind(&PaymentServiceJsonRpcServer::handleDeleteDelayedTransaction, this, std::placeholders::_1, std::placeholders::_2))); handlers.emplace("sendDelayedTransaction", jsonHandler(std::bind(&PaymentServiceJsonRpcServer::handleSendDelayedTransaction, this, std::placeholders::_1, std::placeholders::_2))); handlers.emplace("getViewKey", jsonHandler(std::bind(&PaymentServiceJsonRpcServer::handleGetViewKey, this, std::placeholders::_1, std::placeholders::_2))); handlers.emplace("getStatus", jsonHandler(std::bind(&PaymentServiceJsonRpcServer::handleGetStatus, this, std::placeholders::_1, std::placeholders::_2))); handlers.emplace("getAddresses", jsonHandler(std::bind(&PaymentServiceJsonRpcServer::handleGetAddresses, this, std::placeholders::_1, std::placeholders::_2))); } void PaymentServiceJsonRpcServer::processJsonRpcRequest(const Common::JsonValue& req, Common::JsonValue& resp) { try { prepareJsonResponse(req, resp); if (!req.contains("method")) { logger(Logging::WARNING) << "Field \"method\" is not found in json request: " << req; makeGenericErrorReponse(resp, "Invalid Request", -3600); return; } if (!req("method").isString()) { logger(Logging::WARNING) << "Field \"method\" is not a string type: " << req; makeGenericErrorReponse(resp, "Invalid Request", -3600); return; } std::string method = req("method").getString(); auto it = handlers.find(method); if (it == handlers.end()) { logger(Logging::WARNING) << "Requested method not found: " << method; makeMethodNotFoundResponse(resp); return; } logger(Logging::DEBUGGING) << method << " request came"; Common::JsonValue params(Common::JsonValue::OBJECT); if (req.contains("params")) { params = req("params"); } it->second(params, resp); } catch (std::exception& e) { logger(Logging::WARNING) << "Error occurred while processing JsonRpc request: " << e.what(); makeGenericErrorReponse(resp, e.what()); } } std::error_code PaymentServiceJsonRpcServer::handleReset(const Reset::Request& request, Reset::Response& response) { if (request.viewSecretKey.empty()) { return service.resetWallet(); } else { return service.replaceWithNewWallet(request.viewSecretKey); } } std::error_code PaymentServiceJsonRpcServer::handleCreateAddress(const CreateAddress::Request& request, CreateAddress::Response& response) { if (request.spendSecretKey.empty() && request.spendPublicKey.empty()) { return service.createAddress(response.address); } else if (!request.spendSecretKey.empty()) { return service.createAddress(request.spendSecretKey, response.address); } else { return service.createTrackingAddress(request.spendPublicKey, response.address); } } std::error_code PaymentServiceJsonRpcServer::handleDeleteAddress(const DeleteAddress::Request& request, DeleteAddress::Response& response) { return service.deleteAddress(request.address); } std::error_code PaymentServiceJsonRpcServer::handleGetSpendKeys(const GetSpendKeys::Request& request, GetSpendKeys::Response& response) { return service.getSpendkeys(request.address, response.spendPublicKey, response.spendSecretKey); } std::error_code PaymentServiceJsonRpcServer::handleGetBalance(const GetBalance::Request& request, GetBalance::Response& response) { if (!request.address.empty()) { return service.getBalance(request.address, response.availableBalance, response.lockedAmount); } else { return service.getBalance(response.availableBalance, response.lockedAmount); } } std::error_code PaymentServiceJsonRpcServer::handleGetBlockHashes(const GetBlockHashes::Request& request, GetBlockHashes::Response& response) { return service.getBlockHashes(request.firstBlockIndex, request.blockCount, response.blockHashes); } std::error_code PaymentServiceJsonRpcServer::handleGetTransactionHashes(const GetTransactionHashes::Request& request, GetTransactionHashes::Response& response) { if (!request.blockHash.empty()) { return service.getTransactionHashes(request.addresses, request.blockHash, request.blockCount, request.paymentId, response.items); } else { return service.getTransactionHashes(request.addresses, request.firstBlockIndex, request.blockCount, request.paymentId, response.items); } } std::error_code PaymentServiceJsonRpcServer::handleGetTransactions(const GetTransactions::Request& request, GetTransactions::Response& response) { if (!request.blockHash.empty()) { return service.getTransactions(request.addresses, request.blockHash, request.blockCount, request.paymentId, response.items); } else { return service.getTransactions(request.addresses, request.firstBlockIndex, request.blockCount, request.paymentId, response.items); } } std::error_code PaymentServiceJsonRpcServer::handleGetUnconfirmedTransactionHashes(const GetUnconfirmedTransactionHashes::Request& request, GetUnconfirmedTransactionHashes::Response& response) { return service.getUnconfirmedTransactionHashes(request.addresses, response.transactionHashes); } std::error_code PaymentServiceJsonRpcServer::handleGetTransaction(const GetTransaction::Request& request, GetTransaction::Response& response) { return service.getTransaction(request.transactionHash, response.transaction); } std::error_code PaymentServiceJsonRpcServer::handleSendTransaction(const SendTransaction::Request& request, SendTransaction::Response& response) { return service.sendTransaction(request, response.transactionHash); } std::error_code PaymentServiceJsonRpcServer::handleCreateDelayedTransaction(const CreateDelayedTransaction::Request& request, CreateDelayedTransaction::Response& response) { return service.createDelayedTransaction(request, response.transactionHash); } std::error_code PaymentServiceJsonRpcServer::handleGetDelayedTransactionHashes(const GetDelayedTransactionHashes::Request& request, GetDelayedTransactionHashes::Response& response) { return service.getDelayedTransactionHashes(response.transactionHashes); } std::error_code PaymentServiceJsonRpcServer::handleDeleteDelayedTransaction(const DeleteDelayedTransaction::Request& request, DeleteDelayedTransaction::Response& response) { return service.deleteDelayedTransaction(request.transactionHash); } std::error_code PaymentServiceJsonRpcServer::handleSendDelayedTransaction(const SendDelayedTransaction::Request& request, SendDelayedTransaction::Response& response) { return service.sendDelayedTransaction(request.transactionHash); } std::error_code PaymentServiceJsonRpcServer::handleGetViewKey(const GetViewKey::Request& request, GetViewKey::Response& response) { return service.getViewKey(response.viewSecretKey); } std::error_code PaymentServiceJsonRpcServer::handleGetStatus(const GetStatus::Request& request, GetStatus::Response& response) { return service.getStatus(response.blockCount, response.knownBlockCount, response.lastBlockHash, response.peerCount); } std::error_code PaymentServiceJsonRpcServer::handleGetAddresses(const GetAddresses::Request& request, GetAddresses::Response& response) { return service.getAddresses(response.addresses); } }