starting wallet cleanup. added username, renamed to twisterwallet.dat

This commit is contained in:
Miguel Freitas 2013-07-24 19:50:37 -03:00
parent 26bbd5614f
commit fe57966e05
11 changed files with 41 additions and 173 deletions

View File

@ -229,7 +229,7 @@ std::string HelpMessage()
strUsage += " -upgradewallet " + _("Upgrade wallet to latest format") + "\n";
strUsage += " -keypool=<n> " + _("Set key pool size to <n> (default: 100)") + "\n";
strUsage += " -rescan " + _("Rescan the block chain for missing wallet transactions") + "\n";
strUsage += " -salvagewallet " + _("Attempt to recover private keys from a corrupt wallet.dat") + "\n";
strUsage += " -salvagewallet " + _("Attempt to recover private keys from a corrupt twisterwallet.dat") + "\n";
strUsage += " -checkblocks=<n> " + _("How many blocks to check at startup (default: 288, 0 = all)") + "\n";
strUsage += " -checklevel=<n> " + _("How thorough the block verification is (0-4, default: 3)") + "\n";
strUsage += " -txindex " + _("Maintain a full transaction index (default: 0)") + "\n";
@ -518,23 +518,23 @@ bool AppInit2(boost::thread_group& threadGroup)
if (GetBoolArg("-salvagewallet", false))
{
// Recover readable keypairs:
if (!CWalletDB::Recover(bitdb, "wallet.dat", true))
if (!CWalletDB::Recover(bitdb, "twisterwallet.dat", true))
return false;
}
if (filesystem::exists(GetDataDir() / "wallet.dat"))
if (filesystem::exists(GetDataDir() / "twisterwallet.dat"))
{
CDBEnv::VerifyResult r = bitdb.Verify("wallet.dat", CWalletDB::Recover);
CDBEnv::VerifyResult r = bitdb.Verify("twisterwallet.dat", CWalletDB::Recover);
if (r == CDBEnv::RECOVER_OK)
{
string msg = strprintf(_("Warning: wallet.dat corrupt, data salvaged!"
" Original wallet.dat saved as wallet.{timestamp}.bak in %s; if"
string msg = strprintf(_("Warning: twisterwallet.dat corrupt, data salvaged!"
" Original twisterwallet.dat saved as wallet.{timestamp}.bak in %s; if"
" your balance or transactions are incorrect you should"
" restore from a backup."), strDataDir.c_str());
InitWarning(msg);
}
if (r == CDBEnv::RECOVER_FAIL)
return InitError(_("wallet.dat corrupt, salvage failed"));
return InitError(_("twisterwallet.dat corrupt, salvage failed"));
}
// ********************************************************* Step 6: network initialization
@ -795,20 +795,20 @@ bool AppInit2(boost::thread_group& threadGroup)
nStart = GetTimeMillis();
bool fFirstRun = true;
pwalletMain = new CWallet("wallet.dat");
pwalletMain = new CWallet("twisterwallet.dat");
DBErrors nLoadWalletRet = pwalletMain->LoadWallet(fFirstRun);
if (nLoadWalletRet != DB_LOAD_OK)
{
if (nLoadWalletRet == DB_CORRUPT)
strErrors << _("Error loading wallet.dat: Wallet corrupted") << "\n";
strErrors << _("Error loading twisterwallet.dat: Wallet corrupted") << "\n";
else if (nLoadWalletRet == DB_NONCRITICAL_ERROR)
{
string msg(_("Warning: error reading wallet.dat! All keys read correctly, but transaction data"
string msg(_("Warning: error reading twisterwallet.dat! All keys read correctly, but transaction data"
" or address book entries might be missing or incorrect."));
InitWarning(msg);
}
else if (nLoadWalletRet == DB_TOO_NEW)
strErrors << _("Error loading wallet.dat: Wallet requires newer version of Bitcoin") << "\n";
strErrors << _("Error loading twisterwallet.dat: Wallet requires newer version of Bitcoin") << "\n";
else if (nLoadWalletRet == DB_NEED_REWRITE)
{
strErrors << _("Wallet needed to be rewritten: restart Bitcoin to complete") << "\n";
@ -816,7 +816,7 @@ bool AppInit2(boost::thread_group& threadGroup)
return InitError(strErrors.str());
}
else
strErrors << _("Error loading wallet.dat") << "\n";
strErrors << _("Error loading twisterwallet.dat") << "\n";
}
if (GetBoolArg("-upgradewallet", fFirstRun))
@ -860,7 +860,7 @@ bool AppInit2(boost::thread_group& threadGroup)
pindexRescan = pindexGenesisBlock;
else
{
CWalletDB walletdb("wallet.dat");
CWalletDB walletdb("twisterwallet.dat");
CBlockLocator locator;
if (walletdb.ReadBestBlock(locator))
pindexRescan = locator.GetBlockIndex();

View File

@ -26,31 +26,6 @@ bool CBasicKeyStore::AddKeyPubKey(const CKey& key, const CPubKey &pubkey)
return true;
}
bool CBasicKeyStore::AddCScript(const CScript& redeemScript)
{
LOCK(cs_KeyStore);
mapScripts[redeemScript.GetID()] = redeemScript;
return true;
}
bool CBasicKeyStore::HaveCScript(const CScriptID& hash) const
{
LOCK(cs_KeyStore);
return mapScripts.count(hash) > 0;
}
bool CBasicKeyStore::GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const
{
LOCK(cs_KeyStore);
ScriptMap::const_iterator mi = mapScripts.find(hash);
if (mi != mapScripts.end())
{
redeemScriptOut = (*mi).second;
return true;
}
return false;
}
bool CCryptoKeyStore::SetCrypted()
{
LOCK(cs_KeyStore);

View File

@ -29,22 +29,15 @@ public:
virtual bool GetKey(const CKeyID &address, CKey& keyOut) const =0;
virtual void GetKeys(std::set<CKeyID> &setAddress) const =0;
virtual bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const;
// Support for BIP 0013 : see https://en.bitcoin.it/wiki/BIP_0013
virtual bool AddCScript(const CScript& redeemScript) =0;
virtual bool HaveCScript(const CScriptID &hash) const =0;
virtual bool GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const =0;
};
typedef std::map<CKeyID, CKey> KeyMap;
typedef std::map<CScriptID, CScript > ScriptMap;
/** Basic key store, that keeps keys in an address->secret map */
class CBasicKeyStore : public CKeyStore
{
protected:
KeyMap mapKeys;
ScriptMap mapScripts;
public:
bool AddKeyPubKey(const CKey& key, const CPubKey &pubkey);
@ -83,9 +76,6 @@ public:
}
return false;
}
virtual bool AddCScript(const CScript& redeemScript);
virtual bool HaveCScript(const CScriptID &hash) const;
virtual bool GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const;
};
typedef std::map<CKeyID, std::pair<CPubKey, std::vector<unsigned char> > > CryptedKeyMap;

View File

@ -163,6 +163,7 @@ Value createrawtransaction(const Array& params, bool fHelp)
vector<unsigned char> txData(ParseHexV(params[1], "pubkey"));
rawTx.pubKey << txData;
} else {
pwalletMain->GenerateNewKey(username);
throw JSONRPCError(RPC_INTERNAL_ERROR, "pubkey generation not implemented");
}
@ -319,17 +320,6 @@ Value signrawtransaction(const Array& params, bool fHelp)
// if redeemScript given and not using the local wallet (private keys
// given), add redeemScript to the tempKeystore so it can be signed:
if (fGivenKeys && scriptPubKey.IsPayToScriptHash())
{
RPCTypeCheck(prevOut, map_list_of("txid", str_type)("vout", int_type)("scriptPubKey", str_type)("redeemScript",str_type));
Value v = find_value(prevOut, "redeemScript");
if (!(v == Value::null))
{
vector<unsigned char> rsData(ParseHexV(v, "redeemScript"));
CScript redeemScript(rsData.begin(), rsData.end());
tempKeystore.AddCScript(redeemScript);
}
}
}
}

