Unverified Commit 7483a230 authored by kladko's avatar kladko

bug/SKALE-3751-enable-zeromq

parent 4b0ff8a5
......@@ -28,10 +28,15 @@
class BLSSignRspMessage : public ZMQMessage {
public:
BLSSignRspMessage(shared_ptr<rapidjson::Document>& _d) : ZMQMessage(_d) {};
virtual Json::Value process();
string getSigShare() {
return getStringRapid("sigShare");
}
};
......
......@@ -33,6 +33,7 @@ public:
virtual Json::Value process();
};
......
......@@ -30,4 +30,15 @@
Json::Value ECDSASignRspMessage::process() {
// never called
assert(false);
}
\ No newline at end of file
}
string ECDSASignRspMessage::getSignature() {
string r = getStringRapid( "signature_r" );
string v = getStringRapid( "signature_v" );
string s = getStringRapid("signature_s" );
auto ret = v + ":" + r.substr( 2 ) + ":" + s.substr( 2 );
return ret;
}
......@@ -33,6 +33,9 @@ public:
virtual Json::Value process();
string getSignature();
};
......
......@@ -70,11 +70,11 @@ bin_PROGRAMS = sgxwallet testw sgx_util
## have to be explicitly listed
COMMON_SRC = BLSSignRspMessage.cpp ECDSASignRspMessage.cpp ECDSASignReqMessage.cpp BLSSignReqMessage.cpp ZMQMessage.cpp ZMQServer.cpp ServerWorker.cpp InvalidStateException.cpp Exception.cpp InvalidArgumentException.cpp Log.cpp \
COMMON_SRC = ZMQClient.cpp BLSSignRspMessage.cpp ECDSASignRspMessage.cpp ECDSASignReqMessage.cpp BLSSignReqMessage.cpp ZMQMessage.cpp ZMQServer.cpp ServerWorker.cpp InvalidStateException.cpp Exception.cpp InvalidArgumentException.cpp Log.cpp \
SGXWalletServer.cpp SGXRegistrationServer.cpp CSRManagerServer.cpp BLSCrypto.cpp \
DKGCrypto.cpp ServerInit.cpp BLSPrivateKeyShareSGX.cpp LevelDB.cpp ServerDataChecker.cpp SEKManager.cpp \
third_party/intel/sgx_stub.c third_party/intel/sgx_detect_linux.c third_party/intel/create_enclave.c third_party/intel/oc_alloc.c \
ECDSAImpl.c TestUtils.cpp sgxwallet.c SGXInfoServer.cpp ECDSACrypto.cpp ZMQClient.cpp
ECDSAImpl.c TestUtils.cpp sgxwallet.c SGXInfoServer.cpp ECDSACrypto.cpp
COMMON_ENCLAVE_SRC = secure_enclave_u.c secure_enclave_u.h
sgxwallet_SOURCES = sgxwall.cpp $(COMMON_SRC)
......
......@@ -20,5 +20,83 @@
@author Stan Kladko
@date 2020
*/
#include "common.h"
#include "BLSSignReqMessage.h"
#include "BLSSignRspMessage.h"
#include "ECDSASignReqMessage.h"
#include "ECDSASignRspMessage.h"
#include "ZMQClient.h"
shared_ptr <ZMQMessage> ZMQClient::doRequestReply(Json::Value &_req) {
Json::FastWriter fastWriter;
string reqStr = fastWriter.write(_req);
auto resultStr = doZmqRequestReply(reqStr);
return ZMQMessage::parse(resultStr.c_str(), resultStr.size(), false);
}
string ZMQClient::doZmqRequestReply(string &_req) {
stringstream request;
s_send(*clientSocket, _req);
while (true) {
// Poll socket for a reply, with timeout
zmq::pollitem_t items[] = {
{static_cast<void *>(*clientSocket), 0, ZMQ_POLLIN, 0}};
zmq::poll(&items[0], 1, REQUEST_TIMEOUT);
// If we got a reply, process it
if (items[0].revents & ZMQ_POLLIN) {
string reply = s_recv(*clientSocket);
return reply;
} else {
spdlog::error("W: no response from server, retrying...");
reconnect();
// Send request again, on new socket
s_send(*clientSocket, _req);
}
}
}
ZMQClient::ZMQClient(string &ip, uint16_t port) : ctx(1) {
url = "tcp://" + ip + ":" + to_string(port);
}
void ZMQClient::reconnect() {
clientSocket = nullptr; // delete previous
clientSocket = make_unique<zmq::socket_t>(ctx, ZMQ_REQ);
clientSocket->connect(url);
// Configure socket to not wait at close time
int linger = 0;
clientSocket->setsockopt(ZMQ_LINGER, &linger, sizeof(linger));
}
string ZMQClient::blsSignMessageHash(const std::string &keyShareName, const std::string &messageHash, int t, int n) {
Json::Value p;
p["type"] = ZMQMessage::BLS_SIGN_REQ;
p["keyShareName"] = keyShareName;
p["messageHash"] = messageHash;
p["n"] = n;
p["t"] = t;
auto result = dynamic_pointer_cast<BLSSignRspMessage>(doRequestReply(p));
CHECK_STATE(result);
return result->getSigShare();
}
string ZMQClient::ecdsaSignMessageHash(int base, const std::string &keyName, const std::string &messageHash) {
Json::Value p;
p["type"] = ZMQMessage::ECDSA_SIGN_REQ;
p["base"] = base;
p["keyName"] = keyName;
p["messageHash"] = messageHash;
auto result = dynamic_pointer_cast<ECDSASignRspMessage>(doRequestReply(p));
CHECK_STATE(result);
return result->getSignature();
}
\ No newline at end of file
......@@ -46,79 +46,21 @@ private:
std::unique_ptr <zmq::socket_t> clientSocket;
string url;
shared_ptr <ZMQMessage> doRequestReply(Json::Value &_req) {
shared_ptr <ZMQMessage> doRequestReply(Json::Value &_req);
Json::FastWriter fastWriter;
string reqStr = fastWriter.write(_req);
string doZmqRequestReply(string &_req);
auto resultStr = doZmqRequestReply(reqStr);
return ZMQMessage::parse(resultStr.c_str(), resultStr.size(), false);
}
string doZmqRequestReply(string &_req) {
stringstream request;
s_send(*clientSocket, _req);
public:
while (true) {
// Poll socket for a reply, with timeout
zmq::pollitem_t items[] = {
{static_cast<void *>(*clientSocket), 0, ZMQ_POLLIN, 0}};
zmq::poll(&items[0], 1, REQUEST_TIMEOUT);
// If we got a reply, process it
if (items[0].revents & ZMQ_POLLIN) {
string reply = s_recv(*clientSocket);
return reply;
} else {
spdlog::error("W: no response from server, retrying...");
reconnect();
// Send request again, on new socket
s_send(*clientSocket, _req);
}
}
}
ZMQClient(string &ip, uint16_t port);
public:
void reconnect() ;
string blsSignMessageHash(const std::string &keyShareName, const std::string &messageHash, int t, int n);
ZMQClient(string &ip, uint16_t port) : ctx(1) {
url = "tcp://" + ip + ":" + to_string(port);
}
void reconnect() {
clientSocket = nullptr; // delete previous
clientSocket = make_unique<zmq::socket_t>(ctx, ZMQ_REQ);
clientSocket->connect(url);
// Configure socket to not wait at close time
int linger = 0;
clientSocket->setsockopt(ZMQ_LINGER, &linger, sizeof(linger));
}
string blsSignMessageHash(const std::string &keyShareName, const std::string &messageHash, int t, int n) {
Json::Value p;
p["type"] = ZMQMessage::BLS_SIGN_REQ;
p["keyShareName"] = keyShareName;
p["messageHash"] = messageHash;
p["n"] = n;
p["t"] = t;
auto result = doRequestReply(p);
return "";
}
string ecdsaSignMessageHash(int base, const std::string &keyName, const std::string &messageHash) {
Json::Value p;
p["type"] = ZMQMessage::ECDSA_SIGN_REQ;
p["base"] = base;
p["keyName"] = keyName;
p["messageHash"] = messageHash;
auto result = doRequestReply(p);
return "";
}
string ecdsaSignMessageHash(int base, const std::string &keyName, const std::string &messageHash);
};
......
......@@ -64,4 +64,5 @@ public:
virtual Json::Value process() = 0;
};
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment