Changeset View
Changeset View
Standalone View
Standalone View
src/rest.cpp
// Copyright (c) 2009-2010 Satoshi Nakamoto | // Copyright (c) 2009-2010 Satoshi Nakamoto | ||||
// Copyright (c) 2009-2016 The Bitcoin Core developers | // Copyright (c) 2009-2016 The Bitcoin Core developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#include "chain.h" | #include "chain.h" | ||||
#include "chainparams.h" | #include "chainparams.h" | ||||
#include "config.h" | #include "config.h" | ||||
#include "httpserver.h" | #include "httpserver.h" | ||||
#include "primitives/block.h" | #include "primitives/block.h" | ||||
#include "primitives/transaction.h" | #include "primitives/transaction.h" | ||||
#include "rpc/blockchain.h" | #include "rpc/blockchain.h" | ||||
#include "rpc/server.h" | #include "rpc/server.h" | ||||
#include "rpc/tojson.h" | |||||
#include "streams.h" | #include "streams.h" | ||||
#include "sync.h" | #include "sync.h" | ||||
#include "txmempool.h" | #include "txmempool.h" | ||||
#include "utilstrencodings.h" | #include "utilstrencodings.h" | ||||
#include "validation.h" | #include "validation.h" | ||||
#include "version.h" | #include "version.h" | ||||
#include <boost/algorithm/string.hpp> | #include <boost/algorithm/string.hpp> | ||||
Show All 30 Lines | struct CCoin { | ||||
inline void SerializationOp(Stream &s, Operation ser_action) { | inline void SerializationOp(Stream &s, Operation ser_action) { | ||||
uint32_t nTxVerDummy = 0; | uint32_t nTxVerDummy = 0; | ||||
READWRITE(nTxVerDummy); | READWRITE(nTxVerDummy); | ||||
READWRITE(nHeight); | READWRITE(nHeight); | ||||
READWRITE(out); | READWRITE(out); | ||||
} | } | ||||
}; | }; | ||||
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 mempoolInfoToJSON(); | ||||
extern UniValue mempoolToJSON(bool fVerbose = false); | 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, | static bool RESTERR(HTTPRequest *req, enum HTTPStatusCode status, | ||||
std::string message) { | std::string message) { | ||||
req->WriteHeader("Content-Type", "text/plain"); | req->WriteHeader("Content-Type", "text/plain"); | ||||
req->WriteReply(status, message + "\r\n"); | req->WriteReply(status, message + "\r\n"); | ||||
return false; | return false; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 190 Lines • ▼ Show 20 Lines | switch (rf) { | ||||
case RF_HEX: { | case RF_HEX: { | ||||
std::string strHex = HexStr(ssBlock.begin(), ssBlock.end()) + "\n"; | std::string strHex = HexStr(ssBlock.begin(), ssBlock.end()) + "\n"; | ||||
req->WriteHeader("Content-Type", "text/plain"); | req->WriteHeader("Content-Type", "text/plain"); | ||||
req->WriteReply(HTTP_OK, strHex); | req->WriteReply(HTTP_OK, strHex); | ||||
return true; | return true; | ||||
} | } | ||||
case RF_JSON: { | case RF_JSON: { | ||||
UniValue objBlock = blockToJSON(block, pblockindex, showTxDetails); | UniValue objBlock = | ||||
blockToJSON(config, block, pblockindex, showTxDetails); | |||||
std::string strJSON = objBlock.write() + "\n"; | std::string strJSON = objBlock.write() + "\n"; | ||||
req->WriteHeader("Content-Type", "application/json"); | req->WriteHeader("Content-Type", "application/json"); | ||||
req->WriteReply(HTTP_OK, strJSON); | req->WriteReply(HTTP_OK, strJSON); | ||||
return true; | return true; | ||||
} | } | ||||
default: { | default: { | ||||
return RESTERR(req, HTTP_NOT_FOUND, | return RESTERR(req, HTTP_NOT_FOUND, | ||||
▲ Show 20 Lines • Show All 140 Lines • ▼ Show 20 Lines | switch (rf) { | ||||
std::string strHex = HexStr(ssTx.begin(), ssTx.end()) + "\n"; | std::string strHex = HexStr(ssTx.begin(), ssTx.end()) + "\n"; | ||||
req->WriteHeader("Content-Type", "text/plain"); | req->WriteHeader("Content-Type", "text/plain"); | ||||
req->WriteReply(HTTP_OK, strHex); | req->WriteReply(HTTP_OK, strHex); | ||||
return true; | return true; | ||||
} | } | ||||
case RF_JSON: { | case RF_JSON: { | ||||
UniValue objTx(UniValue::VOBJ); | UniValue objTx(UniValue::VOBJ); | ||||
TxToJSON(*tx, hashBlock, objTx); | TxToJSON(config, *tx, hashBlock, objTx); | ||||
std::string strJSON = objTx.write() + "\n"; | std::string strJSON = objTx.write() + "\n"; | ||||
req->WriteHeader("Content-Type", "application/json"); | req->WriteHeader("Content-Type", "application/json"); | ||||
req->WriteReply(HTTP_OK, strJSON); | req->WriteReply(HTTP_OK, strJSON); | ||||
return true; | return true; | ||||
} | } | ||||
default: { | default: { | ||||
return RESTERR(req, HTTP_NOT_FOUND, | return RESTERR(req, HTTP_NOT_FOUND, | ||||
▲ Show 20 Lines • Show All 200 Lines • ▼ Show 20 Lines | switch (rf) { | ||||
UniValue utxos(UniValue::VARR); | UniValue utxos(UniValue::VARR); | ||||
for (const CCoin &coin : outs) { | for (const CCoin &coin : outs) { | ||||
UniValue utxo(UniValue::VOBJ); | UniValue utxo(UniValue::VOBJ); | ||||
utxo.push_back(Pair("height", int32_t(coin.nHeight))); | utxo.push_back(Pair("height", int32_t(coin.nHeight))); | ||||
utxo.push_back(Pair("value", ValueFromAmount(coin.out.nValue))); | utxo.push_back(Pair("value", ValueFromAmount(coin.out.nValue))); | ||||
// include the script in a json output | // include the script in a json output | ||||
UniValue o(UniValue::VOBJ); | UniValue o(UniValue::VOBJ); | ||||
ScriptPubKeyToJSON(coin.out.scriptPubKey, o, true); | ScriptPubKeyToJSON(config, coin.out.scriptPubKey, o, true); | ||||
utxo.push_back(Pair("scriptPubKey", o)); | utxo.push_back(Pair("scriptPubKey", o)); | ||||
utxos.push_back(utxo); | utxos.push_back(utxo); | ||||
} | } | ||||
objGetUTXOResponse.push_back(Pair("utxos", utxos)); | objGetUTXOResponse.push_back(Pair("utxos", utxos)); | ||||
// return json string | // return json string | ||||
std::string strJSON = objGetUTXOResponse.write() + "\n"; | std::string strJSON = objGetUTXOResponse.write() + "\n"; | ||||
req->WriteHeader("Content-Type", "application/json"); | req->WriteHeader("Content-Type", "application/json"); | ||||
▲ Show 20 Lines • Show All 46 Lines • Show Last 20 Lines |