Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
cryptokatz committed May 24, 2019
2 parents cf52ff1 + 10cd5ae commit 3d72b55
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/PaymentGate/PaymentServiceJsonRpcMessages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
10 changes: 10 additions & 0 deletions src/PaymentGate/PaymentServiceJsonRpcMessages.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 5 additions & 2 deletions src/PaymentGate/PaymentServiceJsonRpcServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ PaymentServiceJsonRpcServer::PaymentServiceJsonRpcServer(System::Dispatcher& sys
, service(service)
, logger(loggerGroup, "PaymentServiceJsonRpcServer")
{
handlers.emplace("save", jsonHandler<Save::Request, Save::Response>(std::bind(&PaymentServiceJsonRpcServer::handleSave, this, std::placeholders::_1, std::placeholders::_2)));
handlers.emplace("createIntegrated", jsonHandler<CreateIntegrated::Request, CreateIntegrated::Response>(std::bind(&PaymentServiceJsonRpcServer::handleCreateIntegrated, this, std::placeholders::_1, std::placeholders::_2)));
handlers.emplace("reset", jsonHandler<Reset::Request, Reset::Response>(std::bind(&PaymentServiceJsonRpcServer::handleReset, this, std::placeholders::_1, std::placeholders::_2)));
handlers.emplace("createAddress", jsonHandler<CreateAddress::Request, CreateAddress::Response>(std::bind(&PaymentServiceJsonRpcServer::handleCreateAddress, this, std::placeholders::_1, std::placeholders::_2)));
Expand Down Expand Up @@ -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)
{
Expand Down
1 change: 1 addition & 0 deletions src/PaymentGate/PaymentServiceJsonRpcServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class PaymentServiceJsonRpcServer : public CryptoNote::JsonRpcServer {
std::unordered_map<std::string, HandlerFunction> 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);
Expand Down
25 changes: 25 additions & 0 deletions src/PaymentGate/WalletService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/PaymentGate/WalletService.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 3d72b55

Please sign in to comment.