4 // Include local headers
5 #include "wallet/walletdb.h"
8 #include "wallet/crypter.h"
9 #include <boost/foreach.hpp>
15 "This program outputs Bitcoin addresses and private keys from a wallet.dat file" << std::endl
17 << "Usage and options: "
19 << " -datadir=<directory> to tell the program where your wallet is"
21 << " -wallet=<name> (Optional) if your wallet is not named wallet.dat"
23 << " -regtest or -testnet (Optional) dumps addresses from regtest/testnet"
25 << " -dumppass (Optional)if you want to extract private keys associated with addresses"
27 << " -pass=<walletpassphrase> if you have encrypted private keys stored in your wallet"
32 class WalletUtilityDB : public CDB
35 typedef std::map<unsigned int, CMasterKey> MasterKeyMap;
36 MasterKeyMap mapMasterKeys;
37 unsigned int nMasterKeyMaxID;
39 std::vector<CKeyingMaterial> vMKeys;
42 WalletUtilityDB(const std::string& strFilename, const char* pszMode = "r+", bool fFlushOnClose = true) : CDB(strFilename, pszMode, fFlushOnClose)
48 std::string getAddress(CDataStream ssKey);
49 std::string getKey(CDataStream ssKey, CDataStream ssValue);
50 std::string getCryptedKey(CDataStream ssKey, CDataStream ssValue, std::string masterPass);
51 bool updateMasterKeys(CDataStream ssKey, CDataStream ssValue);
52 bool parseKeys(bool dumppriv, std::string masterPass);
54 bool DecryptSecret(const std::vector<unsigned char>& vchCiphertext, const uint256& nIV, CKeyingMaterial& vchPlaintext);
56 bool DecryptKey(const std::vector<unsigned char>& vchCryptedSecret, const CPubKey& vchPubKey, CKey& key);
61 * Address from a public key in base58
63 std::string WalletUtilityDB::getAddress(CDataStream ssKey)
67 CKeyID id = vchPubKey.GetID();
68 std::string strAddr = CBitcoinAddress(id).ToString();
75 * Non encrypted private key in WIF
77 std::string WalletUtilityDB::getKey(CDataStream ssKey, CDataStream ssValue)
86 if (key.Load(pkey, vchPubKey, true))
87 strKey = CBitcoinSecret(key).ToString();
93 bool WalletUtilityDB::DecryptSecret(const std::vector<unsigned char>& vchCiphertext, const uint256& nIV, CKeyingMaterial& vchPlaintext)
96 std::vector<unsigned char> chIV(WALLET_CRYPTO_KEY_SIZE);
97 memcpy(&chIV[0], &nIV, WALLET_CRYPTO_KEY_SIZE);
99 BOOST_FOREACH(const CKeyingMaterial vMKey, vMKeys)
101 if(!cKeyCrypter.SetKey(vMKey, chIV))
103 if (cKeyCrypter.Decrypt(vchCiphertext, *((CKeyingMaterial*)&vchPlaintext)))
110 bool WalletUtilityDB::Unlock()
113 CKeyingMaterial vMasterKey;
115 BOOST_FOREACH(const MasterKeyMap::value_type& pMasterKey, mapMasterKeys)
117 if(!crypter.SetKeyFromPassphrase(mPass, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod))
119 if (!crypter.Decrypt(pMasterKey.second.vchCryptedKey, vMasterKey))
120 continue; // try another master key
121 vMKeys.push_back(vMasterKey);
127 bool WalletUtilityDB::DecryptKey(const std::vector<unsigned char>& vchCryptedSecret, const CPubKey& vchPubKey, CKey& key)
129 CKeyingMaterial vchSecret;
130 if(!DecryptSecret(vchCryptedSecret, vchPubKey.GetHash(), vchSecret))
133 if (vchSecret.size() != 32)
136 key.Set(vchSecret.begin(), vchSecret.end(), vchPubKey.IsCompressed());
142 * Encrypted private key in WIF format
144 std::string WalletUtilityDB::getCryptedKey(CDataStream ssKey, CDataStream ssValue, std::string masterPass)
146 mPass = masterPass.c_str();
151 std::vector<unsigned char> vKey;
157 if(!DecryptKey(vKey, vchPubKey, key))
160 std::string strKey = CBitcoinSecret(key).ToString();
166 * Master key derivation
168 bool WalletUtilityDB::updateMasterKeys(CDataStream ssKey, CDataStream ssValue)
172 CMasterKey kMasterKey;
173 ssValue >> kMasterKey;
174 if (mapMasterKeys.count(nID) != 0)
176 std::cout << "Error reading wallet database: duplicate CMasterKey id " << nID << std::endl;
179 mapMasterKeys[nID] = kMasterKey;
181 if (nMasterKeyMaxID < nID)
182 nMasterKeyMaxID = nID;
189 * Look at all the records and parse keys for addresses and private keys
191 bool WalletUtilityDB::parseKeys(bool dumppriv, std::string masterPass)
193 DBErrors result = DB_LOAD_OK;
198 Dbc* pcursor = GetCursor();
201 LogPrintf("Error getting wallet database cursor\n");
207 while (result == DB_LOAD_OK && true)
209 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
210 CDataStream ssValue(SER_DISK, CLIENT_VERSION);
211 int result = ReadAtCursor(pcursor, ssKey, ssValue);
213 if (result == DB_NOTFOUND) {
216 else if (result != 0)
218 LogPrintf("Error reading next record from wallet database\n");
224 if (strType == "mkey")
226 updateMasterKeys(ssKey, ssValue);
230 pcursor = GetCursor();
233 while (result == DB_LOAD_OK && true)
235 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
236 CDataStream ssValue(SER_DISK, CLIENT_VERSION);
237 int ret = ReadAtCursor(pcursor, ssKey, ssValue);
239 if (ret == DB_NOTFOUND)
241 std::cout << " ]" << std::endl;
245 else if (ret != DB_LOAD_OK)
247 LogPrintf("Error reading next record from wallet database\n");
254 if (strType == "key" || strType == "ckey")
256 std::string strAddr = getAddress(ssKey);
257 std::string strKey = "";
260 if (dumppriv && strType == "key")
261 strKey = getKey(ssKey, ssValue);
262 if (dumppriv && strType == "ckey")
264 if (masterPass == "")
266 std::cout << "Encrypted wallet, please provide a password. See help below" << std::endl;
268 result = DB_LOAD_FAIL;
271 strKey = getCryptedKey(ssKey, ssValue, masterPass);
284 std::cout << "{\"addr\" : \"" + strAddr + "\", "
285 << "\"pkey\" : \"" + strKey + "\"}"
290 std::cout << "\"" + strAddr + "\"";
298 } catch (DbException &e) {
299 std::cout << "DBException caught " << e.get_errno() << std::endl;
300 } catch (std::exception &e) {
301 std::cout << "Exception caught " << std::endl;
304 if (result == DB_LOAD_OK)
311 int main(int argc, char* argv[])
313 ParseParameters(argc, argv);
314 std::string walletFile = GetArg("-wallet", "wallet.dat");
315 std::string masterPass = GetArg("-pass", "");
316 bool fDumpPass = GetBoolArg("-dumppass", false);
317 bool help = GetBoolArg("-h", false);
327 SelectParamsFromCommandLine();
328 result = WalletUtilityDB(walletFile, "r").parseKeys(fDumpPass, masterPass);
330 catch (const std::exception& e) {
331 std::cout << "Error opening wallet file " << walletFile << std::endl;
332 std::cout << e.what() << std::endl;