Skip to content

Commit 46494b0

Browse files
committed
Merge #16798: Refactor rawtransaction_util's SignTransaction to separate prevtx parsing
39034f1 Refactor rawtransaction_util's SignTransaction to have previous tx parsing be separate (Andrew Chow) Pull request description: Currently the `SignTransaction` function has to handle both the actual signing and parsing of previous transaction data. This PR splits it so that `SignTransaction` only handles the signing itself and adds a `ParsePrevouts` function which handles parsing the prevtx information. This allows for `SignTransaction` to just take any `SigningProvider`. Split from #16341 ACKs for top commit: MarcoFalke: ACK 39034f1 instagibbs: utACK bitcoin/bitcoin@39034f1 ryanofsky: utACK 39034f1. No change since previously reviewed b49bbb939be92a67ff77c3f7bca5bb94dd141906, bitcoin/bitcoin#16341 (review) other than rebase with no conflicts. Tree-SHA512: 09f7733e90691766bfb5cf0f20e913dbf270bd3b51abdcad966b24d110e562ed85fd3d0d1d7bbea61f903340060052ec73c4817b09aee0dc1f3916d781a9e40c
2 parents ae3e3bd + 39034f1 commit 46494b0

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
@@ -3280,7 +3280,10 @@ UniValue signrawtransactionwithwallet(const JSONRPCRequest& request)
32803280
}
32813281
pwallet->chain().findCoins(coins);
32823282

3283-
return SignTransaction(mtx, request.params[1], pwallet, coins, false, request.params[2]);
3283+
// Parse the prevtxs array
3284+
ParsePrevouts(request.params[1], nullptr, coins);
3285+
3286+
return SignTransaction(mtx, pwallet, coins, request.params[2]);
32843287
}
32853288

32863289
static UniValue bumpfee(const JSONRPCRequest& request)

0 commit comments

Comments
 (0)