4 // Include local headers
5 #include "wallet/walletdb.h"
8 #include "wallet/crypter.h"
9 #include <boost/foreach.hpp>
11 #include "komodo_defs.h"
12 char ASSETCHAINS_SYMBOL[KOMODO_ASSETCHAIN_MAXLEN];
13 int64_t MAX_MONEY = 200000000 * 100000000LL;
14 uint64_t ASSETCHAINS_SUPPLY;
15 uint16_t BITCOIND_RPCPORT = 7771;
16 uint16_t ASSETCHAINS_P2PPORT,ASSETCHAINS_RPCPORT;
17 uint32_t ASSETCHAIN_INIT,ASSETCHAINS_CC;
18 uint32_t ASSETCHAINS_MAGIC = 2387029918;
19 uint32_t ASSETCHAINS_EQUIHASH = 0;
20 uint32_t ASSETCHAINS_VERUSHASH = 1;
21 uint32_t ASSETCHAINS_ALGO = 0;
22 int32_t ASSETCHAINS_LWMAPOS = 0;
23 int32_t VERUS_BLOCK_POSUNITS = 1000;
25 unsigned int MAX_BLOCK_SIGOPS = 20000;
30 "This program outputs Bitcoin addresses and private keys from a wallet.dat file" << std::endl
32 << "Usage and options: "
34 << " -datadir=<directory> to tell the program where your wallet is"
36 << " -wallet=<name> (Optional) if your wallet is not named wallet.dat"
38 << " -regtest or -testnet (Optional) dumps addresses from regtest/testnet"
40 << " -dumppass (Optional)if you want to extract private keys associated with addresses"
42 << " -pass=<walletpassphrase> if you have encrypted private keys stored in your wallet"
47 class WalletUtilityDB : public CDB
50 typedef std::map<unsigned int, CMasterKey> MasterKeyMap;
51 MasterKeyMap mapMasterKeys;
52 unsigned int nMasterKeyMaxID;
54 std::vector<CKeyingMaterial> vMKeys;
57 WalletUtilityDB(const std::string& strFilename, const char* pszMode = "r+", bool fFlushOnClose = true) : CDB(strFilename, pszMode, fFlushOnClose)
63 std::string getAddress(CDataStream ssKey);
64 std::string getKey(CDataStream ssKey, CDataStream ssValue);
65 std::string getCryptedKey(CDataStream ssKey, CDataStream ssValue, std::string masterPass);
66 bool updateMasterKeys(CDataStream ssKey, CDataStream ssValue);
67 bool parseKeys(bool dumppriv, std::string masterPass);
69 bool DecryptSecret(const std::vector<unsigned char>& vchCiphertext, const uint256& nIV, CKeyingMaterial& vchPlaintext);
71 bool DecryptKey(const std::vector<unsigned char>& vchCryptedSecret, const CPubKey& vchPubKey, CKey& key);
76 * Address from a public key in base58
78 std::string WalletUtilityDB::getAddress(CDataStream ssKey)
82 CKeyID id = vchPubKey.GetID();
83 std::string strAddr = CBitcoinAddress(id).ToString();
90 * Non encrypted private key in WIF
92 std::string WalletUtilityDB::getKey(CDataStream ssKey, CDataStream ssValue)
101 if (key.Load(pkey, vchPubKey, true))
102 strKey = CBitcoinSecret(key).ToString();
108 bool WalletUtilityDB::DecryptSecret(const std::vector<unsigned char>& vchCiphertext, const uint256& nIV, CKeyingMaterial& vchPlaintext)
110 CCrypter cKeyCrypter;
111 std::vector<unsigned char> chIV(WALLET_CRYPTO_KEY_SIZE);
112 memcpy(&chIV[0], &nIV, WALLET_CRYPTO_KEY_SIZE);
114 BOOST_FOREACH(const CKeyingMaterial vMKey, vMKeys)
116 if(!cKeyCrypter.SetKey(vMKey, chIV))
118 if (cKeyCrypter.Decrypt(vchCiphertext, *((CKeyingMaterial*)&vchPlaintext)))
125 bool WalletUtilityDB::Unlock()
128 CKeyingMaterial vMasterKey;
130 BOOST_FOREACH(const MasterKeyMap::value_type& pMasterKey, mapMasterKeys)
132 if(!crypter.SetKeyFromPassphrase(mPass, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod))
134 if (!crypter.Decrypt(pMasterKey.second.vchCryptedKey, vMasterKey))
135 continue; // try another master key
136 vMKeys.push_back(vMasterKey);
142 bool WalletUtilityDB::DecryptKey(const std::vector<unsigned char>& vchCryptedSecret, const CPubKey& vchPubKey, CKey& key)
144 CKeyingMaterial vchSecret;
145 if(!DecryptSecret(vchCryptedSecret, vchPubKey.GetHash(), vchSecret))
148 if (vchSecret.size() != 32)
151 key.Set(vchSecret.begin(), vchSecret.end(), vchPubKey.IsCompressed());
157 * Encrypted private key in WIF format
159 std::string WalletUtilityDB::getCryptedKey(CDataStream ssKey, CDataStream ssValue, std::string masterPass)
161 mPass = masterPass.c_str();
166 std::vector<unsigned char> vKey;
172 if(!DecryptKey(vKey, vchPubKey, key))
175 std::string strKey = CBitcoinSecret(key).ToString();
181 * Master key derivation
183 bool WalletUtilityDB::updateMasterKeys(CDataStream ssKey, CDataStream ssValue)
187 CMasterKey kMasterKey;
188 ssValue >> kMasterKey;
189 if (mapMasterKeys.count(nID) != 0)
191 std::cout << "Error reading wallet database: duplicate CMasterKey id " << nID << std::endl;
194 mapMasterKeys[nID] = kMasterKey;
196 if (nMasterKeyMaxID < nID)
197 nMasterKeyMaxID = nID;
204 * Look at all the records and parse keys for addresses and private keys
206 bool WalletUtilityDB::parseKeys(bool dumppriv, std::string masterPass)
208 DBErrors result = DB_LOAD_OK;
213 Dbc* pcursor = GetCursor();
216 LogPrintf("Error getting wallet database cursor\n");
222 while (result == DB_LOAD_OK && true)
224 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
225 CDataStream ssValue(SER_DISK, CLIENT_VERSION);
226 int result = ReadAtCursor(pcursor, ssKey, ssValue);
228 if (result == DB_NOTFOUND) {
231 else if (result != 0)
233 LogPrintf("Error reading next record from wallet database\n");
239 if (strType == "mkey")
241 updateMasterKeys(ssKey, ssValue);
245 pcursor = GetCursor();
248 while (result == DB_LOAD_OK && true)
250 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
251 CDataStream ssValue(SER_DISK, CLIENT_VERSION);
252 int ret = ReadAtCursor(pcursor, ssKey, ssValue);
254 if (ret == DB_NOTFOUND)
256 std::cout << " ]" << std::endl;
260 else if (ret != DB_LOAD_OK)
262 LogPrintf("Error reading next record from wallet database\n");
269 if (strType == "key" || strType == "ckey")
271 std::string strAddr = getAddress(ssKey);
272 std::string strKey = "";
275 if (dumppriv && strType == "key")
276 strKey = getKey(ssKey, ssValue);
277 if (dumppriv && strType == "ckey")
279 if (masterPass == "")
281 std::cout << "Encrypted wallet, please provide a password. See help below" << std::endl;
283 result = DB_LOAD_FAIL;
286 strKey = getCryptedKey(ssKey, ssValue, masterPass);
299 std::cout << "{\"addr\" : \"" + strAddr + "\", "
300 << "\"pkey\" : \"" + strKey + "\"}"
305 std::cout << "\"" + strAddr + "\"";
313 } catch (DbException &e) {
314 std::cout << "DBException caught " << e.get_errno() << std::endl;
315 } catch (std::exception &e) {
316 std::cout << "Exception caught " << std::endl;
319 if (result == DB_LOAD_OK)
326 int main(int argc, char* argv[])
328 ParseParameters(argc, argv);
329 std::string walletFile = GetArg("-wallet", "wallet.dat");
330 std::string masterPass = GetArg("-pass", "");
331 bool fDumpPass = GetBoolArg("-dumppass", false);
332 bool help = GetBoolArg("-h", false);
342 SelectParamsFromCommandLine();
343 result = WalletUtilityDB(walletFile, "r").parseKeys(fDumpPass, masterPass);
345 catch (const std::exception& e) {
346 std::cout << "Error opening wallet file " << walletFile << std::endl;
347 std::cout << e.what() << std::endl;