Skip to content

Commit

Permalink
Implement watchonly support in fundrawtransaction
Browse files Browse the repository at this point in the history
- backports bitcoin/bitcoin@6bdb474

Some code and test cases stolen from Bryan Bishop <bryan@ledgerx.com>
(pull bitcoin#5524).

- backports bitcoin/bitcoin@d042854

[squash commit]
  • Loading branch information
random-zebra committed Jun 5, 2020
1 parent d7332a9 commit 4aad15b
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/coincontrol.h
Expand Up @@ -36,7 +36,7 @@ class CCoinControl
setSelected.clear();
useSwiftTX = false;
fAllowOtherInputs = false;
fAllowWatchOnly = true;
fAllowWatchOnly = false;
nMinimumTotalFee = 0;
fSplitBlock = false;
nSplitBlock = 1;
Expand Down
16 changes: 12 additions & 4 deletions src/rpc/rawtransaction.cpp
Expand Up @@ -554,15 +554,21 @@ static void TxInErrorToJSON(const CTxIn& txin, UniValue& vErrorsRet, const std::

UniValue fundrawtransaction(const UniValue& params, bool fHelp)
{
if (fHelp || params.size() != 1)
if (fHelp || params.size() < 1 || params.size() > 2)
throw std::runtime_error(
"fundrawtransaction \"hexstring\"\n"
"fundrawtransaction \"hexstring\" ( includeWatching )\n"
"\nAdd inputs to a transaction until it has enough in value to meet its out value.\n"
"This will not modify existing inputs, and will add one change output to the outputs.\n"
"Note that inputs which were signed may need to be resigned after completion since in/outputs have been added.\n"
"The inputs added will not be signed, use signrawtransaction for that.\n"
"Note that all existing inputs must have their previous output transaction be in the wallet.\n"
"Note that all inputs selected must be of standard form and P2SH scripts must be"
"in the wallet using importaddress or addmultisigaddress (to calculate fees).\n"
"Only pay-to-pubkey, multisig, and P2SH versions thereof are currently supported for watch-only\n"

"\nArguments:\n"
"1. \"hexstring\" (string, required) The hex string of the raw transaction\n"
"2. includeWatching (boolean, optional, default false) Also select inputs which are watch only\n"
"\nResult:\n"
"{\n"
" \"hex\": \"value\", (string) The resulting raw transaction (hex-encoded string)\n"
Expand All @@ -584,18 +590,20 @@ UniValue fundrawtransaction(const UniValue& params, bool fHelp)
if (!pwalletMain)
throw std::runtime_error("wallet not initialized");

RPCTypeCheck(params, boost::assign::list_of(UniValue::VSTR));
RPCTypeCheck(params, boost::assign::list_of(UniValue::VSTR)(UniValue::VBOOL));

// parse hex string from parameter
CTransaction origTx;
if (!DecodeHexTx(origTx, params[0].get_str()))
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "TX decode failed");

const bool includeWatching = params.size() > 1 && params[1].get_bool();

CMutableTransaction tx(origTx);
CAmount nFee;
std::string strFailReason;
int nChangePos = -1;
if(!pwalletMain->FundTransaction(tx, nFee, nChangePos, strFailReason))
if(!pwalletMain->FundTransaction(tx, nFee, nChangePos, strFailReason, includeWatching))
throw JSONRPCError(RPC_INTERNAL_ERROR, strFailReason);

UniValue result(UniValue::VOBJ);
Expand Down
4 changes: 3 additions & 1 deletion src/wallet/wallet.cpp
Expand Up @@ -2062,6 +2062,7 @@ bool CWallet::AvailableCoins(std::vector<COutput>* pCoins, // --> populates

bool fIsValid = (
((mine & ISMINE_SPENDABLE) != ISMINE_NO) ||
(coinControl && coinControl->fAllowWatchOnly && (mine & ISMINE_WATCH_SOLVABLE) != ISMINE_NO) ||
((mine & ((fIncludeColdStaking ? ISMINE_COLD : ISMINE_NO) |
(fIncludeDelegated ? ISMINE_SPENDABLE_DELEGATED : ISMINE_NO) )) != ISMINE_NO));

Expand Down Expand Up @@ -2359,7 +2360,7 @@ bool CWallet::GetBudgetFinalizationCollateralTX(CWalletTx& tx, uint256 hash, boo
return true;
}

bool CWallet::FundTransaction(CMutableTransaction& tx, CAmount &nFeeRet, int& nChangePosRet, std::string& strFailReason)
bool CWallet::FundTransaction(CMutableTransaction& tx, CAmount &nFeeRet, int& nChangePosRet, std::string& strFailReason, bool includeWatching)
{
std::vector<CRecipient> vecSend;

Expand All @@ -2371,6 +2372,7 @@ bool CWallet::FundTransaction(CMutableTransaction& tx, CAmount &nFeeRet, int& nC

CCoinControl coinControl;
coinControl.fAllowOtherInputs = true;
coinControl.fAllowWatchOnly = includeWatching;
for (const CTxIn& txin : tx.vin)
coinControl.Select(txin.prevout);

Expand Down
2 changes: 1 addition & 1 deletion src/wallet/wallet.h
Expand Up @@ -466,7 +466,7 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
CAmount GetUnconfirmedWatchOnlyBalance() const;
CAmount GetImmatureWatchOnlyBalance() const;
CAmount GetLockedWatchOnlyBalance() const;
bool FundTransaction(CMutableTransaction& tx, CAmount &nFeeRet, int& nChangePosRet, std::string& strFailReason);
bool FundTransaction(CMutableTransaction& tx, CAmount &nFeeRet, int& nChangePosRet, std::string& strFailReason, bool includeWatching);
bool CreateTransaction(const std::vector<CRecipient>& vecSend,
CWalletTx& wtxNew,
CReserveKey& reservekey,
Expand Down

0 comments on commit 4aad15b

Please sign in to comment.