mirror of
https://github.com/twisterarmy/twister-core.git
synced 2025-01-10 06:48:07 +00:00
440 lines
12 KiB
C++
440 lines
12 KiB
C++
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
|
// Copyright (c) 2009-2012 The Bitcoin developers
|
|
// Distributed under the MIT/X11 software license, see the accompanying
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
#ifndef BITCOIN_WALLET_H
|
|
#define BITCOIN_WALLET_H
|
|
|
|
#include "walletdb.h"
|
|
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "main.h"
|
|
#include "key.h"
|
|
#include "keystore.h"
|
|
#include "script.h"
|
|
#include "ui_interface.h"
|
|
#include "util.h"
|
|
|
|
class CWalletTx;
|
|
class COutput;
|
|
class CWalletDB;
|
|
|
|
/** (client) version numbers for particular wallet features */
|
|
enum WalletFeature
|
|
{
|
|
FEATURE_BASE = 10500, // the earliest version new wallets supports (only useful for getinfo's clientversion output)
|
|
|
|
FEATURE_WALLETCRYPT = 40000, // wallet encryption
|
|
FEATURE_COMPRPUBKEY = 60000, // compressed public keys
|
|
|
|
FEATURE_LATEST = 60000
|
|
};
|
|
|
|
|
|
/** A key pool entry */
|
|
class CKeyPool
|
|
{
|
|
public:
|
|
int64 nTime;
|
|
CPubKey vchPubKey;
|
|
|
|
CKeyPool()
|
|
{
|
|
nTime = GetTime();
|
|
}
|
|
|
|
CKeyPool(const CPubKey& vchPubKeyIn)
|
|
{
|
|
nTime = GetTime();
|
|
vchPubKey = vchPubKeyIn;
|
|
}
|
|
|
|
IMPLEMENT_SERIALIZE
|
|
(
|
|
if (!(nType & SER_GETHASH))
|
|
READWRITE(nVersion);
|
|
READWRITE(nTime);
|
|
READWRITE(vchPubKey);
|
|
)
|
|
};
|
|
|
|
/** A CWallet is an extension of a keystore, which also maintains a set of transactions and balances,
|
|
* and provides the ability to create new transactions.
|
|
*/
|
|
class CWallet : public CCryptoKeyStore
|
|
{
|
|
private:
|
|
CWalletDB *pwalletdbEncryption;
|
|
|
|
// the current wallet version: clients below this version are not able to load the wallet
|
|
int nWalletVersion;
|
|
|
|
// the maximum wallet format version: memory-only variable that specifies to what version this wallet may be upgraded
|
|
int nWalletMaxVersion;
|
|
|
|
public:
|
|
mutable CCriticalSection cs_wallet;
|
|
|
|
bool fFileBacked;
|
|
std::string strWalletFile;
|
|
|
|
std::set<int64> setKeyPool;
|
|
std::map<CKeyID, CKeyMetadata> mapKeyMetadata;
|
|
|
|
typedef std::map<unsigned int, CMasterKey> MasterKeyMap;
|
|
MasterKeyMap mapMasterKeys;
|
|
unsigned int nMasterKeyMaxID;
|
|
|
|
CWallet()
|
|
{
|
|
nWalletVersion = FEATURE_BASE;
|
|
nWalletMaxVersion = FEATURE_BASE;
|
|
fFileBacked = false;
|
|
nMasterKeyMaxID = 0;
|
|
pwalletdbEncryption = NULL;
|
|
nOrderPosNext = 0;
|
|
}
|
|
CWallet(std::string strWalletFileIn)
|
|
{
|
|
nWalletVersion = FEATURE_BASE;
|
|
nWalletMaxVersion = FEATURE_BASE;
|
|
strWalletFile = strWalletFileIn;
|
|
fFileBacked = true;
|
|
nMasterKeyMaxID = 0;
|
|
pwalletdbEncryption = NULL;
|
|
nOrderPosNext = 0;
|
|
}
|
|
|
|
std::map<uint256, CWalletTx> mapWallet;
|
|
int64 nOrderPosNext;
|
|
std::map<uint256, int> mapRequestCount;
|
|
|
|
std::map<CTxDestination, std::string> mapAddressBook;
|
|
|
|
CPubKey vchDefaultKey;
|
|
|
|
int64 nTimeFirstKey;
|
|
|
|
// check whether we are allowed to upgrade (or already support) to the named feature
|
|
bool CanSupportFeature(enum WalletFeature wf) { return nWalletMaxVersion >= wf; }
|
|
|
|
// keystore implementation
|
|
// Generate a new key
|
|
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)
|
|
bool LoadKey(const CKey& key, const CPubKey &pubkey) { return CCryptoKeyStore::AddKeyPubKey(key, pubkey); }
|
|
// Load metadata (used by LoadWallet)
|
|
bool LoadKeyMetadata(const CPubKey &pubkey, const CKeyMetadata &metadata);
|
|
// Search metadata for a given username
|
|
bool GetKeyIdFromUsername(std::string username, CKeyID &keyid);
|
|
|
|
bool LoadMinVersion(int nVersion) { nWalletVersion = nVersion; nWalletMaxVersion = std::max(nWalletMaxVersion, nVersion); return true; }
|
|
|
|
// Adds an encrypted key to the store, and saves it to disk.
|
|
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 Unlock(const SecureString& strWalletPassphrase);
|
|
bool ChangeWalletPassphrase(const SecureString& strOldWalletPassphrase, const SecureString& strNewWalletPassphrase);
|
|
bool EncryptWallet(const SecureString& strWalletPassphrase);
|
|
|
|
void GetKeyBirthTimes(std::map<CKeyID, int64> &mapKeyBirth) const;
|
|
|
|
/** Increment the next transaction order id
|
|
@return next transaction order id
|
|
*/
|
|
int64 IncOrderPosNext(CWalletDB *pwalletdb = NULL);
|
|
|
|
void MarkDirty();
|
|
bool AddToWallet(const CWalletTx& wtxIn);
|
|
bool AddToWalletIfInvolvingMe(const uint256 &hash, const CTransaction& tx, const CBlock* pblock, bool fUpdate = false, bool fFindBlock = false);
|
|
bool EraseFromWallet(uint256 hash);
|
|
int ScanForWalletTransactions(CBlockIndex* pindexStart, bool fUpdate = false);
|
|
void ReacceptWalletTransactions();
|
|
void ResendWalletTransactions();
|
|
|
|
bool IsMine(const CTransaction& tx) const
|
|
{
|
|
return false;
|
|
}
|
|
bool IsFromMe(const CTransaction& tx) const
|
|
{
|
|
return false;
|
|
}
|
|
void SetBestChain(const CBlockLocator& loc);
|
|
|
|
DBErrors LoadWallet(bool& fFirstRunRet);
|
|
|
|
void UpdatedTransaction(const uint256 &hashTx);
|
|
|
|
void PrintWallet(const CBlock& block);
|
|
|
|
void Inventory(const uint256 &hash)
|
|
{
|
|
{
|
|
LOCK(cs_wallet);
|
|
std::map<uint256, int>::iterator mi = mapRequestCount.find(hash);
|
|
if (mi != mapRequestCount.end())
|
|
(*mi).second++;
|
|
}
|
|
}
|
|
|
|
bool GetTransaction(const uint256 &hashTx, CWalletTx& wtx);
|
|
|
|
bool SetDefaultKey(const CPubKey &vchPubKey);
|
|
|
|
// signify that a particular wallet feature is now used. this may change nWalletVersion and nWalletMaxVersion if those are lower
|
|
bool SetMinVersion(enum WalletFeature, CWalletDB* pwalletdbIn = NULL, bool fExplicit = false);
|
|
|
|
// change which version we're allowed to upgrade to (note that this does not immediately imply upgrading to that format)
|
|
bool SetMaxVersion(int nVersion);
|
|
|
|
// get the current wallet format (the oldest client version guaranteed to understand this wallet)
|
|
int GetVersion() { return nWalletVersion; }
|
|
|
|
/** Address book entry changed.
|
|
* @note called with lock cs_wallet held.
|
|
*/
|
|
boost::signals2::signal<void (CWallet *wallet, const CTxDestination &address, const std::string &label, bool isMine, ChangeType status)> NotifyAddressBookChanged;
|
|
|
|
/** Wallet transaction added, removed or updated.
|
|
* @note called with lock cs_wallet held.
|
|
*/
|
|
boost::signals2::signal<void (CWallet *wallet, const uint256 &hashTx, ChangeType status)> NotifyTransactionChanged;
|
|
};
|
|
|
|
|
|
typedef std::map<std::string, std::string> mapValue_t;
|
|
|
|
|
|
static void WriteOrderPos(const int64& nOrderPos, mapValue_t& mapValue)
|
|
{
|
|
if (nOrderPos == -1)
|
|
return;
|
|
mapValue["n"] = i64tostr(nOrderPos);
|
|
}
|
|
|
|
|
|
/** A transaction with a bunch of additional info that only the owner cares about.
|
|
* It includes any unrecorded transactions needed to link it back to the block chain.
|
|
*/
|
|
class CWalletTx : public CMerkleTx
|
|
{
|
|
private:
|
|
const CWallet* pwallet;
|
|
|
|
public:
|
|
mapValue_t mapValue;
|
|
std::vector<std::pair<std::string, std::string> > vOrderForm;
|
|
unsigned int fTimeReceivedIsTxTime;
|
|
unsigned int nTimeReceived; // time received by this node
|
|
unsigned int nTimeSmart;
|
|
char fFromMe;
|
|
std::string strFromAccount;
|
|
std::vector<char> vfSpent; // which outputs are already spent
|
|
int64 nOrderPos; // position in ordered transaction list
|
|
|
|
CWalletTx()
|
|
{
|
|
Init(NULL);
|
|
}
|
|
|
|
CWalletTx(const CWallet* pwalletIn)
|
|
{
|
|
Init(pwalletIn);
|
|
}
|
|
|
|
CWalletTx(const CWallet* pwalletIn, const CMerkleTx& txIn) : CMerkleTx(txIn)
|
|
{
|
|
Init(pwalletIn);
|
|
}
|
|
|
|
CWalletTx(const CWallet* pwalletIn, const CTransaction& txIn) : CMerkleTx(txIn)
|
|
{
|
|
Init(pwalletIn);
|
|
}
|
|
|
|
void Init(const CWallet* pwalletIn)
|
|
{
|
|
pwallet = pwalletIn;
|
|
mapValue.clear();
|
|
vOrderForm.clear();
|
|
fTimeReceivedIsTxTime = false;
|
|
nTimeReceived = 0;
|
|
nTimeSmart = 0;
|
|
fFromMe = false;
|
|
strFromAccount.clear();
|
|
vfSpent.clear();
|
|
nOrderPos = -1;
|
|
}
|
|
|
|
IMPLEMENT_SERIALIZE
|
|
(
|
|
CWalletTx* pthis = const_cast<CWalletTx*>(this);
|
|
if (fRead)
|
|
pthis->Init(NULL);
|
|
char fSpent = false;
|
|
|
|
if (!fRead)
|
|
{
|
|
pthis->mapValue["fromaccount"] = pthis->strFromAccount;
|
|
|
|
std::string str;
|
|
BOOST_FOREACH(char f, vfSpent)
|
|
{
|
|
str += (f ? '1' : '0');
|
|
if (f)
|
|
fSpent = true;
|
|
}
|
|
pthis->mapValue["spent"] = str;
|
|
|
|
WriteOrderPos(pthis->nOrderPos, pthis->mapValue);
|
|
|
|
if (nTimeSmart)
|
|
pthis->mapValue["timesmart"] = strprintf("%u", nTimeSmart);
|
|
}
|
|
|
|
nSerSize += SerReadWrite(s, *(CMerkleTx*)this, nType, nVersion,ser_action);
|
|
READWRITE(mapValue);
|
|
READWRITE(vOrderForm);
|
|
READWRITE(fTimeReceivedIsTxTime);
|
|
READWRITE(nTimeReceived);
|
|
READWRITE(fFromMe);
|
|
READWRITE(fSpent);
|
|
pthis->mapValue.erase("fromaccount");
|
|
pthis->mapValue.erase("version");
|
|
pthis->mapValue.erase("spent");
|
|
pthis->mapValue.erase("n");
|
|
pthis->mapValue.erase("timesmart");
|
|
)
|
|
|
|
void BindWallet(CWallet *pwalletIn)
|
|
{
|
|
pwallet = pwalletIn;
|
|
}
|
|
|
|
bool IsFromMe() const
|
|
{
|
|
return false;
|
|
}
|
|
|
|
bool IsConfirmed() const
|
|
{
|
|
if (GetDepthInMainChain() >= 1)
|
|
return true;
|
|
if (!IsFromMe()) // using wtx's cached debit
|
|
return false;
|
|
|
|
return true;
|
|
}
|
|
|
|
bool WriteToDisk();
|
|
|
|
int64 GetTxTime() const;
|
|
int GetRequestCount() const;
|
|
|
|
void AddSupportingTransactions();
|
|
bool AcceptWalletTransaction();
|
|
void RelayWalletTransaction();
|
|
};
|
|
|
|
|
|
|
|
|
|
class COutput
|
|
{
|
|
public:
|
|
const CWalletTx *tx;
|
|
int i;
|
|
int nDepth;
|
|
|
|
COutput(const CWalletTx *txIn, int iIn, int nDepthIn)
|
|
{
|
|
tx = txIn; i = iIn; nDepth = nDepthIn;
|
|
}
|
|
|
|
std::string ToString() const
|
|
{
|
|
return strprintf("COutput(%s, %d, %d)", tx->GetHash().ToString().c_str(), i, nDepth);
|
|
}
|
|
|
|
void print() const
|
|
{
|
|
printf("%s\n", ToString().c_str());
|
|
}
|
|
};
|
|
|
|
|
|
|
|
|
|
/** Private key that includes an expiration date in case it never gets used. */
|
|
class CWalletKey
|
|
{
|
|
public:
|
|
CPrivKey vchPrivKey;
|
|
int64 nTimeCreated;
|
|
int64 nTimeExpires;
|
|
std::string strComment;
|
|
//// todo: add something to note what created it (user, getnewaddress, change)
|
|
//// maybe should have a map<string, string> property map
|
|
|
|
CWalletKey(int64 nExpires=0)
|
|
{
|
|
nTimeCreated = (nExpires ? GetTime() : 0);
|
|
nTimeExpires = nExpires;
|
|
}
|
|
|
|
IMPLEMENT_SERIALIZE
|
|
(
|
|
if (!(nType & SER_GETHASH))
|
|
READWRITE(nVersion);
|
|
READWRITE(vchPrivKey);
|
|
READWRITE(nTimeCreated);
|
|
READWRITE(nTimeExpires);
|
|
READWRITE(strComment);
|
|
)
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Account information.
|
|
* Stored in wallet with key "acc"+string account name.
|
|
*/
|
|
class CAccount
|
|
{
|
|
public:
|
|
CPubKey vchPubKey;
|
|
|
|
CAccount()
|
|
{
|
|
SetNull();
|
|
}
|
|
|
|
void SetNull()
|
|
{
|
|
vchPubKey = CPubKey();
|
|
}
|
|
|
|
IMPLEMENT_SERIALIZE
|
|
(
|
|
if (!(nType & SER_GETHASH))
|
|
READWRITE(nVersion);
|
|
READWRITE(vchPubKey);
|
|
)
|
|
};
|
|
|
|
|
|
bool GetWalletFile(CWallet* pwallet, std::string &strWalletFileOut);
|
|
|
|
#endif
|