Skip to content

Commit 39034f1

Browse files
committed
Refactor rawtransaction_util's SignTransaction to have previous tx parsing be separate
1 parent 7d6f63c commit 39034f1

File tree

4 files changed

+24
-8
lines changed

4 files changed

+24
-8
lines changed

src/rpc/rawtransaction.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,10 @@ static UniValue signrawtransactionwithkey(const JSONRPCRequest& request)
758758
}
759759
FindCoins(coins);
760760

761-
return SignTransaction(mtx, request.params[2], &keystore, coins, true, request.params[3]);
761+
// Parse the prevtxs array
762+
ParsePrevouts(request.params[2], &keystore, coins);
763+
764+
return SignTransaction(mtx, &keystore, coins, request.params[3]);
762765
}
763766

764767
static UniValue sendrawtransaction(const JSONRPCRequest& request)

src/rpc/rawtransaction_util.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,8 @@ static void TxInErrorToJSON(const CTxIn& txin, UniValue& vErrorsRet, const std::
147147
vErrorsRet.push_back(entry);
148148
}
149149

150-
UniValue SignTransaction(CMutableTransaction& mtx, const UniValue& prevTxsUnival, FillableSigningProvider* keystore, std::map<COutPoint, Coin>& coins, bool is_temp_keystore, const UniValue& hashType)
150+
void ParsePrevouts(const UniValue& prevTxsUnival, FillableSigningProvider* keystore, std::map<COutPoint, Coin>& coins)
151151
{
152-
// Add previous txouts given in the RPC call:
153152
if (!prevTxsUnival.isNull()) {
154153
UniValue prevTxs = prevTxsUnival.get_array();
155154
for (unsigned int idx = 0; idx < prevTxs.size(); ++idx) {
@@ -197,7 +196,7 @@ UniValue SignTransaction(CMutableTransaction& mtx, const UniValue& prevTxsUnival
197196
}
198197

199198
// if redeemScript and private keys were given, add redeemScript to the keystore so it can be signed
200-
if (is_temp_keystore && (scriptPubKey.IsPayToScriptHash() || scriptPubKey.IsPayToWitnessScriptHash())) {
199+
if (keystore && (scriptPubKey.IsPayToScriptHash() || scriptPubKey.IsPayToWitnessScriptHash())) {
201200
RPCTypeCheckObj(prevOut,
202201
{
203202
{"redeemScript", UniValueType(UniValue::VSTR)},
@@ -226,7 +225,10 @@ UniValue SignTransaction(CMutableTransaction& mtx, const UniValue& prevTxsUnival
226225
}
227226
}
228227
}
228+
}
229229

230+
UniValue SignTransaction(CMutableTransaction& mtx, const SigningProvider* keystore, std::map<COutPoint, Coin>& coins, const UniValue& hashType)
231+
{
230232
int nHashType = ParseSighashString(hashType);
231233

232234
bool fHashSingle = ((nHashType & ~SIGHASH_ANYONECANPAY) == SIGHASH_SINGLE);

src/rpc/rawtransaction_util.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,27 @@ class UniValue;
1212
struct CMutableTransaction;
1313
class Coin;
1414
class COutPoint;
15+
class SigningProvider;
1516

1617
/**
1718
* Sign a transaction with the given keystore and previous transactions
1819
*
1920
* @param mtx The transaction to-be-signed
20-
* @param prevTxs Array of previous txns outputs that tx depends on but may not yet be in the block chain
2121
* @param keystore Temporary keystore containing signing keys
2222
* @param coins Map of unspent outputs - coins in mempool and current chain UTXO set, may be extended by previous txns outputs after call
23-
* @param tempKeystore Whether to use temporary keystore
2423
* @param hashType The signature hash type
2524
* @returns JSON object with details of signed transaction
2625
*/
27-
UniValue SignTransaction(CMutableTransaction& mtx, const UniValue& prevTxs, FillableSigningProvider* keystore, std::map<COutPoint, Coin>& coins, bool tempKeystore, const UniValue& hashType);
26+
UniValue SignTransaction(CMutableTransaction& mtx, const SigningProvider* keystore, std::map<COutPoint, Coin>& coins, const UniValue& hashType);
27+
28+
/**
29+
* Parse a prevtxs UniValue array and get the map of coins from it
30+
*
31+
* @param prevTxs Array of previous txns outputs that tx depends on but may not yet be in the block chain
32+
* @param keystore A pointer to the temprorary keystore if there is one
33+
* @param coins Map of unspent outputs - coins in mempool and current chain UTXO set, may be extended by previous txns outputs after call
34+
*/
35+
void ParsePrevouts(const UniValue& prevTxsUnival, FillableSigningProvider* keystore, std::map<COutPoint, Coin>& coins);
2836

2937
/** Create a transaction from univalue parameters */
3038
CMutableTransaction ConstructTransaction(const UniValue& inputs_in, const UniValue& outputs_in, const UniValue& locktime, bool rbf);

src/wallet/rpcwallet.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3269,7 +3269,10 @@ UniValue signrawtransactionwithwallet(const JSONRPCRequest& request)
32693269
}
32703270
pwallet->chain().findCoins(coins);
32713271

3272-
return SignTransaction(mtx, request.params[1], pwallet, coins, false, request.params[2]);
3272+
// Parse the prevtxs array
3273+
ParsePrevouts(request.params[1], nullptr, coins);
3274+
3275+
return SignTransaction(mtx, pwallet, coins, request.params[2]);
32733276
}
32743277

32753278
static UniValue bumpfee(const JSONRPCRequest& request)

0 commit comments

Comments
 (0)