Files
thehub/hub/server/rpcrawtransaction.cpp
T

596 lines
25 KiB
C++
Raw Permalink Normal View History

2017-11-09 19:34:51 +01:00
/*
* This file is part of the Flowee project
* Copyright (C) 2010 Satoshi Nakamoto
* Copyright (C) 2009-2015 The Bitcoin Core developers
2021-01-20 19:21:53 +01:00
* Copyright (C) 2016-2021 Tom Zander <tom@flowee.org>
2017-11-09 19:34:51 +01:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2012-05-31 16:01:16 -04:00
2017-07-24 22:40:28 +02:00
#include <Application.h>
2019-04-11 11:33:27 +02:00
#include "encodings_legacy.h"
2015-07-05 14:17:46 +02:00
#include "chain.h"
2018-01-15 15:26:12 +00:00
#include <validation/Engine.h>
#include "core_io.h"
2014-01-11 18:14:29 +01:00
#include "main.h"
#include "merkleblock.h"
2014-01-11 18:14:29 +01:00
#include "net.h"
#include "policy/policy.h"
#include "primitives/transaction.h"
2014-01-11 18:14:29 +01:00
#include "rpcserver.h"
#include "primitives/script.h"
2014-09-14 12:43:56 +02:00
#include "script/standard.h"
2015-07-05 14:17:46 +02:00
#include "txmempool.h"
2017-02-14 11:17:46 +01:00
#include "BlocksDB.h"
2014-01-11 18:14:29 +01:00
#include "uint256.h"
2015-07-05 14:17:46 +02:00
#include "utilstrencodings.h"
2026-05-14 13:13:40 +02:00
#include "chainparams.h"
#include <utxo/UnspentOutputDatabase.h>
2012-05-31 16:01:16 -04:00
2013-04-13 00:13:08 -05:00
#include <boost/assign/list_of.hpp>
2018-12-30 15:33:11 +01:00
#include <boost/foreach.hpp>
#include <primitives/Block.h>
2022-07-06 22:50:53 +02:00
#include <BitcoinVersion.h>
2013-04-13 00:13:08 -05:00
2015-09-04 16:11:34 +02:00
#include <univalue.h>
void ScriptPubKeyToJSON(const CScript& scriptPubKey, UniValue& out, bool fIncludeHex)
2012-05-31 16:01:16 -04:00
{
2019-06-06 21:37:49 +02:00
Script::TxnOutType type;
2017-08-02 19:53:22 +05:30
std::vector<CTxDestination> addresses;
2012-05-31 16:01:16 -04:00
int nRequired;
2015-07-30 19:56:00 -04:00
out.push_back(Pair("asm", ScriptToAsmStr(scriptPubKey)));
2013-07-15 01:22:10 -04:00
if (fIncludeHex)
out.push_back(Pair("hex", HexStr(scriptPubKey.begin(), scriptPubKey.end())));
2012-05-31 16:01:16 -04:00
if (!ExtractDestinations(scriptPubKey, type, addresses, nRequired)) {
2019-06-06 21:37:49 +02:00
out.push_back(Pair("type", Script::getTxnOutputType(type)));
2012-05-31 16:01:16 -04:00
return;
}
out.push_back(Pair("reqSigs", nRequired));
2019-06-06 21:37:49 +02:00
out.push_back(Pair("type", Script::getTxnOutputType(type)));
2012-05-31 16:01:16 -04:00
UniValue a(UniValue::VARR);
2012-05-31 16:01:16 -04:00
BOOST_FOREACH(const CTxDestination& addr, addresses)
a.push_back(CBitcoinAddress(addr).ToString());
out.push_back(Pair("addresses", a));
}
void TxToJSON(const CTransaction& tx, const uint256 hashBlock, UniValue& entry)
2012-05-31 16:01:16 -04:00
{
entry.push_back(Pair("txid", tx.GetHash().GetHex()));
2015-11-21 05:35:11 +03:00
entry.push_back(Pair("size", (int)::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION)));
2012-05-31 16:01:16 -04:00
entry.push_back(Pair("version", tx.nVersion));
2014-05-05 20:08:13 +02:00
entry.push_back(Pair("locktime", (int64_t)tx.nLockTime));
UniValue vin(UniValue::VARR);
BOOST_FOREACH(const CTxIn& txin, tx.vin) {
UniValue in(UniValue::VOBJ);
2012-05-31 16:01:16 -04:00
if (tx.IsCoinBase())
in.push_back(Pair("coinbase", HexStr(txin.scriptSig.begin(), txin.scriptSig.end())));
else {
2012-05-31 16:01:16 -04:00
in.push_back(Pair("txid", txin.prevout.hash.GetHex()));
2014-05-05 20:08:13 +02:00
in.push_back(Pair("vout", (int64_t)txin.prevout.n));
UniValue o(UniValue::VOBJ);
2015-07-30 19:56:00 -04:00
o.push_back(Pair("asm", ScriptToAsmStr(txin.scriptSig, true)));
2012-05-31 16:01:16 -04:00
o.push_back(Pair("hex", HexStr(txin.scriptSig.begin(), txin.scriptSig.end())));
in.push_back(Pair("scriptSig", o));
}
2014-05-05 20:08:13 +02:00
in.push_back(Pair("sequence", (int64_t)txin.nSequence));
2012-05-31 16:01:16 -04:00
vin.push_back(in);
}
entry.push_back(Pair("vin", vin));
UniValue vout(UniValue::VARR);
for (unsigned int i = 0; i < tx.vout.size(); i++) {
2012-05-31 16:01:16 -04:00
const CTxOut& txout = tx.vout[i];
UniValue out(UniValue::VOBJ);
2012-05-31 16:01:16 -04:00
out.push_back(Pair("value", ValueFromAmount(txout.nValue)));
2014-05-05 20:08:13 +02:00
out.push_back(Pair("n", (int64_t)i));
UniValue o(UniValue::VOBJ);
ScriptPubKeyToJSON(txout.scriptPubKey, o, true);
2012-05-31 16:01:16 -04:00
out.push_back(Pair("scriptPubKey", o));
vout.push_back(out);
}
entry.push_back(Pair("vout", vout));
if (!hashBlock.IsNull()) {
2012-05-31 16:01:16 -04:00
entry.push_back(Pair("blockhash", hashBlock.GetHex()));
2018-01-15 15:26:12 +00:00
CBlockIndex* pindex = Blocks::Index::get(hashBlock);
if (pindex) {
if (chainActive.Contains(pindex)) {
entry.push_back(Pair("confirmations", 1 + chainActive.Height() - pindex->nHeight));
2014-06-28 23:36:06 +02:00
entry.push_back(Pair("time", pindex->GetBlockTime()));
entry.push_back(Pair("blocktime", pindex->GetBlockTime()));
2012-05-31 16:01:16 -04:00
}
else
entry.push_back(Pair("confirmations", 0));
}
}
}
UniValue getrawtransaction(const UniValue& params, bool fHelp)
2012-05-31 16:01:16 -04:00
{
2021-03-03 10:17:14 +01:00
if (fHelp || params.size() < 1 || params.size() > 3)
throw std::runtime_error("getrawtransaction"
"\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"
"\n"
"Return the raw transaction data.\n"
"\n"
"If 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"
2013-10-29 22:29:44 +11:00
2021-03-03 10:17:14 +01:00
"\narguments:\n"
2013-10-29 22:29:44 +11:00
"1. \"txid\" (string, required) The transaction id\n"
"2. verbose (numeric, optional, default=0) If 0, return a string, other return a json object\n"
2021-03-03 10:17:14 +01:00
"3. blockhash (string, required) The block in which to look for the transaction\n"
2013-10-29 22:29:44 +11:00
2021-03-03 10:17:14 +01:00
"\nResult (if verbose is not set or set to false):\n"
"\"data\" (string) The serialized, hex-encoded data for "
"'txid'\n"
2013-10-29 22:29:44 +11:00
2021-03-03 10:17:14 +01:00
"\nResult (if verbose is set to true):\n"
2013-10-29 22:29:44 +11:00
"{\n"
" \"hex\" : \"data\", (string) The serialized, hex-encoded data for 'txid'\n"
" \"txid\" : \"id\", (string) The transaction id (same as provided)\n"
2021-03-03 10:17:14 +01:00
" \"hash\" : \"id\", (string) The transaction hash\n"
" \"size\" : n, (numeric) The serialized transaction size\n"
2013-10-29 22:29:44 +11:00
" \"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"
2021-03-03 10:17:14 +01:00
" \"vout\": n, (numeric)\n"
2013-10-29 22:29:44 +11:00
" \"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"
2021-01-20 19:21:53 +01:00
" \"value\" : x.xxx, (numeric) The value in BCH\n"
2013-10-29 22:29:44 +11:00
" \"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"
2021-03-03 10:17:14 +01:00
" \"address\" (string) Bitcoin Cash address\n"
2013-10-29 22:29:44 +11:00
" ,...\n"
" ]\n"
" }\n"
" }\n"
" ,...\n"
" ],\n"
" \"blockhash\" : \"hash\", (string) the block hash\n"
" \"confirmations\" : n, (numeric) The confirmations\n"
" \"time\" : ttt, (numeric) The transaction time in seconds since epoch (Jan 1 1970 GMT)\n"
2021-03-03 10:17:14 +01:00
" \"blocktime\" : ttt, (numeric) The block time in seconds since epoch (Jan 1 1970 GMT)\n"
" \"in_active_chain\": b (bool) Whether specified block is in the active chain or not (only present with explicit \"blockhash\" argument)\n"
2013-10-29 22:29:44 +11:00
"}\n"
2021-03-03 10:17:14 +01:00
"\nExamples:\n" +
HelpExampleCli("getrawtransaction", "\"mytxid\"") +
HelpExampleCli("getrawtransaction", "\"mytxid\" true") +
HelpExampleRpc("getrawtransaction", "\"mytxid\", true") +
HelpExampleCli("getrawtransaction", "\"mytxid\" false \"myblockhash\"") +
HelpExampleCli("getrawtransaction", "\"mytxid\" true \"myblockhash\""));
2012-05-31 16:01:16 -04:00
LOCK(cs_main);
2021-03-03 10:17:14 +01:00
bool in_active_chain = true;
uint256 hash = ParseHashV(params[0], "parameter 1");
2012-05-31 16:01:16 -04:00
bool fVerbose = false;
2020-01-13 23:23:49 +01:00
if (!params[1].isNull()) {
fVerbose = params[1].isNum() ? (params[1].get_int() != 0) : params[1].get_bool();
}
2012-05-31 16:01:16 -04:00
CTransaction tx;
2021-03-03 10:17:14 +01:00
bool success = false;
bool fromBlock = false;
if (!params[2].isNull()) { // we got a block hash, look up in that block.
fromBlock = true;
uint256 blockId = ParseHashV(params[2], "parameter 2");
auto index = Blocks::Index::get(blockId);
if (!index)
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block hash not found");
in_active_chain = Blocks::DB::instance()->headerChain().Contains(index);
2021-03-10 18:04:57 +01:00
2022-02-22 16:11:34 +01:00
Block block;
try {
block = Blocks::DB::instance()->loadBlock(index->GetBlockPos());
} catch (...) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not available");
2021-03-03 10:17:14 +01:00
}
2022-02-22 16:11:34 +01:00
const int ctorHeight = Params().GetConsensus().hf201811Height;
auto ftx = block.findTransaction(hash, index->nHeight >= ctorHeight);
if (ftx.size() > 0) {
success = true;
tx = ftx.createOldTransaction();
2021-03-03 10:17:14 +01:00
}
2021-03-10 18:04:57 +01:00
if (!success)
2021-03-03 10:17:14 +01:00
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No such transaction found in the provided block");
2019-05-08 12:49:33 +02:00
}
2021-03-03 10:17:14 +01:00
else {
// we just get it from the mempool.
success = flApp->mempool()->lookup(hash, tx);
}
if (!success) {
const int ctorHeight = Params().GetConsensus().hf201811Height;
auto index = chainActive.Tip();
for (int i = 0; !success && index && i < 6; ++i) {
2022-02-22 16:11:34 +01:00
Block block = Blocks::DB::instance()->loadBlock(index->GetBlockPos());
auto ftx = block.findTransaction(hash, index->nHeight >= ctorHeight);
if (ftx.size() > 0) {
tx = ftx.createOldTransaction();
success = true;
break;
2021-03-03 10:17:14 +01:00
}
index = index->pprev;
}
if (!success)
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No such mempool transaction.");
}
2012-05-31 16:01:16 -04:00
2017-08-02 19:53:22 +05:30
std::string strHex = EncodeHexTx(tx);
2012-05-31 16:01:16 -04:00
if (!fVerbose)
return strHex;
UniValue result(UniValue::VOBJ);
2012-05-31 16:01:16 -04:00
result.push_back(Pair("hex", strHex));
uint256 hashBlock;
2012-05-31 16:01:16 -04:00
TxToJSON(tx, hashBlock, result);
2021-03-03 10:17:14 +01:00
if (fromBlock) {
result.push_back(Pair("in_active_chain", in_active_chain));
}
2012-05-31 16:01:16 -04:00
return result;
}
UniValue verifytxoutproof(const UniValue& params, bool fHelp)
{
if (fHelp || params.size() != 1)
2017-08-02 19:53:22 +05:30
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\n"
"\nResult:\n"
"[\"txid\"] (array, strings) The txid(s) which the proof commits to, or empty array if the proof is invalid\n"
);
CDataStream ssMB(ParseHexV(params[0], "proof"), SER_NETWORK, PROTOCOL_VERSION);
CMerkleBlock merkleBlock;
ssMB >> merkleBlock;
UniValue res(UniValue::VARR);
2017-08-02 19:53:22 +05:30
std::vector<uint256> vMatch;
if (merkleBlock.txn.ExtractMatches(vMatch) != merkleBlock.header.hashMerkleRoot)
return res;
2021-11-02 09:28:35 +01:00
auto item = Blocks::Index::get(merkleBlock.header.createHash());
2018-01-15 15:26:12 +00:00
if (!item || !chainActive.Contains(item))
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found in chain");
BOOST_FOREACH(const uint256& hash, vMatch)
res.push_back(hash.GetHex());
return res;
}
UniValue createrawtransaction(const UniValue& params, bool fHelp)
2012-05-31 16:01:16 -04:00
{
if (fHelp || params.size() < 2 || params.size() > 4)
2017-08-02 19:53:22 +05:30
throw std::runtime_error(
"createrawtransaction [{\"txid\":\"id\",\"vout\":n},...] {\"address\":amount,\"data\":\"hex\",...} ( locktime, txversion )\n"
"\nCreate a transaction spending the given inputs and creating new outputs.\n"
"Outputs can be addresses or data.\n"
2012-05-31 16:01:16 -04:00
"Returns hex-encoded raw transaction.\n"
"Note that the transaction's inputs are not signed, and\n"
2013-10-29 22:29:44 +11:00
"it is not stored in the wallet or transmitted to the network.\n"
"\nArguments:\n"
"1. \"transactions\" (string, required) A json array of json objects\n"
" [\n"
" {\n"
" \"txid\":\"id\", (string, required) The transaction id\n"
2013-10-29 22:29:44 +11:00
" \"vout\":n (numeric, required) The output number\n"
" }\n"
" ,...\n"
" ]\n"
"2. \"outputs\" (string, required) a json object with outputs\n"
2013-10-29 22:29:44 +11:00
" {\n"
2021-01-20 19:21:53 +01:00
" \"address\": x.xxx (numeric or string, required) The key is the bitcoin address, the numeric value (can be string) is the BCH amount\n"
" \"data\": \"hex\", (string, required) The key is \"data\", the value is hex encoded data\n"
" ...\n"
2013-10-29 22:29:44 +11:00
" }\n"
"3. locktime (numeric, optional, default=0) Raw locktime. Non-0 value also locktime-activates inputs\n"
"4. txversion (numeric, optional, default=1) Specifies the transaction format version\n"
2013-10-29 22:29:44 +11:00
"\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\\\"}\"")
2013-10-29 22:29:44 +11:00
+ HelpExampleRpc("createrawtransaction", "\"[{\\\"txid\\\":\\\"myid\\\",\\\"vout\\\":0}]\", \"{\\\"address\\\":0.01}\"")
+ HelpExampleRpc("createrawtransaction", "\"[{\\\"txid\\\":\\\"myid\\\",\\\"vout\\\":0}]\", \"{\\\"data\\\":\\\"00010203\\\"}\"")
2013-10-29 22:29:44 +11:00
);
2012-05-31 16:01:16 -04:00
LOCK(cs_main);
RPCTypeCheck(params, boost::assign::list_of(UniValue::VARR)(UniValue::VOBJ)(UniValue::VNUM), true);
if (params[0].isNull() || params[1].isNull())
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, arguments 1 and 2 must be non-null");
2012-05-31 16:01:16 -04:00
2015-05-13 21:29:19 +02:00
UniValue inputs = params[0].get_array();
UniValue sendTo = params[1].get_obj();
2012-05-31 16:01:16 -04:00
CMutableTransaction rawTx;
2012-05-31 16:01:16 -04:00
if (params.size() > 2 && !params[2].isNull()) {
int64_t nLockTime = 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;
}
if (params.size() > 3 && !params[3].isNull()) {
int version = params[3].get_int();
if (version != 1 && version != 4)
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, txversion can only be 1 or 4");
rawTx.nVersion = version;
}
for (unsigned int idx = 0; idx < inputs.size(); idx++) {
const UniValue& input = inputs[idx];
const UniValue& o = input.get_obj();
2012-05-31 16:01:16 -04:00
uint256 txid = ParseHashO(o, "txid");
2012-05-31 16:01:16 -04:00
const UniValue& vout_v = find_value(o, "vout");
if (!vout_v.isNum())
2012-10-04 09:34:44 +02:00
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, missing vout key");
2012-05-31 16:01:16 -04:00
int nOutput = vout_v.get_int();
if (nOutput < 0)
2012-10-04 09:34:44 +02:00
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, vout must be positive");
2012-05-31 16:01:16 -04:00
uint32_t nSequence = (rawTx.nLockTime ? std::numeric_limits<uint32_t>::max() - 1 : std::numeric_limits<uint32_t>::max());
CTxIn in(COutPoint(txid, nOutput), CScript(), nSequence);
2012-05-31 16:01:16 -04:00
rawTx.vin.push_back(in);
}
2017-08-02 19:53:22 +05:30
std::set<CBitcoinAddress> setAddress;
std::vector<std::string> addrList = sendTo.getKeys();
BOOST_FOREACH(const std::string& name_, addrList) {
2012-05-31 16:01:16 -04:00
if (name_ == "data") {
std::vector<unsigned char> data = ParseHexV(sendTo[name_].getValStr(),"Data");
2012-05-31 16:01:16 -04:00
CTxOut out(0, CScript() << OP_RETURN << data);
rawTx.vout.push_back(out);
} else {
CBitcoinAddress address(name_);
if (!address.IsValid())
2017-08-02 19:53:22 +05:30
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, std::string("Invalid Bitcoin address: ")+name_);
2012-05-31 16:01:16 -04:00
if (setAddress.count(address))
2017-08-02 19:53:22 +05:30
throw JSONRPCError(RPC_INVALID_PARAMETER, std::string("Invalid parameter, duplicated address: ")+name_);
setAddress.insert(address);
CScript scriptPubKey = GetScriptForDestination(address.Get());
2021-01-20 19:21:53 +01:00
int64_t nAmount = AmountFromValue(sendTo[name_]);
CTxOut out(nAmount, scriptPubKey);
rawTx.vout.push_back(out);
}
2012-05-31 16:01:16 -04:00
}
return EncodeHexTx(rawTx);
2012-05-31 16:01:16 -04:00
}
UniValue decoderawtransaction(const UniValue& params, bool fHelp)
2012-05-31 16:01:16 -04:00
{
if (fHelp || params.size() != 1)
2017-08-02 19:53:22 +05:30
throw std::runtime_error(
2013-10-29 22:29:44 +11:00
"decoderawtransaction \"hexstring\"\n"
"\nReturn a JSON object representing the serialized, hex-encoded transaction.\n"
"\nArguments:\n"
"1. \"hex\" (string, required) The transaction hex string\n"
2013-10-29 22:29:44 +11:00
"\nResult:\n"
"{\n"
" \"txid\" : \"id\", (string) The transaction id\n"
2015-11-21 05:35:11 +03:00
" \"size\" : n, (numeric) The transaction size\n"
2013-10-29 22:29:44 +11:00
" \"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) 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"
2021-01-20 19:21:53 +01:00
" \"value\" : x.xxx, (numeric) The value in BCH\n"
2013-10-29 22:29:44 +11:00
" \"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"
" \"12tvKAXCxZjSmdNbao16dKXC8tRWfcF5oc\" (string) bitcoin address\n"
" ,...\n"
" ]\n"
" }\n"
" }\n"
" ,...\n"
" ],\n"
"}\n"
"\nExamples:\n"
+ HelpExampleCli("decoderawtransaction", "\"hexstring\"")
+ HelpExampleRpc("decoderawtransaction", "\"hexstring\"")
);
2012-05-31 16:01:16 -04:00
LOCK(cs_main);
RPCTypeCheck(params, boost::assign::list_of(UniValue::VSTR));
2012-05-31 16:01:16 -04:00
CTransaction tx;
if (!DecodeHexTx(tx, params[0].get_str()))
2012-10-04 09:34:44 +02:00
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "TX decode failed");
2012-05-31 16:01:16 -04:00
2015-05-10 13:35:44 +02:00
UniValue result(UniValue::VOBJ);
TxToJSON(tx, uint256(), result);
2012-05-31 16:01:16 -04:00
return result;
}
UniValue decodescript(const UniValue& params, bool fHelp)
2013-07-15 01:22:10 -04:00
{
if (fHelp || params.size() != 1)
2017-08-02 19:53:22 +05:30
throw std::runtime_error(
2013-10-29 22:29:44 +11:00
"decodescript \"hex\"\n"
"\nDecode a hex-encoded script.\n"
"\nArguments:\n"
"1. \"hex\" (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) bitcoin address\n"
" ,...\n"
" ],\n"
" \"p2sh\",\"address\" (string) script address\n"
"}\n"
"\nExamples:\n"
+ HelpExampleCli("decodescript", "\"hexstring\"")
+ HelpExampleRpc("decodescript", "\"hexstring\"")
);
2013-07-15 01:22:10 -04:00
2015-05-10 13:35:44 +02:00
RPCTypeCheck(params, boost::assign::list_of(UniValue::VSTR));
2013-07-15 01:22:10 -04:00
UniValue r(UniValue::VOBJ);
2013-07-15 01:22:10 -04:00
CScript script;
if (params[0].get_str().size() > 0){
2017-08-02 19:53:22 +05:30
std::vector<unsigned char> scriptData(ParseHexV(params[0], "argument"));
2013-07-15 01:22:10 -04:00
script = CScript(scriptData.begin(), scriptData.end());
} else {
// Empty scripts are valid
}
ScriptPubKeyToJSON(script, r, false);
r.push_back(Pair("p2sh", CBitcoinAddress(CScriptID(script)).ToString()));
2013-07-15 01:22:10 -04:00
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.push_back(Pair("txid", txin.prevout.hash.ToString()));
entry.push_back(Pair("vout", (uint64_t)txin.prevout.n));
entry.push_back(Pair("scriptSig", HexStr(txin.scriptSig.begin(), txin.scriptSig.end())));
entry.push_back(Pair("sequence", (uint64_t)txin.nSequence));
entry.push_back(Pair("error", strMessage));
vErrorsRet.push_back(entry);
}
UniValue sendrawtransaction(const UniValue& params, bool fHelp)
2012-05-31 16:01:16 -04:00
{
if (fHelp || params.size() < 1 || params.size() > 2)
2017-08-02 19:53:22 +05:30
throw std::runtime_error(
2013-10-29 22:29:44 +11:00
"sendrawtransaction \"hexstring\" ( allowhighfees )\n"
"\nSubmits raw transaction (serialized, hex-encoded) to local node and network.\n"
2026-05-05 00:39:16 +02:00
"\nAlso see createrawtransaction call.\n"
2013-10-29 22:29:44 +11:00
"\nArguments:\n"
"1. \"hexstring\" (string, required) The hex string of the raw transaction)\n"
"2. allowhighfees (boolean, optional, default=false) Allow high fees\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}\"") +
"\nSend the transaction (signed hex)\n"
+ HelpExampleCli("sendrawtransaction", "\"signedhex\"") +
"\nAs a json rpc call\n"
+ HelpExampleRpc("sendrawtransaction", "\"signedhex\"")
);
2018-01-15 15:26:12 +00:00
std::future<std::string> future;
uint256 hashTx;
{
LOCK(cs_main);
RPCTypeCheck(params, boost::assign::list_of(UniValue::VSTR)(UniValue::VBOOL));
2012-05-31 16:01:16 -04:00
// parse hex string from parameter
CTransaction tx;
if (!DecodeHexTx(tx, params[0].get_str()))
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "TX decode failed");
2018-01-15 15:26:12 +00:00
hashTx = tx.GetHash();
2012-05-31 16:01:16 -04:00
bool fOverrideFees = false;
if (params.size() > 1)
fOverrideFees = params[1].get_bool();
auto utxo = g_utxo->find(hashTx, 0);
2014-02-22 12:02:42 +01:00
bool fHaveMempool = mempool.exists(hashTx);
bool fHaveChain = utxo.isValid() && utxo.blockHeight() < 1000000000;
2014-02-22 12:02:42 +01:00
if (!fHaveMempool && !fHaveChain) {
// push to local node and sync with wallets
2021-03-04 15:48:56 +01:00
uint32_t flags = 0;
2018-01-15 15:26:12 +00:00
if (!fOverrideFees)
flags |= Validation::RejectAbsurdFeeTx;
future = Application::instance()->validation()->addTransaction(Tx::fromOldTransaction(tx), flags);
2014-02-22 12:02:42 +01:00
} else if (fHaveChain) {
throw JSONRPCError(RPC_TRANSACTION_ALREADY_IN_CHAIN, "transaction already in block chain");
}
2018-01-15 15:26:12 +00:00
}
if (future.valid()) {
const std::string result = future.get();
if (!result.empty()) {
2018-01-25 09:33:19 +00:00
if (result == "16: missing-inputs")
throw JSONRPCError(RPC_TRANSACTION_ERROR, result);
2018-01-15 15:26:12 +00:00
throw JSONRPCError(RPC_TRANSACTION_REJECTED, result);
}
}
return hashTx.GetHex();
2012-05-31 16:01:16 -04:00
}