mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-22 15:04:44 +01:00
Return error for ignored passphrase through disable private keys option
This commit is contained in:
parent
d6649d16b5
commit
ba1f128d6c
4 changed files with 20 additions and 16 deletions
|
@ -2684,8 +2684,8 @@ static UniValue createwallet(const JSONRPCRequest& request)
|
||||||
|
|
||||||
std::string error;
|
std::string error;
|
||||||
std::string warning;
|
std::string warning;
|
||||||
WalletCreationStatus status;
|
std::shared_ptr<CWallet> wallet;
|
||||||
std::shared_ptr<CWallet> wallet = CreateWallet(*g_rpc_interfaces->chain, passphrase, flags, request.params[0].get_str(), error, warning, status);
|
WalletCreationStatus status = CreateWallet(*g_rpc_interfaces->chain, passphrase, flags, request.params[0].get_str(), error, warning, wallet);
|
||||||
if (status == WalletCreationStatus::CREATION_FAILED) {
|
if (status == WalletCreationStatus::CREATION_FAILED) {
|
||||||
throw JSONRPCError(RPC_WALLET_ERROR, error);
|
throw JSONRPCError(RPC_WALLET_ERROR, error);
|
||||||
} else if (status == WalletCreationStatus::ENCRYPTION_FAILED) {
|
} else if (status == WalletCreationStatus::ENCRYPTION_FAILED) {
|
||||||
|
|
|
@ -160,7 +160,7 @@ std::shared_ptr<CWallet> LoadWallet(interfaces::Chain& chain, const std::string&
|
||||||
return LoadWallet(chain, WalletLocation(name), error, warning);
|
return LoadWallet(chain, WalletLocation(name), error, warning);
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
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)
|
||||||
{
|
{
|
||||||
// Indicate that the wallet is actually supposed to be blank and not just blank to make it encrypted
|
// Indicate that the wallet is actually supposed to be blank and not just blank to make it encrypted
|
||||||
bool create_blank = (wallet_creation_flags & WALLET_FLAG_BLANK_WALLET);
|
bool create_blank = (wallet_creation_flags & WALLET_FLAG_BLANK_WALLET);
|
||||||
|
@ -174,39 +174,40 @@ std::shared_ptr<CWallet> CreateWallet(interfaces::Chain& chain, const SecureStri
|
||||||
WalletLocation location(name);
|
WalletLocation location(name);
|
||||||
if (location.Exists()) {
|
if (location.Exists()) {
|
||||||
error = "Wallet " + location.GetName() + " already exists.";
|
error = "Wallet " + location.GetName() + " already exists.";
|
||||||
status = WalletCreationStatus::CREATION_FAILED;
|
return WalletCreationStatus::CREATION_FAILED;
|
||||||
return nullptr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wallet::Verify will check if we're trying to create a wallet with a duplicate name.
|
// Wallet::Verify will check if we're trying to create a wallet with a duplicate name.
|
||||||
std::string wallet_error;
|
std::string wallet_error;
|
||||||
if (!CWallet::Verify(chain, location, false, wallet_error, warning)) {
|
if (!CWallet::Verify(chain, location, false, wallet_error, warning)) {
|
||||||
error = "Wallet file verification failed: " + wallet_error;
|
error = "Wallet file verification failed: " + wallet_error;
|
||||||
status = WalletCreationStatus::CREATION_FAILED;
|
return WalletCreationStatus::CREATION_FAILED;
|
||||||
return nullptr;
|
}
|
||||||
|
|
||||||
|
// Do not allow a passphrase when private keys are disabled
|
||||||
|
if (!passphrase.empty() && (wallet_creation_flags & WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
|
||||||
|
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.";
|
||||||
|
return WalletCreationStatus::CREATION_FAILED;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make the wallet
|
// Make the wallet
|
||||||
std::shared_ptr<CWallet> wallet = CWallet::CreateWalletFromFile(chain, location, wallet_creation_flags);
|
std::shared_ptr<CWallet> wallet = CWallet::CreateWalletFromFile(chain, location, wallet_creation_flags);
|
||||||
if (!wallet) {
|
if (!wallet) {
|
||||||
error = "Wallet creation failed";
|
error = "Wallet creation failed";
|
||||||
status = WalletCreationStatus::CREATION_FAILED;
|
return WalletCreationStatus::CREATION_FAILED;
|
||||||
return nullptr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Encrypt the wallet
|
// Encrypt the wallet
|
||||||
if (!passphrase.empty() && !(wallet_creation_flags & WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
|
if (!passphrase.empty() && !(wallet_creation_flags & WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
|
||||||
if (!wallet->EncryptWallet(passphrase)) {
|
if (!wallet->EncryptWallet(passphrase)) {
|
||||||
error = "Error: Wallet created but failed to encrypt.";
|
error = "Error: Wallet created but failed to encrypt.";
|
||||||
status = WalletCreationStatus::ENCRYPTION_FAILED;
|
return WalletCreationStatus::ENCRYPTION_FAILED;
|
||||||
return nullptr;
|
|
||||||
}
|
}
|
||||||
if (!create_blank) {
|
if (!create_blank) {
|
||||||
// Unlock the wallet
|
// Unlock the wallet
|
||||||
if (!wallet->Unlock(passphrase)) {
|
if (!wallet->Unlock(passphrase)) {
|
||||||
error = "Error: Wallet was encrypted but could not be unlocked";
|
error = "Error: Wallet was encrypted but could not be unlocked";
|
||||||
status = WalletCreationStatus::ENCRYPTION_FAILED;
|
return WalletCreationStatus::ENCRYPTION_FAILED;
|
||||||
return nullptr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set a seed for the wallet
|
// Set a seed for the wallet
|
||||||
|
@ -220,8 +221,8 @@ std::shared_ptr<CWallet> CreateWallet(interfaces::Chain& chain, const SecureStri
|
||||||
}
|
}
|
||||||
AddWallet(wallet);
|
AddWallet(wallet);
|
||||||
wallet->postInitProcess();
|
wallet->postInitProcess();
|
||||||
status = WalletCreationStatus::SUCCESS;
|
result = wallet;
|
||||||
return wallet;
|
return WalletCreationStatus::SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
const uint32_t BIP32_HARDENED_KEY_LIMIT = 0x80000000;
|
const uint32_t BIP32_HARDENED_KEY_LIMIT = 0x80000000;
|
||||||
|
|
|
@ -55,7 +55,7 @@ enum class WalletCreationStatus {
|
||||||
ENCRYPTION_FAILED
|
ENCRYPTION_FAILED
|
||||||
};
|
};
|
||||||
|
|
||||||
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);
|
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);
|
||||||
|
|
||||||
//! Default for -keypool
|
//! Default for -keypool
|
||||||
static const unsigned int DEFAULT_KEYPOOL_SIZE = 1000;
|
static const unsigned int DEFAULT_KEYPOOL_SIZE = 1000;
|
||||||
|
|
|
@ -119,5 +119,8 @@ class CreateWalletTest(BitcoinTestFramework):
|
||||||
# Empty passphrase, error
|
# Empty passphrase, error
|
||||||
assert_raises_rpc_error(-16, 'Cannot encrypt a wallet with a blank password', self.nodes[0].createwallet, 'w7', False, False, '')
|
assert_raises_rpc_error(-16, 'Cannot encrypt a wallet with a blank password', self.nodes[0].createwallet, 'w7', False, False, '')
|
||||||
|
|
||||||
|
self.log.info('Using a passphrase with private keys disabled returns error')
|
||||||
|
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')
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
CreateWalletTest().main()
|
CreateWalletTest().main()
|
||||||
|
|
Loading…
Add table
Reference in a new issue