View File

@ -733,11 +733,10 @@ Value addmultisigaddress(const Array& params, bool fHelp)
// Construct using pay-to-script-hash:
CScript inner = _createmultisig(params);
CScriptID innerID = inner.GetID();
pwalletMain->AddCScript(inner);
pwalletMain->SetAddressBookName(innerID, strAccount);
return CBitcoinAddress(innerID).ToString();
//pwalletMain->SetAddressBookName(innerID, strAccount);
//return CBitcoinAddress(innerID).ToString();
return CBitcoinAddress().ToString();
}
Value createmultisig(const Array& params, bool fHelp)
@ -1144,7 +1143,7 @@ Value backupwallet(const Array& params, bool fHelp)
if (fHelp || params.size() != 1)
throw runtime_error(
"backupwallet <destination>\n"
"Safely copies wallet.dat to destination, which can be a directory or a path with filename.");
"Safely copies twisterwallet.dat to destination, which can be a directory or a path with filename.");
string strDest = params[0].get_str();
if (!BackupWallet(*pwalletMain, strDest))
@ -1321,25 +1320,6 @@ public:
obj.push_back(Pair("iscompressed", vchPubKey.IsCompressed()));
return obj;
}
Object operator()(const CScriptID &scriptID) const {
Object obj;
obj.push_back(Pair("isscript", true));
CScript subscript;
pwalletMain->GetCScript(scriptID, subscript);
std::vector<CTxDestination> addresses;
txnouttype whichType;
int nRequired;
ExtractDestinations(subscript, whichType, addresses, nRequired);
obj.push_back(Pair("script", GetTxnOutputType(whichType)));
Array a;
BOOST_FOREACH(const CTxDestination& addr, addresses)
a.push_back(CBitcoinAddress(addr).ToString());
obj.push_back(Pair("addresses", a));
if (whichType == TX_MULTISIG)
obj.push_back(Pair("sigsrequired", nRequired));
return obj;
}
};
Value validateaddress(const Array& params, bool fHelp)

