2017-11-09 19:34:51 +01:00
|
|
|
/*
|
|
|
|
|
* This file is part of the Flowee project
|
|
|
|
|
* Copyright (C) 2009-2010 Satoshi Nakamoto
|
|
|
|
|
* Copyright (C) 2009-2015 The Bitcoin Core developers
|
|
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
2013-04-13 00:13:08 -05:00
|
|
|
|
2018-01-16 10:47:52 +00:00
|
|
|
#ifndef FLOWEE_KEYSTORE_H
|
|
|
|
|
#define FLOWEE_KEYSTORE_H
|
2011-06-01 18:27:05 +02:00
|
|
|
|
2023-11-24 18:16:32 +01:00
|
|
|
#include <primitives/PrivateKey.h>
|
2023-11-24 18:01:36 +01:00
|
|
|
#include <primitives/PublicKey.h>
|
2019-03-13 14:50:11 +01:00
|
|
|
#include <primitives/script.h>
|
2012-05-11 17:00:03 +02:00
|
|
|
#include "sync.h"
|
2013-04-13 00:13:08 -05:00
|
|
|
|
2019-08-24 15:01:22 +02:00
|
|
|
#include <utils/support/allocators/secure.h>
|
|
|
|
|
|
2012-05-06 19:40:58 +02:00
|
|
|
#include <boost/signals2/signal.hpp>
|
2013-07-26 01:06:01 +02:00
|
|
|
#include <boost/variant.hpp>
|
2012-04-16 14:56:45 +02:00
|
|
|
|
2012-03-26 16:48:23 +02:00
|
|
|
/** A virtual base class for key stores */
|
2011-06-01 18:28:20 +02:00
|
|
|
class CKeyStore
|
|
|
|
|
{
|
2011-08-26 14:37:23 -04:00
|
|
|
protected:
|
2011-06-25 14:57:32 +02:00
|
|
|
mutable CCriticalSection cs_KeyStore;
|
|
|
|
|
|
2011-08-26 14:37:23 -04:00
|
|
|
public:
|
2012-04-15 12:59:20 +02:00
|
|
|
virtual ~CKeyStore() {}
|
|
|
|
|
|
2014-10-26 16:35:06 +08:00
|
|
|
//! Add a key to the store.
|
2022-07-06 22:12:33 +02:00
|
|
|
virtual bool AddKeyPubKey(const PrivateKey &key, const PublicKey &pubkey) =0;
|
|
|
|
|
virtual bool AddKey(const PrivateKey &key);
|
2011-11-07 00:05:42 +01:00
|
|
|
|
2014-10-26 16:35:06 +08:00
|
|
|
//! Check whether a key corresponding to a given address is present in the store.
|
2022-07-06 21:52:47 +02:00
|
|
|
virtual bool HaveKey(const KeyId &address) const =0;
|
2022-07-06 22:12:33 +02:00
|
|
|
virtual bool GetKey(const KeyId &address, PrivateKey& keyOut) const =0;
|
2022-07-06 21:52:47 +02:00
|
|
|
virtual void GetKeys(std::set<KeyId> &setAddress) const =0;
|
2022-07-06 21:56:34 +02:00
|
|
|
virtual bool GetPubKey(const KeyId &address, PublicKey& vchPubKeyOut) const =0;
|
2011-11-07 00:05:42 +01:00
|
|
|
|
2014-10-26 16:35:06 +08:00
|
|
|
//! Support for BIP 0013 : see https://github.com/bitcoin/bips/blob/master/bip-0013.mediawiki
|
2012-01-04 21:40:52 -05:00
|
|
|
virtual bool AddCScript(const CScript& redeemScript) =0;
|
2012-05-14 23:44:52 +02:00
|
|
|
virtual bool HaveCScript(const CScriptID &hash) const =0;
|
|
|
|
|
virtual bool GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const =0;
|
2013-07-26 01:06:01 +02:00
|
|
|
|
2014-10-26 16:35:06 +08:00
|
|
|
//! Support for Watch-only addresses
|
2014-06-09 21:11:59 +02:00
|
|
|
virtual bool AddWatchOnly(const CScript &dest) =0;
|
2014-07-26 21:05:11 +02:00
|
|
|
virtual bool RemoveWatchOnly(const CScript &dest) =0;
|
2014-06-09 21:11:59 +02:00
|
|
|
virtual bool HaveWatchOnly(const CScript &dest) const =0;
|
2014-07-26 21:05:11 +02:00
|
|
|
virtual bool HaveWatchOnly() const =0;
|
2011-06-25 14:57:32 +02:00
|
|
|
};
|
|
|
|
|
|
2022-07-06 22:12:33 +02:00
|
|
|
typedef std::map<KeyId, PrivateKey> KeyMap;
|
2022-07-06 21:56:34 +02:00
|
|
|
typedef std::map<KeyId, PublicKey> WatchKeyMap;
|
2012-05-14 23:44:52 +02:00
|
|
|
typedef std::map<CScriptID, CScript > ScriptMap;
|
2014-06-09 21:11:59 +02:00
|
|
|
typedef std::set<CScript> WatchOnlySet;
|
2011-07-08 15:47:35 +02:00
|
|
|
|
2012-03-26 16:48:23 +02:00
|
|
|
/** Basic key store, that keeps keys in an address->secret map */
|
2011-06-25 14:57:32 +02:00
|
|
|
class CBasicKeyStore : public CKeyStore
|
|
|
|
|
{
|
|
|
|
|
protected:
|
2011-07-08 15:47:35 +02:00
|
|
|
KeyMap mapKeys;
|
2015-06-10 01:04:08 -07:00
|
|
|
WatchKeyMap mapWatchKeys;
|
2011-11-08 13:20:29 -05:00
|
|
|
ScriptMap mapScripts;
|
2013-07-26 01:06:01 +02:00
|
|
|
WatchOnlySet setWatchOnly;
|
2011-06-25 14:57:32 +02:00
|
|
|
|
|
|
|
|
public:
|
2022-07-06 22:12:33 +02:00
|
|
|
bool AddKeyPubKey(const PrivateKey& key, const PublicKey &pubkey);
|
2022-07-06 21:56:34 +02:00
|
|
|
bool GetPubKey(const KeyId &address, PublicKey& vchPubKeyOut) const;
|
2022-07-06 21:52:47 +02:00
|
|
|
bool HaveKey(const KeyId &address) const
|
2011-06-01 18:28:20 +02:00
|
|
|
{
|
2011-08-26 14:37:23 -04:00
|
|
|
bool result;
|
2012-04-06 18:39:12 +02:00
|
|
|
{
|
|
|
|
|
LOCK(cs_KeyStore);
|
2011-08-26 14:37:23 -04:00
|
|
|
result = (mapKeys.count(address) > 0);
|
2012-04-06 18:39:12 +02:00
|
|
|
}
|
2011-08-26 14:37:23 -04:00
|
|
|
return result;
|
2011-06-01 18:28:20 +02:00
|
|
|
}
|
2022-07-06 21:52:47 +02:00
|
|
|
void GetKeys(std::set<KeyId> &setAddress) const
|
2011-07-11 21:30:40 +02:00
|
|
|
{
|
|
|
|
|
setAddress.clear();
|
|
|
|
|
{
|
2012-04-06 18:39:12 +02:00
|
|
|
LOCK(cs_KeyStore);
|
2011-07-11 21:30:40 +02:00
|
|
|
KeyMap::const_iterator mi = mapKeys.begin();
|
|
|
|
|
while (mi != mapKeys.end())
|
|
|
|
|
{
|
|
|
|
|
setAddress.insert((*mi).first);
|
|
|
|
|
mi++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-07-06 22:12:33 +02:00
|
|
|
bool GetKey(const KeyId &address, PrivateKey &keyOut) const
|
2011-06-01 18:28:20 +02:00
|
|
|
{
|
2011-06-19 18:32:36 +02:00
|
|
|
{
|
2012-04-06 18:39:12 +02:00
|
|
|
LOCK(cs_KeyStore);
|
2011-08-26 14:37:23 -04:00
|
|
|
KeyMap::const_iterator mi = mapKeys.find(address);
|
|
|
|
|
if (mi != mapKeys.end())
|
|
|
|
|
{
|
2013-05-01 06:52:05 +02:00
|
|
|
keyOut = mi->second;
|
2011-08-26 14:37:23 -04:00
|
|
|
return true;
|
|
|
|
|
}
|
2011-06-19 18:32:36 +02:00
|
|
|
}
|
|
|
|
|
return false;
|
2011-06-01 18:28:20 +02:00
|
|
|
}
|
2012-01-04 21:40:52 -05:00
|
|
|
virtual bool AddCScript(const CScript& redeemScript);
|
2012-05-14 23:44:52 +02:00
|
|
|
virtual bool HaveCScript(const CScriptID &hash) const;
|
|
|
|
|
virtual bool GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const;
|
2013-07-26 01:06:01 +02:00
|
|
|
|
2014-06-09 21:11:59 +02:00
|
|
|
virtual bool AddWatchOnly(const CScript &dest);
|
2014-07-26 21:05:11 +02:00
|
|
|
virtual bool RemoveWatchOnly(const CScript &dest);
|
2014-06-09 21:11:59 +02:00
|
|
|
virtual bool HaveWatchOnly(const CScript &dest) const;
|
2014-07-26 21:05:11 +02:00
|
|
|
virtual bool HaveWatchOnly() const;
|
2011-06-25 14:57:32 +02:00
|
|
|
};
|
|
|
|
|
|
2013-04-13 00:13:08 -05:00
|
|
|
typedef std::vector<unsigned char, secure_allocator<unsigned char> > CKeyingMaterial;
|
2022-07-06 21:56:34 +02:00
|
|
|
typedef std::map<KeyId, std::pair<PublicKey, std::vector<unsigned char> > > CryptedKeyMap;
|
2011-07-05 16:42:32 +02:00
|
|
|
|
2018-01-16 10:47:52 +00:00
|
|
|
#endif
|