Line data Source code
1 : // Copyright (c) 2010 Satoshi Nakamoto
2 : // Copyright (c) 2009-2020 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 :
6 : #include <rpc/rawtransaction_util.h>
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 1218 : CMutableTransaction ConstructTransaction(const UniValue& inputs_in, const UniValue& outputs_in, const UniValue& locktime, bool rbf)
23 : {
24 1218 : if (outputs_in.isNull())
25 28 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, output argument must be non-null");
26 :
27 1217 : UniValue inputs;
28 1217 : if (inputs_in.isNull())
29 28 : inputs = UniValue::VARR;
30 : else
31 1189 : inputs = inputs_in.get_array();
32 :
33 1217 : const bool outputs_is_obj = outputs_in.isObject();
34 1217 : UniValue outputs = outputs_is_obj ? outputs_in.get_obj() : outputs_in.get_array();
35 :
36 1216 : CMutableTransaction rawTx;
37 :
38 1216 : if (!locktime.isNull()) {
39 78 : int64_t nLockTime = locktime.get_int64();
40 78 : if (nLockTime < 0 || nLockTime > LOCKTIME_MAX)
41 2 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, locktime out of range");
42 76 : rawTx.nLockTime = nLockTime;
43 78 : }
44 :
45 7222 : for (unsigned int idx = 0; idx < inputs.size(); idx++) {
46 6008 : const UniValue& input = inputs[idx];
47 6008 : const UniValue& o = input.get_obj();
48 :
49 6007 : uint256 txid = ParseHashO(o, "txid");
50 :
51 6004 : const UniValue& vout_v = find_value(o, "vout");
52 6004 : if (!vout_v.isNum())
53 2 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, missing vout key");
54 6002 : int nOutput = vout_v.get_int();
55 6002 : if (nOutput < 0)
56 1 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, vout must be positive");
57 :
58 : uint32_t nSequence;
59 6001 : if (rbf) {
60 : nSequence = MAX_BIP125_RBF_SEQUENCE; /* CTxIn::SEQUENCE_FINAL - 2 */
61 6001 : } else if (rawTx.nLockTime) {
62 : nSequence = CTxIn::SEQUENCE_FINAL - 1;
63 9 : } else {
64 : nSequence = CTxIn::SEQUENCE_FINAL;
65 : }
66 :
67 : // set the sequence number if passed in the parameters object
68 6001 : const UniValue& sequenceObj = find_value(o, "sequence");
69 6001 : if (sequenceObj.isNum()) {
70 31 : int64_t seqNr64 = sequenceObj.get_int64();
71 31 : if (seqNr64 < 0 || seqNr64 > CTxIn::SEQUENCE_FINAL) {
72 3 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, sequence number is out of range");
73 : } else {
74 28 : nSequence = (uint32_t)seqNr64;
75 : }
76 31 : }
77 :
78 5998 : CTxIn in(COutPoint(txid, nOutput), CScript(), nSequence);
79 :
80 5998 : rawTx.vin.push_back(in);
81 6008 : }
82 :
83 1204 : if (!outputs_is_obj) {
84 : // Translate array of key-value pairs into dict
85 152 : UniValue outputs_dict = UniValue(UniValue::VOBJ);
86 309 : for (size_t i = 0; i < outputs.size(); ++i) {
87 159 : const UniValue& output = outputs[i];
88 159 : if (!output.isObject()) {
89 1 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, key-value pair not an object as expected");
90 : }
91 158 : if (output.size() != 1) {
92 1 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, key-value pair must contain exactly one key");
93 : }
94 157 : outputs_dict.pushKVs(output);
95 : }
96 150 : outputs = std::move(outputs_dict);
97 152 : }
98 :
99 : // Duplicate checking
100 1202 : std::set<CTxDestination> destinations;
101 : bool has_data{false};
102 :
103 8394 : for (const std::string& name_ : outputs.getKeys()) {
104 7192 : if (name_ == "data") {
105 14 : if (has_data) {
106 2 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, duplicate key: data");
107 : }
108 : has_data = true;
109 12 : std::vector<unsigned char> data = ParseHexV(outputs[name_].getValStr(), "Data");
110 :
111 8 : CTxOut out(0, CScript() << OP_RETURN << data);
112 8 : rawTx.vout.push_back(out);
113 12 : } else {
114 7178 : CTxDestination destination = DecodeDestination(name_);
115 7178 : if (!IsValidDestination(destination)) {
116 2 : throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, std::string("Invalid Bitcoin address: ") + name_);
117 : }
118 :
119 7176 : if (!destinations.insert(destination).second) {
120 2 : throw JSONRPCError(RPC_INVALID_PARAMETER, std::string("Invalid parameter, duplicated address: ") + name_);
121 : }
122 :
123 7174 : CScript scriptPubKey = GetScriptForDestination(destination);
124 7174 : CAmount nAmount = AmountFromValue(outputs[name_]);
125 :
126 7172 : CTxOut out(nAmount, scriptPubKey);
127 7172 : rawTx.vout.push_back(out);
128 7178 : }
129 : }
130 :
131 1190 : if (rbf && rawTx.vin.size() > 0 && !SignalsOptInRBF(CTransaction(rawTx))) {
132 0 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter combination: Sequence number(s) contradict replaceable option");
133 : }
134 :
135 : return rawTx;
136 1244 : }
137 :
138 : /** Pushes a JSON object for script verification or signing errors to vErrorsRet. */
139 84 : static void TxInErrorToJSON(const CTxIn& txin, UniValue& vErrorsRet, const std::string& strMessage)
140 : {
141 84 : UniValue entry(UniValue::VOBJ);
142 84 : entry.pushKV("txid", txin.prevout.hash.ToString());
143 84 : entry.pushKV("vout", (uint64_t)txin.prevout.n);
144 84 : UniValue witness(UniValue::VARR);
145 314 : for (unsigned int i = 0; i < txin.scriptWitness.stack.size(); i++) {
146 230 : witness.push_back(HexStr(txin.scriptWitness.stack[i]));
147 : }
148 84 : entry.pushKV("witness", witness);
149 84 : entry.pushKV("scriptSig", HexStr(txin.scriptSig));
150 84 : entry.pushKV("sequence", (uint64_t)txin.nSequence);
151 84 : entry.pushKV("error", strMessage);
152 84 : vErrorsRet.push_back(entry);
153 84 : }
154 :
155 1853 : void ParsePrevouts(const UniValue& prevTxsUnival, FillableSigningProvider* keystore, std::map<COutPoint, Coin>& coins)
156 : {
157 1853 : if (!prevTxsUnival.isNull()) {
158 224 : UniValue prevTxs = prevTxsUnival.get_array();
159 345 : for (unsigned int idx = 0; idx < prevTxs.size(); ++idx) {
160 226 : const UniValue& p = prevTxs[idx];
161 226 : if (!p.isObject()) {
162 0 : throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "expected object with {\"txid'\",\"vout\",\"scriptPubKey\"}");
163 : }
164 :
165 226 : UniValue prevOut = p.get_obj();
166 :
167 678 : RPCTypeCheckObj(prevOut,
168 913 : {
169 226 : {"txid", UniValueType(UniValue::VSTR)},
170 226 : {"vout", UniValueType(UniValue::VNUM)},
171 226 : {"scriptPubKey", UniValueType(UniValue::VSTR)},
172 : });
173 :
174 217 : uint256 txid = ParseHashO(prevOut, "txid");
175 :
176 217 : int nOut = find_value(prevOut, "vout").get_int();
177 217 : if (nOut < 0) {
178 0 : throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "vout must be positive");
179 : }
180 :
181 217 : COutPoint out(txid, nOut);
182 217 : std::vector<unsigned char> pkData(ParseHexO(prevOut, "scriptPubKey"));
183 217 : CScript scriptPubKey(pkData.begin(), pkData.end());
184 :
185 : {
186 217 : auto coin = coins.find(out);
187 217 : if (coin != coins.end() && !coin->second.IsSpent() && coin->second.out.scriptPubKey != scriptPubKey) {
188 0 : std::string err("Previous output scriptPubKey mismatch:\n");
189 0 : err = err + ScriptToAsmStr(coin->second.out.scriptPubKey) + "\nvs:\n"+
190 0 : ScriptToAsmStr(scriptPubKey);
191 0 : throw JSONRPCError(RPC_DESERIALIZATION_ERROR, err);
192 0 : }
193 217 : Coin newcoin;
194 217 : newcoin.out.scriptPubKey = scriptPubKey;
195 217 : newcoin.out.nValue = MAX_MONEY;
196 217 : if (prevOut.exists("amount")) {
197 203 : newcoin.out.nValue = AmountFromValue(find_value(prevOut, "amount"));
198 203 : }
199 217 : newcoin.nHeight = 1;
200 217 : coins[out] = std::move(newcoin);
201 217 : }
202 :
203 : // if redeemScript and private keys were given, add redeemScript to the keystore so it can be signed
204 217 : const bool is_p2sh = scriptPubKey.IsPayToScriptHash();
205 217 : const bool is_p2wsh = scriptPubKey.IsPayToWitnessScriptHash();
206 217 : if (keystore && (is_p2sh || is_p2wsh)) {
207 394 : RPCTypeCheckObj(prevOut,
208 591 : {
209 197 : {"redeemScript", UniValueType(UniValue::VSTR)},
210 197 : {"witnessScript", UniValueType(UniValue::VSTR)},
211 : }, true);
212 197 : UniValue rs = find_value(prevOut, "redeemScript");
213 197 : UniValue ws = find_value(prevOut, "witnessScript");
214 197 : if (rs.isNull() && ws.isNull()) {
215 24 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Missing redeemScript/witnessScript");
216 : }
217 :
218 : // work from witnessScript when possible
219 173 : std::vector<unsigned char> scriptData(!ws.isNull() ? ParseHexV(ws, "witnessScript") : ParseHexV(rs, "redeemScript"));
220 173 : CScript script(scriptData.begin(), scriptData.end());
221 173 : keystore->AddCScript(script);
222 : // Automatically also add the P2WSH wrapped version of the script (to deal with P2SH-P2WSH).
223 : // This is done for redeemScript only for compatibility, it is encouraged to use the explicit witnessScript field instead.
224 173 : CScript witness_output_script{GetScriptForDestination(WitnessV0ScriptHash(script))};
225 173 : keystore->AddCScript(witness_output_script);
226 :
227 173 : if (!ws.isNull() && !rs.isNull()) {
228 : // if both witnessScript and redeemScript are provided,
229 : // they should either be the same (for backwards compat),
230 : // or the redeemScript should be the encoded form of
231 : // the witnessScript (ie, for p2sh-p2wsh)
232 51 : if (ws.get_str() != rs.get_str()) {
233 27 : std::vector<unsigned char> redeemScriptData(ParseHexV(rs, "redeemScript"));
234 27 : CScript redeemScript(redeemScriptData.begin(), redeemScriptData.end());
235 27 : if (redeemScript != witness_output_script) {
236 24 : throw JSONRPCError(RPC_INVALID_PARAMETER, "redeemScript does not correspond to witnessScript");
237 : }
238 27 : }
239 : }
240 :
241 149 : if (is_p2sh) {
242 101 : const CTxDestination p2sh{ScriptHash(script)};
243 101 : const CTxDestination p2sh_p2wsh{ScriptHash(witness_output_script)};
244 101 : if (scriptPubKey == GetScriptForDestination(p2sh)) {
245 : // traditional p2sh; arguably an error if
246 : // we got here with rs.IsNull(), because
247 : // that means the p2sh script was specified
248 : // via witnessScript param, but for now
249 : // we'll just quietly accept it
250 67 : } else if (scriptPubKey == GetScriptForDestination(p2sh_p2wsh)) {
251 : // p2wsh encoded as p2sh; ideally the witness
252 : // script was specified in the witnessScript
253 : // param, but also support specifying it via
254 : // redeemScript param for backwards compat
255 : // (in which case ws.IsNull() == true)
256 : } else {
257 : // otherwise, can't generate scriptPubKey from
258 : // either script, so we got unusable parameters
259 32 : throw JSONRPCError(RPC_INVALID_PARAMETER, "redeemScript/witnessScript does not match scriptPubKey");
260 : }
261 149 : } else if (is_p2wsh) {
262 : // plain p2wsh; could throw an error if script
263 : // was specified by redeemScript rather than
264 : // witnessScript (ie, ws.IsNull() == true), but
265 : // accept it for backwards compat
266 48 : const CTxDestination p2wsh{WitnessV0ScriptHash(script)};
267 48 : if (scriptPubKey != GetScriptForDestination(p2wsh)) {
268 16 : throw JSONRPCError(RPC_INVALID_PARAMETER, "redeemScript/witnessScript does not match scriptPubKey");
269 : }
270 48 : }
271 197 : }
272 226 : }
273 224 : }
274 1862 : }
275 :
276 109 : void SignTransaction(CMutableTransaction& mtx, const SigningProvider* keystore, const std::map<COutPoint, Coin>& coins, const UniValue& hashType, UniValue& result)
277 : {
278 109 : int nHashType = ParseSighashString(hashType);
279 :
280 : // Script verification errors
281 109 : std::map<int, std::string> input_errors;
282 :
283 109 : bool complete = SignTransaction(mtx, keystore, coins, nHashType, input_errors);
284 109 : SignTransactionResultToJSON(mtx, complete, coins, input_errors, result);
285 109 : }
286 :
287 1748 : void SignTransactionResultToJSON(CMutableTransaction& mtx, bool complete, const std::map<COutPoint, Coin>& coins, std::map<int, std::string>& input_errors, UniValue& result)
288 : {
289 : // Make errors UniValue
290 1748 : UniValue vErrors(UniValue::VARR);
291 1834 : for (const auto& err_pair : input_errors) {
292 86 : if (err_pair.second == "Missing amount") {
293 : // This particular error needs to be an exception for some reason
294 2 : throw JSONRPCError(RPC_TYPE_ERROR, strprintf("Missing amount for %s", coins.at(mtx.vin.at(err_pair.first).prevout).out.ToString()));
295 : }
296 84 : TxInErrorToJSON(mtx.vin.at(err_pair.first), vErrors, err_pair.second);
297 2 : }
298 :
299 1746 : result.pushKV("hex", EncodeHexTx(CTransaction(mtx)));
300 1746 : result.pushKV("complete", complete);
301 1746 : if (!vErrors.empty()) {
302 82 : if (result.exists("errors")) {
303 0 : vErrors.push_backV(result["errors"].getValues());
304 0 : }
305 82 : result.pushKV("errors", vErrors);
306 82 : }
307 1750 : }
|