Skip to content

Commit 45f1519

Browse files
committed
Merge #16373: bumpfee: Return PSBT when wallet has privkeys disabled
091a876 Test watchonly wallet bumpfee with PSBT return (Gregory Sanders) e9b4f94 bumpfee: Return PSBT when wallet has privkeys disabled (Gregory Sanders) 75a5e47 Change bumpfee to use watch-only funds for legacy watchonly wallets (Gregory Sanders) Pull request description: The main use-case here is for using with watch-only wallets with PSBT-signing cold wallets of all kinds. ACKs for top commit: achow101: ACK 091a876 Sjors: Tested ACK 091a876 meshcollider: utACK 091a876 Tree-SHA512: f7cf663e1af0b029e5c99eac88c5fdc3bc9e9a3841da8a608e8a9957e9bcf6a78864b8c2706fcaf78a480ffe11badd80c4fad29f97c0bb929e0470fafda5c22e
2 parents bcb4cdc + 091a876 commit 45f1519

File tree

3 files changed

+123
-19
lines changed

3 files changed

+123
-19
lines changed

src/wallet/feebumper.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ static feebumper::Result PreconditionChecks(const CWallet& wallet, const CWallet
4747

4848
// check that original tx consists entirely of our inputs
4949
// if not, we can't bump the fee, because the wallet has no way of knowing the value of the other inputs (thus the fee)
50-
if (!wallet.IsAllFromMe(*wtx.tx, ISMINE_SPENDABLE)) {
50+
isminefilter filter = wallet.GetLegacyScriptPubKeyMan() && wallet.IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS) ? ISMINE_WATCH_ONLY : ISMINE_SPENDABLE;
51+
if (!wallet.IsAllFromMe(*wtx.tx, filter)) {
5152
errors.push_back("Transaction contains inputs that don't belong to this wallet");
5253
return feebumper::Result::WALLET_ERROR;
5354
}
@@ -78,7 +79,8 @@ static feebumper::Result CheckFeeRate(const CWallet& wallet, const CWalletTx& wt
7879
CFeeRate incrementalRelayFee = std::max(wallet.chain().relayIncrementalFee(), CFeeRate(WALLET_INCREMENTAL_RELAY_FEE));
7980

8081
// Given old total fee and transaction size, calculate the old feeRate
81-
CAmount old_fee = wtx.GetDebit(ISMINE_SPENDABLE) - wtx.tx->GetValueOut();
82+
isminefilter filter = wallet.GetLegacyScriptPubKeyMan() && wallet.IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS) ? ISMINE_WATCH_ONLY : ISMINE_SPENDABLE;
83+
CAmount old_fee = wtx.GetDebit(filter) - wtx.tx->GetValueOut();
8284
const int64_t txSize = GetVirtualTransactionSize(*(wtx.tx));
8385
CFeeRate nOldFeeRate(old_fee, txSize);
8486
// Min total fee is old fee + relay fee
@@ -195,7 +197,8 @@ Result CreateTotalBumpTransaction(const CWallet* wallet, const uint256& txid, co
195197
}
196198

197199
// calculate the old fee and fee-rate
198-
old_fee = wtx.GetDebit(ISMINE_SPENDABLE) - wtx.tx->GetValueOut();
200+
isminefilter filter = wallet->GetLegacyScriptPubKeyMan() && wallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS) ? ISMINE_WATCH_ONLY : ISMINE_SPENDABLE;
201+
old_fee = wtx.GetDebit(filter) - wtx.tx->GetValueOut();
199202
CFeeRate nOldFeeRate(old_fee, txSize);
200203
// The wallet uses a conservative WALLET_INCREMENTAL_RELAY_FEE value to
201204
// future proof against changes to network wide policy for incremental relay
@@ -308,7 +311,8 @@ Result CreateRateBumpTransaction(CWallet& wallet, const uint256& txid, const CCo
308311
}
309312
}
310313

311-
old_fee = wtx.GetDebit(ISMINE_SPENDABLE) - wtx.tx->GetValueOut();
314+
isminefilter filter = wallet.GetLegacyScriptPubKeyMan() && wallet.IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS) ? ISMINE_WATCH_ONLY : ISMINE_SPENDABLE;
315+
old_fee = wtx.GetDebit(filter) - wtx.tx->GetValueOut();
312316

