Skip to content

Commit e6c58d3

Browse files
committed
Do not import private keys to wallets with private keys disabled
1 parent b5c5021 commit e6c58d3

File tree

4 files changed

+31
-0
lines changed

4 files changed

+31
-0
lines changed

src/wallet/rpcdump.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,9 @@ UniValue importprivkey(const JSONRPCRequest& request)
133133
+ HelpExampleRpc("importprivkey", "\"mykey\", \"testing\", false")
134134
);
135135

136+
if (pwallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
137+
throw JSONRPCError(RPC_WALLET_ERROR, "Cannot import private keys to a wallet with private keys disabled");
138+
}
136139

137140
WalletRescanReserver reserver(pwallet);
138141
bool fRescan = true;
@@ -617,6 +620,11 @@ UniValue importwallet(const JSONRPCRequest& request)
617620
}
618621
}
619622
file.close();
623+
// We now know whether we are importing private keys, so we can error if private keys are disabled
624+
if (keys.size() > 0 && pwallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
625+
uiInterface.ShowProgress("", 100, false); // hide progress dialog in GUI
626+
throw JSONRPCError(RPC_WALLET_ERROR, "Importing wallets is disabled when private keys are disabled");
627+
}
620628
double total = (double)(keys.size() + scripts.size());
621629
double progress = 0;
622630
for (const auto& key_tuple : keys) {
@@ -967,6 +975,11 @@ static UniValue ProcessImport(CWallet * const pwallet, const UniValue& data, con
967975
const bool watchOnly = data.exists("watchonly") ? data["watchonly"].get_bool() : false;
968976
const std::string& label = data.exists("label") ? data["label"].get_str() : "";
969977

978+
// If private keys are disabled, abort if private keys are being imported
979+
if (pwallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS) && !keys.isNull()) {
980+
throw JSONRPCError(RPC_WALLET_ERROR, "Cannot import private keys to a wallet with private keys disabled");
981+
}
982+
970983
// Generate the script and destination for the scriptPubKey provided
971984
CScript script;
972985
CTxDestination dest;

src/wallet/rpcwallet.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3830,6 +3830,10 @@ UniValue sethdseed(const JSONRPCRequest& request)
38303830
throw JSONRPCError(RPC_CLIENT_IN_INITIAL_DOWNLOAD, "Cannot set a new HD seed while still in Initial Block Download");
38313831
}
38323832

3833+
if (pwallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
3834+
throw JSONRPCError(RPC_WALLET_ERROR, "Cannot set a HD seed to a wallet with private keys disabled");
3835+
}
3836+
38333837
auto locked_chain = pwallet->chain().lock();
38343838
LOCK(pwallet->cs_wallet);
38353839

src/wallet/wallet.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,9 @@ bool CWallet::AddKeyPubKeyWithDB(WalletBatch &batch, const CKey& secret, const C
251251
{
252252
AssertLockHeld(cs_wallet); // mapKeyMetadata
253253

254+
// Make sure we aren't adding private keys to private key disabled wallets
255+
assert(!IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS));
256+
254257
// CCryptoKeyStore has no concept of wallet databases, but calls AddCryptedKey
255258
// which is overridden below. To avoid flushes, the database handle is
256259
// tunneled through to it.

test/functional/wallet_disableprivatekeys.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from test_framework.test_framework import BitcoinTestFramework
99
from test_framework.util import (
10+
assert_equal,
1011
assert_raises_rpc_error,
1112
)
1213

@@ -31,5 +32,15 @@ def run_test(self):
3132
assert_raises_rpc_error(-4,"Error: Private keys are disabled for this wallet", w1.getrawchangeaddress)
3233
w1.importpubkey(w2.getaddressinfo(w2.getnewaddress())['pubkey'])
3334

35+
self.log.info('Test that private keys cannot be imported')
36+
addr = w2.getnewaddress('', 'legacy')
37+
privkey = w2.dumpprivkey(addr)
38+
assert_raises_rpc_error(-4, 'Cannot import private keys to a wallet with private keys disabled', w1.importprivkey, privkey)
39+
result = w1.importmulti([{'scriptPubKey': {'address': addr}, 'timestamp': 'now', 'keys': [privkey]}])
40+
assert(not result[0]['success'])
41+
assert('warning' not in result[0])
42+
assert_equal(result[0]['error']['code'], -4)
43+
assert_equal(result[0]['error']['message'], 'Cannot import private keys to a wallet with private keys disabled')
44+
3445
if __name__ == '__main__':
3546
DisablePrivateKeysTest().main()

0 commit comments

Comments
 (0)