MCPcopy Create free account
hub / github.com/ElementsProject/elements / rest_tx

Function rest_tx

src/rest.cpp:632–689  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

630}
631
632static bool rest_tx(const std::any& context, HTTPRequest* req, const std::string& strURIPart)
633{
634 if (!CheckWarmup(req))
635 return false;
636 std::string hashStr;
637 const RetFormat rf = ParseDataFormat(hashStr, strURIPart);
638
639 uint256 hash;
640 if (!ParseHashStr(hashStr, hash))
641 return RESTERR(req, HTTP_BAD_REQUEST, "Invalid hash: " + hashStr);
642
643 if (g_txindex) {
644 g_txindex->BlockUntilSyncedToCurrentChain();
645 }
646
647 const NodeContext* const node = GetNodeContext(context, req);
648 if (!node) return false;
649 uint256 hashBlock = uint256();
650 const CTransactionRef tx = GetTransaction(/* block_index */ nullptr, node->mempool.get(), hash, Params().GetConsensus(), hashBlock);
651 if (!tx) {
652 return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not found");
653 }
654
655 switch (rf) {
656 case RetFormat::BINARY: {
657 CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION | RPCSerializationFlags());
658 ssTx << tx;
659
660 std::string binaryTx = ssTx.str();
661 req->WriteHeader("Content-Type", "application/octet-stream");
662 req->WriteReply(HTTP_OK, binaryTx);
663 return true;
664 }
665
666 case RetFormat::HEX: {
667 CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION | RPCSerializationFlags());
668 ssTx << tx;
669
670 std::string strHex = HexStr(ssTx) + "\n";
671 req->WriteHeader("Content-Type", "text/plain");
672 req->WriteReply(HTTP_OK, strHex);
673 return true;
674 }
675
676 case RetFormat::JSON: {
677 UniValue objTx(UniValue::VOBJ);
678 TxToUniv(*tx, hashBlock, objTx);
679 std::string strJSON = objTx.write() + "\n";
680 req->WriteHeader("Content-Type", "application/json");
681 req->WriteReply(HTTP_OK, strJSON);
682 return true;
683 }
684
685 default: {
686 return RESTERR(req, HTTP_NOT_FOUND, "output format not found (available: " + AvailableDataFormatsString() + ")");
687 }
688 }
689}

Callers

nothing calls this directly

Calls 15

CheckWarmupFunction · 0.85
ParseDataFormatFunction · 0.85
ParseHashStrFunction · 0.85
RESTERRFunction · 0.85
GetNodeContextFunction · 0.85
GetTransactionFunction · 0.85
RPCSerializationFlagsFunction · 0.85
TxToUnivFunction · 0.85
strMethod · 0.80
WriteHeaderMethod · 0.80
WriteReplyMethod · 0.80

Tested by

no test coverage detected