View File

@ -77,7 +77,6 @@ const char* GetTxnOutputType(txnouttype t)
case TX_NONSTANDARD: return "nonstandard";
case TX_PUBKEY: return "pubkey";
case TX_PUBKEYHASH: return "pubkeyhash";
case TX_SCRIPTHASH: return "scripthash";
case TX_MULTISIG: return "multisig";
}
return NULL;
@ -1135,16 +1134,6 @@ bool Solver(const CScript& scriptPubKey, txnouttype& typeRet, vector<vector<unsi
mTemplates.insert(make_pair(TX_MULTISIG, CScript() << OP_SMALLINTEGER << OP_PUBKEYS << OP_SMALLINTEGER << OP_CHECKMULTISIG));
}
// Shortcut for pay-to-script-hash, which are more constrained than the other types:
// it is always OP_HASH160 20 [20 byte hash] OP_EQUAL
if (scriptPubKey.IsPayToScriptHash())
{
typeRet = TX_SCRIPTHASH;
vector<unsigned char> hashBytes(scriptPubKey.begin()+2, scriptPubKey.begin()+22);
vSolutionsRet.push_back(hashBytes);
return true;
}
// Scan templates
const CScript& script1 = scriptPubKey;
BOOST_FOREACH(const PAIRTYPE(txnouttype, CScript)& tplate, mTemplates)
@ -1294,8 +1283,6 @@ bool Solver(const CKeyStore& keystore, const CScript& scriptPubKey, uint256 hash
scriptSigRet << vch;
}
return true;
case TX_SCRIPTHASH:
return keystore.GetCScript(uint160(vSolutions[0]), scriptSigRet);
case TX_MULTISIG:
scriptSigRet << OP_0; // workaround CHECKMULTISIG bug
@ -1318,8 +1305,6 @@ int ScriptSigArgsExpected(txnouttype t, const std::vector<std::vector<unsigned c
if (vSolutions.size() < 1 || vSolutions[0].size() < 1)
return -1;
return vSolutions[0][0] + 1;
case TX_SCRIPTHASH:
return 1; // doesn't include args needed by the script
}
return -1;
}
@ -1367,7 +1352,6 @@ public:
CKeyStoreIsMineVisitor(const CKeyStore *keystoreIn) : keystore(keystoreIn) { }
bool operator()(const CNoDestination &dest) const { return false; }
bool operator()(const CKeyID &keyID) const { return keystore->HaveKey(keyID); }
bool operator()(const CScriptID &scriptID) const { return keystore->HaveCScript(scriptID); }
};
bool IsMine(const CKeyStore &keystore, const CTxDestination &dest)
@ -1393,13 +1377,6 @@ bool IsMine(const CKeyStore &keystore, const CScript& scriptPubKey)
case TX_PUBKEYHASH:
keyID = CKeyID(uint160(vSolutions[0]));
return keystore.HaveKey(keyID);
case TX_SCRIPTHASH:
{
CScript subscript;
if (!keystore.GetCScript(CScriptID(uint160(vSolutions[0])), subscript))
return false;
return IsMine(keystore, subscript);
}
case TX_MULTISIG:
{
// Only consider transactions "mine" if we own ALL the
@ -1431,11 +1408,6 @@ bool ExtractDestination(const CScript& scriptPubKey, CTxDestination& addressRet)
addressRet = CKeyID(uint160(vSolutions[0]));
return true;
}
else if (whichType == TX_SCRIPTHASH)
{
addressRet = CScriptID(uint160(vSolutions[0]));
return true;
}
// Multisig txns have more than one address...
return false;
}
@ -1492,12 +1464,6 @@ public:
vKeys.push_back(keyId);
}
void operator()(const CScriptID &scriptId) {
CScript script;
if (keystore.GetCScript(scriptId, script))
Process(script);
}
void operator()(const CNoDestination &none) {}
};
@ -1678,26 +1644,6 @@ static CScript CombineSignatures(CScript scriptPubKey, const CTransaction& txTo,
if (sigs1.empty() || sigs1[0].empty())
return PushAll(sigs2);
return PushAll(sigs1);
case TX_SCRIPTHASH:
if (sigs1.empty() || sigs1.back().empty())
return PushAll(sigs2);
else if (sigs2.empty() || sigs2.back().empty())
return PushAll(sigs1);
else
{
// Recur to combine:
valtype spk = sigs1.back();
CScript pubKey2(spk.begin(), spk.end());
txnouttype txType2;
vector<vector<unsigned char> > vSolutions2;
Solver(pubKey2, txType2, vSolutions2);
sigs1.pop_back();
sigs2.pop_back();
CScript result = CombineSignatures(pubKey2, txTo, nIn, txType2, vSolutions2, sigs1, sigs2);
result << spk;
return result;
}
case TX_MULTISIG:
return CombineMultisig(scriptPubKey, txTo, nIn, vSolutions, sigs1, sigs2);
}

