text: // Copyright (c) 2010 Satoshi Nakamoto
  // Copyright (c) 2009-2015 The Bitcoin Core developers
  // Copyright (c) 2014-2021 The Dash Core developers
  // Distributed under the MIT software license, see the accompanying
  // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  
  #include <chain.h>
  #include <coins.h>
  #include <consensus/validation.h>
  #include <core_io.h>
  #include <init.h>
  #include <keystore.h>
  #include <validation.h>
  #include <validationinterface.h>
  #include <key_io.h>
  #include <merkleblock.h>
  #include <net.h>
  #include <policy/policy.h>
  #include <primitives/transaction.h>
  #include <rpc/rawtransaction.h>
  #include <rpc/server.h>
  #include <script/script.h>
  #include <script/script_error.h>
  #include <script/sign.h>
  #include <script/standard.h>
  #include <txmempool.h>
  #include <uint256.h>
  #include <utilstrencodings.h>
  #ifdef ENABLE_WALLET
  #include <wallet/rpcwallet.h>
  #endif
  
  #include <evo/specialtx.h>
  #include <evo/providertx.h>
  #include <evo/cbtx.h>
  
  #include <llmq/quorums_chainlocks.h>
  #include <llmq/quorums_commitment.h>
  #include <llmq/quorums_instantsend.h>
  
  #include <future>
  #include <stdint.h>
  
  #include <univalue.h>
  
  
  void TxToJSON(const CTransaction& tx, const uint256 hashBlock, UniValue& entry)
  {
      // Call into TxToUniv() in bitcoin-common to decode the transaction hex.
      //
      // Blockchain contextual information (confirmations and blocktime) is not
      // available to code in bitcoin-common, so we query them here and push the
      // data into the returned UniValue.
  
      uint256 txid = tx.GetHash();
  
      // Add spent information if spentindex is enabled
      CSpentIndexTxInfo txSpentInfo;
      for (const auto& txin : tx.vin) {
          if (!tx.IsCoinBase()) {
              CSpentIndexValue spentInfo;
              CSpentIndexKey spentKey(txin.prevout.hash, txin.prevout.n);
              if (GetSpentIndex(spentKey, spentInfo)) {
                  txSpentInfo.mSpentInfo.emplace(spentKey, spentInfo);
              }
          }
      }
      for (unsigned int i = 0; i < tx.vout.size(); i++) {
          CSpentIndexValue spentInfo;
          CSpentIndexKey spentKey(txid, i);
          if (GetSpentIndex(spentKey, spentInfo)) {
              txSpentInfo.mSpentInfo.emplace(spentKey, spentInfo);
          }
      }
  
      TxToUniv(tx, uint256(), entry, true, &txSpentInfo);
  
      bool chainLock = false;
      if (!hashBlock.IsNull()) {
          entry.pushKV("blockhash", hashBlock.GetHex());
          CBlockIndex* pindex = LookupBlockIndex(hashBlock);
          if (pindex) {
              if (chainActive.Contains(pindex)) {
                  entry.pushKV("height", pindex->nHeight);
                  entry.pushKV("confirmations", 1 + chainActive.Height() - pindex->nHeight);
                  entry.pushKV("time", pindex->GetBlockTime());
                  entry.pushKV("blocktime", pindex->GetBlockTime());
  
                  chainLock = llmq::chainLocksHandler->HasChainLock(pindex->nHeight, pindex->GetBlockHash());
              } else {
                  entry.pushKV("height", -1);
                  entry.pushKV("confirmations", 0);
              }
          }
      }
  
      bool fLocked = llmq::quorumInstantSendManager->IsLocked(txid);
      entry.pushKV("instantlock", fLocked || chainLock);
      entry.pushKV("instantlock_internal", fLocked);
      entry.pushKV("chainlock", chainLock);
  }
  
  UniValue getrawtransaction(const JSONRPCRequest& request)
  {
      if (request.fHelp || request.params.size() < 1 || request.params.size() > 3)
          throw std::runtime_error(
              "getrawtransaction \"txid\" ( verbose \"blockhash\" )\n"
  
              "\nNOTE: By default this function only works for mempool transactions. If the -txindex option is\n"
              "enabled, it also works for blockchain transactions. If the block which contains the transaction\n"
              "is known, its hash can be provided even for nodes without -txindex. Note that if a blockhash is\n"
              "provided, only that block will be searched and if the transaction is in the mempool or other\n"
              "blocks, or if this node does not have the given block available, the transaction will not be found.\n"
              "DEPRECATED: for now, it also works for transactions with unspent outputs.\n"
  
              "\nReturn the raw transaction data.\n"
              "\nIf verbose is 'true', returns an Object with information about 'txid'.\n"
              "If verbose is 'false' or omitted, returns a string that is serialized, hex-encoded data for 'txid'.\n"
  
              "\nArguments:\n"
              "1. \"txid\"      (string, required) The transaction id\n"
              "2. verbose     (bool, optional, default=false) If false, return a string, otherwise return a json object\n"
              "3. \"blockhash\" (string, optional) The block in which to look for the transaction\n"
  
              "\nResult (if verbose is not set or set to false):\n"
              "\"data\"      (string) The serialized, hex-encoded data for 'txid'\n"
  
              "\nResult (if verbose is set to true):\n"
              "{\n"
              "  \"in_active_chain\": b, (bool) Whether specified block is in the active chain or not (only present with explicit \"blockhash\" argument)\n"
              "  \"txid\" : \"id\",        (string) The transaction id (same as provided)\n"
              "  \"size\" : n,             (numeric) The transaction size\n"
              "  \"version\" : n,          (numeric) The version\n"
              "  \"locktime\" : ttt,       (numeric) The lock time\n"
              "  \"vin\" : [               (array of json objects)\n"
              "     {\n"
              "       \"txid\": \"id\",    (string) The transaction id\n"
              "       \"vout\": n,         (numeric) \n"
              "       \"scriptSig\": {     (json object) The script\n"
              "         \"asm\": \"asm\",  (string) asm\n"
              "         \"hex\": \"hex\"   (string) hex\n"
              "       },\n"
              "       \"sequence\": n      (numeric) The script sequence number\n"
              "     }\n"
              "     ,...\n"
              "  ],\n"
              "  \"vout\" : [              (array of json objects)\n"
              "     {\n"
              "       \"value\" : x.xxx,            (numeric) The value in " + CURRENCY_UNIT + "\n"
              "       \"n\" : n,                    (numeric) index\n"
              "       \"scriptPubKey\" : {          (json object)\n"
              "         \"asm\" : \"asm\",          (string) the asm\n"
              "         \"hex\" : \"hex\",          (string) the hex\n"
              "         \"reqSigs\" : n,            (numeric) The required sigs\n"
              "         \"type\" : \"pubkeyhash\",  (string) The type, eg 'pubkeyhash'\n"
              "         \"addresses\" : [           (json array of string)\n"
              "           \"address\"        (string) ebou address\n"
              "           ,...\n"
              "         ]\n"
              "       }\n"
              "     }\n"
              "     ,...\n"
              "  ],\n"
              "  \"extraPayloadSize\" : n    (numeric) Size of DIP2 extra payload. Only present if it's a special TX\n"
              "  \"extraPayload\" : \"hex\"    (string) Hex encoded DIP2 extra payload data. Only present if it's a special TX\n"
              "  \"hex\" : \"data\",         (string) The serialized, hex-encoded data for 'txid'\n"
              "  \"blockhash\" : \"hash\",   (string) the block hash\n"
              "  \"height\" : n,             (numeric) The block height\n"
              "  \"confirmations\" : n,      (numeric) The confirmations\n"
              "  \"time\" : ttt,             (numeric) The transaction time in seconds since epoch (Jan 1 1970 GMT)\n"
              "  \"blocktime\" : ttt         (numeric) The block time in seconds since epoch (Jan 1 1970 GMT)\n"
              "  \"instantlock\" : true|false, (bool) Current transaction lock state\n"
              "  \"instantlock_internal\" : true|false, (bool) Current internal transaction lock state\n"
              "  \"chainlock\" : true|false, (bool) The state of the corresponding block chainlock\n"
              "}\n"
  
              "\nExamples:\n"
              + HelpExampleCli("getrawtransaction", "\"mytxid\"")
              + HelpExampleCli("getrawtransaction", "\"mytxid\" true")
              + HelpExampleRpc("getrawtransaction", "\"mytxid\", true")
              + HelpExampleCli("getrawtransaction", "\"mytxid\" false \"myblockhash\"")
              + HelpExampleCli("getrawtransaction", "\"mytxid\" true \"myblockhash\"")
          );
  
      LOCK(cs_main);
  
      bool in_active_chain = true;
      uint256 hash = ParseHashV(request.params[0], "parameter 1");
      CBlockIndex* blockindex = nullptr;
  
      if (hash == Params().GenesisBlock().hashMerkleRoot) {
          // Special exception for the genesis block coinbase transaction
          throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "The genesis block coinbase is not considered an ordinary transaction and cannot be retrieved");
      }
  
      // Accept either a bool (true) or a num (>=1) to indicate verbose output.
      bool fVerbose = false;
      if (!request.params[1].isNull()) {
          fVerbose = request.params[1].isNum() ? (request.params[1].get_int() != 0) : request.params[1].get_bool();
      }
  
      if (!request.params[2].isNull()) {
          uint256 blockhash = ParseHashV(request.params[2], "parameter 3");
          blockindex = LookupBlockIndex(blockhash);
          if (!blockindex) {
              throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block hash not found");
          }
          in_active_chain = chainActive.Contains(blockindex);
      }
  
      CTransactionRef tx;
      uint256 hash_block;
      if (!GetTransaction(hash, tx, Params().GetConsensus(), hash_block, true, blockindex)) {
          std::string errmsg;
          if (blockindex) {
              if (!(blockindex->nStatus & BLOCK_HAVE_DATA)) {
                  throw JSONRPCError(RPC_MISC_ERROR, "Block not available");
              }
              errmsg = "No such transaction found in the provided block";
          } else {
              errmsg = fTxIndex
                ? "No such mempool or blockchain transaction"
                : "No such mempool transaction. Use -txindex to enable blockchain transaction queries";
          }
          throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, errmsg + ". Use gettransaction for wallet transactions.");
      }
  
      if (!fVerbose) {
          return EncodeHexTx(*tx);
      }
  
      UniValue result(UniValue::VOBJ);
      if (blockindex) result.pushKV("in_active_chain", in_active_chain);
      TxToJSON(*tx, hash_block, result);
      return result;
  }
  
  UniValue gettxoutproof(const JSONRPCRequest& request)
  {
      if (request.fHelp || (request.params.size() != 1 && request.params.size() != 2))
          throw std::runtime_error(
              "gettxoutproof [\"txid\",...] ( blockhash )\n"
              "\nReturns a hex-encoded proof that \"txid\" was included in a block.\n"
              "\nNOTE: By default this function only works sometimes. This is when there is an\n"
              "unspent output in the utxo for this transaction. To make it always work,\n"
              "you need to maintain a transaction index, using the -txindex command line option or\n"
              "specify the block in which the transaction is included manually (by blockhash).\n"
              "\nArguments:\n"
              "1. \"txids\"       (string) A json array of txids to filter\n"
              "    [\n"
              "      \"txid\"     (string) A transaction hash\n"
              "      ,...\n"
              "    ]\n"
              "2. \"blockhash\"   (string, optional) If specified, looks for txid in the block with this hash\n"
              "\nResult:\n"
              "\"data\"           (string) A string that is a serialized, hex-encoded data for the proof.\n"
  
              "\nExamples:\n"
              + HelpExampleCli("gettxoutproof", "'[\"mytxid\",...]'")
              + HelpExampleCli("gettxoutproof", "'[\"mytxid\",...]' \"blockhash\"")
              + HelpExampleRpc("gettxoutproof", "[\"mytxid\",...], \"blockhash\"")
          );
  
      std::set<uint256> setTxids;
      uint256 oneTxid;
      UniValue txids = request.params[0].get_array();
      for (unsigned int idx = 0; idx < txids.size(); idx++) {
          const UniValue& txid = txids[idx];
          if (txid.get_str().length() != 64 || !IsHex(txid.get_str()))
              throw JSONRPCError(RPC_INVALID_PARAMETER, std::string("Invalid txid ")+txid.get_str());
          uint256 hash(uint256S(txid.get_str()));
          if (setTxids.count(hash))
              throw JSONRPCError(RPC_INVALID_PARAMETER, std::string("Invalid parameter, duplicated txid: ")+txid.get_str());
         setTxids.insert(hash);
         oneTxid = hash;
      }
  
      LOCK(cs_main);
  
      CBlockIndex* pblockindex = nullptr;
  
      uint256 hashBlock;
      if (!request.params[1].isNull())
      {
          hashBlock = uint256S(request.params[1].get_str());
          pblockindex = LookupBlockIndex(hashBlock);
          if (!pblockindex) {
              throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found");
          }
      } else {
          // Loop through txids and try to find which block they're in. Exit loop once a block is found.
          for (const auto& tx : setTxids) {
              const Coin& coin = AccessByTxid(*pcoinsTip, tx);
              if (!coin.IsSpent()) {
                  pblockindex = chainActive[coin.nHeight];
                  break;
              }
          }
      }
  
      if (pblockindex == nullptr)
      {
          CTransactionRef tx;
          if (!GetTransaction(oneTxid, tx, Params().GetConsensus(), hashBlock, false) || hashBlock.IsNull())
              throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Transaction not yet in block");
          pblockindex = LookupBlockIndex(hashBlock);
          if (!pblockindex) {
              throw JSONRPCError(RPC_INTERNAL_ERROR, "Transaction index corrupt");
          }
      }
  
      CBlock block;
      if(!ReadBlockFromDisk(block, pblockindex, Params().GetConsensus()))
          throw JSONRPCError(RPC_INTERNAL_ERROR, "Can't read block from disk");
  
      unsigned int ntxFound = 0;
      for (const auto& tx : block.vtx)
          if (setTxids.count(tx->GetHash()))
              ntxFound++;
      if (ntxFound != setTxids.size())
          throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Not all transactions found in specified or retrieved block");
  
      CDataStream ssMB(SER_NETWORK, PROTOCOL_VERSION);
      CMerkleBlock mb(block, setTxids);
      ssMB << mb;
      std::string strHex = HexStr(ssMB.begin(), ssMB.end());
      return strHex;
  }
  
  UniValue verifytxoutproof(const JSONRPCRequest& request)
  {
      if (request.fHelp || request.params.size() != 1)
          throw std::runtime_error(
              "verifytxoutproof \"proof\"\n"
              "\nVerifies that a proof points to a transaction in a block, returning the transaction it commits to\n"
              "and throwing an RPC error if the block is not in our best chain\n"
              "\nArguments:\n"
              "1. \"proof\"    (string, required) The hex-encoded proof generated by gettxoutproof\n"
              "\nResult:\n"
              "[\"txid\"]      (array, strings) The txid(s) which the proof commits to, or empty array if the proof can not be validated.\n"
  
              "\nExamples:\n"
              + HelpExampleCli("verifytxoutproof", "\"proof\"")
              + HelpExampleRpc("gettxoutproof", "\"proof\"")
          );
  
      CDataStream ssMB(ParseHexV(request.params[0], "proof"), SER_NETWORK, PROTOCOL_VERSION);
      CMerkleBlock merkleBlock;
      ssMB >> merkleBlock;
  
      UniValue res(UniValue::VARR);
  
      std::vector<uint256> vMatch;
      std::vector<unsigned int> vIndex;
      if (merkleBlock.txn.ExtractMatches(vMatch, vIndex) != merkleBlock.header.hashMerkleRoot)
          return res;
  
      LOCK(cs_main);
  
      const CBlockIndex* pindex = LookupBlockIndex(merkleBlock.header.GetHash());
      if (!pindex || !chainActive.Contains(pindex) || pindex->nTx == 0) {
          throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found in chain");
      }
  
      // Check if proof is valid, only add results if so
      if (pindex->nTx == merkleBlock.txn.GetNumTransactions()) {
          for (const uint256& hash : vMatch) {
              res.push_back(hash.GetHex());
          }
      }
  
      return res;
  }
  
  UniValue createrawtransaction(const JSONRPCRequest& request)
  {
      if (request.fHelp || request.params.size() < 2 || request.params.size() > 3) {
          throw std::runtime_error(
              // clang-format off
              "createrawtransaction [{\"txid\":\"id\",\"vout\":n},...] [{\"address\":amount},{\"data\":\"hex\"},...] ( locktime )\n"
              "\nCreate a transaction spending the given inputs and creating new outputs.\n"
              "Outputs can be addresses or data.\n"
              "Returns hex-encoded raw transaction.\n"
              "Note that the transaction's inputs are not signed, and\n"
              "it is not stored in the wallet or transmitted to the network.\n"
  
              "\nArguments:\n"
              "1. \"inputs\"                (array, required) A json array of json objects\n"
              "     [\n"
              "       {\n"
              "         \"txid\":\"id\",      (string, required) The transaction id\n"
              "         \"vout\":n,         (numeric, required) The output number\n"
              "         \"sequence\":n      (numeric, optional) The sequence number\n"
              "       } \n"
              "       ,...\n"
              "     ]\n"
              "2. \"outputs\"               (array, required) a json array with outputs (key-value pairs)\n"
              "   [\n"
              "    {\n"
              "      \"address\": x.xxx,    (obj, optional) A key-value pair. The key (string) is the ebou address, the value (float or string) is the amount in " + CURRENCY_UNIT + "\n"
              "    },\n"
              "    {\n"
              "      \"data\": \"hex\"        (obj, optional) A key-value pair. The key must be \"data\", the value is hex encoded data\n"
              "    }\n"
              "    ,...                     More key-value pairs of the above form. For compatibility reasons, a dictionary, which holds the key-value pairs directly, is also\n"
              "                             accepted as second parameter.\n"
              "   ]\n"
              "3. locktime                  (numeric, optional, default=0) Raw locktime. Non-0 value also locktime-activates inputs\n"
              "\nResult:\n"
              "\"transaction\"              (string) hex string of the transaction\n"
  
              "\nExamples:\n"
              + HelpExampleCli("createrawtransaction", "\"[{\\\"txid\\\":\\\"myid\\\",\\\"vout\\\":0}]\" \"[{\\\"address\\\":0.01}]\"")
              + HelpExampleCli("createrawtransaction", "\"[{\\\"txid\\\":\\\"myid\\\",\\\"vout\\\":0}]\" \"[{\\\"data\\\":\\\"00010203\\\"}]\"")
              + HelpExampleRpc("createrawtransaction", "\"[{\\\"txid\\\":\\\"myid\\\",\\\"vout\\\":0}]\", \"[{\\\"address\\\":0.01}]\"")
              + HelpExampleRpc("createrawtransaction", "\"[{\\\"txid\\\":\\\"myid\\\",\\\"vout\\\":0}]\", \"[{\\\"data\\\":\\\"00010203\\\"}]\"")
              // clang-format on
          );
      }
  
      RPCTypeCheck(request.params, {
          UniValue::VARR,
          UniValueType(), // ARR or OBJ, checked later
          UniValue::VNUM,
          UniValue::VBOOL
          }, true
      );
      if (request.params[0].isNull() || request.params[1].isNull())
          throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, arguments 1 and 2 must be non-null");
  
      UniValue inputs = request.params[0].get_array();
      const bool outputs_is_obj = request.params[1].isObject();
      UniValue outputs = outputs_is_obj ?
                             request.params[1].get_obj() :
                             request.params[1].get_array();
  
      CMutableTransaction rawTx;
  
      if (!request.params[2].isNull()) {
          int64_t nLockTime = request.params[2].get_int64();
          if (nLockTime < 0 || nLockTime > std::numeric_limits<uint32_t>::max())
              throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, locktime out of range");
          rawTx.nLockTime = nLockTime;
      }
  
      for (unsigned int idx = 0; idx < inputs.size(); idx++) {
          const UniValue& input = inputs[idx];
          const UniValue& o = input.get_obj();
  
          uint256 txid = ParseHashO(o, "txid");
  
          const UniValue& vout_v = find_value(o, "vout");
          if (!vout_v.isNum())
              throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, missing vout key");
          int nOutput = vout_v.get_int();
          if (nOutput < 0)
              throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, vout must be positive");
  
          uint32_t nSequence = (rawTx.nLockTime ? std::numeric_limits<uint32_t>::max() - 1 : std::numeric_limits<uint32_t>::max());
  
          // set the sequence number if passed in the parameters object
          const UniValue& sequenceObj = find_value(o, "sequence");
          if (sequenceObj.isNum()) {
              int64_t seqNr64 = sequenceObj.get_int64();
              if (seqNr64 < 0 || seqNr64 > std::numeric_limits<uint32_t>::max())
                  throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, sequence number is out of range");
              else
                  nSequence = (uint32_t)seqNr64;
          }
  
          CTxIn in(COutPoint(txid, nOutput), CScript(), nSequence);
  
          rawTx.vin.push_back(in);
      }
  
      std::set<CTxDestination> destinations;
      if (!outputs_is_obj) {
          // Translate array of key-value pairs into dict
          UniValue outputs_dict = UniValue(UniValue::VOBJ);
          for (size_t i = 0; i < outputs.size(); ++i) {
              const UniValue& output = outputs[i];
              if (!output.isObject()) {
                  throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, key-value pair not an object as expected");
              }
              if (output.size() != 1) {
                  throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, key-value pair must contain exactly one key");
              }
              outputs_dict.pushKVs(output);
          }
          outputs = std::move(outputs_dict);
      }
      for (const std::string& name_ : outputs.getKeys()) {
          if (name_ == "data") {
              std::vector<unsigned char> data = ParseHexV(outputs[name_].getValStr(), "Data");
  
              CTxOut out(0, CScript() << OP_RETURN << data);
              rawTx.vout.push_back(out);
          } else {
              CTxDestination destination = DecodeDestination(name_);
              if (!IsValidDestination(destination)) {
                  throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, std::string("Invalid ebou address: ") + name_);
              }
  
              if (!destinations.insert(destination).second) {
                  throw JSONRPCError(RPC_INVALID_PARAMETER, std::string("Invalid parameter, duplicated address: ") + name_);
              }
  
              CScript scriptPubKey = GetScriptForDestination(destination);
              CAmount nAmount = AmountFromValue(outputs[name_]);
  
              CTxOut out(nAmount, scriptPubKey);
              rawTx.vout.push_back(out);
          }
      }
  
      return EncodeHexTx(rawTx);
  }
  
  UniValue decoderawtransaction(const JSONRPCRequest& request)
  {
      if (request.fHelp || request.params.size() != 1)
          throw std::runtime_error(
              "decoderawtransaction \"hexstring\"\n"
              "\nReturn a JSON object representing the serialized, hex-encoded transaction.\n"
  
              "\nArguments:\n"
              "1. \"hexstring\"      (string, required) The transaction hex string\n"
  
              "\nResult:\n"
              "{\n"
              "  \"txid\" : \"id\",        (string) The transaction id\n"
              "  \"size\" : n,             (numeric) The transaction size\n"
              "  \"version\" : n,          (numeric) The version\n"
              "  \"type\" : n,             (numeric) The type\n"
              "  \"locktime\" : ttt,       (numeric) The lock time\n"
              "  \"vin\" : [               (array of json objects)\n"
              "     {\n"
              "       \"txid\": \"id\",    (string) The transaction id\n"
              "       \"vout\": n,         (numeric) The output number\n"
              "       \"scriptSig\": {     (json object) The script\n"
              "         \"asm\": \"asm\",  (string) asm\n"
              "         \"hex\": \"hex\"   (string) hex\n"
              "       },\n"
              "       \"sequence\": n     (numeric) The script sequence number\n"
              "     }\n"
              "     ,...\n"
              "  ],\n"
              "  \"vout\" : [             (array of json objects)\n"
              "     {\n"
              "       \"value\" : x.xxx,            (numeric) The value in " + CURRENCY_UNIT + "\n"
              "       \"n\" : n,                    (numeric) index\n"
              "       \"scriptPubKey\" : {          (json object)\n"
              "         \"asm\" : \"asm\",          (string) the asm\n"
              "         \"hex\" : \"hex\",          (string) the hex\n"
              "         \"reqSigs\" : n,            (numeric) The required sigs\n"
              "         \"type\" : \"pubkeyhash\",  (string) The type, eg 'pubkeyhash'\n"
              "         \"addresses\" : [           (json array of string)\n"
              "           \"XwnLY9Tf7Zsef8gMGL2fhWA9ZmMjt4KPwG\"   (string) ebou address\n"
              "           ,...\n"
              "         ]\n"
              "       }\n"
              "     }\n"
              "     ,...\n"
              "  ],\n"
              "  \"extraPayloadSize\" : n           (numeric) Size of DIP2 extra payload. Only present if it's a special TX\n"
              "  \"extraPayload\" : \"hex\"           (string) Hex encoded DIP2 extra payload data. Only present if it's a special TX\n"
              "}\n"
  
              "\nExamples:\n"
              + HelpExampleCli("decoderawtransaction", "\"hexstring\"")
              + HelpExampleRpc("decoderawtransaction", "\"hexstring\"")
          );
  
      LOCK(cs_main);
      RPCTypeCheck(request.params, {UniValue::VSTR});
  
      CMutableTransaction mtx;
  
      if (!DecodeHexTx(mtx, request.params[0].get_str()))
          throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "TX decode failed");
  
      UniValue result(UniValue::VOBJ);
      TxToUniv(CTransaction(std::move(mtx)), uint256(), result, false);
  
      return result;
  }
  
  UniValue decodescript(const JSONRPCRequest& request)
  {
      if (request.fHelp || request.params.size() != 1)
          throw std::runtime_error(
              "decodescript \"hexstring\"\n"
              "\nDecode a hex-encoded script.\n"
              "\nArguments:\n"
              "1. \"hexstring\"     (string) the hex encoded script\n"
              "\nResult:\n"
              "{\n"
              "  \"asm\":\"asm\",   (string) Script public key\n"
              "  \"hex\":\"hex\",   (string) hex encoded public key\n"
              "  \"type\":\"type\", (string) The output type\n"
              "  \"reqSigs\": n,    (numeric) The required signatures\n"
              "  \"addresses\": [   (json array of string)\n"
              "     \"address\"     (string) ebou address\n"
              "     ,...\n"
              "  ],\n"
              "  \"p2sh\",\"address\" (string) address of P2SH script wrapping this redeem script (not returned if the script is already a P2SH).\n"
              "}\n"
              "\nExamples:\n"
              + HelpExampleCli("decodescript", "\"hexstring\"")
              + HelpExampleRpc("decodescript", "\"hexstring\"")
          );
  
      RPCTypeCheck(request.params, {UniValue::VSTR});
  
      UniValue r(UniValue::VOBJ);
      CScript script;
      if (request.params[0].get_str().size() > 0){
          std::vector<unsigned char> scriptData(ParseHexV(request.params[0], "argument"));
          script = CScript(scriptData.begin(), scriptData.end());
      } else {
          // Empty scripts are valid
      }
      ScriptPubKeyToUniv(script, r, false);
  
      UniValue type;
  
      type = find_value(r, "type");
  
      if (type.isStr() && type.get_str() != "scripthash") {
          // P2SH cannot be wrapped in a P2SH. If this script is already a P2SH,
          // don't return the address for a P2SH of the P2SH.
          r.pushKV("p2sh", EncodeDestination(CScriptID(script)));
      }
  
      return r;
  }
  
  /** Pushes a JSON object for script verification or signing errors to vErrorsRet. */
  static void TxInErrorToJSON(const CTxIn& txin, UniValue& vErrorsRet, const std::string& strMessage)
  {
      UniValue entry(UniValue::VOBJ);
      entry.pushKV("txid", txin.prevout.hash.ToString());
      entry.pushKV("vout", (uint64_t)txin.prevout.n);
      entry.pushKV("scriptSig", HexStr(txin.scriptSig.begin(), txin.scriptSig.end()));
      entry.pushKV("sequence", (uint64_t)txin.nSequence);
      entry.pushKV("error", strMessage);
      vErrorsRet.push_back(entry);
  }
  
  UniValue combinerawtransaction(const JSONRPCRequest& request)
  {
  
      if (request.fHelp || request.params.size() != 1)
          throw std::runtime_error(
              "combinerawtransaction [\"hexstring\",...]\n"
              "\nCombine multiple partially signed transactions into one transaction.\n"
              "The combined transaction may be another partially signed transaction or a \n"
              "fully signed transaction."
  
              "\nArguments:\n"
              "1. \"txs\"         (string) A json array of hex strings of partially signed transactions\n"
              "    [\n"
              "      \"hexstring\"     (string) A transaction hash\n"
              "      ,...\n"
              "    ]\n"
  
              "\nResult:\n"
              "\"hex\"            (string) The hex-encoded raw transaction with signature(s)\n"
  
              "\nExamples:\n"
              + HelpExampleCli("combinerawtransaction", "'[\"myhex1\", \"myhex2\", \"myhex3\"]'")
          );
  
  
      UniValue txs = request.params[0].get_array();
      std::vector<CMutableTransaction> txVariants(txs.size());
  
      for (unsigned int idx = 0; idx < txs.size(); idx++) {
          if (!DecodeHexTx(txVariants[idx], txs[idx].get_str())) {
              throw JSONRPCError(RPC_DESERIALIZATION_ERROR, strprintf("TX decode failed for tx %d", idx));
          }
      }
  
      if (txVariants.empty()) {
          throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "Missing transactions");
      }
  
      // mergedTx will end up with all the signatures; it
      // starts as a clone of the rawtx:
      CMutableTransaction mergedTx(txVariants[0]);
  
      // Fetch previous transactions (inputs):
      CCoinsView viewDummy;
      CCoinsViewCache view(&viewDummy);
      {
          LOCK(cs_main);
          LOCK(mempool.cs);
          CCoinsViewCache &viewChain = *pcoinsTip;
          CCoinsViewMemPool viewMempool(&viewChain, mempool);
          view.SetBackend(viewMempool); // temporarily switch cache backend to db+mempool view
  
          for (const CTxIn& txin : mergedTx.vin) {
              view.AccessCoin(txin.prevout); // Load entries from viewChain into view; can fail.
          }
  
          view.SetBackend(viewDummy); // switch back to avoid locking mempool for too long
      }
  
      // Use CTransaction for the constant parts of the
      // transaction to avoid rehashing.
      const CTransaction txConst(mergedTx);
      // Sign what we can:
      for (unsigned int i = 0; i < mergedTx.vin.size(); i++) {
          CTxIn& txin = mergedTx.vin[i];
          const Coin& coin = view.AccessCoin(txin.prevout);
          if (coin.IsSpent()) {
              throw JSONRPCError(RPC_VERIFY_ERROR, "Input not found or already spent");
          }
          const CScript& prevPubKey = coin.out.scriptPubKey;
          const CAmount& amount = coin.out.nValue;
  
          SignatureData sigdata;
  
          // ... and merge in other signatures:
          for (const CMutableTransaction& txv : txVariants) {
              if (txv.vin.size() > i) {
                  sigdata = CombineSignatures(prevPubKey, TransactionSignatureChecker(&txConst, i, amount), sigdata, DataFromTransaction(txv, i));
              }
          }
  
          UpdateTransaction(mergedTx, i, sigdata);
      }
  
      return EncodeHexTx(mergedTx);
  }
  
  UniValue SignTransaction(CMutableTransaction& mtx, const UniValue& prevTxsUnival, CBasicKeyStore *keystore, bool is_temp_keystore, const UniValue& hashType)
  {
      // Fetch previous transactions (inputs):
      CCoinsView viewDummy;
      CCoinsViewCache view(&viewDummy);
      {
          LOCK2(cs_main, mempool.cs);
          CCoinsViewCache &viewChain = *pcoinsTip;
          CCoinsViewMemPool viewMempool(&viewChain, mempool);
          view.SetBackend(viewMempool); // temporarily switch cache backend to db+mempool view
  
          for (const CTxIn& txin : mtx.vin) {
              view.AccessCoin(txin.prevout); // Load entries from viewChain into view; can fail.
          }
  
          view.SetBackend(viewDummy); // switch back to avoid locking mempool for too long
      }
  
      // Add previous txouts given in the RPC call:
      if (!prevTxsUnival.isNull()) {
          UniValue prevTxs = prevTxsUnival.get_array();
          for (unsigned int idx = 0; idx < prevTxs.size(); ++idx) {
              const UniValue& p = prevTxs[idx];
              if (!p.isObject()) {
                  throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "expected object with {\"txid'\",\"vout\",\"scriptPubKey\"}");
              }
  
              UniValue prevOut = p.get_obj();
  
              RPCTypeCheckObj(prevOut,
                  {
                      {"txid", UniValueType(UniValue::VSTR)},
                      {"vout", UniValueType(UniValue::VNUM)},
                      {"scriptPubKey", UniValueType(UniValue::VSTR)},
                  });
  
              uint256 txid = ParseHashO(prevOut, "txid");
  
              int nOut = find_value(prevOut, "vout").get_int();
              if (nOut < 0) {
                  throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "vout must be positive");
              }
  
              COutPoint out(txid, nOut);
              std::vector<unsigned char> pkData(ParseHexO(prevOut, "scriptPubKey"));
              CScript scriptPubKey(pkData.begin(), pkData.end());
  
              {
                  const Coin& coin = view.AccessCoin(out);
                  if (!coin.IsSpent() && coin.out.scriptPubKey != scriptPubKey) {
                      std::string err("Previous output scriptPubKey mismatch:\n");
                      err = err + ScriptToAsmStr(coin.out.scriptPubKey) + "\nvs:\n"+
                          ScriptToAsmStr(scriptPubKey);
                      throw JSONRPCError(RPC_DESERIALIZATION_ERROR, err);
                  }
                  Coin newcoin;
                  newcoin.out.scriptPubKey = scriptPubKey;
                  newcoin.out.nValue = 0;
                  if (prevOut.exists("amount")) {
                      newcoin.out.nValue = AmountFromValue(find_value(prevOut, "amount"));
                  }
                  newcoin.nHeight = 1;
                  view.AddCoin(out, std::move(newcoin), true);
              }
  
              // if redeemScript given and not using the local wallet (private keys
              // given), add redeemScript to the keystore so it can be signed:
              if (is_temp_keystore && scriptPubKey.IsPayToScriptHash()) {
                  RPCTypeCheckObj(prevOut,
                      {
                          {"redeemScript", UniValueType(UniValue::VSTR)},
                      });
                  UniValue v = find_value(prevOut, "redeemScript");
                  if (!v.isNull()) {
                      std::vector<unsigned char> rsData(ParseHexV(v, "redeemScript"));
                      CScript redeemScript(rsData.begin(), rsData.end());
                      keystore->AddCScript(redeemScript);
                  }
              }
          }
      }
  
      int nHashType = SIGHASH_ALL;
      if (!hashType.isNull()) {
          static std::map<std::string, int> mapSigHashValues = {
              {std::string("ALL"), int(SIGHASH_ALL)},
              {std::string("ALL|ANYONECANPAY"), int(SIGHASH_ALL|SIGHASH_ANYONECANPAY)},
              {std::string("NONE"), int(SIGHASH_NONE)},
              {std::string("NONE|ANYONECANPAY"), int(SIGHASH_NONE|SIGHASH_ANYONECANPAY)},
              {std::string("SINGLE"), int(SIGHASH_SINGLE)},
              {std::string("SINGLE|ANYONECANPAY"), int(SIGHASH_SINGLE|SIGHASH_ANYONECANPAY)},
          };
          std::string strHashType = hashType.get_str();
          if (mapSigHashValues.count(strHashType)) {
              nHashType = mapSigHashValues[strHashType];
          } else {
              throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid sighash param");
          }
      }
  
      bool fHashSingle = ((nHashType & ~SIGHASH_ANYONECANPAY) == SIGHASH_SINGLE);
  
      // Script verification errors
      UniValue vErrors(UniValue::VARR);
  
      // Use CTransaction for the constant parts of the
      // transaction to avoid rehashing.
      const CTransaction txConst(mtx);
      // Sign what we can:
      for (unsigned int i = 0; i < mtx.vin.size(); i++) {
          CTxIn& txin = mtx.vin[i];
          const Coin& coin = view.AccessCoin(txin.prevout);
          if (coin.IsSpent()) {
              TxInErrorToJSON(txin, vErrors, "Input not found or already spent");
              continue;
          }
          const CScript& prevPubKey = coin.out.scriptPubKey;
          const CAmount& amount = coin.out.nValue;
  
          SignatureData sigdata;
          // Only sign SIGHASH_SINGLE if there's a corresponding output:
          if (!fHashSingle || (i < mtx.vout.size())) {
              ProduceSignature(MutableTransactionSignatureCreator(keystore, &mtx, i, amount, nHashType), prevPubKey, sigdata);
          }
          sigdata = CombineSignatures(prevPubKey, TransactionSignatureChecker(&txConst, i, amount), sigdata, DataFromTransaction(mtx, i));
  
          UpdateTransaction(mtx, i, sigdata);
  
          ScriptError serror = SCRIPT_ERR_OK;
          if (!VerifyScript(txin.scriptSig, prevPubKey, STANDARD_SCRIPT_VERIFY_FLAGS, TransactionSignatureChecker(&txConst, i, amount), &serror)) {
              if (serror == SCRIPT_ERR_INVALID_STACK_OPERATION) {
                  // Unable to sign input and verification failed (possible attempt to partially sign).
                  TxInErrorToJSON(txin, vErrors, "Unable to sign input, invalid stack size (possibly missing key)");
              } else {
                  TxInErrorToJSON(txin, vErrors, ScriptErrorString(serror));
              }
          }
      }
      bool fComplete = vErrors.empty();
  
      UniValue result(UniValue::VOBJ);
      result.pushKV("hex", EncodeHexTx(mtx));
      result.pushKV("complete", fComplete);
      if (!vErrors.empty()) {
          result.pushKV("errors", vErrors);
      }
  
      return result;
  }
  
  UniValue signrawtransactionwithkey(const JSONRPCRequest& request)
  {
      if (request.fHelp || request.params.size() < 2 || request.params.size() > 4)
          throw std::runtime_error(
              "signrawtransactionwithkey \"hexstring\" [\"privatekey1\",...] ( [{\"txid\":\"id\",\"vout\":n,\"scriptPubKey\":\"hex\",\"redeemScript\":\"hex\"},...] sighashtype )\n"
              "\nSign inputs for raw transaction (serialized, hex-encoded).\n"
              "The second argument is an array of base58-encoded private\n"
              "keys that will be the only keys used to sign the transaction.\n"
              "The third optional argument (may be null) is an array of previous transaction outputs that\n"
              "this transaction depends on but may not yet be in the block chain.\n"
  
              "\nArguments:\n"
              "1. \"hexstring\"                      (string, required) The transaction hex string\n"
              "2. \"privkeys\"                       (string, required) A json array of base58-encoded private keys for signing\n"
              "    [                               (json array of strings)\n"
              "      \"privatekey\"                  (string) private key in base58-encoding\n"
              "      ,...\n"
              "    ]\n"
              "3. \"prevtxs\"                        (string, optional) An json array of previous dependent transaction outputs\n"
              "     [                              (json array of json objects, or 'null' if none provided)\n"
              "       {\n"
              "         \"txid\":\"id\",               (string, required) The transaction id\n"
              "         \"vout\":n,                  (numeric, required) The output number\n"
              "         \"scriptPubKey\": \"hex\",     (string, required) script key\n"
              "         \"redeemScript\": \"hex\",     (string, required for P2SH) redeem script\n"
              "         \"amount\": value            (numeric, required) The amount spent\n"
              "       }\n"
              "       ,...\n"
              "    ]\n"
              "4. \"sighashtype\"                    (string, optional, default=ALL) The signature hash type. Must be one of\n"
              "       \"ALL\"\n"
              "       \"NONE\"\n"
              "       \"SINGLE\"\n"
              "       \"ALL|ANYONECANPAY\"\n"
              "       \"NONE|ANYONECANPAY\"\n"
              "       \"SINGLE|ANYONECANPAY\"\n"
  
              "\nResult:\n"
              "{\n"
              "  \"hex\" : \"value\",                  (string) The hex-encoded raw transaction with signature(s)\n"
              "  \"complete\" : true|false,          (boolean) If the transaction has a complete set of signatures\n"
              "  \"errors\" : [                      (json array of objects) Script verification errors (if there are any)\n"
              "    {\n"
              "      \"txid\" : \"hash\",              (string) The hash of the referenced, previous transaction\n"
              "      \"vout\" : n,                   (numeric) The index of the output to spent and used as input\n"
              "      \"scriptSig\" : \"hex\",          (string) The hex-encoded signature script\n"
              "      \"sequence\" : n,               (numeric) Script sequence number\n"
              "      \"error\" : \"text\"              (string) Verification or signing error related to the input\n"
              "    }\n"
              "    ,...\n"
              "  ]\n"
              "}\n"
  
              "\nExamples:\n"
              + HelpExampleCli("signrawtransactionwithkey", "\"myhex\"")
              + HelpExampleRpc("signrawtransactionwithkey", "\"myhex\"")
          );
  
      RPCTypeCheck(request.params, {UniValue::VSTR, UniValue::VARR, UniValue::VARR, UniValue::VSTR}, true);
  
      CMutableTransaction mtx;
      if (!DecodeHexTx(mtx, request.params[0].get_str())) {
          throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "TX decode failed");
      }
  
      CBasicKeyStore keystore;
      const UniValue& keys = request.params[1].get_array();
      for (unsigned int idx = 0; idx < keys.size(); ++idx) {
          UniValue k = keys[idx];
          CKey key = DecodeSecret(k.get_str());
          if (!key.IsValid()) {
              throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid private key");
          }
          keystore.AddKey(key);
      }
  
      return SignTransaction(mtx, request.params[2], &keystore, true, request.params[3]);
  }
  
  UniValue signrawtransaction(const JSONRPCRequest& request)
  {
  #ifdef ENABLE_WALLET
      std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest(request);
      CWallet* const pwallet = wallet.get();
  #endif
  
      if (request.fHelp || request.params.size() < 1 || request.params.size() > 4)
          throw std::runtime_error(
              "signrawtransaction \"hexstring\" ( [{\"txid\":\"id\",\"vout\":n,\"scriptPubKey\":\"hex\",\"redeemScript\":\"hex\"},...] [\"privatekey1\",...] sighashtype )\n"
              "\nDEPRECATED. Sign inputs for raw transaction (serialized, hex-encoded).\n"
              "The second optional argument (may be null) is an array of previous transaction outputs that\n"
              "this transaction depends on but may not yet be in the block chain.\n"
              "The third optional argument (may be null) is an array of base58-encoded private\n"
              "keys that, if given, will be the only keys used to sign the transaction.\n"
  #ifdef ENABLE_WALLET
              + HelpRequiringPassphrase(pwallet) + "\n"
  #endif
              "\nArguments:\n"
              "1. \"hexstring\"     (string, required) The transaction hex string\n"
              "2. \"prevtxs\"       (string, optional) An json array of previous dependent transaction outputs\n"
              "     [               (json array of json objects, or 'null' if none provided)\n"
              "       {\n"
              "         \"txid\":\"id\",             (string, required) The transaction id\n"
              "         \"vout\":n,                  (numeric, required) The output number\n"
              "         \"scriptPubKey\": \"hex\",   (string, required) script key\n"
              "         \"redeemScript\": \"hex\",   (string, required for P2SH) redeem script\n"
              "         \"amount\": value            (numeric, required) The amount spent\n"
              "       }\n"
              "       ,...\n"
              "    ]\n"
              "3. \"privkeys\"     (string, optional) A json array of base58-encoded private keys for signing\n"
              "    [                  (json array of strings, or 'null' if none provided)\n"
              "      \"privatekey\"   (string) private key in base58-encoding\n"
              "      ,...\n"
              "    ]\n"
              "4. \"sighashtype\"     (string, optional, default=ALL) The signature hash type. Must be one of\n"
              "       \"ALL\"\n"
              "       \"NONE\"\n"
              "       \"SINGLE\"\n"
              "       \"ALL|ANYONECANPAY\"\n"
              "       \"NONE|ANYONECANPAY\"\n"
              "       \"SINGLE|ANYONECANPAY\"\n"
  
              "\nResult:\n"
              "{\n"
              "  \"hex\" : \"value\",           (string) The hex-encoded raw transaction with signature(s)\n"
              "  \"complete\" : true|false,   (boolean) If the transaction has a complete set of signatures\n"
              "  \"errors\" : [                 (json array of objects) Script verification errors (if there are any)\n"
              "    {\n"
              "      \"txid\" : \"hash\",           (string) The hash of the referenced, previous transaction\n"
              "      \"vout\" : n,                (numeric) The index of the output to spent and used as input\n"
              "      \"scriptSig\" : \"hex\",       (string) The hex-encoded signature script\n"
              "      \"sequence\" : n,            (numeric) Script sequence number\n"
              "      \"error\" : \"text\"           (string) Verification or signing error related to the input\n"
              "    }\n"
              "    ,...\n"
              "  ]\n"
              "}\n"
  
              "\nExamples:\n"
              + HelpExampleCli("signrawtransaction", "\"myhex\"")
              + HelpExampleRpc("signrawtransaction", "\"myhex\"")
          );
  
      if (!IsDeprecatedRPCEnabled("signrawtransaction")) {
          throw JSONRPCError(RPC_METHOD_DEPRECATED, "signrawtransaction is deprecated and will be fully removed in v0.18. "
              "To use signrawtransaction in v0.17, restart eboud with -deprecatedrpc=signrawtransaction.\n"
              "Projects should transition to using signrawtransactionwithkey and signrawtransactionwithwallet before upgrading to v0.18");
      }
  
      RPCTypeCheck(request.params, {UniValue::VSTR, UniValue::VARR, UniValue::VARR, UniValue::VSTR}, true);
  
      // Make a JSONRPCRequest to pass on to the right signrawtransaction* command
      JSONRPCRequest new_request;
      new_request.id = request.id;
      new_request.params.setArray();
  
      // For signing with private keys
      if (!request.params[2].isNull()) {
          new_request.params.push_back(request.params[0]);
          // Note: the prevtxs and privkeys are reversed for signrawtransactionwithkey
          new_request.params.push_back(request.params[2]);
          new_request.params.push_back(request.params[1]);
          new_request.params.push_back(request.params[3]);
          return signrawtransactionwithkey(new_request);
      } else {
  #ifdef ENABLE_WALLET
          // Otherwise sign with the wallet which does not take a privkeys parameter
          new_request.params.push_back(request.params[0]);
          new_request.params.push_back(request.params[1]);
          new_request.params.push_back(request.params[3]);
          return signrawtransactionwithwallet(new_request);
  #else
          // If we have made it this far, then wallet is disabled and no private keys were given, so fail here.
          throw JSONRPCError(RPC_INVALID_PARAMETER, "No private keys available.");
  #endif
      }
  }
  
  UniValue sendrawtransaction(const JSONRPCRequest& request)
  {
      if (request.fHelp || request.params.size() < 1 || request.params.size() > 4)
          throw std::runtime_error(
              "sendrawtransaction \"hexstring\" ( allowhighfees instantsend bypasslimits)\n"
              "\nSubmits raw transaction (serialized, hex-encoded) to local node and network.\n"
              "\nAlso see createrawtransaction and signrawtransaction calls.\n"
              "\nArguments:\n"
              "1. \"hexstring\"    (string, required) The hex string of the raw transaction)\n"
              "2. allowhighfees  (boolean, optional, default=false) Allow high fees\n"
              "3. instantsend    (boolean, optional, default=false) Deprecated and ignored\n"
              "4. bypasslimits   (boolean, optional, default=false) Bypass transaction policy limits\n"
              "\nResult:\n"
              "\"hex\"             (string) The transaction hash in hex\n"
              "\nExamples:\n"
              "\nCreate a transaction\n"
              + HelpExampleCli("createrawtransaction", "\"[{\\\"txid\\\" : \\\"mytxid\\\",\\\"vout\\\":0}]\" \"{\\\"myaddress\\\":0.01}\"") +
              "Sign the transaction, and get back the hex\n"
              + HelpExampleCli("signrawtransaction", "\"myhex\"") +
              "\nSend the transaction (signed hex)\n"
              + HelpExampleCli("sendrawtransaction", "\"signedhex\"") +
              "\nAs a json rpc call\n"
              + HelpExampleRpc("sendrawtransaction", "\"signedhex\"")
          );
  
      std::promise<void> promise;
  
      RPCTypeCheck(request.params, {UniValue::VSTR, UniValue::VBOOL, UniValue::VBOOL});
  
      // parse hex string from parameter
      CMutableTransaction mtx;
      if (!DecodeHexTx(mtx, request.params[0].get_str()))
          throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "TX decode failed");
      CTransactionRef tx(MakeTransactionRef(std::move(mtx)));
      const uint256& hashTx = tx->GetHash();
  
      CAmount nMaxRawTxFee = maxTxFee;
      if (!request.params[1].isNull() && request.params[1].get_bool())
          nMaxRawTxFee = 0;
  
      bool fBypassLimits = false;
      if (!request.params[3].isNull())
          fBypassLimits = request.params[3].get_bool();
  
      { // cs_main scope
      LOCK(cs_main);
      CCoinsViewCache &view = *pcoinsTip;
      bool fHaveChain = false;
      for (size_t o = 0; !fHaveChain && o < tx->vout.size(); o++) {
          const Coin& existingCoin = view.AccessCoin(COutPoint(hashTx, o));
          fHaveChain = !existingCoin.IsSpent();
      }
      bool fHaveMempool = mempool.exists(hashTx);
      if (!fHaveMempool && !fHaveChain) {
          // push to local node and sync with wallets
          CValidationState state;
          bool fMissingInputs;
          if (!AcceptToMemoryPool(mempool, state, std::move(tx), &fMissingInputs,
                                  fBypassLimits /* bypass_limits */, nMaxRawTxFee)) {
              if (state.IsInvalid()) {
                  throw JSONRPCError(RPC_TRANSACTION_REJECTED, FormatStateMessage(state));
              } else {
                  if (fMissingInputs) {
                      throw JSONRPCError(RPC_TRANSACTION_ERROR, "Missing inputs");
                  }
                  throw JSONRPCError(RPC_TRANSACTION_ERROR, FormatStateMessage(state));
              }
          } else {
              // If wallet is enabled, ensure that the wallet has been made aware
              // of the new transaction prior to returning. This prevents a race
              // where a user might call sendrawtransaction with a transaction
              // to/from their wallet, immediately call some wallet RPC, and get
              // a stale result because callbacks have not yet been processed.
              CallFunctionInValidationInterfaceQueue([&promise] {
                  promise.set_value();
              });
          }
      } else if (fHaveChain) {
          throw JSONRPCError(RPC_TRANSACTION_ALREADY_IN_CHAIN, "transaction already in block chain");
      } else {
          // Make sure we don't block forever if re-sending
          // a transaction already in mempool.
          promise.set_value();
      }
  
      } // cs_main
  
      promise.get_future().wait();
  
      if(!g_connman)
          throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
  
      g_connman->RelayTransaction(*tx);
  
      return hashTx.GetHex();
  }
  
  static const CRPCCommand commands[] =
  { //  category              name                            actor (function)            argNames
    //  --------------------- ------------------------        -----------------------     ----------
      { "rawtransactions",    "getrawtransaction",            &getrawtransaction,         {"txid","verbose","blockhash"} },
      { "rawtransactions",    "createrawtransaction",         &createrawtransaction,      {"inputs","outputs","locktime"} },
      { "rawtransactions",    "decoderawtransaction",         &decoderawtransaction,      {"hexstring"} },
      { "rawtransactions",    "decodescript",                 &decodescript,              {"hexstring"} },
      { "rawtransactions",    "sendrawtransaction",           &sendrawtransaction,        {"hexstring","allowhighfees","instantsend","bypasslimits"} },
      { "rawtransactions",    "combinerawtransaction",        &combinerawtransaction,     {"txs"} },
      { "rawtransactions",    "signrawtransaction",           &signrawtransaction,        {"hexstring","prevtxs","privkeys","sighashtype"} }, /* uses wallet if enabled */
      { "rawtransactions",    "signrawtransactionwithkey",    &signrawtransactionwithkey, {"hexstring","privkeys","prevtxs","sighashtype"} },
  
      { "blockchain",         "gettxoutproof",                &gettxoutproof,             {"txids", "blockhash"} },
      { "blockchain",         "verifytxoutproof",             &verifytxoutproof,          {"proof"} },
  };
  
  void RegisterRawTransactionRPCCommands(CRPCTable &t)
  {
      for (unsigned int vcidx = 0; vcidx < ARRAYLEN(commands); vcidx++)
          t.appendCommand(commands[vcidx].name, &commands[vcidx]);
  }
  