From e3d484b603abff69c6ebfca5cfb78cf82743d090 Mon Sep 17 00:00:00 2001 From: Matthew Zipkin Date: Tue, 5 Sep 2023 08:45:07 -0400 Subject: [PATCH] wallet rpc: return final tx hex from walletprocesspsbt if complete --- src/wallet/rpc/spend.cpp | 9 +++++++++ test/functional/rpc_psbt.py | 21 +++++++++++++++------ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/wallet/rpc/spend.cpp b/src/wallet/rpc/spend.cpp index 0c2be26ddf..c4206e9897 100644 --- a/src/wallet/rpc/spend.cpp +++ b/src/wallet/rpc/spend.cpp @@ -1566,6 +1566,7 @@ RPCHelpMan walletprocesspsbt() { {RPCResult::Type::STR, "psbt", "The base64-encoded partially signed transaction"}, {RPCResult::Type::BOOL, "complete", "If the transaction has a complete set of signatures"}, + {RPCResult::Type::STR_HEX, "hex", /*optional=*/true, "The hex-encoded network transaction if complete"}, } }, RPCExamples{ @@ -1609,6 +1610,14 @@ RPCHelpMan walletprocesspsbt() ssTx << psbtx; result.pushKV("psbt", EncodeBase64(ssTx.str())); result.pushKV("complete", complete); + if (complete) { + CMutableTransaction mtx; + // Returns true if complete, which we already think it is. + CHECK_NONFATAL(FinalizeAndExtractPSBT(psbtx, mtx)); + CDataStream ssTx_final(SER_NETWORK, PROTOCOL_VERSION); + ssTx_final << mtx; + result.pushKV("hex", HexStr(ssTx_final)); + } return result; }, diff --git a/test/functional/rpc_psbt.py b/test/functional/rpc_psbt.py index b574a370d6..9affca43e3 100755 --- a/test/functional/rpc_psbt.py +++ b/test/functional/rpc_psbt.py @@ -217,12 +217,21 @@ class PSBTTest(BitcoinTestFramework): self.nodes[0].walletpassphrase(passphrase="password", timeout=1000000) - # Sign the transaction and send - signed_tx = self.nodes[0].walletprocesspsbt(psbt=psbtx, finalize=False)['psbt'] - finalized_tx = self.nodes[0].walletprocesspsbt(psbt=psbtx, finalize=True)['psbt'] - assert signed_tx != finalized_tx - final_tx = self.nodes[0].finalizepsbt(signed_tx)['hex'] - self.nodes[0].sendrawtransaction(final_tx) + # Sign the transaction but don't finalize + processed_psbt = self.nodes[0].walletprocesspsbt(psbt=psbtx, finalize=False) + assert "hex" not in processed_psbt + signed_psbt = processed_psbt['psbt'] + + # Finalize and send + finalized_hex = self.nodes[0].finalizepsbt(signed_psbt)['hex'] + self.nodes[0].sendrawtransaction(finalized_hex) + + # Alternative method: sign AND finalize in one command + processed_finalized_psbt = self.nodes[0].walletprocesspsbt(psbt=psbtx, finalize=True) + finalized_psbt = processed_finalized_psbt['psbt'] + finalized_psbt_hex = processed_finalized_psbt['hex'] + assert signed_psbt != finalized_psbt + assert finalized_psbt_hex == finalized_hex # Manually selected inputs can be locked: assert_equal(len(self.nodes[0].listlockunspent()), 0)