Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: calchuchesta on September 24, 2021, 02:21:53 AM



Title: What happens when you unlock the wallet?
Post by: calchuchesta on September 24, 2021, 02:21:53 AM
Specifically, what goes on when you get prompted by the GUI to enter your password in order to make a transaction?

Does the code run the default walletpassphrase <your password> <timeout (seconds)> command line with the default 60 seconds or something else happens?

I use the walletpassphrase to see if I still can remember my password and I don't want to transact, so im curious about what goes on when the GUI unlocks it when making a transaction.


Title: Re: What happens when you unlock the wallet?
Post by: nc50lc on September 24, 2021, 05:07:44 AM
This has everything you need to know about wallet encryption: github.com/bitcoin/bitcoin/blob/6b8a5ab622e5c9386c872036646bf94da983b190/doc/README (https://github.com/bitcoin/bitcoin/blob/6b8a5ab622e5c9386c872036646bf94da983b190/doc/README#:~:text=At%20runtime%2C%20the,removed%20from%20memory.)

I use the walletpassphrase to see if I still can remember my password and I don't want to transact, so im curious about what goes on when the GUI unlocks it when making a transaction.
If you need to do that, you can set a timeout of '1' second so your passphrase master key wont stay in your RAM for too long.


Title: Re: What happens when you unlock the wallet?
Post by: calchuchesta on September 26, 2021, 03:36:22 PM
This has everything you need to know about wallet encryption: github.com/bitcoin/bitcoin/blob/6b8a5ab622e5c9386c872036646bf94da983b190/doc/README (https://github.com/bitcoin/bitcoin/blob/6b8a5ab622e5c9386c872036646bf94da983b190/doc/README#:~:text=At%20runtime%2C%20the,removed%20from%20memory.)

Unfortunately it doesn't answer about what GUI do when you unlock the wallet.

I use the walletpassphrase to see if I still can remember my password and I don't want to transact, so im curious about what goes on when the GUI unlocks it when making a transaction.
If you need to do that, you can set a timeout of '1' second so your passphrase wont stay in your RAM for too long.

You also could use command walletlock if you accidentally use default unlock duration and being extremely careful.

It should be somewhere on the code. Im trying to find things related to transactions on the qt folder source where it it's related to the passphrase being used:

https://github.com/bitcoin/bitcoin/blob/master/src/qt/askpassphrasedialog.h
https://github.com/bitcoin/bitcoin/blob/master/src/qt/askpassphrasedialog.cpp

I think this only relates to the actual window but not what happens with the passphrase. There's also this:

https://github.com/bitcoin/bitcoin/blob/master/src/interfaces/wallet.h

Code:
    //! Encrypt wallet.
    virtual bool encryptWallet(const SecureString& wallet_passphrase) = 0;

    //! Return whether wallet is encrypted.
    virtual bool isCrypted() = 0;

    //! Lock wallet.
    virtual bool lock() = 0;

    //! Unlock wallet.
    virtual bool unlock(const SecureString& wallet_passphrase) = 0;

    //! Return whether wallet is locked.
    virtual bool isLocked() = 0;

    //! Change wallet passphrase.
    virtual bool changeWalletPassphrase(const SecureString& old_wallet_passphrase,
        const SecureString& new_wallet_passphrase) = 0;

I can't find what the "default timeout" would be.



Title: Re: What happens when you unlock the wallet?
Post by: achow101 on September 26, 2021, 11:56:47 PM
When you unlock the wallet, the password is used to decrypt the key actually used to encrypt the private keys. That decryption key is stored in the wallet as vMasterKey. It will remain there in memory (and thus the wallet is unlocked) until CWallet::Lock is called to remove it from memory and thus lock the wallet.

When you use the walletpassphrase RPC, a timer thread will be scheduled to run after the given timeout. That thread will just call CWallet::Lock and lock the wallet.

The GUI doesn't need a timer like the RPC does because it can ask for the passphrase when needed, and lock the wallet when it is done with what it is doing. So it does not have a timeout in the same way the RPC does. Rather each action that needs the wallet to be unlocked for it to succeed will create a WalletModel::UnlockContext object, which in doing so, will spawn the AskPassphraseDialog to get your passphrase. That sets CWallet::vMasterKey thereby unlocking your wallet. The UnlockContext object has a destructor which calls CWallet::Lock so when it is destroyed, the wallet is locked. The object is destroyed when it goes out of scope, i.e. when the action being performed that needed the wallet to be unlocked is completed.