Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -576,10 +576,13 @@ void CWallet::UpgradeDescriptorCache()

bool CWallet::ChangeWalletPassphrase(const SecureString& strOldWalletPassphrase, const SecureString& strNewWalletPassphrase)
{
bool fWasLocked = IsLocked(true);

{
LOCK2(m_relock_mutex, cs_wallet);
const bool was_locked = IsLocked(true);
const bool was_mixing_only = fOnlyMixingAllowed;
const auto relock = interfaces::MakeCleanupHandler([this, was_locked] {
if (was_locked) Lock();
});
Lock();

CCrypter crypter;
Expand All @@ -590,7 +593,7 @@ bool CWallet::ChangeWalletPassphrase(const SecureString& strOldWalletPassphrase,
return false;
if (!crypter.Decrypt(pMasterKey.second.vchCryptedKey, _vMasterKey))
return false;
if (Unlock(_vMasterKey))
if (Unlock(_vMasterKey, was_mixing_only))
{
constexpr MillisecondsDouble target{100};
auto start{SteadyClock::now()};
Expand All @@ -611,9 +614,6 @@ bool CWallet::ChangeWalletPassphrase(const SecureString& strOldWalletPassphrase,
if (!crypter.Encrypt(_vMasterKey, pMasterKey.second.vchCryptedKey))
return false;
WalletBatch(GetDatabase()).WriteMasterKey(pMasterKey.first, pMasterKey.second);
if (fWasLocked)
Lock();

return true;
}
}
Expand Down
25 changes: 25 additions & 0 deletions test/functional/wallet_encryption.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,29 @@ def set_test_params(self):
def skip_test_if_missing_module(self):
self.skip_if_no_wallet()

def test_passphrase_change_permissions(self):
self.log.info("Passphrase changes preserve locked, mixing-only and full permissions")
node = self.nodes[0]
for mode in ("locked", "mixing", "full"):
node.createwallet("change-" + mode, descriptors=self.options.descriptors)
wallet = node.get_wallet_rpc("change-" + mode)
address = wallet.getnewaddress()
wallet.encryptwallet("old passphrase")
if mode != "locked":
wallet.walletpassphrase("old passphrase", 100, mode == "mixing")
wallet.walletpassphrasechange("old passphrase", "new passphrase")
if mode == "full":
signature = wallet.signmessage(address, "passphrase change")
assert node.verifymessage(address, signature, "passphrase change")
else:
assert_raises_rpc_error(-13, "Please enter the wallet passphrase", wallet.signmessage, address, "passphrase change")
if mode == "locked":
assert_raises_rpc_error(-13, "Please unlock wallet for mixing", wallet.coinjoin, "start")
else:
assert_equal(wallet.coinjoin("start"), "Mixing requested")
wallet.coinjoin("stop")
wallet.unloadwallet()

def run_test(self):
passphrase = "WalletPassphrase"
passphrase2 = "SecondWalletPassphrase"
Expand Down Expand Up @@ -109,6 +132,8 @@ def run_test(self):
assert_equal(self.nodes[0].getwalletinfo()['unlocked_until'], unlocked_until)
self.nodes[0].walletlock()

self.test_passphrase_change_permissions()


if __name__ == '__main__':
WalletEncryptionTest().main()
Loading