About restore the wallet #23313

issue huangwb8 opened this issue on October 20, 2021
  1. huangwb8 commented at 2:03 AM on October 20, 2021: none

    Hi~ I just have some questions about restoring a wallet. Most users had showed how to use Bitcoin core, but few focus on how to restore the wallet. I've read the book "grokking Bitcoin", where Kalle said that we should keep wallet.dat file safe. My try is as following:

    First I create a test wallet:

    $ bitcoin-cli createwallet testwallet false false -passphrase=test111 false false true false
    

    Next, I started to backup the wallet and kept it safe. This step seemed that the passphrase was not necessary.

    $  bitcoin-cli -rpcwallet=testwallet backupwallet ~/testwallet.dat
    

    Get an adress for someone who would send me some bitcoins:

    $ bitcoin-cli -rpcwallet="testwallet" -named getnewaddress address_type=bech32
    bc1q7y3uj...unpn9
    
    # Get private key
    $ bitcoin-cli -rpcwallet="testwallet" dumpprivkey "bc1q7y3uj...unpn9"  
    KzqZ3UoP...98K
    

    Supposed that I've got the pay, but suddenly my server was exploded. I decided to restore the wallet in a new Linux server. After install Bitcoin core software completely, I created a new wallet:

    $ bitcoin-cli createwallet testwallet2 false false -passphrase=test444 false false true false 
    

    Unlock it for 120s:

    $ bitcoin-cli -rpcwallet=testwallet2 walletpassphrase -passphrase=test444 120 
    

    Restored the wallet:

    $ bitcoin-cli -rpcwallet=testwallet2 importwallet <testwallet.dat> 
    # without any error information
    

    However, it seemed that the wallet could not access the private key

    $ bitcoin-cli -rpcwallet="testwallet2" dumpprivkey "bc1q7y3uj...unpn9"  
    error code: -4
    error message:
    Private key for address bc1q7y3uj...unpn9 is not known
    

    Here are some questions: a. Was I do the right thing to restore the wallet? b. If I was right, it means that anyone (hackers of my servers, for example) who own the testwallet.dat file could get all the bitcoins. In the importwallet step, I didn't need to use any passphrase (test111) of the previous wallet. It seemed not very safe, because my server may be easy to be cracked. So I guess I was wrong, but I have no idea how to do this. Any suggestions?

    My Bitcoin core version is :

    "version": 220000,
    "subversion": "/Satoshi:22.0.0/"
    
  2. huangwb8 added the label Feature on Oct 20, 2021
  3. fanquake removed the label Feature on Oct 20, 2021
  4. fanquake added the label Questions and Help on Oct 20, 2021
  5. achow101 commented at 2:42 AM on October 20, 2021: member

    a. Was I do the right thing to restore the wallet?

    No. You used importwallet which imports a wallet given a text dump file generated by dumpwallet. You did not restore your wallet. Because you provided it your wallet file and not a proper dump file, it did nothing. importwallet could be better at communicating that, but the RPC is being phased out as it is specific to the legacy wallet which is slated for removal.

    #22541 introduced a proper restorewallet RPC which will be available in 23.0.

  6. huangwb8 commented at 4:00 AM on October 20, 2021: none

    @achow101 @fanquake

    Thanks a lot! According to #22541, I tested the method——just replace the wallet.dat. It works! Here are some example codes, which I hope can help rookies of bitcoin like me :

    Note: you should leave a blank space in front of the codes if you want them private! Or the shell would record it, which may be not safe. Or you can use -stdin if necessary. You can check the book 'Groking Bitcoin' for advanced usage.

    First, create and use a wallet:

    # Create a wallet
    <blank space>bitcoin-cli createwallet testwallet false false -passphrase=test111 false false true false
    
    # Unlock it for a while
    <blank space>bitcoin-cli -rpcwallet=testwallet walletpassphrase -passphrase=test111 120 
    
    # Backup and kept `wallet.dat` safe
    <blank space>bitcoin-cli -rpcwallet=testwallet backupwallet <your path>/testwallet.dat
    
    # Get an address for someone who would send me bitcoins
    bitcoin-cli -rpcwallet="testwallet" -named getnewaddress address_type=bech32
    bc1q7y3uj...unpn9
    
    # check the private key
    bitcoin-cli -rpcwallet="testwallet" dumpprivkey "bc1q7y3uj...unpn9"  
    KzqZ3UoP...98K
    

    Then, delete the testwallet document to mimic the solution that you lost the wallet:

    # Delete the raw dir
    <blank space>rm -r <path of Bitcoin core datadir>/testwallet
    
    # restart Bitcoin core
    bitcoin-cli stop
    bitcoind -daemon -conf=<path of the bitcoin.conf file>
    

    Next, create a new wallet:

    # Create a new wallet
    $ <blank space>bitcoin-cli createwallet testwallet2 false false -passphrase=test444 false false true false
    
    

    replace the new wallet.dat with the old one and restart Bitcoin core

    # Replace wallet.dat
    cd <path of Bitcoin core datadir>/testwallet2
    rm wallet.dat
    <blank space>cp <your path>/testwallet.dat wallet.dat
    
    # Restart Bitcoin core
    bitcoin-cli stop
    bitcoind -daemon -conf=<path of the bitcoin.conf file>
    

    Now, check whether the new wallet could get the same private key of the old address

    # Unlock it for a while
    <blank space>bitcoin-cli -rpcwallet=testwallet2 walletpassphrase -passphrase=test111 120 # You have to use the old passphrase of 'testwallet.dat'
    
    bitcoin-cli -rpcwallet="testwallet2" dumpprivkey "bc1q7y3uj...unpn9"  
    KzqZ3UoP...98K
    

    Done!

    I think it should be the right way for restoring wallets in current version of Bitcoin core, because the old address could be identified by the new, restored-version wallet.

    Also, the people who owns both the wallet.dat and the passphrase of the wallet have its despositon. Even though somebody (hakers for example) gets the wallet.dat, it could do nothing about the wallet without the passphrase, because It's hard to use wallet.dat to decrypt private keys without passphrase mathematically. Am I correct?

    By the way, it's nice for the developers to add new functions like restorewallet in the future, which would be more convenient!

    Here are some other questions: a. Could I backup the wallet just for once? When restoring the wallet, the status of the wallet would be the time I backup, or the latest one of the account? b. As the Bitcoin core project developing rapidly, is it possible that users could not restore an old wallet in a brand-new version of the software?

  7. fanquake commented at 12:40 PM on October 22, 2021: member

    This issue tracker is for problems / technical questions related to Bitcoin Core, not for general usage help. You might be better asking your questions on the Bitcoin StackExchange, or IRC.

  8. fanquake closed this on Oct 22, 2021

  9. DrahtBot locked this on Oct 30, 2022

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-04-26 06:14 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me