ZMQClient.h 3.75 KB
Newer Older
kladko's avatar
kladko committed
1 2 3
/*
    Copyright (C) 2018-2019 SKALE Labs

4
    This file is part of sgxwallet.
kladko's avatar
kladko committed
5

6
    sgxwallet is free software: you can redistribute it and/or modify
kladko's avatar
kladko committed
7 8 9 10
    it under the terms of the GNU Affero General Public License as published
    by the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

11
    sgxwallet is distributed in the hope that it will be useful,
kladko's avatar
kladko committed
12 13 14 15 16
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
17
    along with sgxwallet.  If not, see <https://www.gnu.org/licenses/>.
kladko's avatar
kladko committed
18 19 20 21 22 23

    @file ZMQClient.h
    @author Stan Kladko
    @date 2021
*/

Stan Kladko's avatar
Stan Kladko committed
24 25 26
#ifndef SGXWALLET_ZMQCLIENT_H
#define SGXWALLET_ZMQCLIENT_H

kladko's avatar
kladko committed
27 28 29 30 31 32
#include <openssl/pem.h>
#include <openssl/evp.h>
#include <openssl/err.h>
#include <openssl/sha.h>
#include <openssl/rand.h>

kladko's avatar
kladko committed
33 34 35
#include "third_party/spdlog/spdlog.h"
#include <zmq.hpp>
#include "zhelpers.hpp"
Stan Kladko's avatar
Stan Kladko committed
36
#include <jsonrpccpp/client.h>
Stan Kladko's avatar
Stan Kladko committed
37
#include "ZMQMessage.h"
Stan Kladko's avatar
Stan Kladko committed
38

kladko's avatar
kladko committed
39
#define REQUEST_TIMEOUT     10000    //  msecs, (> 1000!)
kladko's avatar
kladko committed
40

Stan Kladko's avatar
Stan Kladko committed
41
class ZMQClient {
kladko's avatar
kladko committed
42
private:
kladko's avatar
kladko committed
43

kladko's avatar
kladko committed
44
    EVP_PKEY* pkey = 0;
kladko's avatar
kladko committed
45 46 47
    EVP_PKEY* pubkey = 0;
    X509* x509Cert = 0;

kladko's avatar
kladko committed
48
    bool sign = true;
kladko's avatar
kladko committed
49 50
    string certFileName = "";
    string certKeyName = "";
kladko's avatar
kladko committed
51
    string certificate = "";
kladko's avatar
kladko committed
52 53
    string key = "";

kladko's avatar
kladko committed
54 55
    recursive_mutex mutex;

kladko's avatar
kladko committed
56
    zmq::context_t ctx;
kladko's avatar
kladko committed
57

kladko's avatar
kladko committed
58
    string url;
Stan Kladko's avatar
Stan Kladko committed
59

kladko's avatar
kladko committed
60
    // generate random identity
kladko's avatar
kladko committed
61 62

    map<uint64_t , shared_ptr <zmq::socket_t>> clientSockets;
kladko's avatar
kladko committed
63

kladko's avatar
kladko committed
64
    shared_ptr <ZMQMessage> doRequestReply(Json::Value &_req);
Stan Kladko's avatar
Stan Kladko committed
65

kladko's avatar
kladko committed
66
    string doZmqRequestReply(string &_req);
kladko's avatar
kladko committed
67

kladko's avatar
kladko committed
68 69
    uint64_t getProcessID();

kladko's avatar
kladko committed
70
    static string readFileIntoString(const string& _fileName);
kladko's avatar
kladko committed
71

kladko's avatar
kladko committed
72
public:
kladko's avatar
kladko committed
73

kladko's avatar
kladko committed
74 75 76
    ZMQClient(const string &ip, uint16_t port, bool _sign, const string&  _certPathName,
              const string& _certKeyName);

77
    void reconnect();
kladko's avatar
kladko committed
78

kladko's avatar
kladko committed
79 80
    static pair<EVP_PKEY*, X509*>  readPublicKeyFromCertStr(const string& _cert);

kladko's avatar
kladko committed
81 82
    static string signString(EVP_PKEY* _pkey, const string& _str);

83
    static void verifySig(EVP_PKEY* _pubkey, const string& _str, const string& _sig);
kladko's avatar
kladko committed
84

kladko's avatar
kladko committed
85
    string blsSignMessageHash(const std::string &keyShareName, const std::string &messageHash, int t, int n);
kladko's avatar
kladko committed
86

kladko's avatar
kladko committed
87
    string ecdsaSignMessageHash(int base, const std::string &keyName, const std::string &messageHash);
kladko's avatar
kladko committed
88

89
    bool importBLSKeyShare(const std::string& keyShare, const std::string& keyName);
90 91 92 93 94 95 96

    string importECDSAKey(const std::string& keyShare, const std::string& keyName);

    pair<string, string> generateECDSAKey();

    string getECDSAPublicKey(const string& keyName);

Oleh Nikolaiev's avatar
Oleh Nikolaiev committed
97
    bool generateDKGPoly(const string& polyName, int t);
98 99 100 101 102 103 104 105

    Json::Value getVerificationVector(const string& polyName, int t);

    string getSecretShare(const string& polyName, const Json::Value& pubKeys, int t, int n);

    bool dkgVerification(const string& publicShares, const string& ethKeyName,
                        const string& secretShare, int t, int n, int idx);

106
    bool createBLSPrivateKey(const string& blsKeyName, const string& ethKeyName, const string& polyName,
107 108 109 110 111 112
                            const string& secretShare, int t, int n);
    
    Json::Value getBLSPublicKey(const string& blsKeyName);

    Json::Value getAllBlsPublicKeys(const Json::Value& publicShares, int n, int t);

113
    tuple<string, string, Json::Value> complaintResponse(const string& polyName, int t, int n, int idx);
114 115 116 117 118 119 120 121 122 123

    Json::Value multG2(const string& x);

    bool isPolyExists(const string& polyName);

    void getServerStatus();

    string getServerVersion();

    bool deleteBLSKey(const string& blsKeyName);
124

125
    Json::Value getDecryptionShares(const string& blsKeyName, const Json::Value& publicDecryptionValues);
Stan Kladko's avatar
Stan Kladko committed
126 127 128 129
};


#endif //SGXWALLET_ZMQCLIENT_H