diff --git a/src/Makefile.am b/src/Makefile.am --- a/src/Makefile.am +++ b/src/Makefile.am @@ -142,6 +142,7 @@ rpc/misc.h \ rpc/protocol.h \ rpc/server.h \ + rpc/tojson.h \ rpc/register.h \ scheduler.h \ script/scriptcache.h \ diff --git a/src/rest.cpp b/src/rest.cpp --- a/src/rest.cpp +++ b/src/rest.cpp @@ -11,6 +11,7 @@ #include "primitives/transaction.h" #include "rpc/blockchain.h" #include "rpc/server.h" +#include "rpc/tojson.h" #include "streams.h" #include "sync.h" #include "txmempool.h" @@ -57,15 +58,8 @@ } }; -extern void TxToJSON(const CTransaction &tx, const uint256 hashBlock, - UniValue &entry); -extern UniValue blockToJSON(const CBlock &block, const CBlockIndex *blockindex, - bool txDetails = false); extern UniValue mempoolInfoToJSON(); extern UniValue mempoolToJSON(bool fVerbose = false); -extern void ScriptPubKeyToJSON(const CScript &scriptPubKey, UniValue &out, - bool fIncludeHex); -extern UniValue blockheaderToJSON(const CBlockIndex *blockindex); static bool RESTERR(HTTPRequest *req, enum HTTPStatusCode status, std::string message) { @@ -272,7 +266,8 @@ } case RF_JSON: { - UniValue objBlock = blockToJSON(block, pblockindex, showTxDetails); + UniValue objBlock = + blockToJSON(config, block, pblockindex, showTxDetails); std::string strJSON = objBlock.write() + "\n"; req->WriteHeader("Content-Type", "application/json"); req->WriteReply(HTTP_OK, strJSON); @@ -429,7 +424,7 @@ case RF_JSON: { UniValue objTx(UniValue::VOBJ); - TxToJSON(*tx, hashBlock, objTx); + TxToJSON(config, *tx, hashBlock, objTx); std::string strJSON = objTx.write() + "\n"; req->WriteHeader("Content-Type", "application/json"); req->WriteReply(HTTP_OK, strJSON); @@ -646,7 +641,7 @@ // include the script in a json output UniValue o(UniValue::VOBJ); - ScriptPubKeyToJSON(coin.out.scriptPubKey, o, true); + ScriptPubKeyToJSON(config, coin.out.scriptPubKey, o, true); utxo.push_back(Pair("scriptPubKey", o)); utxos.push_back(utxo); } diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -16,6 +16,7 @@ #include "policy/policy.h" #include "primitives/transaction.h" #include "rpc/server.h" +#include "rpc/tojson.h" #include "streams.h" #include "sync.h" #include "txmempool.h" @@ -23,11 +24,10 @@ #include "utilstrencodings.h" #include "validation.h" -#include - #include // boost::thread::interrupt #include +#include #include struct CUpdatedBlock { @@ -39,11 +39,6 @@ static std::condition_variable cond_blockchange; static CUpdatedBlock latestblock; -extern void TxToJSON(const CTransaction &tx, const uint256 hashBlock, - UniValue &entry); -void ScriptPubKeyToJSON(const CScript &scriptPubKey, UniValue &out, - bool fIncludeHex); - static double GetDifficultyFromBits(uint32_t nBits) { int nShift = (nBits >> 24) & 0xff; double dDiff = 0x0000ffff / double(nBits & 0x00ffffff); @@ -104,8 +99,8 @@ return result; } -UniValue blockToJSON(const CBlock &block, const CBlockIndex *blockindex, - bool txDetails = false) { +UniValue blockToJSON(const Config &config, const CBlock &block, + const CBlockIndex *blockindex, bool txDetails) { UniValue result(UniValue::VOBJ); result.push_back(Pair("hash", blockindex->GetBlockHash().GetHex())); int confirmations = -1; @@ -124,10 +119,11 @@ for (const auto &tx : block.vtx) { if (txDetails) { UniValue objTx(UniValue::VOBJ); - TxToJSON(*tx, uint256(), objTx); + TxToJSON(config, *tx, uint256(), objTx); txs.push_back(objTx); - } else + } else { txs.push_back(tx->GetId().GetHex()); + } } result.push_back(Pair("tx", txs)); result.push_back(Pair("time", block.GetBlockTime())); @@ -851,7 +847,7 @@ return strHex; } - return blockToJSON(block, pblockindex); + return blockToJSON(config, block, pblockindex); } struct CCoinsStats { @@ -1116,7 +1112,7 @@ } ret.push_back(Pair("value", ValueFromAmount(coin.GetTxOut().nValue))); UniValue o(UniValue::VOBJ); - ScriptPubKeyToJSON(coin.GetTxOut().scriptPubKey, o, true); + ScriptPubKeyToJSON(config, coin.GetTxOut().scriptPubKey, o, true); ret.push_back(Pair("scriptPubKey", o)); ret.push_back(Pair("coinbase", coin.IsCoinBase())); diff --git a/src/rpc/rawtransaction.cpp b/src/rpc/rawtransaction.cpp --- a/src/rpc/rawtransaction.cpp +++ b/src/rpc/rawtransaction.cpp @@ -17,6 +17,7 @@ #include "policy/policy.h" #include "primitives/transaction.h" #include "rpc/server.h" +#include "rpc/tojson.h" #include "script/script.h" #include "script/script_error.h" #include "script/sign.h" @@ -33,8 +34,8 @@ #include -void ScriptPubKeyToJSON(const CScript &scriptPubKey, UniValue &out, - bool fIncludeHex) { +void ScriptPubKeyToJSON(const Config &config, const CScript &scriptPubKey, + UniValue &out, bool fIncludeHex) { txnouttype type; std::vector addresses; int nRequired; @@ -61,8 +62,8 @@ out.push_back(Pair("addresses", a)); } -void TxToJSON(const CTransaction &tx, const uint256 hashBlock, - UniValue &entry) { +void TxToJSON(const Config &config, const CTransaction &tx, + const uint256 hashBlock, UniValue &entry) { entry.push_back(Pair("txid", tx.GetId().GetHex())); entry.push_back(Pair("hash", tx.GetHash().GetHex())); entry.push_back(Pair( @@ -99,7 +100,7 @@ out.push_back(Pair("value", ValueFromAmount(txout.nValue))); out.push_back(Pair("n", (int64_t)i)); UniValue o(UniValue::VOBJ); - ScriptPubKeyToJSON(txout.scriptPubKey, o, true); + ScriptPubKeyToJSON(config, txout.scriptPubKey, o, true); out.push_back(Pair("scriptPubKey", o)); vout.push_back(out); } @@ -250,7 +251,7 @@ UniValue result(UniValue::VOBJ); result.push_back(Pair("hex", strHex)); - TxToJSON(*tx, hashBlock, result); + TxToJSON(config, *tx, hashBlock, result); return result; } @@ -636,7 +637,7 @@ } UniValue result(UniValue::VOBJ); - TxToJSON(CTransaction(std::move(mtx)), uint256(), result); + TxToJSON(config, CTransaction(std::move(mtx)), uint256(), result); return result; } @@ -680,7 +681,7 @@ // Empty scripts are valid. } - ScriptPubKeyToJSON(script, r, false); + ScriptPubKeyToJSON(config, script, r, false); UniValue type; type = find_value(r, "type"); diff --git a/src/rpc/tojson.h b/src/rpc/tojson.h new file mode 100644 --- /dev/null +++ b/src/rpc/tojson.h @@ -0,0 +1,22 @@ +// Copyright (c) 2017 The Bitcoin developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#ifndef BITCOIN_RPCTOJSON_H +#define BITCOIN_RPCTOJSON_H + +#include "uint256.h" + +#include + +class CScript; + +void ScriptPubKeyToJSON(const Config &config, const CScript &scriptPubKey, + UniValue &out, bool fIncludeHex); +void TxToJSON(const Config &config, const CTransaction &tx, + const uint256 hashBlock, UniValue &entry); +UniValue blockToJSON(const Config &config, const CBlock &block, + const CBlockIndex *blockindex, bool txDetails = false); +UniValue blockheaderToJSON(const CBlockIndex *blockindex); + +#endif // BITCOIN_RPCTOJSON_H