View File

@ -43,7 +43,6 @@ enum txnouttype
// 'standard' transaction types:
TX_PUBKEY,
TX_PUBKEYHASH,
TX_SCRIPTHASH,
TX_MULTISIG,
};

View File

@ -27,7 +27,7 @@ struct CompareValueOnly
}
};
CPubKey CWallet::GenerateNewKey()
CPubKey CWallet::GenerateNewKey(std::string username)
{
bool fCompressed = CanSupportFeature(FEATURE_COMPRPUBKEY); // default to compressed public keys if we want 0.6.0 wallets
@ -43,7 +43,7 @@ CPubKey CWallet::GenerateNewKey()
// Create new metadata
int64 nCreationTime = GetTime();
mapKeyMetadata[pubkey.GetID()] = CKeyMetadata(nCreationTime);
mapKeyMetadata[pubkey.GetID()] = CKeyMetadata(nCreationTime, username);
if (!nTimeFirstKey || nCreationTime < nTimeFirstKey)
nTimeFirstKey = nCreationTime;
@ -101,15 +101,6 @@ bool CWallet::LoadCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigne
return CCryptoKeyStore::AddCryptedKey(vchPubKey, vchCryptedSecret);
}
bool CWallet::AddCScript(const CScript& redeemScript)
{
if (!CCryptoKeyStore::AddCScript(redeemScript))
return false;
if (!fFileBacked)
return true;
return CWalletDB(strWalletFile).WriteCScript(Hash160(redeemScript), redeemScript);
}
bool CWallet::Unlock(const SecureString& strWalletPassphrase)
{
CCrypter crypter;
@ -1247,6 +1238,7 @@ bool GetWalletFile(CWallet* pwallet, string &strWalletFileOut)
//
bool CWallet::NewKeyPool()
{
/*
{
LOCK(cs_wallet);
CWalletDB walletdb(strWalletFile);
@ -1266,11 +1258,13 @@ bool CWallet::NewKeyPool()
}
printf("CWallet::NewKeyPool wrote %"PRI64d" new keys\n", nKeys);
}
*/
return true;
}
bool CWallet::TopUpKeyPool()
{
/*
{
LOCK(cs_wallet);
@ -1292,6 +1286,7 @@ bool CWallet::TopUpKeyPool()
printf("keypool added key %"PRI64d", size=%"PRIszu"\n", nEnd, setKeyPool.size());
}
}
*/
return true;
}
@ -1360,6 +1355,7 @@ void CWallet::ReturnKey(int64 nIndex)
bool CWallet::GetKeyFromPool(CPubKey& result, bool fAllowReuse)
{
/* [MF] pool is going to die.
int64 nIndex = 0;
CKeyPool keypool;
{
@ -1380,6 +1376,8 @@ bool CWallet::GetKeyFromPool(CPubKey& result, bool fAllowReuse)
result = keypool.vchPubKey;
}
return true;
*/
return false;
}
int64 CWallet::GetOldestKeyPoolTime()

View File

@ -135,7 +135,7 @@ public:
// keystore implementation
// Generate a new key
CPubKey GenerateNewKey();
CPubKey GenerateNewKey(std::string username);
// Adds a key to the store, and saves it to disk.
bool AddKeyPubKey(const CKey& key, const CPubKey &pubkey);
// Adds a key to the store, without saving it to disk (used by LoadWallet)
@ -149,8 +149,6 @@ public:
bool AddCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret);
// Adds an encrypted key to the store, without saving it to disk (used by LoadWallet)
bool LoadCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret);
bool AddCScript(const CScript& redeemScript);
bool LoadCScript(const CScript& redeemScript) { return CCryptoKeyStore::AddCScript(redeemScript); }
bool Unlock(const SecureString& strWalletPassphrase);
bool ChangeWalletPassphrase(const SecureString& strOldWalletPassphrase, const SecureString& strNewWalletPassphrase);

