text: // Copyright (c) 2018 The Polis Core developers
  // Distributed under the MIT software license, see the accompanying
  // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  
  #include "base58.h"
  #include "consensus/validation.h"
  #include "core_io.h"
  #include "init.h"
  #include "messagesigner.h"
  #include "rpc/server.h"
  #include "utilmoneystr.h"
  #include "validation.h"
  
  #ifdef ENABLE_WALLET
  #include "wallet/coincontrol.h"
  #include "wallet/wallet.h"
  #endif//ENABLE_WALLET
  
  #include "netbase.h"
  
  #include "evo/specialtx.h"
  #include "evo/providertx.h"
  #include "evo/deterministicmns.h"
  #include "evo/simplifiedmns.h"
  
  #include "bls/bls.h"
  
  #ifdef ENABLE_WALLET
  extern UniValue signrawtransaction(const JSONRPCRequest& request);
  extern UniValue sendrawtransaction(const JSONRPCRequest& request);
  #endif//ENABLE_WALLET
  
  std::string GetHelpString(int nParamNum, std::string strParamName)
  {
      static const std::map<std::string, std::string> mapParamHelp = {
          {"collateralAddress",
              "%d. \"collateralAddress\"        (string, required) The dash address to send the collateral to.\n"
              "                              Must be a P2PKH address.\n"
          },
          {"collateralHash",
              "%d. \"collateralHash\"           (string, required) The collateral transaction hash.\n"
          },
          {"collateralIndex",
              "%d. collateralIndex            (numeric, required) The collateral transaction output index.\n"
          },
          {"feeSourceAddress",
              "%d. \"feeSourceAddress\"         (string, optional) If specified wallet will only use coins from this address to fund ProTx.\n"
              "                              If not specified, payoutAddress is the one that is going to be used.\n"
              "                              The private key belonging to this address must be known in your wallet.\n"
          },
          {"fundAddress",
              "%d. \"fundAddress\"              (string, optional) If specified wallet will only use coins from this address to fund ProTx.\n"
              "                              If not specified, payoutAddress is the one that is going to be used.\n"
              "                              The private key belonging to this address must be known in your wallet.\n"
          },
          {"ipAndPort",
              "%d. \"ipAndPort\"                (string, required) IP and port in the form \"IP:PORT\".\n"
              "                              Must be unique on the network. Can be set to 0, which will require a ProUpServTx afterwards.\n"
          },
          {"operatorKey",
              "%d. \"operatorKey\"              (string, required) The operator private key belonging to the\n"
              "                              registered operator public key.\n"
          },
          {"operatorPayoutAddress",
              "%d. \"operatorPayoutAddress\"    (string, optional) The address used for operator reward payments.\n"
              "                              Only allowed when the ProRegTx had a non-zero operatorReward value.\n"
              "                              If set to an empty string, the currently active payout address is reused.\n"
          },
          {"operatorPubKey",
              "%d. \"operatorPubKey\"           (string, required) The operator BLS public key. The private key does not have to be known.\n"
              "                              It has to match the private key which is later used when operating the masternode.\n"
          },
          {"operatorReward",
              "%d. \"operatorReward\"           (numeric, required) The fraction in %% to share with the operator. The value must be\n"
              "                              between 0.00 and 100.00.\n"
          },
          {"ownerAddress",
              "%d. \"ownerAddress\"             (string, required) The dash address to use for payee updates and proposal voting.\n"
              "                              The private key belonging to this address must be known in your wallet. The address must\n"
              "                              be unused and must differ from the collateralAddress\n"
          },
          {"payoutAddress",
              "%d. \"payoutAddress\"            (string, required) The dash address to use for masternode reward payments.\n"
          },
          {"proTxHash",
              "%d. \"proTxHash\"                (string, required) The hash of the initial ProRegTx.\n"
          },
          {"reason",
              "%d. reason                     (numeric, optional) The reason for masternode service revocation.\n"
          },
          {"votingAddress",
              "%d. \"votingAddress\"            (string, required) The voting key address. The private key does not have to be known by your wallet.\n"
              "                              It has to match the private key which is later used when voting on proposals.\n"
              "                              If set to an empty string, ownerAddress will be used.\n"
          },
      };
  
      auto it = mapParamHelp.find(strParamName);
      if (it == mapParamHelp.end())
          throw std::runtime_error(strprintf("FIXME: WRONG PARAM NAME %s!", strParamName));
  
      return strprintf(it->second, nParamNum);
  }
  
  // Allows to specify Dash address or priv key. In case of Dash address, the priv key is taken from the wallet
  static CKey ParsePrivKey(const std::string &strKeyOrAddress, bool allowAddresses = true) {
      CBitcoinAddress address;
      if (allowAddresses && address.SetString(strKeyOrAddress) && address.IsValid()) {
  #ifdef ENABLE_WALLET
          if (!pwalletMain) {
              throw std::runtime_error("addresses not supported when wallet is disabled");
          }
          CKeyID keyId;
          CKey key;
          if (!address.GetKeyID(keyId) || !pwalletMain->GetKey(keyId, key))
              throw std::runtime_error(strprintf("non-wallet or invalid address %s", strKeyOrAddress));
          return key;
  #else//ENABLE_WALLET
          throw std::runtime_error("addresses not supported in no-wallet builds");
  #endif//ENABLE_WALLET
      }
  
      CBitcoinSecret secret;
      if (!secret.SetString(strKeyOrAddress) || !secret.IsValid()) {
          throw std::runtime_error(strprintf("invalid priv-key/address %s", strKeyOrAddress));
      }
      return secret.GetKey();
  }
  
  static CKeyID ParsePubKeyIDFromAddress(const std::string& strAddress, const std::string& paramName)
  {
      CBitcoinAddress address(strAddress);
      CKeyID keyID;
      if (!address.IsValid() || !address.GetKeyID(keyID)) {
          throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("%s must be a valid P2PKH address, not %s", paramName, strAddress));
      }
      return keyID;
  }
  
  static CBLSPublicKey ParseBLSPubKey(const std::string& hexKey, const std::string& paramName)
  {
      auto binKey = ParseHex(hexKey);
      CBLSPublicKey pubKey;
      pubKey.SetBuf(binKey);
      if (!pubKey.IsValid()) {
          throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("%s must be a valid BLS address, not %s", paramName, hexKey));
      }
      return pubKey;
  }
  
  static CBLSSecretKey ParseBLSSecretKey(const std::string& hexKey, const std::string& paramName)
  {
      auto binKey = ParseHex(hexKey);
      CBLSSecretKey secKey;
      secKey.SetBuf(binKey);
      if (!secKey.IsValid()) {
          throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("%s must be a valid BLS secret key", paramName));
      }
      return secKey;
  }
  
  #ifdef ENABLE_WALLET
  
  template<typename SpecialTxPayload>
  static void FundSpecialTx(CMutableTransaction& tx, const SpecialTxPayload& payload, const CTxDestination& fundDest)
  {
      assert(pwalletMain != NULL);
      LOCK2(cs_main, pwalletMain->cs_wallet);
  
      CTxDestination nodest = CNoDestination();
      if (fundDest == nodest) {
          throw JSONRPCError(RPC_INTERNAL_ERROR, "No source of funds specified");
      }
  
      CDataStream ds(SER_NETWORK, PROTOCOL_VERSION);
      ds << payload;
      tx.vExtraPayload.assign(ds.begin(), ds.end());
  
      static CTxOut dummyTxOut(0, CScript() << OP_RETURN);
      std::vector<CRecipient> vecSend;
      bool dummyTxOutAdded = false;
  
      if (tx.vout.empty()) {
          // add dummy txout as CreateTransaction requires at least one recipient
          tx.vout.emplace_back(dummyTxOut);
          dummyTxOutAdded = true;
      }
  
      for (const auto& txOut : tx.vout) {
          CRecipient recipient = {txOut.scriptPubKey, txOut.nValue, false};
          vecSend.push_back(recipient);
      }
  
      CCoinControl coinControl;
      coinControl.destChange = fundDest;
      coinControl.fRequireAllInputs = false;
  
      std::vector<COutput> vecOutputs;
      pwalletMain->AvailableCoins(vecOutputs);
  
      for (const auto& out : vecOutputs) {
          CTxDestination txDest;
          if (ExtractDestination(out.tx->tx->vout[out.i].scriptPubKey, txDest) && txDest == fundDest) {
              coinControl.Select(COutPoint(out.tx->tx->GetHash(), out.i));
          }
      }
  
      if (!coinControl.HasSelected()) {
          throw JSONRPCError(RPC_INTERNAL_ERROR, "No funds at specified address");
      }
  
      CWalletTx wtx;
      CReserveKey reservekey(pwalletMain);
      CAmount nFee;
      int nChangePos = -1;
      std::string strFailReason;
  
      if (!pwalletMain->CreateTransaction(vecSend, wtx, reservekey, nFee, nChangePos, strFailReason, &coinControl, false, ALL_COINS, false, tx.vExtraPayload.size())) {
          throw JSONRPCError(RPC_INTERNAL_ERROR, strFailReason);
      }
  
      tx.vin = wtx.tx->vin;
      tx.vout = wtx.tx->vout;
  
      if (dummyTxOutAdded && tx.vout.size() > 1) {
          // CreateTransaction added a change output, so we don't need the dummy txout anymore.
          // Removing it results in slight overpayment of fees, but we ignore this for now (as it's a very low amount).
          auto it = std::find(tx.vout.begin(), tx.vout.end(), dummyTxOut);
          assert(it != tx.vout.end());
          tx.vout.erase(it);
      }
  }
  
  template<typename SpecialTxPayload>
  static void UpdateSpecialTxInputsHash(const CMutableTransaction& tx, SpecialTxPayload& payload)
  {
      payload.inputsHash = CalcTxInputsHash(tx);
  }
  
  template<typename SpecialTxPayload>
  static void SignSpecialTxPayloadByHash(const CMutableTransaction& tx, SpecialTxPayload& payload, const CKey& key)
  {
      UpdateSpecialTxInputsHash(tx, payload);
      payload.vchSig.clear();
  
      uint256 hash = ::SerializeHash(payload);
      if (!CHashSigner::SignHash(hash, key, payload.vchSig)) {
          throw JSONRPCError(RPC_INTERNAL_ERROR, "failed to sign special tx");
      }
  }
  
  template<typename SpecialTxPayload>
  static void SignSpecialTxPayloadByString(const CMutableTransaction& tx, SpecialTxPayload& payload, const CKey& key)
  {
      UpdateSpecialTxInputsHash(tx, payload);
      payload.vchSig.clear();
  
      std::string m = payload.MakeSignString();
      if (!CMessageSigner::SignMessage(m, payload.vchSig, key)) {
          throw JSONRPCError(RPC_INTERNAL_ERROR, "failed to sign special tx");
      }
  }
  
  template<typename SpecialTxPayload>
  static void SignSpecialTxPayloadByHash(const CMutableTransaction& tx, SpecialTxPayload& payload, const CBLSSecretKey& key)
  {
      UpdateSpecialTxInputsHash(tx, payload);
  
      uint256 hash = ::SerializeHash(payload);
      payload.sig = key.Sign(hash);
  }
  
  static std::string SignAndSendSpecialTx(const CMutableTransaction& tx)
  {
      LOCK(cs_main);
  
      CValidationState state;
      if (!CheckSpecialTx(tx, chainActive.Tip(), state)) {
          throw std::runtime_error(FormatStateMessage(state));
      }
  
      CDataStream ds(SER_NETWORK, PROTOCOL_VERSION);
      ds << tx;
  
      JSONRPCRequest signReqeust;
      signReqeust.params.setArray();
      signReqeust.params.push_back(HexStr(ds.begin(), ds.end()));
      UniValue signResult = signrawtransaction(signReqeust);
  
      JSONRPCRequest sendRequest;
      sendRequest.params.setArray();
      sendRequest.params.push_back(signResult["hex"].get_str());
      return sendrawtransaction(sendRequest).get_str();
  }
  
  void protx_register_fund_help()
  {
      throw std::runtime_error(
              "protx register_fund \"collateralAddress\" \"ipAndPort\" \"ownerAddress\" \"operatorPubKey\" \"votingAddress\" operatorReward \"payoutAddress\" ( \"fundAddress\" )\n"
              "\nCreates, funds and sends a ProTx to the network. The resulting transaction will move 1000 Dash\n"
              "to the address specified by collateralAddress and will then function as the collateral of your\n"
              "masternode.\n"
              "A few of the limitations you see in the arguments are temporary and might be lifted after DIP3\n"
              "is fully deployed.\n"
              "\nArguments:\n"
              + GetHelpString(1, "collateralAddress")
              + GetHelpString(2, "ipAndPort")
              + GetHelpString(3, "ownerAddress")
              + GetHelpString(4, "operatorPubKey")
              + GetHelpString(5, "votingAddress")
              + GetHelpString(6, "operatorReward")
              + GetHelpString(7, "payoutAddress")
              + GetHelpString(8, "fundAddress") +
              "\nResult:\n"
              "\"txid\"                        (string) The transaction id.\n"
              "\nExamples:\n"
              + HelpExampleCli("protx", "register_fund \"XrVhS9LogauRJGJu2sHuryjhpuex4RNPSb\" \"1.2.3.4:1234\" \"Xt9AMWaYSz7tR7Uo7gzXA3m4QmeWgrR3rr\" \"93746e8731c57f87f79b3620a7982924e2931717d49540a85864bd543de11c43fb868fd63e501a1db37e19ed59ae6db4\" \"Xt9AMWaYSz7tR7Uo7gzXA3m4QmeWgrR3rr\" 0 \"XrVhS9LogauRJGJu2sHuryjhpuex4RNPSb\"")
      );
  }
  
  void protx_register_help()
  {
      throw std::runtime_error(
              "protx register \"collateralHash\" collateralIndex \"ipAndPort\" \"ownerAddress\" \"operatorPubKey\" \"votingAddress\" operatorReward \"payoutAddress\" ( \"feeSourceAddress\" )\n"
              "\nSame as \"protx register_fund\", but with an externally referenced collateral.\n"
              "The collateral is specified through \"collateralHash\" and \"collateralIndex\" and must be an unspent\n"
              "transaction output spendable by this wallet. It must also not be used by any other masternode.\n"
              "\nArguments:\n"
              + GetHelpString(1, "collateralHash")
              + GetHelpString(2, "collateralIndex")
              + GetHelpString(3, "ipAndPort")
              + GetHelpString(4, "ownerAddress")
              + GetHelpString(5, "operatorPubKey")
              + GetHelpString(6, "votingAddress")
              + GetHelpString(7, "operatorReward")
              + GetHelpString(8, "payoutAddress")
              + GetHelpString(9, "feeSourceAddress") +
              "\nResult:\n"
              "\"txid\"                        (string) The transaction id.\n"
              "\nExamples:\n"
              + HelpExampleCli("protx", "register \"0123456701234567012345670123456701234567012345670123456701234567\" 0 \"1.2.3.4:1234\" \"Xt9AMWaYSz7tR7Uo7gzXA3m4QmeWgrR3rr\" \"93746e8731c57f87f79b3620a7982924e2931717d49540a85864bd543de11c43fb868fd63e501a1db37e19ed59ae6db4\" \"Xt9AMWaYSz7tR7Uo7gzXA3m4QmeWgrR3rr\" 0 \"XrVhS9LogauRJGJu2sHuryjhpuex4RNPSb\"")
      );
  }
  
  void protx_register_prepare_help()
  {
      throw std::runtime_error(
              "protx register_prepare \"collateralHash\" collateralIndex \"ipAndPort\" \"ownerAddress\" \"operatorPubKey\" \"votingAddress\" operatorReward \"payoutAddress\" ( \"feeSourceAddress\" )\n"
              "\nCreates an unsigned ProTx and returns it. The ProTx must be signed externally with the collateral\n"
              "key and then passed to \"protx register_submit\". The prepared transaction will also contain inputs\n"
              "and outputs to cover fees.\n"
              "\nArguments:\n"
              + GetHelpString(1, "collateralHash")
              + GetHelpString(2, "collateralIndex")
              + GetHelpString(3, "ipAndPort")
              + GetHelpString(4, "ownerAddress")
              + GetHelpString(5, "operatorPubKey")
              + GetHelpString(6, "votingAddress")
              + GetHelpString(7, "operatorReward")
              + GetHelpString(8, "payoutAddress")
              + GetHelpString(9, "feeSourceAddress") +
              "\nResult:\n"
              "{                             (json object)\n"
              "  \"tx\" :                      (string) The serialized ProTx in hex format.\n"
              "  \"collateralAddress\" :       (string) The collateral address.\n"
              "  \"signMessage\" :             (string) The string message that needs to be signed with\n"
              "                              the collateral key.\n"
              "}\n"
              "\nExamples:\n"
              + HelpExampleCli("protx", "register_prepare \"0123456701234567012345670123456701234567012345670123456701234567\" 0 \"1.2.3.4:1234\" \"Xt9AMWaYSz7tR7Uo7gzXA3m4QmeWgrR3rr\" \"93746e8731c57f87f79b3620a7982924e2931717d49540a85864bd543de11c43fb868fd63e501a1db37e19ed59ae6db4\" \"Xt9AMWaYSz7tR7Uo7gzXA3m4QmeWgrR3rr\" 0 \"XrVhS9LogauRJGJu2sHuryjhpuex4RNPSb\"")
      );
  }
  
  void protx_register_submit_help()
  {
      throw std::runtime_error(
              "protx register_submit \"tx\" \"sig\"\n"
              "\nSubmits the specified ProTx to the network. This command will also sign the inputs of the transaction\n"
              "which were previously added by \"protx register_prepare\" to cover transaction fees\n"
              "\nArguments:\n"
              "1. \"tx\"                 (string, required) The serialized transaction previously returned by \"protx register_prepare\"\n"
              "2. \"sig\"                (string, required) The signature signed with the collateral key. Must be in base64 format.\n"
              "\nResult:\n"
              "\"txid\"                  (string) The transaction id.\n"
              "\nExamples:\n"
              + HelpExampleCli("protx", "register_submit \"tx\" \"sig\"")
      );
  }
  
  // handles register, register_prepare and register_fund in one method
  UniValue protx_register(const JSONRPCRequest& request)
  {
      bool isExternalRegister = request.params[0].get_str() == "register";
      bool isFundRegister = request.params[0].get_str() == "register_fund";
      bool isPrepareRegister = request.params[0].get_str() == "register_prepare";
  
      if (isFundRegister && (request.fHelp || (request.params.size() != 8 && request.params.size() != 9))) {
          protx_register_fund_help();
      } else if (isExternalRegister && (request.fHelp || (request.params.size() != 9 && request.params.size() != 10))) {
          protx_register_help();
      } else if (isPrepareRegister && (request.fHelp || (request.params.size() != 9 && request.params.size() != 10))) {
          protx_register_prepare_help();
      }
  
      size_t paramIdx = 1;
  
      CAmount collateralAmount = 1000 * COIN;
  
      CMutableTransaction tx;
      tx.nVersion = 3;
      tx.nType = TRANSACTION_PROVIDER_REGISTER;
  
      CProRegTx ptx;
      ptx.nVersion = CProRegTx::CURRENT_VERSION;
  
      if (isFundRegister) {
          CBitcoinAddress collateralAddress(request.params[paramIdx].get_str());
          if (!collateralAddress.IsValid()) {
              throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("invalid collaterall address: %s", request.params[paramIdx].get_str()));
          }
          CScript collateralScript = GetScriptForDestination(collateralAddress.Get());
  
          CTxOut collateralTxOut(collateralAmount, collateralScript);
          tx.vout.emplace_back(collateralTxOut);
  
          paramIdx++;
      } else {
          uint256 collateralHash = ParseHashV(request.params[paramIdx], "collateralHash");
          int32_t collateralIndex = ParseInt32V(request.params[paramIdx + 1], "collateralIndex");
          if (collateralHash.IsNull() || collateralIndex < 0) {
              throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("invalid hash or index: %s-%d", collateralHash.ToString(), collateralIndex));
          }
  
          ptx.collateralOutpoint = COutPoint(collateralHash, (uint32_t)collateralIndex);
          paramIdx += 2;
  
          // TODO unlock on failure
          LOCK(pwalletMain->cs_wallet);
          pwalletMain->LockCoin(ptx.collateralOutpoint);
      }
  
      if (request.params[paramIdx].get_str() != "") {
          if (!Lookup(request.params[paramIdx].get_str().c_str(), ptx.addr, Params().GetDefaultPort(), false)) {
              throw std::runtime_error(strprintf("invalid network address %s", request.params[paramIdx].get_str()));
          }
      }
  
      CKey keyOwner = ParsePrivKey(request.params[paramIdx + 1].get_str(), true);
      CBLSPublicKey pubKeyOperator = ParseBLSPubKey(request.params[paramIdx + 2].get_str(), "operator BLS address");
      CKeyID keyIDVoting = keyOwner.GetPubKey().GetID();
      if (request.params[paramIdx + 3].get_str() != "") {
          keyIDVoting = ParsePubKeyIDFromAddress(request.params[paramIdx + 3].get_str(), "voting address");
      }
  
      int64_t operatorReward;
      if (!ParseFixedPoint(request.params[paramIdx + 4].getValStr(), 2, &operatorReward)) {
          throw JSONRPCError(RPC_INVALID_PARAMETER, "operatorReward must be a number");
      }
      if (operatorReward < 0 || operatorReward > 10000) {
          throw JSONRPCError(RPC_INVALID_PARAMETER, "operatorReward must be between 0.00 and 100.00");
      }
      ptx.nOperatorReward = operatorReward;
  
      CBitcoinAddress payoutAddress(request.params[paramIdx + 5].get_str());
      if (!payoutAddress.IsValid()) {
          throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("invalid payout address: %s", request.params[paramIdx + 5].get_str()));
      }
  
      ptx.keyIDOwner = keyOwner.GetPubKey().GetID();
      ptx.pubKeyOperator = pubKeyOperator;
      ptx.keyIDVoting = keyIDVoting;
      ptx.scriptPayout = GetScriptForDestination(payoutAddress.Get());
  
      if (!isFundRegister) {
          // make sure fee calculation works
          ptx.vchSig.resize(65);
      }
  
      CBitcoinAddress fundAddress = payoutAddress;
      if (request.params.size() > paramIdx + 6) {
          fundAddress = CBitcoinAddress(request.params[paramIdx + 6].get_str());
          if (!fundAddress.IsValid())
              throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, std::string("Invalid Dash address: ") + request.params[paramIdx + 6].get_str());
      }
  
      FundSpecialTx(tx, ptx, fundAddress.Get());
      UpdateSpecialTxInputsHash(tx, ptx);
  
      if (isFundRegister) {
          uint32_t collateralIndex = (uint32_t) -1;
          for (uint32_t i = 0; i < tx.vout.size(); i++) {
              if (tx.vout[i].nValue == collateralAmount) {
                  collateralIndex = i;
                  break;
              }
          }
          assert(collateralIndex != (uint32_t) -1);
          ptx.collateralOutpoint.n = collateralIndex;
  
          SetTxPayload(tx, ptx);
          return SignAndSendSpecialTx(tx);
      } else {
          // referencing external collateral
  
          Coin coin;
          if (!GetUTXOCoin(ptx.collateralOutpoint, coin)) {
              throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("collateral not found: %s", ptx.collateralOutpoint.ToStringShort()));
          }
          CTxDestination txDest;
          CKeyID keyID;
          if (!ExtractDestination(coin.out.scriptPubKey, txDest) || !CBitcoinAddress(txDest).GetKeyID(keyID)) {
              throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("collateral type not supported: %s", ptx.collateralOutpoint.ToStringShort()));
          }
  
          if (isPrepareRegister) {
              // external signing with collateral key
              ptx.vchSig.clear();
              SetTxPayload(tx, ptx);
  
              UniValue ret(UniValue::VOBJ);
              ret.push_back(Pair("tx", EncodeHexTx(tx)));
              ret.push_back(Pair("collateralAddress", CBitcoinAddress(txDest).ToString()));
              ret.push_back(Pair("signMessage", ptx.MakeSignString()));
              return ret;
          } else {
              // lets prove we own the collateral
              CKey key;
              if (!pwalletMain->GetKey(keyID, key)) {
                  throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("collateral key not in wallet: %s", CBitcoinAddress(keyID).ToString()));
              }
              SignSpecialTxPayloadByString(tx, ptx, key);
              SetTxPayload(tx, ptx);
              return SignAndSendSpecialTx(tx);
          }
      }
  }
  
  UniValue protx_register_submit(const JSONRPCRequest& request)
  {
      if (request.fHelp || request.params.size() != 3) {
          protx_register_submit_help();
      }
  
      CMutableTransaction tx;
      if (!DecodeHexTx(tx, request.params[1].get_str())) {
          throw JSONRPCError(RPC_INVALID_PARAMETER, "transaction not deserializable");
      }
      if (tx.nType != TRANSACTION_PROVIDER_REGISTER) {
          throw JSONRPCError(RPC_INVALID_PARAMETER, "transaction not a ProRegTx");
      }
      CProRegTx ptx;
      if (!GetTxPayload(tx, ptx)) {
          throw JSONRPCError(RPC_INVALID_PARAMETER, "transaction payload not deserializable");
      }
      if (!ptx.vchSig.empty()) {
          throw JSONRPCError(RPC_INVALID_PARAMETER, "payload signature not empty");
      }
  
      ptx.vchSig = DecodeBase64(request.params[2].get_str().c_str());
  
      SetTxPayload(tx, ptx);
      return SignAndSendSpecialTx(tx);
  }
  
  void protx_update_service_help()
  {
      throw std::runtime_error(
              "protx update_service \"proTxHash\" \"ipAndPort\" \"operatorKey\" (\"operatorPayoutAddress\" \"feeSourceAddress\" )\n"
              "\nCreates and sends a ProUpServTx to the network. This will update the IP address\n"
              "of a masternode.\n"
              "If this is done for a masternode that got PoSe-banned, the ProUpServTx will also revive this masternode.\n"
              "\nArguments:\n"
              + GetHelpString(1, "proTxHash")
              + GetHelpString(2, "ipAndPort")
              + GetHelpString(3, "operatorKey")
              + GetHelpString(4, "operatorPayoutAddress")
              + GetHelpString(5, "feeSourceAddress") +
              "\nResult:\n"
              "\"txid\"                        (string) The transaction id.\n"
              "\nExamples:\n"
              + HelpExampleCli("protx", "update_service \"0123456701234567012345670123456701234567012345670123456701234567\" \"1.2.3.4:1234\" 5a2e15982e62f1e0b7cf9783c64cf7e3af3f90a52d6c40f6f95d624c0b1621cd")
      );
  }
  
  UniValue protx_update_service(const JSONRPCRequest& request)
  {
      if (request.fHelp || (request.params.size() < 4 || request.params.size() > 6))
          protx_update_service_help();
  
      CProUpServTx ptx;
      ptx.nVersion = CProRegTx::CURRENT_VERSION;
      ptx.proTxHash = ParseHashV(request.params[1], "proTxHash");
  
      if (!Lookup(request.params[2].get_str().c_str(), ptx.addr, Params().GetDefaultPort(), false)) {
          throw std::runtime_error(strprintf("invalid network address %s", request.params[2].get_str()));
      }
  
      CBLSSecretKey keyOperator = ParseBLSSecretKey(request.params[3].get_str(), "operatorKey");
  
      auto dmn = deterministicMNManager->GetListAtChainTip().GetMN(ptx.proTxHash);
      if (!dmn) {
          throw std::runtime_error(strprintf("masternode with proTxHash %s not found", ptx.proTxHash.ToString()));
      }
  
      if (keyOperator.GetPublicKey() != dmn->pdmnState->pubKeyOperator) {
          throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("the operator key does not belong to the registered public key"));
      }
  
      CMutableTransaction tx;
      tx.nVersion = 3;
      tx.nType = TRANSACTION_PROVIDER_UPDATE_SERVICE;
  
      // param operatorPayoutAddress
      if (request.params.size() >= 5) {
          if (request.params[4].get_str().empty()) {
              ptx.scriptOperatorPayout = dmn->pdmnState->scriptOperatorPayout;
          } else {
              CBitcoinAddress payoutAddress(request.params[4].get_str());
              if (!payoutAddress.IsValid()) {
                  throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("invalid operator payout address: %s", request.params[4].get_str()));
              }
              ptx.scriptOperatorPayout = GetScriptForDestination(payoutAddress.Get());
          }
      } else {
          ptx.scriptOperatorPayout = dmn->pdmnState->scriptOperatorPayout;
      }
  
      CTxDestination feeSource;
  
      // param feeSourceAddress
      if (request.params.size() >= 6) {
          CBitcoinAddress feeSourceAddress = CBitcoinAddress(request.params[5].get_str());
          if (!feeSourceAddress.IsValid())
              throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, std::string("Invalid Dash address: ") + request.params[5].get_str());
          feeSource = feeSourceAddress.Get();
      } else {
          if (ptx.scriptOperatorPayout != CScript()) {
              // use operator reward address as default source for fees
              ExtractDestination(ptx.scriptOperatorPayout, feeSource);
          } else {
              // use payout address as default source for fees
              ExtractDestination(dmn->pdmnState->scriptPayout, feeSource);
          }
      }
  
      FundSpecialTx(tx, ptx, feeSource);
  
      SignSpecialTxPayloadByHash(tx, ptx, keyOperator);
      SetTxPayload(tx, ptx);
  
      return SignAndSendSpecialTx(tx);
  }
  
  void protx_update_registrar_help()
  {
      throw std::runtime_error(
              "protx update_registrar \"proTxHash\" \"operatorPubKey\" \"votingAddress\" \"payoutAddress\" ( \"feeSourceAddress\" )\n"
              "\nCreates and sends a ProUpRegTx to the network. This will update the operator key, voting key and payout\n"
              "address of the masternode specified by \"proTxHash\".\n"
              "The owner key of the masternode must be known to your wallet.\n"
              "\nArguments:\n"
              + GetHelpString(1, "proTxHash")
              + GetHelpString(2, "operatorPubKey")
              + GetHelpString(3, "votingAddress")
              + GetHelpString(4, "payoutAddress")
              + GetHelpString(5, "feeSourceAddress") +
              "\nResult:\n"
              "\"txid\"                        (string) The transaction id.\n"
              "\nExamples:\n"
              + HelpExampleCli("protx", "update_registrar \"0123456701234567012345670123456701234567012345670123456701234567\" \"982eb34b7c7f614f29e5c665bc3605f1beeef85e3395ca12d3be49d2868ecfea5566f11cedfad30c51b2403f2ad95b67\" \"XwnLY9Tf7Zsef8gMGL2fhWA9ZmMjt4KPwG\"")
      );
  }
  
  UniValue protx_update_registrar(const JSONRPCRequest& request)
  {
      if (request.fHelp || (request.params.size() != 5 && request.params.size() != 6)) {
          protx_update_registrar_help();
      }
  
      CProUpRegTx ptx;
      ptx.nVersion = CProRegTx::CURRENT_VERSION;
      ptx.proTxHash = ParseHashV(request.params[1], "proTxHash");
  
      auto dmn = deterministicMNManager->GetListAtChainTip().GetMN(ptx.proTxHash);
      if (!dmn) {
          throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("masternode %s not found", ptx.proTxHash.ToString()));
      }
      ptx.pubKeyOperator = dmn->pdmnState->pubKeyOperator;
      ptx.keyIDVoting = dmn->pdmnState->keyIDVoting;
      ptx.scriptPayout = dmn->pdmnState->scriptPayout;
  
      if (request.params[2].get_str() != "") {
          ptx.pubKeyOperator = ParseBLSPubKey(request.params[2].get_str(), "operator BLS address");
      }
      if (request.params[3].get_str() != "") {
          ptx.keyIDVoting = ParsePubKeyIDFromAddress(request.params[3].get_str(), "voting address");
      }
  
      CBitcoinAddress payoutAddress(request.params[4].get_str());
      if (!payoutAddress.IsValid()) {
          throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("invalid payout address: %s", request.params[4].get_str()));
      }
      ptx.scriptPayout = GetScriptForDestination(payoutAddress.Get());
  
      CKey keyOwner;
      if (!pwalletMain->GetKey(dmn->pdmnState->keyIDOwner, keyOwner)) {
          throw std::runtime_error(strprintf("Private key for owner address %s not found in your wallet", CBitcoinAddress(dmn->pdmnState->keyIDOwner).ToString()));
      }
  
      CMutableTransaction tx;
      tx.nVersion = 3;
      tx.nType = TRANSACTION_PROVIDER_UPDATE_REGISTRAR;
  
      // make sure we get anough fees added
      ptx.vchSig.resize(65);
  
      CBitcoinAddress feeSourceAddress = payoutAddress;
      if (request.params.size() > 5) {
          feeSourceAddress = CBitcoinAddress(request.params[5].get_str());
          if (!feeSourceAddress.IsValid())
              throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, std::string("Invalid Dash address: ") + request.params[5].get_str());
      }
  
      FundSpecialTx(tx, ptx, feeSourceAddress.Get());
      SignSpecialTxPayloadByHash(tx, ptx, keyOwner);
      SetTxPayload(tx, ptx);
  
      return SignAndSendSpecialTx(tx);
  }
  
  void protx_revoke_help()
  {
      throw std::runtime_error(
              "protx revoke \"proTxHash\" \"operatorKey\" ( reason \"feeSourceAddress\")\n"
              "\nCreates and sends a ProUpRevTx to the network. This will revoke the operator key of the masternode and\n"
              "put it into the PoSe-banned state. It will also set the service field of the masternode\n"
              "to zero. Use this in case your operator key got compromised or you want to stop providing your service\n"
              "to the masternode owner.\n"
              "\nArguments:\n"
              + GetHelpString(1, "proTxHash")
              + GetHelpString(2, "operatorKey")
              + GetHelpString(3, "reason")
              + GetHelpString(4, "feeSourceAddress") +
              "\nResult:\n"
              "\"txid\"                        (string) The transaction id.\n"
              "\nExamples:\n"
              + HelpExampleCli("protx", "revoke \"0123456701234567012345670123456701234567012345670123456701234567\" \"072f36a77261cdd5d64c32d97bac417540eddca1d5612f416feb07ff75a8e240\"")
      );
  }
  
  UniValue protx_revoke(const JSONRPCRequest& request)
  {
      if (request.fHelp || (request.params.size() < 3 || request.params.size() > 5)) {
          protx_revoke_help();
      }
  
      CProUpRevTx ptx;
      ptx.nVersion = CProRegTx::CURRENT_VERSION;
      ptx.proTxHash = ParseHashV(request.params[1], "proTxHash");
  
      CBLSSecretKey keyOperator = ParseBLSSecretKey(request.params[2].get_str(), "operatorKey");
  
      if (request.params.size() > 3) {
          int32_t nReason = ParseInt32V(request.params[3], "reason");
          if (nReason < 0 || nReason > CProUpRevTx::REASON_LAST) {
              throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("invalid reason %d, must be between 0 and %d", nReason, CProUpRevTx::REASON_LAST));
          }
          ptx.nReason = (uint16_t)nReason;
      }
  
      auto dmn = deterministicMNManager->GetListAtChainTip().GetMN(ptx.proTxHash);
      if (!dmn) {
          throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("masternode %s not found", ptx.proTxHash.ToString()));
      }
  
      if (keyOperator.GetPublicKey() != dmn->pdmnState->pubKeyOperator) {
          throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("the operator key does not belong to the registered public key"));
      }
  
      CMutableTransaction tx;
      tx.nVersion = 3;
      tx.nType = TRANSACTION_PROVIDER_UPDATE_REVOKE;
  
      if (request.params.size() > 4) {
          CBitcoinAddress feeSourceAddress = CBitcoinAddress(request.params[4].get_str());
          if (!feeSourceAddress.IsValid())
              throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, std::string("Invalid Dash address: ") + request.params[4].get_str());
          FundSpecialTx(tx, ptx, feeSourceAddress.Get());
      } else if (dmn->pdmnState->scriptOperatorPayout != CScript()) {
          // Using funds from previousely specified operator payout address
          CTxDestination txDest;
          ExtractDestination(dmn->pdmnState->scriptOperatorPayout, txDest);
          FundSpecialTx(tx, ptx, txDest);
      } else if (dmn->pdmnState->scriptPayout != CScript()) {
          // Using funds from previousely specified masternode payout address
          CTxDestination txDest;
          ExtractDestination(dmn->pdmnState->scriptPayout, txDest);
          FundSpecialTx(tx, ptx, txDest);
      } else {
          throw JSONRPCError(RPC_INTERNAL_ERROR, "No payout or fee source addresses found, can't revoke");
      }
  
      SignSpecialTxPayloadByHash(tx, ptx, keyOperator);
      SetTxPayload(tx, ptx);
  
      return SignAndSendSpecialTx(tx);
  }
  #endif//ENABLE_WALLET
  
  void protx_list_help()
  {
      throw std::runtime_error(
              "protx list (\"type\" \"detailed\" \"height\")\n"
              "\nLists all ProTxs in your wallet or on-chain, depending on the given type.\n"
              "If \"type\" is not specified, it defaults to \"registered\".\n"
              "If \"detailed\" is not specified, it defaults to \"false\" and only the hashes of the ProTx will be returned.\n"
              "If \"height\" is not specified, it defaults to the current chain-tip.\n"
              "\nAvailable types:\n"
              "  registered   - List all ProTx which are registered at the given chain height.\n"
              "                 This will also include ProTx which failed PoSe verfication.\n"
              "  valid        - List only ProTx which are active/valid at the given chain height.\n"
  #ifdef ENABLE_WALLET
              "  wallet       - List only ProTx which are found in your wallet at the given chain height.\n"
              "                 This will also include ProTx which failed PoSe verfication.\n"
  #endif
      );
  }
  
  static bool CheckWalletOwnsKey(const CKeyID& keyID) {
  #ifndef ENABLE_WALLET
      return false;
  #else
      if (!pwalletMain) {
          return false;
      }
      return pwalletMain->HaveKey(keyID);
  #endif
  }
  
  static bool CheckWalletOwnsScript(const CScript& script) {
  #ifndef ENABLE_WALLET
      return false;
  #else
      if (!pwalletMain) {
          return false;
      }
  
      CTxDestination dest;
      if (ExtractDestination(script, dest)) {
          if ((boost::get<CKeyID>(&dest) && pwalletMain->HaveKey(*boost::get<CKeyID>(&dest))) || (boost::get<CScriptID>(&dest) && pwalletMain->HaveCScript(*boost::get<CScriptID>(&dest)))) {
              return true;
          }
      }
      return false;
  #endif
  }
  
  UniValue BuildDMNListEntry(const CDeterministicMNCPtr& dmn, bool detailed)
  {
      if (!detailed) {
          return dmn->proTxHash.ToString();
      }
  
      UniValue o(UniValue::VOBJ);
  
      dmn->ToJson(o);
  
      int confirmations = GetUTXOConfirmations(dmn->collateralOutpoint);
      o.push_back(Pair("confirmations", confirmations));
  
      bool hasOwnerKey = CheckWalletOwnsKey(dmn->pdmnState->keyIDOwner);
      bool hasOperatorKey = false; //CheckWalletOwnsKey(dmn->pdmnState->keyIDOperator);
      bool hasVotingKey = CheckWalletOwnsKey(dmn->pdmnState->keyIDVoting);
  
      bool ownsCollateral = false;
      CTransactionRef collateralTx;
      uint256 tmpHashBlock;
      if (GetTransaction(dmn->collateralOutpoint.hash, collateralTx, Params().GetConsensus(), tmpHashBlock)) {
          ownsCollateral = CheckWalletOwnsScript(collateralTx->vout[dmn->collateralOutpoint.n].scriptPubKey);
      }
  
      UniValue walletObj(UniValue::VOBJ);
      walletObj.push_back(Pair("hasOwnerKey", hasOwnerKey));
      walletObj.push_back(Pair("hasOperatorKey", hasOperatorKey));
      walletObj.push_back(Pair("hasVotingKey", hasVotingKey));
      walletObj.push_back(Pair("ownsCollateral", ownsCollateral));
      walletObj.push_back(Pair("ownsPayeeScript", CheckWalletOwnsScript(dmn->pdmnState->scriptPayout)));
      walletObj.push_back(Pair("ownsOperatorRewardScript", CheckWalletOwnsScript(dmn->pdmnState->scriptOperatorPayout)));
      o.push_back(Pair("wallet", walletObj));
  
      return o;
  }
  
  UniValue protx_list(const JSONRPCRequest& request)
  {
      if (request.fHelp) {
          protx_list_help();
      }
  
  #ifdef ENABLE_WALLET
      bool hasWallet = pwalletMain != nullptr;
  #else
      bool hasWallet = false;
  #endif
  
      std::string type = "registered";
      if (request.params.size() > 1) {
          type = request.params[1].get_str();
      }
  
      UniValue ret(UniValue::VARR);
  
      LOCK(cs_main);
  
      if (type == "wallet") {
          if (!hasWallet) {
              throw std::runtime_error("\"protx list wallet\" not supported when wallet is disabled");
          }
  #ifdef ENABLE_WALLET
          LOCK2(cs_main, pwalletMain->cs_wallet);
  
          if (request.params.size() > 3) {
              protx_list_help();
          }
  
          bool detailed = request.params.size() > 2 ? ParseBoolV(request.params[2], "detailed") : false;
  
          int height = request.params.size() > 3 ? ParseInt32V(request.params[3], "height") : chainActive.Height();
          if (height < 1 || height > chainActive.Height()) {
              throw JSONRPCError(RPC_INVALID_PARAMETER, "invalid height specified");
          }
  
          std::vector<COutPoint> vOutpts;
          pwalletMain->ListProTxCoins(vOutpts);
          std::set<COutPoint> setOutpts;
          for (const auto& outpt : vOutpts) {
              setOutpts.emplace(outpt);
          }
  
          CDeterministicMNList mnList = deterministicMNManager->GetListForBlock(chainActive[height]->GetBlockHash());
          mnList.ForEachMN(false, [&](const CDeterministicMNCPtr& dmn) {
              if (setOutpts.count(dmn->collateralOutpoint) ||
                  CheckWalletOwnsKey(dmn->pdmnState->keyIDOwner) ||
                  CheckWalletOwnsKey(dmn->pdmnState->keyIDVoting) ||
                  CheckWalletOwnsScript(dmn->pdmnState->scriptPayout) ||
                  CheckWalletOwnsScript(dmn->pdmnState->scriptOperatorPayout)) {
                  ret.push_back(BuildDMNListEntry(dmn, detailed));
              }
          });
  #endif
      } else if (type == "valid" || type == "registered") {
          if (request.params.size() > 4) {
              protx_list_help();
          }
  
          LOCK(cs_main);
  
          bool detailed = request.params.size() > 2 ? ParseBoolV(request.params[2], "detailed") : false;
  
          int height = request.params.size() > 3 ? ParseInt32V(request.params[3], "height") : chainActive.Height();
          if (height < 1 || height > chainActive.Height()) {
              throw JSONRPCError(RPC_INVALID_PARAMETER, "invalid height specified");
          }
  
          CDeterministicMNList mnList = deterministicMNManager->GetListForBlock(chainActive[height]->GetBlockHash());
          bool onlyValid = type == "valid";
          mnList.ForEachMN(onlyValid, [&](const CDeterministicMNCPtr& dmn) {
              ret.push_back(BuildDMNListEntry(dmn, detailed));
          });
      } else {
          throw JSONRPCError(RPC_INVALID_PARAMETER, "invalid type specified");
      }
  
      return ret;
  }
  
  void protx_info_help()
  {
      throw std::runtime_error(
              "protx info \"proTxHash\"\n"
              "\nReturns detailed information about a deterministic masternode.\n"
              "\nArguments:\n"
              + GetHelpString(1, "proTxHash") +
              "\nResult:\n"
              "{                             (json object) Details about a specific deterministic masternode\n"
              "}\n"
              "\nExamples:\n"
              + HelpExampleCli("protx", "info \"0123456701234567012345670123456701234567012345670123456701234567\"")
      );
  }
  
  UniValue protx_info(const JSONRPCRequest& request)
  {
      if (request.fHelp || request.params.size() != 2) {
          protx_info_help();
      }
  
      uint256 proTxHash = ParseHashV(request.params[1], "proTxHash");
      auto mnList = deterministicMNManager->GetListAtChainTip();
      auto dmn = mnList.GetMN(proTxHash);
      if (!dmn) {
          throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("%s not found", proTxHash.ToString()));
      }
      return BuildDMNListEntry(dmn, true);
  }
  
  void protx_diff_help()
  {
      throw std::runtime_error(
              "protx diff \"baseBlock\" \"block\"\n"
              "\nCalculates a diff between two deterministic masternode lists. The result also contains proof data.\n"
              "\nArguments:\n"
              "1. \"baseBlock\"           (numeric, required) The starting block height.\n"
              "2. \"block\"               (numeric, required) The ending block height.\n"
      );
  }
  
  static uint256 ParseBlock(const UniValue& v, std::string strName)
  {
      AssertLockHeld(cs_main);
  
      try {
          return ParseHashV(v, strName);
      } catch (...) {
          int h = ParseInt32V(v, strName);
          if (h < 1 || h > chainActive.Height())
              throw std::runtime_error(strprintf("%s must be a block hash or chain height and not %s", strName, v.getValStr()));
          return *chainActive[h]->phashBlock;
      }
  }
  
  UniValue protx_diff(const JSONRPCRequest& request)
  {
      if (request.fHelp || request.params.size() != 3) {
          protx_diff_help();
      }
  
      LOCK(cs_main);
      uint256 baseBlockHash = ParseBlock(request.params[1], "baseBlock");
      uint256 blockHash = ParseBlock(request.params[2], "block");
  
      CSimplifiedMNListDiff mnListDiff;
      std::string strError;
      if (!BuildSimplifiedMNListDiff(baseBlockHash, blockHash, mnListDiff, strError)) {
          throw std::runtime_error(strError);
      }
  
      UniValue ret;
      mnListDiff.ToJson(ret);
      return ret;
  }
  
  [[ noreturn ]] void protx_help()
  {
      throw std::runtime_error(
              "protx \"command\" ...\n"
              "Set of commands to execute ProTx related actions.\n"
              "To get help on individual commands, use \"help protx command\".\n"
              "\nArguments:\n"
              "1. \"command\"        (string, required) The command to execute\n"
              "\nAvailable commands:\n"
  #ifdef ENABLE_WALLET
              "  register          - Create and send ProTx to network\n"
              "  register_fund     - Fund, create and send ProTx to network\n"
              "  register_prepare  - Create an unsigned ProTx\n"
              "  register_submit   - Sign and submit a ProTx\n"
  #endif
              "  list              - List ProTxs\n"
              "  info              - Return information about a ProTx\n"
  #ifdef ENABLE_WALLET
              "  update_service    - Create and send ProUpServTx to network\n"
              "  update_registrar  - Create and send ProUpRegTx to network\n"
              "  revoke            - Create and send ProUpRevTx to network\n"
  #endif
              "  diff              - Calculate a diff and a proof between two masternode lists\n"
      );
  }
  
  UniValue protx(const JSONRPCRequest& request)
  {
      if (request.fHelp && request.params.empty()) {
          protx_help();
      }
  
      std::string command;
      if (request.params.size() >= 1) {
          command = request.params[0].get_str();
      }
  
  #ifdef ENABLE_WALLET
      bool hasWallet = pwalletMain != nullptr;
  #else
      bool hasWallet = false;
  #endif
  
      auto checkWallet = [&](const std::string& command) {
          if (!hasWallet) {
              throw std::runtime_error(strprintf("%s not supported when wallet is disabled", command));
          }
      };
  
  #ifdef ENABLE_WALLET
      if (command == "register" || command == "register_fund" || command == "register_prepare") {
          checkWallet(command);
          return protx_register(request);
      } if (command == "register_submit") {
          checkWallet(command);
          return protx_register_submit(request);
      } else if (command == "update_service") {
          checkWallet(command);
          return protx_update_service(request);
      } else if (command == "update_registrar") {
          checkWallet(command);
          return protx_update_registrar(request);
      } else if (command == "revoke") {
          checkWallet(command);
          return protx_revoke(request);
      } else
  #endif
      if (command == "list") {
          return protx_list(request);
      } else if (command == "info") {
          return protx_info(request);
      } else if (command == "diff") {
          return protx_diff(request);
      } else {
          protx_help();
      }
  }
  
  void bls_generate_help()
  {
      throw std::runtime_error(
              "bls generate\n"
              "\nReturns a BLS secret/public key pair.\n"
              "\nResult:\n"
              "{\n"
              "  \"secret\": \"xxxx\",        (string) BLS secret key\n"
              "  \"public\": \"xxxx\",        (string) BLS public key\n"
              "}\n"
              "\nExamples:\n"
              + HelpExampleCli("bls generate", "")
      );
  }
  
  UniValue bls_generate(const JSONRPCRequest& request)
  {
      if (request.fHelp || request.params.size() != 1) {
          bls_generate_help();
      }
  
      CBLSSecretKey sk;
      sk.MakeNewKey();
  
      UniValue ret(UniValue::VOBJ);
      ret.push_back(Pair("secret", sk.ToString()));
      ret.push_back(Pair("public", sk.GetPublicKey().ToString()));
      return ret;
  }
  
  [[ noreturn ]] void bls_help()
  {
      throw std::runtime_error(
              "bls \"command\" ...\n"
              "Set of commands to execute BLS related actions.\n"
              "To get help on individual commands, use \"help bls command\".\n"
              "\nArguments:\n"
              "1. \"command\"        (string, required) The command to execute\n"
              "\nAvailable commands:\n"
              "  generate          - Create a BLS secret/public key pair\n"
              );
  }
  
  UniValue _bls(const JSONRPCRequest& request)
  {
      if (request.fHelp && request.params.empty()) {
          bls_help();
      }
  
      std::string command;
      if (request.params.size() >= 1) {
          command = request.params[0].get_str();
      }
  
      if (command == "generate") {
          return bls_generate(request);
      } else {
          bls_help();
      }
  }
  
  static const CRPCCommand commands[] =
  { //  category              name                      actor (function)         okSafeMode
    //  --------------------- ------------------------  -----------------------  ----------
      { "evo",                "bls",                    &_bls,                   false, {}  },
      { "evo",                "protx",                  &protx,                  false, {}  },
  };
  
  void RegisterEvoRPCCommands(CRPCTable &tableRPC)
  {
      for (unsigned int vcidx = 0; vcidx < ARRAYLEN(commands); vcidx++) {
          tableRPC.appendCommand(commands[vcidx].name, &commands[vcidx]);
      }
  }
  