Skip to content

Commit ba1f128

Browse files
Fabian Jahrfjahr
authored andcommitted
Return error for ignored passphrase through disable private keys option
1 parent d6649d1 commit ba1f128

File tree

4 files changed

+20
-16
lines changed

4 files changed

+20
-16
lines changed

src/wallet/rpcwallet.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2684,8 +2684,8 @@ static UniValue createwallet(const JSONRPCRequest& request)
26842684

26852685
std::string error;
26862686
std::string warning;
2687-
WalletCreationStatus status;
2688-
std::shared_ptr<CWallet> wallet = CreateWallet(*g_rpc_interfaces->chain, passphrase, flags, request.params[0].get_str(), error, warning, status);
2687+
std::shared_ptr<CWallet> wallet;
2688+
WalletCreationStatus status = CreateWallet(*g_rpc_interfaces->chain, passphrase, flags, request.params[0].get_str(), error, warning, wallet);
26892689
if (status == WalletCreationStatus::CREATION_FAILED) {
26902690
throw JSONRPCError(RPC_WALLET_ERROR, error);
26912691
} else if (status == WalletCreationStatus::ENCRYPTION_FAILED) {

src/wallet/wallet.cpp

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ std::shared_ptr<CWallet> LoadWallet(interfaces::Chain& chain, const std::string&
160160
return LoadWallet(chain, WalletLocation(name), error, warning);
161161
}
162162

163-
std::shared_ptr<CWallet> CreateWallet(interfaces::Chain& chain, const SecureString& passphrase, uint64_t wallet_creation_flags, const std::string& name, std::string& error, std::string& warning, WalletCreationStatus& status)
163+
WalletCreationStatus CreateWallet(interfaces::Chain& chain, const SecureString& passphrase, uint64_t wallet_creation_flags, const std::string& name, std::string& error, std::string& warning, std::shared_ptr<CWallet>& result)
164164
{
165165
// Indicate that the wallet is actually supposed to be blank and not just blank to make it encrypted
166166
bool create_blank = (wallet_creation_flags & WALLET_FLAG_BLANK_WALLET);
@@ -174,39 +174,40 @@ std::shared_ptr<CWallet> CreateWallet(interfaces::Chain& chain, const SecureStri
174174
WalletLocation location(name);
175175
if (location.Exists()) {
176176
error = "Wallet " + location.GetName() + " already exists.";
177-
status = WalletCreationStatus::CREATION_FAILED;
178-
return nullptr;
177+
return WalletCreationStatus::CREATION_FAILED;
179178
}
180179

181180
// Wallet::Verify will check if we're trying to create a wallet with a duplicate name.
182181
std::string wallet_error;
183182
if (!CWallet::Verify(chain, location, false, wallet_error, warning)) {
184183
error = "Wallet file verification failed: " + wallet_error;
185-
status = WalletCreationStatus::CREATION_FAILED;
186-
return nullptr;
184+
return WalletCreationStatus::CREATION_FAILED;
185+
}
186+
187+
// Do not allow a passphrase when private keys are disabled
188+
if (!passphrase.empty() && (wallet_creation_flags & WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
189+
error = "Passphrase provided but private keys are disabled. A passphrase is only used to encrypt private keys, so cannot be used for wallets with private keys disabled.";
190+
return WalletCreationStatus::CREATION_FAILED;
187191
}
188192

189193
// Make the wallet
190194
std::shared_ptr<CWallet> wallet = CWallet::CreateWalletFromFile(chain, location, wallet_creation_flags);
191195
if (!wallet) {
192196
error = "Wallet creation failed";
193-
status = WalletCreationStatus::CREATION_FAILED;
194-
return nullptr;
197+
return WalletCreationStatus::CREATION_FAILED;
195198
}
196199

197200
// Encrypt the wallet
198201
if (!passphrase.empty() && !(wallet_creation_flags & WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
199202
if (!wallet->EncryptWallet(passphrase)) {
200203
error = "Error: Wallet created but failed to encrypt.";
201-
status = WalletCreationStatus::ENCRYPTION_FAILED;
202-
return nullptr;
204+
return WalletCreationStatus::ENCRYPTION_FAILED;
203205
}
204206
if (!create_blank) {
205207
// Unlock the wallet
206208
if (!wallet->Unlock(passphrase)) {
207209
error = "Error: Wallet was encrypted but could not be unlocked";
208-
status = WalletCreationStatus::ENCRYPTION_FAILED;
209-
return nullptr;
210+
return WalletCreationStatus::ENCRYPTION_FAILED;
210211
}
211212

212213
// Set a seed for the wallet
@@ -220,8 +221,8 @@ std::shared_ptr<CWallet> CreateWallet(interfaces::Chain& chain, const SecureStri
220221
}
221222
AddWallet(wallet);
222223
wallet->postInitProcess();
223-
status = WalletCreationStatus::SUCCESS;
224-
return wallet;
224+
result = wallet;
225+
return WalletCreationStatus::SUCCESS;
225226
}
226227

227228
const uint32_t BIP32_HARDENED_KEY_LIMIT = 0x80000000;

src/wallet/wallet.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ enum class WalletCreationStatus {
5555
ENCRYPTION_FAILED
5656
};
5757

58-
std::shared_ptr<CWallet> CreateWallet(interfaces::Chain& chain, const SecureString& passphrase, uint64_t wallet_creation_flags, const std::string& name, std::string& error, std::string& warning, WalletCreationStatus& status);
58+
WalletCreationStatus CreateWallet(interfaces::Chain& chain, const SecureString& passphrase, uint64_t wallet_creation_flags, const std::string& name, std::string& error, std::string& warning, std::shared_ptr<CWallet>& result);
5959

6060
//! Default for -keypool
6161
static const unsigned int DEFAULT_KEYPOOL_SIZE = 1000;

test/functional/wallet_createwallet.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,5 +119,8 @@ def run_test(self):
119119
# Empty passphrase, error
120120
assert_raises_rpc_error(-16, 'Cannot encrypt a wallet with a blank password', self.nodes[0].createwallet, 'w7', False, False, '')
121121

122+
self.log.info('Using a passphrase with private keys disabled returns error')
123+
assert_raises_rpc_error(-4, 'Passphrase provided but private keys are disabled. A passphrase is only used to encrypt private keys, so cannot be used for wallets with private keys disabled.', self.nodes[0].createwallet, wallet_name='w8', disable_private_keys=True, passphrase='thisisapassphrase')
124+
122125
if __name__ == '__main__':
123126
CreateWalletTest().main()

0 commit comments

Comments
 (0)