mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-02-18 11:29:06 +00:00
93ce4a0b6fb54efb1f424a71dfc09cc33307e5b9 Move WatchOnly stuff from SigningProvider to CWallet (Andrew Chow)
8f5b81e6edae9cb22559545de63f391d97c15701 Remove CCryptoKeyStore and move all of it's functionality into CWallet (Andrew Chow)
37a79a4fccbf6cd65a933594e24e59d36e674653 Move various SigningProviders to signingprovider.{cpp,h} (Andrew Chow)
16f8096e911e4d59292240a17e2d4004f0500b9e Move KeyOriginInfo to its own header file (Andrew Chow)
d9becff4e13da8e182631baa79b9794c03d44434 scripted-diff: rename CBasicKeyStore to FillableSigningProvider (Andrew Chow)
a913e3f2fbeb1352fc66f334d4f5f7332ea89ad7 Move HaveKey static function from keystore to rpcwallet where it is used (Andrew Chow)
c7797ec65544bd23a2e571b2892e1bf512f2a485 Remove CKeyStore and squash into CBasicKeyStore (Andrew Chow)
1b699a5083b435c2b79f3951f94ac9f967d24f6c Add HaveKey and HaveCScript to SigningProvider (Andrew Chow)
Pull request description:
This PR compresses the `CWallet` chain of inheritance from 5 classes to 3 classes. `CBasicKeyStore` is renamed to `FillableSigningProvider` and some parts of it (the watchonly parts) are moved into `CWallet`. `CKeyStore` and `CCrypoKeyStore` are completely removed. `CKeyStore`'s `Have*` functions are moved into `SigningProvider` and the `Add*` moved into `FillableSigningProvider`, thus allowing it to go away entirely. `CCryptoKeyStore`'s functionality is moved into `CWallet`. The new inheritance chain is:
```
SigningProvider -> FillableSigningProvider -> CWallet
```
`SigningProvider` now is the class the provides keys and scripts and indicates whether keys and scripts are present. `FillableSigningProvider` allows keys and scripts to be added to the signing provider via `Add*` functions. `CWallet` handles all of the watchonly stuff (`AddWatchOnly`, `HaveWatchOnly`, `RemoveWatchOnly` which were previously in `CKeyStore`) and key encryption (previously in `CCryptoKeyStore`).
Implements the 2nd [prerequisite](https://github.com/bitcoin-core/bitcoin-devwiki/wiki/Wallet-Class-Structure-Changes#cwallet-subclass-stack) from the wallet restructure.
ACKs for top commit:
Sjors:
re-ACK 93ce4a0; it keeps `EncryptSecret`, `DecryptSecret` and `DecryptKey` in `wallet/crypter.cpp`, but makes them not static. It improves alphabetical includes, reorders some function definitions, fixes commit message, brings back lost code comment.
instagibbs:
utACK 93ce4a0b6f
Tree-SHA512: 393dfd0623ad2dac38395eb89b862424318d6072f0b7083c92a0d207fd032c48b284f5f2cb13bc492f34557de350c5fee925da02e47daf011c5c6930a721b6d3
287 lines
12 KiB
C++
287 lines
12 KiB
C++
// Copyright (c) 2010 Satoshi Nakamoto
|
|
// Copyright (c) 2009-2019 The Bitcoin Core developers
|
|
// Distributed under the MIT software license, see the accompanying
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
#include <rpc/rawtransaction_util.h>
|
|
|
|
#include <coins.h>
|
|
#include <core_io.h>
|
|
#include <key_io.h>
|
|
#include <policy/policy.h>
|
|
#include <primitives/transaction.h>
|
|
#include <rpc/request.h>
|
|
#include <rpc/util.h>
|
|
#include <script/sign.h>
|
|
#include <script/signingprovider.h>
|
|
#include <tinyformat.h>
|
|
#include <univalue.h>
|
|
#include <util/rbf.h>
|
|
#include <util/strencodings.h>
|
|
|
|
CMutableTransaction ConstructTransaction(const UniValue& inputs_in, const UniValue& outputs_in, const UniValue& locktime, const UniValue& rbf)
|
|
{
|
|
if (inputs_in.isNull() || outputs_in.isNull())
|
|
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, arguments 1 and 2 must be non-null");
|
|
|
|
UniValue inputs = inputs_in.get_array();
|
|
const bool outputs_is_obj = outputs_in.isObject();
|
|
UniValue outputs = outputs_is_obj ? outputs_in.get_obj() : outputs_in.get_array();
|
|
|
|
CMutableTransaction rawTx;
|
|
|
|
if (!locktime.isNull()) {
|
|
int64_t nLockTime = locktime.get_int64();
|
|
if (nLockTime < 0 || nLockTime > LOCKTIME_MAX)
|
|
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, locktime out of range");
|
|
rawTx.nLockTime = nLockTime;
|
|
}
|
|
|
|
bool rbfOptIn = rbf.isTrue();
|
|
|
|
for (unsigned int idx = 0; idx < inputs.size(); idx++) {
|
|
const UniValue& input = inputs[idx];
|
|
const UniValue& o = input.get_obj();
|
|
|
|
uint256 txid = ParseHashO(o, "txid");
|
|
|
|
const UniValue& vout_v = find_value(o, "vout");
|
|
if (!vout_v.isNum())
|
|
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, missing vout key");
|
|
int nOutput = vout_v.get_int();
|
|
if (nOutput < 0)
|
|
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, vout must be positive");
|
|
|
|
uint32_t nSequence;
|
|
if (rbfOptIn) {
|
|
nSequence = MAX_BIP125_RBF_SEQUENCE; /* CTxIn::SEQUENCE_FINAL - 2 */
|
|
} else if (rawTx.nLockTime) {
|
|
nSequence = CTxIn::SEQUENCE_FINAL - 1;
|
|
} else {
|
|
nSequence = CTxIn::SEQUENCE_FINAL;
|
|
}
|
|
|
|
// set the sequence number if passed in the parameters object
|
|
const UniValue& sequenceObj = find_value(o, "sequence");
|
|
if (sequenceObj.isNum()) {
|
|
int64_t seqNr64 = sequenceObj.get_int64();
|
|
if (seqNr64 < 0 || seqNr64 > CTxIn::SEQUENCE_FINAL) {
|
|
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, sequence number is out of range");
|
|
} else {
|
|
nSequence = (uint32_t)seqNr64;
|
|
}
|
|
}
|
|
|
|
CTxIn in(COutPoint(txid, nOutput), CScript(), nSequence);
|
|
|
|
rawTx.vin.push_back(in);
|
|
}
|
|
|
|
if (!outputs_is_obj) {
|
|
// Translate array of key-value pairs into dict
|
|
UniValue outputs_dict = UniValue(UniValue::VOBJ);
|
|
for (size_t i = 0; i < outputs.size(); ++i) {
|
|
const UniValue& output = outputs[i];
|
|
if (!output.isObject()) {
|
|
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, key-value pair not an object as expected");
|
|
}
|
|
if (output.size() != 1) {
|
|
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, key-value pair must contain exactly one key");
|
|
}
|
|
outputs_dict.pushKVs(output);
|
|
}
|
|
outputs = std::move(outputs_dict);
|
|
}
|
|
|
|
// Duplicate checking
|
|
std::set<CTxDestination> destinations;
|
|
bool has_data{false};
|
|
|
|
for (const std::string& name_ : outputs.getKeys()) {
|
|
if (name_ == "data") {
|
|
if (has_data) {
|
|
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, duplicate key: data");
|
|
}
|
|
has_data = true;
|
|
std::vector<unsigned char> data = ParseHexV(outputs[name_].getValStr(), "Data");
|
|
|
|
CTxOut out(0, CScript() << OP_RETURN << data);
|
|
rawTx.vout.push_back(out);
|
|
} else {
|
|
CTxDestination destination = DecodeDestination(name_);
|
|
if (!IsValidDestination(destination)) {
|
|
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, std::string("Invalid Bitcoin address: ") + name_);
|
|
}
|
|
|
|
if (!destinations.insert(destination).second) {
|
|
throw JSONRPCError(RPC_INVALID_PARAMETER, std::string("Invalid parameter, duplicated address: ") + name_);
|
|
}
|
|
|
|
CScript scriptPubKey = GetScriptForDestination(destination);
|
|
CAmount nAmount = AmountFromValue(outputs[name_]);
|
|
|
|
CTxOut out(nAmount, scriptPubKey);
|
|
rawTx.vout.push_back(out);
|
|
}
|
|
}
|
|
|
|
if (!rbf.isNull() && rawTx.vin.size() > 0 && rbfOptIn != SignalsOptInRBF(CTransaction(rawTx))) {
|
|
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter combination: Sequence number(s) contradict replaceable option");
|
|
}
|
|
|
|
return rawTx;
|
|
}
|
|
|
|
/** 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.pushKV("txid", txin.prevout.hash.ToString());
|
|
entry.pushKV("vout", (uint64_t)txin.prevout.n);
|
|
UniValue witness(UniValue::VARR);
|
|
for (unsigned int i = 0; i < txin.scriptWitness.stack.size(); i++) {
|
|
witness.push_back(HexStr(txin.scriptWitness.stack[i].begin(), txin.scriptWitness.stack[i].end()));
|
|
}
|
|
entry.pushKV("witness", witness);
|
|
entry.pushKV("scriptSig", HexStr(txin.scriptSig.begin(), txin.scriptSig.end()));
|
|
entry.pushKV("sequence", (uint64_t)txin.nSequence);
|
|
entry.pushKV("error", strMessage);
|
|
vErrorsRet.push_back(entry);
|
|
}
|
|
|
|
UniValue SignTransaction(CMutableTransaction& mtx, const UniValue& prevTxsUnival, FillableSigningProvider* keystore, std::map<COutPoint, Coin>& coins, bool is_temp_keystore, const UniValue& hashType)
|
|
{
|
|
// Add previous txouts given in the RPC call:
|
|
if (!prevTxsUnival.isNull()) {
|
|
UniValue prevTxs = prevTxsUnival.get_array();
|
|
for (unsigned int idx = 0; idx < prevTxs.size(); ++idx) {
|
|
const UniValue& p = prevTxs[idx];
|
|
if (!p.isObject()) {
|
|
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "expected object with {\"txid'\",\"vout\",\"scriptPubKey\"}");
|
|
}
|
|
|
|
UniValue prevOut = p.get_obj();
|
|
|
|
RPCTypeCheckObj(prevOut,
|
|
{
|
|
{"txid", UniValueType(UniValue::VSTR)},
|
|
{"vout", UniValueType(UniValue::VNUM)},
|
|
{"scriptPubKey", UniValueType(UniValue::VSTR)},
|
|
});
|
|
|
|
uint256 txid = ParseHashO(prevOut, "txid");
|
|
|
|
int nOut = find_value(prevOut, "vout").get_int();
|
|
if (nOut < 0) {
|
|
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "vout must be positive");
|
|
}
|
|
|
|
COutPoint out(txid, nOut);
|
|
std::vector<unsigned char> pkData(ParseHexO(prevOut, "scriptPubKey"));
|
|
CScript scriptPubKey(pkData.begin(), pkData.end());
|
|
|
|
{
|
|
auto coin = coins.find(out);
|
|
if (coin != coins.end() && !coin->second.IsSpent() && coin->second.out.scriptPubKey != scriptPubKey) {
|
|
std::string err("Previous output scriptPubKey mismatch:\n");
|
|
err = err + ScriptToAsmStr(coin->second.out.scriptPubKey) + "\nvs:\n"+
|
|
ScriptToAsmStr(scriptPubKey);
|
|
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, err);
|
|
}
|
|
Coin newcoin;
|
|
newcoin.out.scriptPubKey = scriptPubKey;
|
|
newcoin.out.nValue = MAX_MONEY;
|
|
if (prevOut.exists("amount")) {
|
|
newcoin.out.nValue = AmountFromValue(find_value(prevOut, "amount"));
|
|
}
|
|
newcoin.nHeight = 1;
|
|
coins[out] = std::move(newcoin);
|
|
}
|
|
|
|
// if redeemScript and private keys were given, add redeemScript to the keystore so it can be signed
|
|
if (is_temp_keystore && (scriptPubKey.IsPayToScriptHash() || scriptPubKey.IsPayToWitnessScriptHash())) {
|
|
RPCTypeCheckObj(prevOut,
|
|
{
|
|
{"redeemScript", UniValueType(UniValue::VSTR)},
|
|
{"witnessScript", UniValueType(UniValue::VSTR)},
|
|
}, true);
|
|
UniValue rs = find_value(prevOut, "redeemScript");
|
|
if (!rs.isNull()) {
|
|
std::vector<unsigned char> rsData(ParseHexV(rs, "redeemScript"));
|
|
CScript redeemScript(rsData.begin(), rsData.end());
|
|
keystore->AddCScript(redeemScript);
|
|
// Automatically also add the P2WSH wrapped version of the script (to deal with P2SH-P2WSH).
|
|
// This is only for compatibility, it is encouraged to use the explicit witnessScript field instead.
|
|
keystore->AddCScript(GetScriptForWitness(redeemScript));
|
|
}
|
|
UniValue ws = find_value(prevOut, "witnessScript");
|
|
if (!ws.isNull()) {
|
|
std::vector<unsigned char> wsData(ParseHexV(ws, "witnessScript"));
|
|
CScript witnessScript(wsData.begin(), wsData.end());
|
|
keystore->AddCScript(witnessScript);
|
|
// Automatically also add the P2WSH wrapped version of the script (to deal with P2SH-P2WSH).
|
|
keystore->AddCScript(GetScriptForWitness(witnessScript));
|
|
}
|
|
if (rs.isNull() && ws.isNull()) {
|
|
throw JSONRPCError(RPC_INVALID_PARAMETER, "Missing redeemScript/witnessScript");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
int nHashType = ParseSighashString(hashType);
|
|
|
|
bool fHashSingle = ((nHashType & ~SIGHASH_ANYONECANPAY) == SIGHASH_SINGLE);
|
|
|
|
// Script verification errors
|
|
UniValue vErrors(UniValue::VARR);
|
|
|
|
// Use CTransaction for the constant parts of the
|
|
// transaction to avoid rehashing.
|
|
const CTransaction txConst(mtx);
|
|
// Sign what we can:
|
|
for (unsigned int i = 0; i < mtx.vin.size(); i++) {
|
|
CTxIn& txin = mtx.vin[i];
|
|
auto coin = coins.find(txin.prevout);
|
|
if (coin == coins.end() || coin->second.IsSpent()) {
|
|
TxInErrorToJSON(txin, vErrors, "Input not found or already spent");
|
|
continue;
|
|
}
|
|
const CScript& prevPubKey = coin->second.out.scriptPubKey;
|
|
const CAmount& amount = coin->second.out.nValue;
|
|
|
|
SignatureData sigdata = DataFromTransaction(mtx, i, coin->second.out);
|
|
// Only sign SIGHASH_SINGLE if there's a corresponding output:
|
|
if (!fHashSingle || (i < mtx.vout.size())) {
|
|
ProduceSignature(*keystore, MutableTransactionSignatureCreator(&mtx, i, amount, nHashType), prevPubKey, sigdata);
|
|
}
|
|
|
|
UpdateInput(txin, sigdata);
|
|
|
|
// amount must be specified for valid segwit signature
|
|
if (amount == MAX_MONEY && !txin.scriptWitness.IsNull()) {
|
|
throw JSONRPCError(RPC_TYPE_ERROR, strprintf("Missing amount for %s", coin->second.out.ToString()));
|
|
}
|
|
|
|
ScriptError serror = SCRIPT_ERR_OK;
|
|
if (!VerifyScript(txin.scriptSig, prevPubKey, &txin.scriptWitness, STANDARD_SCRIPT_VERIFY_FLAGS, TransactionSignatureChecker(&txConst, i, amount), &serror)) {
|
|
if (serror == SCRIPT_ERR_INVALID_STACK_OPERATION) {
|
|
// Unable to sign input and verification failed (possible attempt to partially sign).
|
|
TxInErrorToJSON(txin, vErrors, "Unable to sign input, invalid stack size (possibly missing key)");
|
|
} else {
|
|
TxInErrorToJSON(txin, vErrors, ScriptErrorString(serror));
|
|
}
|
|
}
|
|
}
|
|
bool fComplete = vErrors.empty();
|
|
|
|
UniValue result(UniValue::VOBJ);
|
|
result.pushKV("hex", EncodeHexTx(CTransaction(mtx)));
|
|
result.pushKV("complete", fComplete);
|
|
if (!vErrors.empty()) {
|
|
result.pushKV("errors", vErrors);
|
|
}
|
|
|
|
return result;
|
|
}
|