Today we’re going to learn how to recover damaged private keys for Ethereum and Bitcoin.
First: what a private key is. In simple terms, it’s just a random number that nobody can guess (unless they have access to your brain). To generate one in Ruby, you can use the SecureRandom gem like this:
# This script uses the SecureRandom gem to generate a random private key for Ethereum and Bitcoin.
require "securerandom" # This line imports the SecureRandom gem, allowing us to use its methods.
secret = SecureRandom.hex(32) # This line calls the hex method from the SecureRandom module, passing in 32 as the argument to generate a 32-character hexadecimal string. This string will serve as our private key.
# => "bec52dffb33ec1f4d629f88232796e898f4294079c5894a6645e8a4f7261fabe"
Now, let’s say you accidentally spilled coffee on your laptop and now your private key is all messed up. Don’t worry! You can still recover it using a tool called BTCRecover (for Bitcoin) or Ethereum Wallet Recovery Tool (for Ethereum).
For example, let’s say you have a damaged private key for an Ethereum address that looks like this:
// This script is used to recover a damaged private key for an Ethereum address.
// It is recommended to use the Ethereum Wallet Recovery Tool for this purpose.
// The damaged private key is represented as a string of hexadecimal characters.
// The '%' symbol is used to separate each segment of the private key.
// The first segment of the private key is the address prefix, which is always "0x".
// This is followed by the actual private key, which is a combination of letters and numbers.
// The private key is divided into three segments, each separated by the '%' symbol.
// The first segment is 16 characters long, the second is 16 characters long, and the third is 32 characters long.
// The private key can be recovered by removing the '%' symbols and combining the three segments together.
// For example, a damaged private key for an Ethereum address may look like this:
// 0x5db77aa7aea5%2h7d6b4c64dab21%h972cf4763d4937d3e6e17f580436dcb10%3h
// After removing the '%' symbols and combining the segments, the private key would look like this:
// 0x5db77aa7aea52h7d6b4c64dab21h972cf4763d4937d3e6e17f580436dcb103h
This is a standard Eth private key (with the leading 0x removed) where there are three damaged parts. To recover it, you can use BTCRecover or Ethereum Wallet Recovery Tool with a tokenlist that contains all possible combinations of those damaged parts. For example:
# This script uses the btcrecover.py tool to recover a damaged private key for Ethereum.
# The private key is in raw format and the damaged parts will be replaced using a tokenlist.
# The recovered key will be used to generate a Bitcoin address for the specified wallet type.
# The maximum number of tokens to be used for recovery is set to 1.
# The tokenlist file is located in the specified directory.
python btcrecover.py \ # Calls the btcrecover.py tool
--rawprivatekey \ # Specifies that the private key is in raw format
--addrs bc1qafy0ftpk5teeayjaqukyd244un8gxvdk8hl5j6 \ # Specifies the Bitcoin address to be generated
--wallet-type bitcoin \ # Specifies the type of wallet to be used for address generation
--max-tokens 1 \ # Sets the maximum number of tokens to be used for recovery to 1
--tokenlist ./docs/Usage_Examples/btc_privkey_tokenlist.txt # Specifies the location of the tokenlist file
Or, if you don’t have a record of the corresponding address:
# This script uses the btcrecover.py tool to recover a raw private key and corresponding address from a given address database.
# It specifies the wallet type as bitcoin and sets the maximum number of tokens to 1.
# It also provides a tokenlist file to use for the recovery process.
# The following line imports the btcrecover.py tool and specifies the use of a raw private key.
python btcrecover.py --rawprivatekey
# The next line specifies the location of the address database to be used for the recovery process.
# In this case, it is located in the test-addressdbs folder within the btcrecover folder.
# The database is named addresses-BTC-Test.db and is specific to the bitcoin wallet type.
--addressdb ./btcrecover/test/test-addressdbs/addresses-BTC-Test.db --wallet-type bitcoin
# The following line sets the maximum number of tokens to be used for the recovery process to 1.
# This means that only one token from the tokenlist file will be used at a time.
--max-tokens 1
# The final line specifies the tokenlist file to be used for the recovery process.
# In this case, it is located in the docs/Usage_Examples folder and is named btc_privkey_tokenlist.txt.
--tokenlist ./docs/Usage_Examples/btc_privkey_tokenlist.txt
And that’s it! You can now recover your damaged private key and get back to enjoying your cryptocurrency without any coffee stains or toilet water ruining the party.