313317
if (coin_control.m_feerate) {
314318
// The user provided a feeRate argument.

src/wallet/rpcwallet.cpp

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3365,10 +3365,11 @@ static UniValue bumpfee(const JSONRPCRequest& request)
33653365
},
33663366
RPCResult{
33673367
"{\n"
3368-
" \"txid\": \"value\", (string) The id of the new transaction\n"
3369-
" \"origfee\": n, (numeric) Fee of the replaced transaction\n"
3370-
" \"fee\": n, (numeric) Fee of the new transaction\n"
3371-
" \"errors\": [ str... ] (json array of strings) Errors encountered during processing (may be empty)\n"
3368+
" \"psbt\": \"psbt\", (string) The base64-encoded unsigned PSBT of the new transaction. Only returned when wallet private keys are disabled.\n"
3369+
" \"txid\": \"value\", (string) The id of the new transaction. Only returned when wallet private keys are enabled.\n"
3370+
" \"origfee\": n, (numeric) The fee of the replaced transaction.\n"
3371+
" \"fee\": n, (numeric) The fee of the new transaction.\n"
3372+
" \"errors\": [ str... ] (json array of strings) Errors encountered during processing (may be empty).\n"
33723373
"}\n"
33733374
},
33743375
RPCExamples{
@@ -3380,10 +3381,12 @@ static UniValue bumpfee(const JSONRPCRequest& request)
33803381
RPCTypeCheck(request.params, {UniValue::VSTR, UniValue::VOBJ});
33813382
uint256 hash(ParseHashV(request.params[0], "txid"));
33823383

3384+
CCoinControl coin_control;
3385+
coin_control.fAllowWatchOnly = pwallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS);
33833386
// optional parameters
33843387
CAmount totalFee = 0;
3385-
CCoinControl coin_control;
33863388
coin_control.m_signal_bip125_rbf = true;
3389+
33873390
if (!request.params[1].isNull()) {
33883391
UniValue options = request.params[1];
33893392
RPCTypeCheckObj(options,
@@ -3468,17 +3471,32 @@ static UniValue bumpfee(const JSONRPCRequest& request)
34683471
}
34693472
}
34703473

3471-
// sign bumped transaction
3472-
if (!feebumper::SignTransaction(*pwallet, mtx)) {
3473-
throw JSONRPCError(RPC_WALLET_ERROR, "Can't sign transaction.");
3474-
}
3475-
// commit the bumped transaction
3476-
uint256 txid;
3477-
if (feebumper::CommitTransaction(*pwallet, hash, std::move(mtx), errors, txid) != feebumper::Result::OK) {
3478-
throw JSONRPCError(RPC_WALLET_ERROR, errors[0]);
3479-
}
34803474
UniValue result(UniValue::VOBJ);
3481-
result.pushKV("txid", txid.GetHex());
3475+
3476+
// If wallet private keys are enabled, return the new transaction id,
3477+
// otherwise return the base64-encoded unsigned PSBT of the new transaction.
3478+
if (!pwallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
3479+
if (!feebumper::SignTransaction(*pwallet, mtx)) {
3480+
throw JSONRPCError(RPC_WALLET_ERROR, "Can't sign transaction.");
3481+
}
3482+
3483+
uint256 txid;
3484+
if (feebumper::CommitTransaction(*pwallet, hash, std::move(mtx), errors, txid) != feebumper::Result::OK) {
3485+
throw JSONRPCError(RPC_WALLET_ERROR, errors[0]);
3486+
}
3487+
3488+
result.pushKV("txid", txid.GetHex());
3489+
} else {
3490+
PartiallySignedTransaction psbtx(mtx);
3491+
bool complete = false;
3492+
const TransactionError err = FillPSBT(pwallet, psbtx, complete, SIGHASH_ALL, false /* sign */, true /* bip32derivs */);
3493+
CHECK_NONFATAL(err == TransactionError::OK);
3494+
CHECK_NONFATAL(!complete);
3495+
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
3496+
ssTx << psbtx;
3497+
result.pushKV("psbt", EncodeBase64(ssTx.str()));
3498+
}
3499+
34823500
result.pushKV("origfee", ValueFromAmount(old_fee));
34833501
result.pushKV("fee", ValueFromAmount(new_fee));
34843502
UniValue result_errors(UniValue::VARR);

test/functional/wallet_bumpfee.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ def run_test(self):
7878
test_small_output_fails(rbf_node, dest_address)
7979
test_dust_to_fee(rbf_node, dest_address)
8080
test_settxfee(rbf_node, dest_address)
81+
test_watchonly_psbt(self, peer_node, rbf_node, dest_address)
8182
test_rebumping(rbf_node, dest_address)
8283
test_rebumping_not_replaceable(rbf_node, dest_address)
8384
test_unconfirmed_not_spendable(rbf_node, rbf_node_address)
@@ -103,6 +104,7 @@ def test_simple_bumpfee_succeeds(self, mode, rbf_node, peer_node, dest_address):
103104
assert_equal(bumped_tx["errors"], [])
104105
assert bumped_tx["fee"] > -rbftx["fee"]
105106
assert_equal(bumped_tx["origfee"], -rbftx["fee"])
107+
assert "psbt" not in bumped_tx
106108
# check that bumped_tx propagates, original tx was evicted and has a wallet conflict
107109
self.sync_mempools((rbf_node, peer_node))
108110
assert bumped_tx["txid"] in rbf_node.getrawmempool()
@@ -280,6 +282,86 @@ def test_maxtxfee_fails(test, rbf_node, dest_address):
280282
test.restart_node(1, test.extra_args[1])
281283
rbf_node.walletpassphrase(WALLET_PASSPHRASE, WALLET_PASSPHRASE_TIMEOUT)
282284

