Skip to content

Commit

Permalink
walletd offline signature support
Browse files Browse the repository at this point in the history
  • Loading branch information
dynexcoin committed Dec 23, 2023
1 parent d30c774 commit 55dd7a8
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 1 deletion.
1 change: 1 addition & 0 deletions include/IWallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ class IWallet {
virtual std::vector<TransactionOutputInformation> getTransfers(size_t index, uint32_t flags) const = 0;

virtual std::string getReserveProof(const uint64_t &reserve, const std::string& address, const std::string &message) = 0;
virtual std::string getSpendableOutputs(const std::string& address) = 0;

virtual size_t transfer(const TransactionParameters& sendingTransaction, Crypto::SecretKey &txSecretKey) = 0;

Expand Down
12 changes: 12 additions & 0 deletions src/PaymentGate/PaymentServiceJsonRpcMessages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,18 @@ void GetReserveProof::Response::serialize(DynexCN::ISerializer& serializer) {
serializer(reserveProof, "reserveProof");
}

// offline-signature:
void ExportOutputs::Request::serialize(DynexCN::ISerializer& serializer) {
if (!serializer(address, "address")) {
throw RequestSerializationError();
}
}

void ExportOutputs::Response::serialize(DynexCN::ISerializer& serializer) {
serializer(message, "message");
}
// --

void WalletRpcOrder::serialize(DynexCN::ISerializer& serializer) {
bool r = serializer(address, "address");
r &= serializer(amount, "amount");
Expand Down
15 changes: 15 additions & 0 deletions src/PaymentGate/PaymentServiceJsonRpcMessages.h
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,21 @@ struct GetReserveProof {
};
};

// offline-signature:
struct ExportOutputs {
struct Request {
std::string address;

void serialize(DynexCN::ISerializer& serializer);
};

struct Response {
std::string message;

void serialize(DynexCN::ISerializer& serializer);
};
};

struct WalletRpcOrder {
std::string address;
uint64_t amount;
Expand Down
6 changes: 6 additions & 0 deletions src/PaymentGate/PaymentServiceJsonRpcServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ PaymentServiceJsonRpcServer::PaymentServiceJsonRpcServer(System::Dispatcher& sys
handlers.emplace("estimateFusion", jsonHandler<EstimateFusion::Request, EstimateFusion::Response>(std::bind(&PaymentServiceJsonRpcServer::handleEstimateFusion, this, std::placeholders::_1, std::placeholders::_2)));
handlers.emplace("validateAddress", jsonHandler<ValidateAddress::Request, ValidateAddress::Response>(std::bind(&PaymentServiceJsonRpcServer::handleValidateAddress, this, std::placeholders::_1, std::placeholders::_2)));
handlers.emplace("getReserveProof", jsonHandler<GetReserveProof::Request, GetReserveProof::Response>(std::bind(&PaymentServiceJsonRpcServer::handleGetReserveProof, this, std::placeholders::_1, std::placeholders::_2)));
handlers.emplace("exportOutputs", jsonHandler<ExportOutputs::Request, ExportOutputs::Response>(std::bind(&PaymentServiceJsonRpcServer::handleexportOutputs, this, std::placeholders::_1, std::placeholders::_2)));
}

void PaymentServiceJsonRpcServer::processJsonRpcRequest(const Common::JsonValue& req, Common::JsonValue& resp) {
Expand Down Expand Up @@ -229,6 +230,11 @@ std::error_code PaymentServiceJsonRpcServer::handleGetReserveProof(const GetRese
return service.getReserveProof(response.reserveProof, request.address, request.message, request.amount);
}

// offline-signature
std::error_code PaymentServiceJsonRpcServer::handleexportOutputs(const ExportOutputs::Request& request, ExportOutputs::Response& response) {
return service.getExportOutputs(response.message, request.address);
}

std::error_code PaymentServiceJsonRpcServer::handleSendTransaction(const SendTransaction::Request& request, SendTransaction::Response& response) {
return service.sendTransaction(request, response.transactionHash, response.transactionSecretKey);
}
Expand Down
3 changes: 2 additions & 1 deletion src/PaymentGate/PaymentServiceJsonRpcServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,10 @@ class PaymentServiceJsonRpcServer : public DynexCN::JsonRpcServer {
std::error_code handleGetAddresses(const GetAddresses::Request& request, GetAddresses::Response& response);
std::error_code handleValidateAddress(const ValidateAddress::Request& request, ValidateAddress::Response& response);
std::error_code handleGetReserveProof(const GetReserveProof::Request& request, GetReserveProof::Response& response);

std::error_code handleSendFusionTransaction(const SendFusionTransaction::Request& request, SendFusionTransaction::Response& response);
std::error_code handleEstimateFusion(const EstimateFusion::Request& request, EstimateFusion::Response& response);
// offline signature:
std::error_code handleexportOutputs(const ExportOutputs::Request& request, ExportOutputs::Response& response);
};

}//namespace PaymentService
15 changes: 15 additions & 0 deletions src/PaymentGate/WalletService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1183,6 +1183,21 @@ std::error_code WalletService::getReserveProof(std::string& reserveProof, const
return std::error_code();
}

// offline-signature
std::error_code WalletService::getExportOutputs(std::string& message, const std::string& address) {
try {
System::EventLock lk(readyEvent);
message = wallet.getSpendableOutputs(address);
} catch (std::system_error& x) {
logger(Logging::WARNING, Logging::BRIGHT_YELLOW) << "Error while getting outputs: " << x.what();
return x.code();
} catch (std::exception& x) {
logger(Logging::WARNING, Logging::BRIGHT_YELLOW) << "Error while getting outputs: " << x.what();
return make_error_code(DynexCN::error::INTERNAL_WALLET_ERROR);
}
return std::error_code();
}

std::error_code WalletService::getAddresses(std::vector<std::string>& addresses) {
try {
System::EventLock lk(readyEvent);
Expand Down
1 change: 1 addition & 0 deletions src/PaymentGate/WalletService.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ class WalletService {
std::error_code estimateFusion(uint64_t threshold, const std::vector<std::string>& addresses, uint32_t& fusionReadyCount, uint32_t& totalOutputCount);
std::error_code validateAddress(const std::string& address, bool& isvalid, std::string& _address, std::string& spendPublicKey, std::string& viewPublicKey);
std::error_code getReserveProof(std::string& reserveProof, const std::string& address, const std::string& message, const uint64_t& amount = 0);
std::error_code getExportOutputs(std::string& message, const std::string& address);

private:
void refresh();
Expand Down
26 changes: 26 additions & 0 deletions src/Wallet/WalletGreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@
#include "WalletUtils.h"
#include "DynexCNCore/TransactionExtra.h"

// offline signature:
#include <Common/Base64.h>

extern "C"
{
#include "crypto/keccak.h"
Expand Down Expand Up @@ -2830,6 +2833,29 @@ bool WalletGreen::getTransactionProof(const Crypto::Hash& transactionHash, const
return true;
}

// offline-signature:
std::string WalletGreen::getSpendableOutputs(const std::string& address) {
throwIfNotInitialized();
throwIfStopped();

WalletOuts outs = pickWallet(address);

std::cout << "Exporting outputs: " << outs.outs.size() << std::endl;

std::stringstream fout;
for (auto& t : outs.outs) {
fout.write(reinterpret_cast<char*>(&t.amount), sizeof(t.amount));
fout.write(reinterpret_cast<char*>(&t.globalOutputIndex), sizeof(t.globalOutputIndex));
fout.write(reinterpret_cast<char*>(&t.outputInTransaction), sizeof(t.outputInTransaction));
fout.write(reinterpret_cast<char*>(&t.transactionHash), sizeof(t.transactionHash));
fout.write(reinterpret_cast<char*>(&t.transactionPublicKey), sizeof(t.transactionPublicKey));
fout.write(reinterpret_cast<char*>(&t.outputKey), sizeof(t.outputKey));
fout.write(reinterpret_cast<char*>(&t.requiredSignatures), sizeof(t.requiredSignatures));
}

return Tools::Base64::encode(fout.str());
}

std::string WalletGreen::getReserveProof(const uint64_t &reserve, const std::string& address, const std::string &message) {
throwIfNotInitialized();
throwIfTrackingMode();
Expand Down
2 changes: 2 additions & 0 deletions src/Wallet/WalletGreen.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ class WalletGreen : public IWallet,
virtual std::vector<TransactionOutputInformation> getTransfers(size_t index, uint32_t flags) const override;

virtual std::string getReserveProof(const uint64_t &reserve, const std::string& address, const std::string &message) override;
// offline signature:
virtual std::string getSpendableOutputs(const std::string& address) override;

virtual size_t transfer(const TransactionParameters& sendingTransaction, Crypto::SecretKey& txSecretKey) override;

Expand Down

0 comments on commit 55dd7a8

Please sign in to comment.