2011-08-09 07:27:58 -04:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2018-01-02 14:12:05 -03:00
|
|
|
// Copyright (c) 2009-2017 The Bitcoin Core developers
|
2014-10-26 05:35:06 -03:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2012-05-18 10:02:28 -04:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2013-04-13 02:13:08 -03:00
|
|
|
|
2011-06-01 12:27:05 -04:00
|
|
|
#ifndef BITCOIN_KEYSTORE_H
|
|
|
|
#define BITCOIN_KEYSTORE_H
|
|
|
|
|
2017-11-09 21:57:53 -03:00
|
|
|
#include <key.h>
|
|
|
|
#include <pubkey.h>
|
|
|
|
#include <script/script.h>
|
2018-03-17 23:19:09 -03:00
|
|
|
#include <script/sign.h>
|
2017-11-09 21:57:53 -03:00
|
|
|
#include <script/standard.h>
|
|
|
|
#include <sync.h>
|
2013-04-13 02:13:08 -03:00
|
|
|
|
2012-05-06 13:40:58 -04:00
|
|
|
#include <boost/signals2/signal.hpp>
|
2012-04-16 09:56:45 -03:00
|
|
|
|
2012-03-26 11:48:23 -03:00
|
|
|
/** A virtual base class for key stores */
|
2018-03-17 23:19:09 -03:00
|
|
|
class CKeyStore : public SigningProvider
|
CWallet class
* A new class CKeyStore manages private keys, and script.cpp depends on access to CKeyStore.
* A new class CWallet extends CKeyStore, and contains all former wallet-specific globals; CWallet depends on script.cpp, not the other way around.
* Wallet-specific functions in CTransaction/CTxIn/CTxOut (GetDebit, GetCredit, GetChange, IsMine, IsFromMe), are moved to CWallet, taking their former 'this' argument as an explicit parameter
* CWalletTx objects know which CWallet they belong to, for convenience, so they have their own direct (and caching) GetDebit/... functions.
* Some code was moved from CWalletDB to CWallet, such as handling of reserve keys.
* Main.cpp keeps a set of all 'registered' wallets, which should be informed about updates to the block chain, and does not have any notion about any 'main' wallet. Function in main.cpp that require a wallet (such as GenerateCoins), take an explicit CWallet* argument.
* The actual CWallet instance used by the application is defined in init.cpp as "CWallet* pwalletMain". rpc.cpp and ui.cpp use this variable.
* Functions in main.cpp and db.cpp that are not used by other modules are marked static.
* The code for handling the 'submitorder' message is removed, as it not really compatible with the idea that a node is independent from the wallet(s) connected to it, and obsolete anyway.
2011-06-01 12:28:20 -04:00
|
|
|
{
|
2011-08-26 15:37:23 -03:00
|
|
|
public:
|
2014-10-26 05:35:06 -03:00
|
|
|
//! Add a key to the store.
|
2013-05-01 00:52:05 -04:00
|
|
|
virtual bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey) =0;
|
2011-11-06 20:05:42 -03:00
|
|
|
|
2014-10-26 05:35:06 -03:00
|
|
|
//! Check whether a key corresponding to a given address is present in the store.
|
2012-05-14 17:44:52 -04:00
|
|
|
virtual bool HaveKey(const CKeyID &address) const =0;
|
2017-07-23 17:32:57 -04:00
|
|
|
virtual std::set<CKeyID> GetKeys() const =0;
|
2011-11-06 20:05:42 -03:00
|
|
|
|
2014-10-26 05:35:06 -03:00
|
|
|
//! Support for BIP 0013 : see https://github.com/bitcoin/bips/blob/master/bip-0013.mediawiki
|
2012-01-04 23:40:52 -03:00
|
|
|
virtual bool AddCScript(const CScript& redeemScript) =0;
|
2012-05-14 17:44:52 -04:00
|
|
|
virtual bool HaveCScript(const CScriptID &hash) const =0;
|
2017-11-11 04:49:27 -03:00
|
|
|
virtual std::set<CScriptID> GetCScripts() const =0;
|
2013-07-25 19:06:01 -04:00
|
|
|
|
2014-10-26 05:35:06 -03:00
|
|
|
//! Support for Watch-only addresses
|
2014-06-09 15:11:59 -04:00
|
|
|
virtual bool AddWatchOnly(const CScript &dest) =0;
|
2014-07-26 15:05:11 -04:00
|
|
|
virtual bool RemoveWatchOnly(const CScript &dest) =0;
|
2014-06-09 15:11:59 -04:00
|
|
|
virtual bool HaveWatchOnly(const CScript &dest) const =0;
|
2014-07-26 15:05:11 -04:00
|
|
|
virtual bool HaveWatchOnly() const =0;
|
2011-06-25 08:57:32 -04:00
|
|
|
};
|
|
|
|
|
2012-03-26 11:48:23 -03:00
|
|
|
/** Basic key store, that keeps keys in an address->secret map */
|
2011-06-25 08:57:32 -04:00
|
|
|
class CBasicKeyStore : public CKeyStore
|
|
|
|
{
|
|
|
|
protected:
|
2018-03-22 21:57:33 -03:00
|
|
|
mutable CCriticalSection cs_KeyStore;
|
|
|
|
|
2018-07-10 14:55:53 -04:00
|
|
|
using KeyMap = std::map<CKeyID, CKey>;
|
|
|
|
using WatchKeyMap = std::map<CKeyID, CPubKey>;
|
|
|
|
using ScriptMap = std::map<CScriptID, CScript>;
|
|
|
|
using WatchOnlySet = std::set<CScript>;
|
|
|
|
|
2018-04-29 15:14:27 -03:00
|
|
|
KeyMap mapKeys GUARDED_BY(cs_KeyStore);
|
|
|
|
WatchKeyMap mapWatchKeys GUARDED_BY(cs_KeyStore);
|
|
|
|
ScriptMap mapScripts GUARDED_BY(cs_KeyStore);
|
|
|
|
WatchOnlySet setWatchOnly GUARDED_BY(cs_KeyStore);
|
2011-06-25 08:57:32 -04:00
|
|
|
|
2018-04-25 17:27:36 -03:00
|
|
|
void ImplicitlyLearnRelatedKeyScripts(const CPubKey& pubkey) EXCLUSIVE_LOCKS_REQUIRED(cs_KeyStore);
|
2017-11-30 21:49:04 -03:00
|
|
|
|
2011-06-25 08:57:32 -04:00
|
|
|
public:
|
2017-06-20 15:58:56 -04:00
|
|
|
bool AddKeyPubKey(const CKey& key, const CPubKey &pubkey) override;
|
2018-03-22 21:57:09 -03:00
|
|
|
bool AddKey(const CKey &key) { return AddKeyPubKey(key, key.GetPubKey()); }
|
2017-06-20 15:58:56 -04:00
|
|
|
bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const override;
|
2017-09-07 17:24:20 -03:00
|
|
|
bool HaveKey(const CKeyID &address) const override;
|
|
|
|
std::set<CKeyID> GetKeys() const override;
|
|
|
|
bool GetKey(const CKeyID &address, CKey &keyOut) const override;
|
2017-06-29 06:57:45 -04:00
|
|
|
bool AddCScript(const CScript& redeemScript) override;
|
|
|
|
bool HaveCScript(const CScriptID &hash) const override;
|
2017-11-11 04:49:27 -03:00
|
|
|
std::set<CScriptID> GetCScripts() const override;
|
2017-06-29 06:57:45 -04:00
|
|
|
bool GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const override;
|
2013-07-25 19:06:01 -04:00
|
|
|
|
2017-06-29 06:57:45 -04:00
|
|
|
bool AddWatchOnly(const CScript &dest) override;
|
|
|
|
bool RemoveWatchOnly(const CScript &dest) override;
|
|
|
|
bool HaveWatchOnly(const CScript &dest) const override;
|
|
|
|
bool HaveWatchOnly() const override;
|
2011-06-25 08:57:32 -04:00
|
|
|
};
|
|
|
|
|
2017-11-30 21:48:41 -03:00
|
|
|
/** Return the CKeyID of the key involved in a script (if there is a unique one). */
|
|
|
|
CKeyID GetKeyForDestination(const CKeyStore& store, const CTxDestination& dest);
|
|
|
|
|
2018-04-12 18:04:49 -03:00
|
|
|
/** Checks if a CKey is in the given CKeyStore compressed or otherwise*/
|
|
|
|
bool HaveKey(const CKeyStore& store, const CKey& key);
|
|
|
|
|
2014-08-28 16:21:03 -04:00
|
|
|
#endif // BITCOIN_KEYSTORE_H
|