View File

@ -374,7 +374,7 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue,
// stored metadata for that key later, which is fine.
CKeyID keyid = keypool.vchPubKey.GetID();
if (pwallet->mapKeyMetadata.count(keyid) == 0)
pwallet->mapKeyMetadata[keyid] = CKeyMetadata(keypool.nTime);
pwallet->mapKeyMetadata[keyid] = CKeyMetadata(keypool.nTime, std::string());
}
else if (strType == "version")
{
@ -382,18 +382,6 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue,
if (wss.nFileVersion == 10300)
wss.nFileVersion = 300;
}
else if (strType == "cscript")
{
uint160 hash;
ssKey >> hash;
CScript script;
ssValue >> script;
if (!pwallet->LoadCScript(script))
{
strErr = "Error reading wallet database: LoadCScript failed";
return false;
}
}
else if (strType == "orderposnext")
{
ssValue >> pwallet->nOrderPosNext;
@ -557,7 +545,7 @@ void ThreadFlushWalletDB(const string& strFile)
map<string, int>::iterator mi = bitdb.mapFileUseCount.find(strFile);
if (mi != bitdb.mapFileUseCount.end())
{
printf("Flushing wallet.dat\n");
printf("Flushing twisterwallet.dat\n");
nLastFlushed = nWalletDBUpdated;
int64 nStart = GetTimeMillis();
@ -566,7 +554,7 @@ void ThreadFlushWalletDB(const string& strFile)
bitdb.CheckpointLSN(strFile);
bitdb.mapFileUseCount.erase(mi++);
printf("Flushed wallet.dat %"PRI64d"ms\n", GetTimeMillis() - nStart);
printf("Flushed twisterwallet.dat %"PRI64d"ms\n", GetTimeMillis() - nStart);
}
}
}
@ -589,7 +577,7 @@ bool BackupWallet(const CWallet& wallet, const string& strDest)
bitdb.CheckpointLSN(wallet.strWalletFile);
bitdb.mapFileUseCount.erase(wallet.strWalletFile);
// Copy wallet.dat
// Copy twisterwallet.dat
filesystem::path pathSrc = GetDataDir() / wallet.strWalletFile;
filesystem::path pathDest(strDest);
if (filesystem::is_directory(pathDest))
@ -601,10 +589,10 @@ bool BackupWallet(const CWallet& wallet, const string& strDest)
#else
filesystem::copy_file(pathSrc, pathDest);
#endif
printf("copied wallet.dat to %s\n", pathDest.string().c_str());
printf("copied twisterwallet.dat to %s\n", pathDest.string().c_str());
return true;
} catch(const filesystem::filesystem_error &e) {
printf("error copying wallet.dat to %s - %s\n", pathDest.string().c_str(), e.what());
printf("error copying twisterwallet.dat to %s - %s\n", pathDest.string().c_str(), e.what());
return false;
}
}

View File

@ -31,15 +31,17 @@ public:
static const int CURRENT_VERSION=1;
int nVersion;
int64 nCreateTime; // 0 means unknown
std::string username;
CKeyMetadata()
{
SetNull();
}
CKeyMetadata(int64 nCreateTime_)
CKeyMetadata(int64 nCreateTime_, std::string username_)
{
nVersion = CKeyMetadata::CURRENT_VERSION;
nCreateTime = nCreateTime_;
username = username_;
}
IMPLEMENT_SERIALIZE
@ -47,16 +49,18 @@ public:
READWRITE(this->nVersion);
nVersion = this->nVersion;
READWRITE(nCreateTime);
READWRITE(username);
)
void SetNull()
{
nVersion = CKeyMetadata::CURRENT_VERSION;
nCreateTime = 0;
username.clear();
}
};
/** Access to the wallet database (wallet.dat) */
/** Access to the wallet database (twisterwallet.dat) */
class CWalletDB : public CDB
{
public:
@ -168,7 +172,7 @@ public:
return Erase(std::make_pair(std::string("pool"), nPool));
}
// Settings are no longer stored in wallet.dat; these are
// Settings are no longer stored in twisterwallet.dat; these are
// used only for backwards compatibility:
template<typename T>
bool ReadSetting(const std::string& strKey, T& value)