285+
def test_watchonly_psbt(test, peer_node, rbf_node, dest_address):
286+
priv_rec_desc = "wpkh([00000001/84'/1'/0']tprv8ZgxMBicQKsPd7Uf69XL1XwhmjHopUGep8GuEiJDZmbQz6o58LninorQAfcKZWARbtRtfnLcJ5MQ2AtHcQJCCRUcMRvmDUjyEmNUWwx8UbK/0/*)#rweraev0"
287+
pub_rec_desc = rbf_node.getdescriptorinfo(priv_rec_desc)["descriptor"]
288+
priv_change_desc = "wpkh([00000001/84'/1'/0']tprv8ZgxMBicQKsPd7Uf69XL1XwhmjHopUGep8GuEiJDZmbQz6o58LninorQAfcKZWARbtRtfnLcJ5MQ2AtHcQJCCRUcMRvmDUjyEmNUWwx8UbK/1/*)#j6uzqvuh"
289+
pub_change_desc = rbf_node.getdescriptorinfo(priv_change_desc)["descriptor"]
290+
# Create a wallet with private keys that can sign PSBTs
291+
rbf_node.createwallet(wallet_name="signer", disable_private_keys=False, blank=True)
292+
signer = rbf_node.get_wallet_rpc("signer")
293+
assert signer.getwalletinfo()['private_keys_enabled']
294+
result = signer.importmulti([{
295+
"desc": priv_rec_desc,
296+
"timestamp": 0,
297+
"range": [0,1],
298+
"internal": False,
299+
"keypool": False # Keys can only be imported to the keypool when private keys are disabled
300+
},
301+
{
302+
"desc": priv_change_desc,
303+
"timestamp": 0,
304+
"range": [0, 0],
305+
"internal": True,
306+
"keypool": False
307+
}])
308+
assert_equal(result, [{'success': True}, {'success': True}])
309+
310+
# Create another wallet with just the public keys, which creates PSBTs
311+
rbf_node.createwallet(wallet_name="watcher", disable_private_keys=True, blank=True)
312+
watcher = rbf_node.get_wallet_rpc("watcher")
313+
assert not watcher.getwalletinfo()['private_keys_enabled']
314+
315+
result = watcher.importmulti([{
316+
"desc": pub_rec_desc,
317+
"timestamp": 0,
318+
"range": [0,10],
319+
"internal": False,
320+
"keypool": True,
321+
"watchonly": True
322+
},
323+
{
324+
"desc": pub_change_desc,
325+
"timestamp": 0,
326+
"range": [0, 10],
327+
"internal": True,
328+
"keypool": True,
329+
"watchonly": True
330+
}])
331+
assert_equal(result, [{'success': True}, {'success': True}])
332+
333+
funding_address1 = watcher.getnewaddress(address_type='bech32')
334+
funding_address2 = watcher.getnewaddress(address_type='bech32')
335+
peer_node.sendmany("", {funding_address1: 0.001, funding_address2: 0.001})
336+
peer_node.generate(1)
337+
test.sync_all()
338+
339+
# Create single-input PSBT for transaction to be bumped
340+
psbt = watcher.walletcreatefundedpsbt([], {dest_address:0.0005}, 0, {"feeRate": 0.00001}, True)['psbt']
341+
psbt_signed = signer.walletprocesspsbt(psbt=psbt, sign=True, sighashtype="ALL", bip32derivs=True)
342+
psbt_final = watcher.finalizepsbt(psbt_signed["psbt"])
343+
original_txid = watcher.sendrawtransaction(psbt_final["hex"])
344+
assert_equal(len(watcher.decodepsbt(psbt)["tx"]["vin"]), 1)
345+
346+
# Bump fee, obnoxiously high to add additional watchonly input
347+
bumped_psbt = watcher.bumpfee(original_txid, {"fee_rate":0.005})
348+
assert_greater_than(len(watcher.decodepsbt(bumped_psbt['psbt'])["tx"]["vin"]), 1)
349+
assert "txid" not in bumped_psbt
350+
assert_equal(bumped_psbt["origfee"], -watcher.gettransaction(original_txid)["fee"])
351+
assert not watcher.finalizepsbt(bumped_psbt["psbt"])["complete"]
352+
353+
# Sign bumped transaction
354+
bumped_psbt_signed = signer.walletprocesspsbt(psbt=bumped_psbt["psbt"], sign=True, sighashtype="ALL", bip32derivs=True)
355+
bumped_psbt_final = watcher.finalizepsbt(bumped_psbt_signed["psbt"])
356+
assert bumped_psbt_final["complete"]
357+
358+
# Broadcast bumped transaction
359+
bumped_txid = watcher.sendrawtransaction(bumped_psbt_final["hex"])
360+
assert bumped_txid in rbf_node.getrawmempool()
361+
assert original_txid not in rbf_node.getrawmempool()
362+
363+
rbf_node.unloadwallet("watcher")
364+
rbf_node.unloadwallet("signer")
283365

284366
def test_rebumping(rbf_node, dest_address):
285367
# check that re-bumping the original tx fails, but bumping the bumper succeeds

0 commit comments

Comments
 (0)