Bitcoin Core  0.19.99
P2P Digital Currency
rawtransaction_util.cpp
Go to the documentation of this file.
1 // Copyright (c) 2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2019 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
7 
8 #include <coins.h>
9 #include <core_io.h>
10 #include <key_io.h>
11 #include <policy/policy.h>
12 #include <primitives/transaction.h>
13 #include <rpc/request.h>
14 #include <rpc/util.h>
15 #include <script/sign.h>
16 #include <script/signingprovider.h>
17 #include <tinyformat.h>
18 #include <univalue.h>
19 #include <util/rbf.h>
20 #include <util/strencodings.h>
21 
22 CMutableTransaction ConstructTransaction(const UniValue& inputs_in, const UniValue& outputs_in, const UniValue& locktime, bool rbf)
23 {
24  if (inputs_in.isNull() || outputs_in.isNull())
25  throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, arguments 1 and 2 must be non-null");
26 
27  UniValue inputs = inputs_in.get_array();
28  const bool outputs_is_obj = outputs_in.isObject();
29  UniValue outputs = outputs_is_obj ? outputs_in.get_obj() : outputs_in.get_array();
30 
31  CMutableTransaction rawTx;
32 
33  if (!locktime.isNull()) {
34  int64_t nLockTime = locktime.get_int64();
35  if (nLockTime < 0 || nLockTime > LOCKTIME_MAX)
36  throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, locktime out of range");
37  rawTx.nLockTime = nLockTime;
38  }
39 
40  for (unsigned int idx = 0; idx < inputs.size(); idx++) {
41  const UniValue& input = inputs[idx];
42  const UniValue& o = input.get_obj();
43 
44  uint256 txid = ParseHashO(o, "txid");
45 
46  const UniValue& vout_v = find_value(o, "vout");
47  if (!vout_v.isNum())
48  throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, missing vout key");
49  int nOutput = vout_v.get_int();
50  if (nOutput < 0)
51  throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, vout must be positive");
52 
53  uint32_t nSequence;
54  if (rbf) {
55  nSequence = MAX_BIP125_RBF_SEQUENCE; /* CTxIn::SEQUENCE_FINAL - 2 */
56  } else if (rawTx.nLockTime) {
57  nSequence = CTxIn::SEQUENCE_FINAL - 1;
58  } else {
59  nSequence = CTxIn::SEQUENCE_FINAL;
60  }
61 
62  // set the sequence number if passed in the parameters object
63  const UniValue& sequenceObj = find_value(o, "sequence");
64  if (sequenceObj.isNum()) {
65  int64_t seqNr64 = sequenceObj.get_int64();
66  if (seqNr64 < 0 || seqNr64 > CTxIn::SEQUENCE_FINAL) {
67  throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, sequence number is out of range");
68  } else {
69  nSequence = (uint32_t)seqNr64;
70  }
71  }
72 
73  CTxIn in(COutPoint(txid, nOutput), CScript(), nSequence);
74 
75  rawTx.vin.push_back(in);
76  }
77 
78  if (!outputs_is_obj) {
79  // Translate array of key-value pairs into dict
80  UniValue outputs_dict = UniValue(UniValue::VOBJ);
81  for (size_t i = 0; i < outputs.size(); ++i) {
82  const UniValue& output = outputs[i];
83  if (!output.isObject()) {
84  throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, key-value pair not an object as expected");
85  }
86  if (output.size() != 1) {
87  throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, key-value pair must contain exactly one key");
88  }
89  outputs_dict.pushKVs(output);
90  }
91  outputs = std::move(outputs_dict);
92  }
93 
94  // Duplicate checking
95  std::set<CTxDestination> destinations;
96  bool has_data{false};
97 
98  for (const std::string& name_ : outputs.getKeys()) {
99  if (name_ == "data") {
100  if (has_data) {
101  throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, duplicate key: data");
102  }
103  has_data = true;
104  std::vector<unsigned char> data = ParseHexV(outputs[name_].getValStr(), "Data");
105 
106  CTxOut out(0, CScript() << OP_RETURN << data);
107  rawTx.vout.push_back(out);
108  } else {
109  CTxDestination destination = DecodeDestination(name_);
110  if (!IsValidDestination(destination)) {
111  throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, std::string("Invalid Bitcoin address: ") + name_);
112  }
113 
114  if (!destinations.insert(destination).second) {
115  throw JSONRPCError(RPC_INVALID_PARAMETER, std::string("Invalid parameter, duplicated address: ") + name_);
116  }
117 
118  CScript scriptPubKey = GetScriptForDestination(destination);
119  CAmount nAmount = AmountFromValue(outputs[name_]);
120 
121  CTxOut out(nAmount, scriptPubKey);
122  rawTx.vout.push_back(out);
123  }
124  }
125 
126  if (rbf && rawTx.vin.size() > 0 && !SignalsOptInRBF(CTransaction(rawTx))) {
127  throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter combination: Sequence number(s) contradict replaceable option");
128  }
129 
130  return rawTx;
131 }
132 
134 static void TxInErrorToJSON(const CTxIn& txin, UniValue& vErrorsRet, const std::string& strMessage)
135 {
136  UniValue entry(UniValue::VOBJ);
137  entry.pushKV("txid", txin.prevout.hash.ToString());
138  entry.pushKV("vout", (uint64_t)txin.prevout.n);
139  UniValue witness(UniValue::VARR);
140  for (unsigned int i = 0; i < txin.scriptWitness.stack.size(); i++) {
141  witness.push_back(HexStr(txin.scriptWitness.stack[i].begin(), txin.scriptWitness.stack[i].end()));
142  }
143  entry.pushKV("witness", witness);
144  entry.pushKV("scriptSig", HexStr(txin.scriptSig.begin(), txin.scriptSig.end()));
145  entry.pushKV("sequence", (uint64_t)txin.nSequence);
146  entry.pushKV("error", strMessage);
147  vErrorsRet.push_back(entry);
148 }
149 
150 void ParsePrevouts(const UniValue& prevTxsUnival, FillableSigningProvider* keystore, std::map<COutPoint, Coin>& coins)
151 {
152  if (!prevTxsUnival.isNull()) {
153  UniValue prevTxs = prevTxsUnival.get_array();
154  for (unsigned int idx = 0; idx < prevTxs.size(); ++idx) {
155  const UniValue& p = prevTxs[idx];
156  if (!p.isObject()) {
157  throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "expected object with {\"txid'\",\"vout\",\"scriptPubKey\"}");
158  }
159 
160  UniValue prevOut = p.get_obj();
161 
162  RPCTypeCheckObj(prevOut,
163  {
164  {"txid", UniValueType(UniValue::VSTR)},
165  {"vout", UniValueType(UniValue::VNUM)},
166  {"scriptPubKey", UniValueType(UniValue::VSTR)},
167  });
168 
169  uint256 txid = ParseHashO(prevOut, "txid");
170 
171  int nOut = find_value(prevOut, "vout").get_int();
172  if (nOut < 0) {
173  throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "vout must be positive");
174  }
175 
176  COutPoint out(txid, nOut);
177  std::vector<unsigned char> pkData(ParseHexO(prevOut, "scriptPubKey"));
178  CScript scriptPubKey(pkData.begin(), pkData.end());
179 
180  {
181  auto coin = coins.find(out);
182  if (coin != coins.end() && !coin->second.IsSpent() && coin->second.out.scriptPubKey != scriptPubKey) {
183  std::string err("Previous output scriptPubKey mismatch:\n");
184  err = err + ScriptToAsmStr(coin->second.out.scriptPubKey) + "\nvs:\n"+
185  ScriptToAsmStr(scriptPubKey);
187  }
188  Coin newcoin;
189  newcoin.out.scriptPubKey = scriptPubKey;
190  newcoin.out.nValue = MAX_MONEY;
191  if (prevOut.exists("amount")) {
192  newcoin.out.nValue = AmountFromValue(find_value(prevOut, "amount"));
193  }
194  newcoin.nHeight = 1;
195  coins[out] = std::move(newcoin);
196  }
197 
198  // if redeemScript and private keys were given, add redeemScript to the keystore so it can be signed
199  const bool is_p2sh = scriptPubKey.IsPayToScriptHash();
200  const bool is_p2wsh = scriptPubKey.IsPayToWitnessScriptHash();
201  if (keystore && (is_p2sh || is_p2wsh)) {
202  RPCTypeCheckObj(prevOut,
203  {
204  {"redeemScript", UniValueType(UniValue::VSTR)},
205  {"witnessScript", UniValueType(UniValue::VSTR)},
206  }, true);
207  UniValue rs = find_value(prevOut, "redeemScript");
208  UniValue ws = find_value(prevOut, "witnessScript");
209  if (rs.isNull() && ws.isNull()) {
210  throw JSONRPCError(RPC_INVALID_PARAMETER, "Missing redeemScript/witnessScript");
211  }
212 
213  // work from witnessScript when possible
214  std::vector<unsigned char> scriptData(!ws.isNull() ? ParseHexV(ws, "witnessScript") : ParseHexV(rs, "redeemScript"));
215  CScript script(scriptData.begin(), scriptData.end());
216  keystore->AddCScript(script);
217  // Automatically also add the P2WSH wrapped version of the script (to deal with P2SH-P2WSH).
218  // This is done for redeemScript only for compatibility, it is encouraged to use the explicit witnessScript field instead.
219  CScript witness_output_script{GetScriptForWitness(script)};
220  keystore->AddCScript(witness_output_script);
221 
222  if (!ws.isNull() && !rs.isNull()) {
223  // if both witnessScript and redeemScript are provided,
224  // they should either be the same (for backwards compat),
225  // or the redeemScript should be the encoded form of
226  // the witnessScript (ie, for p2sh-p2wsh)
227  if (ws.get_str() != rs.get_str()) {
228  std::vector<unsigned char> redeemScriptData(ParseHexV(rs, "redeemScript"));
229  CScript redeemScript(redeemScriptData.begin(), redeemScriptData.end());
230  if (redeemScript != witness_output_script) {
231  throw JSONRPCError(RPC_INVALID_PARAMETER, "redeemScript does not correspond to witnessScript");
232  }
233  }
234  }
235 
236  if (is_p2sh) {
237  const CTxDestination p2sh{ScriptHash(script)};
238  const CTxDestination p2sh_p2wsh{ScriptHash(witness_output_script)};
239  if (scriptPubKey == GetScriptForDestination(p2sh)) {
240  // traditional p2sh; arguably an error if
241  // we got here with rs.IsNull(), because
242  // that means the p2sh script was specified
243  // via witnessScript param, but for now
244  // we'll just quietly accept it
245  } else if (scriptPubKey == GetScriptForDestination(p2sh_p2wsh)) {
246  // p2wsh encoded as p2sh; ideally the witness
247  // script was specified in the witnessScript
248  // param, but also support specifying it via
249  // redeemScript param for backwards compat
250  // (in which case ws.IsNull() == true)
251  } else {
252  // otherwise, can't generate scriptPubKey from
253  // either script, so we got unusable parameters
254  throw JSONRPCError(RPC_INVALID_PARAMETER, "redeemScript/witnessScript does not match scriptPubKey");
255  }
256  } else if (is_p2wsh) {
257  // plain p2wsh; could throw an error if script
258  // was specified by redeemScript rather than
259  // witnessScript (ie, ws.IsNull() == true), but
260  // accept it for backwards compat
261  const CTxDestination p2wsh{WitnessV0ScriptHash(script)};
262  if (scriptPubKey != GetScriptForDestination(p2wsh)) {
263  throw JSONRPCError(RPC_INVALID_PARAMETER, "redeemScript/witnessScript does not match scriptPubKey");
264  }
265  }
266  }
267  }
268  }
269 }
270 
271 void SignTransaction(CMutableTransaction& mtx, const SigningProvider* keystore, const std::map<COutPoint, Coin>& coins, const UniValue& hashType, UniValue& result)
272 {
273  int nHashType = ParseSighashString(hashType);
274 
275  bool fHashSingle = ((nHashType & ~SIGHASH_ANYONECANPAY) == SIGHASH_SINGLE);
276 
277  // Script verification errors
278  UniValue vErrors(UniValue::VARR);
279 
280  // Use CTransaction for the constant parts of the
281  // transaction to avoid rehashing.
282  const CTransaction txConst(mtx);
283  // Sign what we can:
284  for (unsigned int i = 0; i < mtx.vin.size(); i++) {
285  CTxIn& txin = mtx.vin[i];
286  auto coin = coins.find(txin.prevout);
287  if (coin == coins.end() || coin->second.IsSpent()) {
288  TxInErrorToJSON(txin, vErrors, "Input not found or already spent");
289  continue;
290  }
291  const CScript& prevPubKey = coin->second.out.scriptPubKey;
292  const CAmount& amount = coin->second.out.nValue;
293 
294  SignatureData sigdata = DataFromTransaction(mtx, i, coin->second.out);
295  // Only sign SIGHASH_SINGLE if there's a corresponding output:
296  if (!fHashSingle || (i < mtx.vout.size())) {
297  ProduceSignature(*keystore, MutableTransactionSignatureCreator(&mtx, i, amount, nHashType), prevPubKey, sigdata);
298  }
299 
300  UpdateInput(txin, sigdata);
301 
302  // amount must be specified for valid segwit signature
303  if (amount == MAX_MONEY && !txin.scriptWitness.IsNull()) {
304  throw JSONRPCError(RPC_TYPE_ERROR, strprintf("Missing amount for %s", coin->second.out.ToString()));
305  }
306 
307  ScriptError serror = SCRIPT_ERR_OK;
308  if (!VerifyScript(txin.scriptSig, prevPubKey, &txin.scriptWitness, STANDARD_SCRIPT_VERIFY_FLAGS, TransactionSignatureChecker(&txConst, i, amount), &serror)) {
309  if (serror == SCRIPT_ERR_INVALID_STACK_OPERATION) {
310  // Unable to sign input and verification failed (possible attempt to partially sign).
311  TxInErrorToJSON(txin, vErrors, "Unable to sign input, invalid stack size (possibly missing key)");
312  } else if (serror == SCRIPT_ERR_SIG_NULLFAIL) {
313  // Verification failed (possibly due to insufficient signatures).
314  TxInErrorToJSON(txin, vErrors, "CHECK(MULTI)SIG failing with non-zero signature (possibly need more signatures)");
315  } else {
316  TxInErrorToJSON(txin, vErrors, ScriptErrorString(serror));
317  }
318  }
319  }
320  bool fComplete = vErrors.empty();
321 
322  result.pushKV("hex", EncodeHexTx(CTransaction(mtx)));
323  result.pushKV("complete", fComplete);
324  if (!vErrors.empty()) {
325  if (result.exists("errors")) {
326  vErrors.push_backV(result["errors"].getValues());
327  }
328  result.pushKV("errors", vErrors);
329  }
330 }
CAmount nValue
Definition: transaction.h:136
bool isObject() const
Definition: univalue.h:84
void SignTransaction(CMutableTransaction &mtx, const SigningProvider *keystore, const std::map< COutPoint, Coin > &coins, const UniValue &hashType, UniValue &result)
Sign a transaction with the given keystore and previous transactions.
static const uint32_t MAX_BIP125_RBF_SEQUENCE
Definition: rbf.h:12
enum ScriptError_t ScriptError
CScript scriptPubKey
Definition: transaction.h:137
void ParsePrevouts(const UniValue &prevTxsUnival, FillableSigningProvider *keystore, std::map< COutPoint, Coin > &coins)
Parse a prevtxs UniValue array and get the map of coins from it.
std::vector< unsigned char > ParseHexO(const UniValue &o, std::string strKey)
Definition: util.cpp:107
A UTXO entry.
Definition: coins.h:30
static const CAmount MAX_MONEY
No amount larger than this (in satoshi) is valid.
Definition: amount.h:25
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1164
bool VerifyScript(const CScript &scriptSig, const CScript &scriptPubKey, const CScriptWitness *witness, unsigned int flags, const BaseSignatureChecker &checker, ScriptError *serror)
std::vector< CTxIn > vin
Definition: transaction.h:368
CScriptWitness scriptWitness
Only serialized through CTransaction.
Definition: transaction.h:69
std::vector< unsigned char > ParseHexV(const UniValue &v, std::string strName)
Definition: util.cpp:98
static const uint32_t SEQUENCE_FINAL
Definition: transaction.h:73
virtual bool AddCScript(const CScript &redeemScript)
bool IsValidDestination(const CTxDestination &dest)
Check whether a CTxDestination is a CNoDestination.
Definition: standard.cpp:325
CTxOut out
unspent transaction output
Definition: coins.h:34
std::vector< std::vector< unsigned char > > stack
Definition: script.h:574
const std::string & get_str() const
bool isNum() const
Definition: univalue.h:82
const UniValue & get_array() const
int64_t get_int64() const
A signature creator for transactions.
Definition: sign.h:35
bool pushKVs(const UniValue &obj)
Definition: univalue.cpp:146
const std::vector< std::string > & getKeys() const
bool SignalsOptInRBF(const CTransaction &tx)
Definition: rbf.cpp:9
bool IsNull() const
Definition: script.h:579
CMutableTransaction ConstructTransaction(const UniValue &inputs_in, const UniValue &outputs_in, const UniValue &locktime, bool rbf)
Create a transaction from univalue parameters.
static void TxInErrorToJSON(const CTxIn &txin, UniValue &vErrorsRet, const std::string &strMessage)
Pushes a JSON object for script verification or signing errors to vErrorsRet.
static constexpr unsigned int STANDARD_SCRIPT_VERIFY_FLAGS
Standard script verification flags that standard transactions will comply with.
Definition: policy.h:56
Invalid, missing or duplicate parameter.
Definition: protocol.h:43
void RPCTypeCheckObj(const UniValue &o, const std::map< std::string, UniValueType > &typesExpected, bool fAllowNull, bool fStrict)
Definition: util.cpp:43
const UniValue & find_value(const UniValue &obj, const std::string &name)
Definition: univalue.cpp:234
const char * ScriptErrorString(const ScriptError serror)
Definition: script_error.cpp:8
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
uint32_t nHeight
at which height this containing transaction was included in the active block chain ...
Definition: coins.h:40
iterator end()
Definition: prevector.h:292
UniValue JSONRPCError(int code, const std::string &message)
Definition: request.cpp:51
bool push_back(const UniValue &val)
Definition: univalue.cpp:108
std::string ScriptToAsmStr(const CScript &script, const bool fAttemptSighashDecode=false)
Create the assembly string representation of a CScript object.
Definition: core_write.cpp:86
static CAmount AmountFromValue(const UniValue &value)
Definition: bitcoin-tx.cpp:532
int ParseSighashString(const UniValue &sighash)
Definition: core_read.cpp:197
An input of a transaction.
Definition: transaction.h:63
bool exists(const std::string &key) const
Definition: univalue.h:75
Fillable signing provider that keeps keys in an address->secret map.
uint32_t n
Definition: transaction.h:22
Unexpected type was passed as parameter.
Definition: protocol.h:40
bool empty() const
Definition: univalue.h:66
bool pushKV(const std::string &key, const UniValue &val)
Definition: univalue.cpp:133
An output of a transaction.
Definition: transaction.h:133
int get_int() const
std::string ToString() const
Definition: uint256.cpp:60
Invalid address or key.
Definition: protocol.h:41
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
Definition: standard.cpp:289
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:18
std::vector< CTxOut > vout
Definition: transaction.h:369
bool isNull() const
Definition: univalue.h:77
CScript scriptSig
Definition: transaction.h:67
256-bit opaque blob.
Definition: uint256.h:120
CTxDestination DecodeDestination(const std::string &str)
Definition: key_io.cpp:215
An interface to be implemented by keystores that support signing.
SignatureData DataFromTransaction(const CMutableTransaction &tx, unsigned int nIn, const CTxOut &txout)
Extract signature data from a transaction input, and insert it.
Definition: sign.cpp:286
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:390
uint32_t nSequence
Definition: transaction.h:68
const UniValue & get_obj() const
bool push_backV(const std::vector< UniValue > &vec)
Definition: univalue.cpp:117
void UpdateInput(CTxIn &input, const SignatureData &data)
Definition: sign.cpp:351
std::string HexStr(const T itbegin, const T itend)
Definition: strencodings.h:125
std::string EncodeHexTx(const CTransaction &tx, const int serializeFlags=0)
Definition: core_write.cpp:130
bool ProduceSignature(const SigningProvider &provider, const BaseSignatureCreator &creator, const CScript &fromPubKey, SignatureData &sigdata)
Produce a script signature using a generic signature creator.
Definition: sign.cpp:196
iterator begin()
Definition: prevector.h:290
A mutable version of CTransaction.
Definition: transaction.h:366
size_t size() const
Definition: univalue.h:68
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:270
boost::variant< CNoDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessUnknown > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:143
static const uint32_t LOCKTIME_MAX
Definition: script.h:45
COutPoint prevout
Definition: transaction.h:66
CScript GetScriptForWitness(const CScript &redeemscript)
Generate a pay-to-witness script for the given redeem script.
Definition: standard.cpp:313
Wrapper for UniValue::VType, which includes typeAny: Used to denote don&#39;t care type.
Definition: util.h:43
Error parsing or validating structure in raw format.
Definition: protocol.h:45
uint256 hash
Definition: transaction.h:21
uint256 ParseHashO(const UniValue &o, std::string strKey)
Definition: util.cpp:94