@@ -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;
0 commit comments