diff --git a/src/PaymentGate/PaymentServiceJsonRpcMessages.cpp b/src/PaymentGate/PaymentServiceJsonRpcMessages.cpp index 33771cb3..9a8ffa09 100644 --- a/src/PaymentGate/PaymentServiceJsonRpcMessages.cpp +++ b/src/PaymentGate/PaymentServiceJsonRpcMessages.cpp @@ -9,6 +9,12 @@ namespace PaymentService { +void Save::Request::serialize(CryptoNote::ISerializer& /*serializer*/) { +} + +void Save::Response::serialize(CryptoNote::ISerializer& /*serializer*/) { +} + void Reset::Request::serialize(CryptoNote::ISerializer& serializer) { serializer(viewSecretKey, "viewSecretKey"); } diff --git a/src/PaymentGate/PaymentServiceJsonRpcMessages.h b/src/PaymentGate/PaymentServiceJsonRpcMessages.h index c569149c..0ff8f5c2 100644 --- a/src/PaymentGate/PaymentServiceJsonRpcMessages.h +++ b/src/PaymentGate/PaymentServiceJsonRpcMessages.h @@ -21,6 +21,16 @@ class RequestSerializationError: public std::exception { virtual const char* what() const throw() override { return "Request error"; } }; +struct Save { + struct Request { + void serialize(CryptoNote::ISerializer& serializer); + }; + + struct Response { + void serialize(CryptoNote::ISerializer& serializer); + }; +}; + struct Reset { struct Request { std::string viewSecretKey; diff --git a/src/PaymentGate/PaymentServiceJsonRpcServer.cpp b/src/PaymentGate/PaymentServiceJsonRpcServer.cpp index d20445c5..d8d34195 100644 --- a/src/PaymentGate/PaymentServiceJsonRpcServer.cpp +++ b/src/PaymentGate/PaymentServiceJsonRpcServer.cpp @@ -37,6 +37,7 @@ PaymentServiceJsonRpcServer::PaymentServiceJsonRpcServer(System::Dispatcher& sys , service(service) , logger(loggerGroup, "PaymentServiceJsonRpcServer") { + handlers.emplace("save", jsonHandler(std::bind(&PaymentServiceJsonRpcServer::handleSave, this, std::placeholders::_1, std::placeholders::_2))); handlers.emplace("createIntegrated", jsonHandler(std::bind(&PaymentServiceJsonRpcServer::handleCreateIntegrated, this, std::placeholders::_1, std::placeholders::_2))); 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))); @@ -118,9 +119,11 @@ std::error_code PaymentServiceJsonRpcServer::handleCreateAddress(const CreateAdd } } -/* ------------------------------------------------------------------------------ */ +std::error_code PaymentServiceJsonRpcServer::handleSave(const Save::Request& /*request*/, Save::Response& /*response*/) +{ + return service.saveWalletNoThrow(); +} -/* CREATE INTEGRATED */ std::error_code PaymentServiceJsonRpcServer::handleCreateIntegrated(const CreateIntegrated::Request& request, CreateIntegrated::Response& response) { diff --git a/src/PaymentGate/PaymentServiceJsonRpcServer.h b/src/PaymentGate/PaymentServiceJsonRpcServer.h index f3d21b75..e4d84670 100644 --- a/src/PaymentGate/PaymentServiceJsonRpcServer.h +++ b/src/PaymentGate/PaymentServiceJsonRpcServer.h @@ -61,6 +61,7 @@ class PaymentServiceJsonRpcServer : public CryptoNote::JsonRpcServer { std::unordered_map handlers; std::error_code handleReset(const Reset::Request& request, Reset::Response& response); + std::error_code handleSave(const Save::Request& request, Save::Response& response); std::error_code handleCreateIntegrated(const CreateIntegrated::Request& request, CreateIntegrated::Response& response); std::error_code handleCreateAddress(const CreateAddress::Request& request, CreateAddress::Response& response); std::error_code handleDeleteAddress(const DeleteAddress::Request& request, DeleteAddress::Response& response); diff --git a/src/PaymentGate/WalletService.cpp b/src/PaymentGate/WalletService.cpp index 3fd84e48..eac75acf 100644 --- a/src/PaymentGate/WalletService.cpp +++ b/src/PaymentGate/WalletService.cpp @@ -478,10 +478,35 @@ void WalletService::init() { } void WalletService::saveWallet() { + logger(Logging::INFO) << "Saving wallet..."; PaymentService::secureSaveWallet(wallet, config.walletFile, true, true); logger(Logging::INFO) << "Wallet is saved"; } +std::error_code WalletService::saveWalletNoThrow() { + try { + System::EventLock lk(readyEvent); + + logger(Logging::INFO, Logging::BRIGHT_WHITE) << "Saving wallet..."; + + if (!inited) { + logger(Logging::WARNING, Logging::BRIGHT_YELLOW) << "Save impossible: Wallet Service is not initialized"; + return make_error_code(CryptoNote::error::NOT_INITIALIZED); + } + + PaymentService::secureSaveWallet(wallet, config.walletFile, true, true); + logger(Logging::INFO) << "Wallet is saved"; + } catch (std::system_error& x) { + logger(Logging::WARNING, Logging::BRIGHT_YELLOW) << "Error while saving wallet: " << x.what(); + return x.code(); + } catch (std::exception& x) { + logger(Logging::WARNING, Logging::BRIGHT_YELLOW) << "Error while saving wallet: " << x.what(); + return make_error_code(CryptoNote::error::INTERNAL_WALLET_ERROR); + } + + return std::error_code(); +} + void WalletService::loadWallet() { std::ifstream inputWalletFile; inputWalletFile.open(config.walletFile.c_str(), std::fstream::in | std::fstream::binary); diff --git a/src/PaymentGate/WalletService.h b/src/PaymentGate/WalletService.h index 9ba1bc42..a297565f 100644 --- a/src/PaymentGate/WalletService.h +++ b/src/PaymentGate/WalletService.h @@ -45,6 +45,7 @@ class WalletService { void init(); void saveWallet(); + std::error_code saveWalletNoThrow(); std::error_code resetWallet(); std::error_code replaceWithNewWallet(const std::string& viewSecretKey); std::error_code createAddress(const std::string& spendSecretKeyText, std::string& address);