Skip to content

Commit 91389e5

Browse files
committed
Merge pull request #6088
2085895 fundrawtransaction tests (Jonas Schnelli) 21bbd92 Add fundrawtransaction RPC method (Matt Corallo) 1e0d1a2 Add FundTransaction method to wallet (Matt Corallo) 2d84e22 Small tweaks to CCoinControl for fundrawtransaction (Matt Corallo) 9b4e7d9 Add DummySignatureCreator which just creates zeroed sigs (Pieter Wuille)
2 parents b77fbe0 + 2085895 commit 91389e5

File tree

12 files changed

+787
-14
lines changed

12 files changed

+787
-14
lines changed

qa/pull-tester/rpc-tests.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ testScripts=(
3030
'zapwallettxes.py'
3131
'proxy_test.py'
3232
'merkle_blocks.py'
33+
'fundrawtransaction.py'
3334
'signrawtransactions.py'
3435
'walletbackup.py'
3536
'nodehandling.py'

qa/rpc-tests/fundrawtransaction.py

Lines changed: 556 additions & 0 deletions
Large diffs are not rendered by default.

src/coincontrol.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ class CCoinControl
1212
{
1313
public:
1414
CTxDestination destChange;
15+
//! If false, allows unselected inputs, but requires all selected inputs be used
16+
bool fAllowOtherInputs;
1517

1618
CCoinControl()
1719
{
@@ -21,6 +23,7 @@ class CCoinControl
2123
void SetNull()
2224
{
2325
destChange = CNoDestination();
26+
fAllowOtherInputs = false;
2427
setSelected.clear();
2528
}
2629

@@ -50,7 +53,7 @@ class CCoinControl
5053
setSelected.clear();
5154
}
5255

53-
void ListSelected(std::vector<COutPoint>& vOutpoints)
56+
void ListSelected(std::vector<COutPoint>& vOutpoints) const
5457
{
5558
vOutpoints.assign(setSelected.begin(), setSelected.end());
5659
}

src/rpcclient.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ static const CRPCConvertParam vRPCConvertParams[] =
7878
{ "signrawtransaction", 1 },
7979
{ "signrawtransaction", 2 },
8080
{ "sendrawtransaction", 1 },
81+
{ "fundrawtransaction", 1 },
8182
{ "gettxout", 1 },
8283
{ "gettxout", 2 },
8384
{ "gettxoutproof", 0 },

src/rpcserver.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,9 @@ static const CRPCCommand vRPCCommands[] =
320320
{ "rawtransactions", "getrawtransaction", &getrawtransaction, true },
321321
{ "rawtransactions", "sendrawtransaction", &sendrawtransaction, false },
322322
{ "rawtransactions", "signrawtransaction", &signrawtransaction, false }, /* uses wallet if enabled */
323+
#ifdef ENABLE_WALLET
324+
{ "rawtransactions", "fundrawtransaction", &fundrawtransaction, false },
325+
#endif
323326

324327
/* Utility functions */
325328
{ "util", "createmultisig", &createmultisig, true },

src/rpcserver.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ extern UniValue listlockunspent(const UniValue& params, bool fHelp);
221221
extern UniValue createrawtransaction(const UniValue& params, bool fHelp);
222222
extern UniValue decoderawtransaction(const UniValue& params, bool fHelp);
223223
extern UniValue decodescript(const UniValue& params, bool fHelp);
224+
extern UniValue fundrawtransaction(const UniValue& params, bool fHelp);
224225
extern UniValue signrawtransaction(const UniValue& params, bool fHelp);
225226
extern UniValue sendrawtransaction(const UniValue& params, bool fHelp);
226227
extern UniValue gettxoutproof(const UniValue& params, bool fHelp);

src/script/sign.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,3 +275,39 @@ CScript CombineSignatures(const CScript& scriptPubKey, const BaseSignatureChecke
275275

276276
return CombineSignatures(scriptPubKey, checker, txType, vSolutions, stack1, stack2);
277277
}
278+
279+
namespace {
280+
/** Dummy signature checker which accepts all signatures. */
281+
class DummySignatureChecker : public BaseSignatureChecker
282+
{
283+
public:
284+
DummySignatureChecker() {}
285+
286+
bool CheckSig(const std::vector<unsigned char>& scriptSig, const std::vector<unsigned char>& vchPubKey, const CScript& scriptCode) const
287+
{
288+
return true;
289+
}
290+
};
291+
const DummySignatureChecker dummyChecker;
292+
}
293+
294+
const BaseSignatureChecker& DummySignatureCreator::Checker() const
295+
{
296+
return dummyChecker;
297+
}
298+
299+
bool DummySignatureCreator::CreateSig(std::vector<unsigned char>& vchSig, const CKeyID& keyid, const CScript& scriptCode) const
300+
{
301+
// Create a dummy signature that is a valid DER-encoding
302+
vchSig.assign(72, '\000');
303+
vchSig[0] = 0x30;
304+
vchSig[1] = 69;
305+
vchSig[2] = 0x02;
306+
vchSig[3] = 33;
307+
vchSig[4] = 0x01;
308+
vchSig[4 + 33] = 0x02;
309+
vchSig[5 + 33] = 32;
310+
vchSig[6 + 33] = 0x01;
311+
vchSig[6 + 33 + 32] = SIGHASH_ALL;
312+
return true;
313+
}

src/script/sign.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ class TransactionSignatureCreator : public BaseSignatureCreator {
4343
bool CreateSig(std::vector<unsigned char>& vchSig, const CKeyID& keyid, const CScript& scriptCode) const;
4444
};
4545

46+
/** A signature creator that just produces 72-byte empty signatyres. */
47+
class DummySignatureCreator : public BaseSignatureCreator {
48+
public:
49+
DummySignatureCreator(const CKeyStore* keystoreIn) : BaseSignatureCreator(keystoreIn) {}
50+
const BaseSignatureChecker& Checker() const;
51+
bool CreateSig(std::vector<unsigned char>& vchSig, const CKeyID& keyid, const CScript& scriptCode) const;
52+
};
53+
4654
/** Produce a script signature using a generic signature creator. */
4755
bool ProduceSignature(const BaseSignatureCreator& creator, const CScript& scriptPubKey, CScript& scriptSig);
4856

src/test/rpc_wallet_tests.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,12 @@ BOOST_AUTO_TEST_CASE(rpc_wallet)
217217
UniValue arr = retValue.get_array();
218218
BOOST_CHECK(arr.size() > 0);
219219
BOOST_CHECK(CBitcoinAddress(arr[0].get_str()).Get() == demoAddress.Get());
220+
221+
/*********************************
222+
* fundrawtransaction
223+
*********************************/
224+
BOOST_CHECK_THROW(CallRPC("fundrawtransaction 28z"), runtime_error);
225+
BOOST_CHECK_THROW(CallRPC("fundrawtransaction 01000000000180969800000000001976a91450ce0a4b0ee0ddeb633da85199728b940ac3fe9488ac00000000"), runtime_error);
220226
}
221227

222228
BOOST_AUTO_TEST_SUITE_END()

src/wallet/rpcwallet.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2361,3 +2361,57 @@ UniValue listunspent(const UniValue& params, bool fHelp)
23612361

23622362
return results;
23632363
}
2364+
2365+
UniValue fundrawtransaction(const UniValue& params, bool fHelp)
2366+
{
2367+
if (!EnsureWalletIsAvailable(fHelp))
2368+
return NullUniValue;
2369+
2370+
if (fHelp || params.size() != 1)
2371+
throw runtime_error(
2372+
"fundrawtransaction \"hexstring\"\n"
2373+
"\nAdd inputs to a transaction until it has enough in value to meet its out value.\n"
2374+
"This will not modify existing inputs, and will add one change output to the outputs.\n"
2375+
"Note that inputs which were signed may need to be resigned after completion since in/outputs have been added.\n"
2376+
"The inputs added will not be signed, use signrawtransaction for that.\n"
2377+
"\nArguments:\n"
2378+
"1. \"hexstring\" (string, required) The hex string of the raw transaction\n"
2379+
"\nResult:\n"
2380+
"{\n"
2381+
" \"hex\": \"value\", (string) The resulting raw transaction (hex-encoded string)\n"
2382+
" \"fee\": n, (numeric) The fee added to the transaction\n"
2383+
" \"changepos\": n (numeric) The position of the added change output, or -1\n"
2384+
"}\n"
2385+
"\"hex\" \n"
2386+
"\nExamples:\n"
2387+
"\nCreate a transaction with no inputs\n"
2388+
+ HelpExampleCli("createrawtransaction", "\"[]\" \"{\\\"myaddress\\\":0.01}\"") +
2389+
"\nAdd sufficient unsigned inputs to meet the output value\n"
2390+
+ HelpExampleCli("fundrawtransaction", "\"rawtransactionhex\"") +
2391+
"\nSign the transaction\n"
2392+
+ HelpExampleCli("signrawtransaction", "\"fundedtransactionhex\"") +
2393+
"\nSend the transaction\n"
2394+
+ HelpExampleCli("sendrawtransaction", "\"signedtransactionhex\"")
2395+
);
2396+
2397+
RPCTypeCheck(params, boost::assign::list_of(UniValue::VSTR));
2398+
2399+
// parse hex string from parameter
2400+
CTransaction origTx;
2401+
if (!DecodeHexTx(origTx, params[0].get_str()))
2402+
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "TX decode failed");
2403+
2404+
CMutableTransaction tx(origTx);
2405+
CAmount nFee;
2406+
string strFailReason;
2407+
int nChangePos = -1;
2408+
if(!pwalletMain->FundTransaction(tx, nFee, nChangePos, strFailReason))
2409+
throw JSONRPCError(RPC_INTERNAL_ERROR, strFailReason);
2410+
2411+
UniValue result(UniValue::VOBJ);
2412+
result.push_back(Pair("hex", EncodeHexTx(tx)));
2413+
result.push_back(Pair("changepos", nChangePos));
2414+
result.push_back(Pair("fee", ValueFromAmount(nFee)));
2415+
2416+
return result;
2417+
}

0 commit comments

Comments
 (0)