2d0aec2cd7
This follows the coding style guideline that the file that contains a
class should follow the exact name of this (main) class.
key.{h|cpp} -> PrivateKey.{h|cpp}
130 lines
4.3 KiB
C++
130 lines
4.3 KiB
C++
/*
|
|
* 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/>.
|
|
*/
|
|
|
|
#ifndef FLOWEE_KEYSTORE_H
|
|
#define FLOWEE_KEYSTORE_H
|
|
|
|
#include <primitives/PrivateKey.h>
|
|
#include <primitives/PublicKey.h>
|
|
#include <primitives/script.h>
|
|
#include "sync.h"
|
|
|
|
#include <utils/support/allocators/secure.h>
|
|
|
|
#include <boost/signals2/signal.hpp>
|
|
#include <boost/variant.hpp>
|
|
|
|
/** A virtual base class for key stores */
|
|
class CKeyStore
|
|
{
|
|
protected:
|
|
mutable CCriticalSection cs_KeyStore;
|
|
|
|
public:
|
|
virtual ~CKeyStore() {}
|
|
|
|
//! Add a key to the store.
|
|
virtual bool AddKeyPubKey(const PrivateKey &key, const PublicKey &pubkey) =0;
|
|
virtual bool AddKey(const PrivateKey &key);
|
|
|
|
//! Check whether a key corresponding to a given address is present in the store.
|
|
virtual bool HaveKey(const KeyId &address) const =0;
|
|
virtual bool GetKey(const KeyId &address, PrivateKey& keyOut) const =0;
|
|
virtual void GetKeys(std::set<KeyId> &setAddress) const =0;
|
|
virtual bool GetPubKey(const KeyId &address, PublicKey& vchPubKeyOut) const =0;
|
|
|
|
//! Support for BIP 0013 : see https://github.com/bitcoin/bips/blob/master/bip-0013.mediawiki
|
|
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;
|
|
|
|
//! Support for Watch-only addresses
|
|
virtual bool AddWatchOnly(const CScript &dest) =0;
|
|
virtual bool RemoveWatchOnly(const CScript &dest) =0;
|
|
virtual bool HaveWatchOnly(const CScript &dest) const =0;
|
|
virtual bool HaveWatchOnly() const =0;
|
|
};
|
|
|
|
typedef std::map<KeyId, PrivateKey> KeyMap;
|
|
typedef std::map<KeyId, PublicKey> WatchKeyMap;
|
|
typedef std::map<CScriptID, CScript > ScriptMap;
|
|
typedef std::set<CScript> WatchOnlySet;
|
|
|
|
/** Basic key store, that keeps keys in an address->secret map */
|
|
class CBasicKeyStore : public CKeyStore
|
|
{
|
|
protected:
|
|
KeyMap mapKeys;
|
|
WatchKeyMap mapWatchKeys;
|
|
ScriptMap mapScripts;
|
|
WatchOnlySet setWatchOnly;
|
|
|
|
public:
|
|
bool AddKeyPubKey(const PrivateKey& key, const PublicKey &pubkey);
|
|
bool GetPubKey(const KeyId &address, PublicKey& vchPubKeyOut) const;
|
|
bool HaveKey(const KeyId &address) const
|
|
{
|
|
bool result;
|
|
{
|
|
LOCK(cs_KeyStore);
|
|
result = (mapKeys.count(address) > 0);
|
|
}
|
|
return result;
|
|
}
|
|
void GetKeys(std::set<KeyId> &setAddress) const
|
|
{
|
|
setAddress.clear();
|
|
{
|
|
LOCK(cs_KeyStore);
|
|
KeyMap::const_iterator mi = mapKeys.begin();
|
|
while (mi != mapKeys.end())
|
|
{
|
|
setAddress.insert((*mi).first);
|
|
mi++;
|
|
}
|
|
}
|
|
}
|
|
bool GetKey(const KeyId &address, PrivateKey &keyOut) const
|
|
{
|
|
{
|
|
LOCK(cs_KeyStore);
|
|
KeyMap::const_iterator mi = mapKeys.find(address);
|
|
if (mi != mapKeys.end())
|
|
{
|
|
keyOut = mi->second;
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
virtual bool AddCScript(const CScript& redeemScript);
|
|
virtual bool HaveCScript(const CScriptID &hash) const;
|
|
virtual bool GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const;
|
|
|
|
virtual bool AddWatchOnly(const CScript &dest);
|
|
virtual bool RemoveWatchOnly(const CScript &dest);
|
|
virtual bool HaveWatchOnly(const CScript &dest) const;
|
|
virtual bool HaveWatchOnly() const;
|
|
};
|
|
|
|
typedef std::vector<unsigned char, secure_allocator<unsigned char> > CKeyingMaterial;
|
|
typedef std::map<KeyId, std::pair<PublicKey, std::vector<unsigned char> > > CryptedKeyMap;
|
|
|
|
#endif
|