Skip to content

Commit a9d0cec

Browse files
author
MarcoFalke
committed
Merge bitcoin/bitcoin#23106: Ensure wallet is unlocked before signing PSBT with walletprocesspsbt and GUI
7e3ee4c GUI: Ask user to unlock wallet before signing psbt (Samuel Dobson) 0f3acec Add test that walletprocesspsbt requires unlocked wallet when signing (Samuel Dobson) 0e89521 Ensure wallet is unlocked before signing in walletprocesspsbt (Samuel Dobson) Pull request description: If signing a PSBT, we need to ensure the wallet is unlocked. Fixes #22874, fixes #312 ACKs for top commit: achow101: ACK 7e3ee4c lsilva01: Code Review ACK bitcoin/bitcoin@7e3ee4c benthecarman: ACK 7e3ee4c Tree-SHA512: 6726a873582747900ab454ea21153a92be86808a4c1517dc2856b389876a2da9e8df1ffa9b567b6bd017038342c3544ecf5ca3c97744e7debe0a5ee65563687d
2 parents a8bbd4c + 7e3ee4c commit a9d0cec

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

src/qt/psbtoperationsdialog.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ void PSBTOperationsDialog::signTransaction()
7171
{
7272
bool complete;
7373
size_t n_signed;
74+
75+
WalletModel::UnlockContext ctx(m_wallet_model->requestUnlock());
76+
7477
TransactionError err = m_wallet_model->wallet().fillPSBT(SIGHASH_ALL, true /* sign */, true /* bip32derivs */, &n_signed, m_transaction_data, complete);
7578

7679
if (err != TransactionError::OK) {
@@ -81,7 +84,9 @@ void PSBTOperationsDialog::signTransaction()
8184

8285
updateTransactionDisplay();
8386

84-
if (!complete && n_signed < 1) {
87+
if (!complete && !ctx.isValid()) {
88+
showStatus(tr("Cannot sign inputs while wallet is locked."), StatusLevel::WARN);
89+
} else if (!complete && n_signed < 1) {
8590
showStatus(tr("Could not sign any more inputs."), StatusLevel::WARN);
8691
} else if (!complete) {
8792
showStatus(tr("Signed %1 inputs, but more signatures are still required.").arg(n_signed),

src/wallet/rpcwallet.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4420,7 +4420,7 @@ static RPCHelpMan walletprocesspsbt()
44204420
HELP_REQUIRING_PASSPHRASE,
44214421
{
44224422
{"psbt", RPCArg::Type::STR, RPCArg::Optional::NO, "The transaction base64 string"},
4423-
{"sign", RPCArg::Type::BOOL, RPCArg::Default{true}, "Also sign the transaction when updating"},
4423+
{"sign", RPCArg::Type::BOOL, RPCArg::Default{true}, "Also sign the transaction when updating (requires wallet to be unlocked)"},
44244424
{"sighashtype", RPCArg::Type::STR, RPCArg::Default{"DEFAULT"}, "The signature hash type to sign with if not specified by the PSBT. Must be one of\n"
44254425
" \"DEFAULT\"\n"
44264426
" \"ALL\"\n"
@@ -4467,6 +4467,9 @@ static RPCHelpMan walletprocesspsbt()
44674467
bool sign = request.params[1].isNull() ? true : request.params[1].get_bool();
44684468
bool bip32derivs = request.params[3].isNull() ? true : request.params[3].get_bool();
44694469
bool complete = true;
4470+
4471+
if (sign) EnsureWalletIsUnlocked(*pwallet);
4472+
44704473
const TransactionError err{wallet.FillPSBT(psbtx, complete, nHashType, sign, bip32derivs)};
44714474
if (err != TransactionError::OK) {
44724475
throw JSONRPCTransactionError(err);

test/functional/rpc_psbt.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,16 @@ def run_test(self):
108108
psbtx = self.nodes[1].walletprocesspsbt(psbtx1)['psbt']
109109
assert_equal(psbtx1, psbtx)
110110

111+
# Node 0 should not be able to sign the transaction with the wallet is locked
112+
self.nodes[0].encryptwallet("password")
113+
assert_raises_rpc_error(-13, "Please enter the wallet passphrase with walletpassphrase first", self.nodes[0].walletprocesspsbt, psbtx)
114+
115+
# Node 0 should be able to process without signing though
116+
unsigned_tx = self.nodes[0].walletprocesspsbt(psbtx, False)
117+
assert_equal(unsigned_tx['complete'], False)
118+
119+
self.nodes[0].walletpassphrase(passphrase="password", timeout=1000000)
120+
111121
# Sign the transaction and send
112122
signed_tx = self.nodes[0].walletprocesspsbt(psbtx)['psbt']
113123
final_tx = self.nodes[0].finalizepsbt(signed_tx)['hex']

0 commit comments

Comments
 (0)