Skip to content

Commit 99e88a3

Browse files
author
Antoine Riard
committed
rpc: Remove dependency on interfaces::Chain in SignTransaction
Comment SignTransaction utility
1 parent c536dfb commit 99e88a3

File tree

4 files changed

+34
-19
lines changed

4 files changed

+34
-19
lines changed

src/rpc/rawtransaction.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <key_io.h>
1616
#include <keystore.h>
1717
#include <merkleblock.h>
18+
#include <node/coin.h>
1819
#include <node/psbt.h>
1920
#include <node/transaction.h>
2021
#include <policy/policy.h>
@@ -770,7 +771,14 @@ static UniValue signrawtransactionwithkey(const JSONRPCRequest& request)
770771
keystore.AddKey(key);
771772
}
772773

773-
return SignTransaction(*g_rpc_interfaces->chain, mtx, request.params[2], &keystore, true, request.params[3]);
774+
// Fetch previous transactions (inputs):
775+
std::map<COutPoint, Coin> coins;
776+
for (const CTxIn& txin : mtx.vin) {
777+
coins[txin.prevout]; // Create empty map entry keyed by prevout.
778+
}
779+
FindCoins(coins);
780+
781+
return SignTransaction(mtx, request.params[2], &keystore, coins, true, request.params[3]);
774782
}
775783

776784
static UniValue sendrawtransaction(const JSONRPCRequest& request)

src/rpc/rawtransaction_util.cpp

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -149,18 +149,8 @@ static void TxInErrorToJSON(const CTxIn& txin, UniValue& vErrorsRet, const std::
149149
vErrorsRet.push_back(entry);
150150
}
151151

152-
// TODO(https://github.com/bitcoin/bitcoin/pull/10973#discussion_r267084237):
153-
// The dependency on interfaces::Chain should be removed, so
154-
// signrawtransactionwithkey doesn't need access to a Chain instance.
155-
UniValue SignTransaction(interfaces::Chain& chain, CMutableTransaction& mtx, const UniValue& prevTxsUnival, CBasicKeyStore *keystore, bool is_temp_keystore, const UniValue& hashType)
152+
UniValue SignTransaction(CMutableTransaction& mtx, const UniValue& prevTxsUnival, CBasicKeyStore* keystore, std::map<COutPoint, Coin>& coins, bool is_temp_keystore, const UniValue& hashType)
156153
{
157-
// Fetch previous transactions (inputs):
158-
std::map<COutPoint, Coin> coins;
159-
for (const CTxIn& txin : mtx.vin) {
160-
coins[txin.prevout]; // Create empty map entry keyed by prevout.
161-
}
162-
chain.findCoins(coins);
163-
164154
// Add previous txouts given in the RPC call:
165155
if (!prevTxsUnival.isNull()) {
166156
UniValue prevTxs = prevTxsUnival.get_array();

src/rpc/rawtransaction_util.h

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,26 @@
55
#ifndef BITCOIN_RPC_RAWTRANSACTION_UTIL_H
66
#define BITCOIN_RPC_RAWTRANSACTION_UTIL_H
77

8+
#include <map>
9+
810
class CBasicKeyStore;
911
class UniValue;
1012
struct CMutableTransaction;
13+
class Coin;
14+
class COutPoint;
1115

12-
namespace interfaces {
13-
class Chain;
14-
} // namespace interfaces
15-
16-
/** Sign a transaction with the given keystore and previous transactions */
17-
UniValue SignTransaction(interfaces::Chain& chain, CMutableTransaction& mtx, const UniValue& prevTxs, CBasicKeyStore *keystore, bool tempKeystore, const UniValue& hashType);
16+
/**
17+
* Sign a transaction with the given keystore and previous transactions
18+
*
19+
* @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
21+
* @param keystore Temporary keystore containing signing keys
22+
* @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
24+
* @param hashType The signature hash type
25+
* @returns JSON object with details of signed transaction
26+
*/
27+
UniValue SignTransaction(CMutableTransaction& mtx, const UniValue& prevTxs, CBasicKeyStore* keystore, std::map<COutPoint, Coin>& coins, bool tempKeystore, const UniValue& hashType);
1828

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

src/wallet/rpcwallet.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3150,7 +3150,14 @@ UniValue signrawtransactionwithwallet(const JSONRPCRequest& request)
31503150
LOCK(pwallet->cs_wallet);
31513151
EnsureWalletIsUnlocked(pwallet);
31523152

3153-
return SignTransaction(pwallet->chain(), mtx, request.params[1], pwallet, false, request.params[2]);
3153+
// Fetch previous transactions (inputs):
3154+
std::map<COutPoint, Coin> coins;
3155+
for (const CTxIn& txin : mtx.vin) {
3156+
coins[txin.prevout]; // Create empty map entry keyed by prevout.
3157+
}
3158+
pwallet->chain().findCoins(coins);
3159+
3160+
return SignTransaction(mtx, request.params[1], pwallet, coins, false, request.params[2]);
31543161
}
31553162

31563163
static UniValue bumpfee(const JSONRPCRequest& request)

0 commit comments

Comments
 (0)