aboutsummaryrefslogtreecommitdiff
path: root/src/wallet
diff options
context:
space:
mode:
authorMurch <murch@murch.one>2022-02-08 15:43:00 -0500
committerMurch <murch@murch.one>2022-03-25 11:16:46 -0400
commit902793c7772e5bdd5aae5b0d20a32c02a1a6dc7c (patch)
treed35eaa22242dd951bf8e9a19bb21cac9d86812a7 /src/wallet
parent6d2208a3f6849a3732af6ff010eeea629b9b10d0 (diff)
downloadbitcoin-902793c7772e5bdd5aae5b0d20a32c02a1a6dc7c.tar.xz
Extract FinishTransaction from send()
The final step of send either produces a PSBT or the final transaction. We extract these steps to a new helper function `FinishTransaction()` to reuse them in `sendall`.
Diffstat (limited to 'src/wallet')
-rw-r--r--src/wallet/rpc/spend.cpp94
1 files changed, 46 insertions, 48 deletions
diff --git a/src/wallet/rpc/spend.cpp b/src/wallet/rpc/spend.cpp
index d551593f06..cde41b42c9 100644
--- a/src/wallet/rpc/spend.cpp
+++ b/src/wallet/rpc/spend.cpp
@@ -74,6 +74,50 @@ static void InterpretFeeEstimationInstructions(const UniValue& conf_target, cons
}
}
+static UniValue FinishTransaction(const std::shared_ptr<CWallet> pwallet, const UniValue& options, const CMutableTransaction& rawTx)
+{
+
+ // Make a blank psbt
+ PartiallySignedTransaction psbtx(rawTx);
+
+ // First fill transaction with our data without signing,
+ // so external signers are not asked sign more than once.
+ bool complete;
+ pwallet->FillPSBT(psbtx, complete, SIGHASH_DEFAULT, false, true);
+ const TransactionError err{pwallet->FillPSBT(psbtx, complete, SIGHASH_DEFAULT, true, false)};
+ if (err != TransactionError::OK) {
+ throw JSONRPCTransactionError(err);
+ }
+
+ CMutableTransaction mtx;
+ complete = FinalizeAndExtractPSBT(psbtx, mtx);
+
+ UniValue result(UniValue::VOBJ);
+
+ const bool psbt_opt_in{options.exists("psbt") && options["psbt"].get_bool()};
+ bool add_to_wallet{options.exists("add_to_wallet") ? options["add_to_wallet"].get_bool() : true};
+ if (psbt_opt_in || !complete || !add_to_wallet) {
+ // Serialize the PSBT
+ CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
+ ssTx << psbtx;
+ result.pushKV("psbt", EncodeBase64(ssTx.str()));
+ }
+
+ if (complete) {
+ std::string hex{EncodeHexTx(CTransaction(mtx))};
+ CTransactionRef tx(MakeTransactionRef(std::move(mtx)));
+ result.pushKV("txid", tx->GetHash().GetHex());
+ if (add_to_wallet && !psbt_opt_in) {
+ pwallet->CommitTransaction(tx, {}, {} /* orderForm */);
+ } else {
+ result.pushKV("hex", hex);
+ }
+ }
+ result.pushKV("complete", complete);
+
+ return result;
+}
+
static void PreventOutdatedOptions(const UniValue& options)
{
if (options.exists("feeRate")) {
@@ -1174,14 +1218,10 @@ RPCHelpMan send()
InterpretFeeEstimationInstructions(/*conf_target=*/request.params[1], /*estimate_mode=*/request.params[2], /*fee_rate=*/request.params[3], options);
PreventOutdatedOptions(options);
- const bool psbt_opt_in = options.exists("psbt") && options["psbt"].get_bool();
CAmount fee;
int change_position;
- bool rbf = pwallet->m_signal_rbf;
- if (options.exists("replaceable")) {
- rbf = options["replaceable"].get_bool();
- }
+ bool rbf = options.exists("replaceable") ? options["replaceable"].get_bool() : pwallet->m_signal_rbf;
CMutableTransaction rawTx = ConstructTransaction(options["inputs"], request.params[0], options["locktime"], rbf);
CCoinControl coin_control;
// Automatically select coins, unless at least one is manually selected. Can
@@ -1190,49 +1230,7 @@ RPCHelpMan send()
SetOptionsInputWeights(options["inputs"], options);
FundTransaction(*pwallet, rawTx, fee, change_position, options, coin_control, /* override_min_fee */ false);
- bool add_to_wallet = true;
- if (options.exists("add_to_wallet")) {
- add_to_wallet = options["add_to_wallet"].get_bool();
- }
-
- // Make a blank psbt
- PartiallySignedTransaction psbtx(rawTx);
-
- // First fill transaction with our data without signing,
- // so external signers are not asked sign more than once.
- bool complete;
- pwallet->FillPSBT(psbtx, complete, SIGHASH_DEFAULT, false, true);
- const TransactionError err = pwallet->FillPSBT(psbtx, complete, SIGHASH_DEFAULT, true, false);
- if (err != TransactionError::OK) {
- throw JSONRPCTransactionError(err);
- }
-
- CMutableTransaction mtx;
- complete = FinalizeAndExtractPSBT(psbtx, mtx);
-
- UniValue result(UniValue::VOBJ);
-
- if (psbt_opt_in || !complete || !add_to_wallet) {
- // Serialize the PSBT
- CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
- ssTx << psbtx;
- result.pushKV("psbt", EncodeBase64(ssTx.str()));
- }
-
- if (complete) {
- std::string err_string;
- std::string hex = EncodeHexTx(CTransaction(mtx));
- CTransactionRef tx(MakeTransactionRef(std::move(mtx)));
- result.pushKV("txid", tx->GetHash().GetHex());
- if (add_to_wallet && !psbt_opt_in) {
- pwallet->CommitTransaction(tx, {}, {} /* orderForm */);
- } else {
- result.pushKV("hex", hex);
- }
- }
- result.pushKV("complete", complete);
-
- return result;
+ return FinishTransaction(pwallet, options, rawTx);
}
};
}