2011-08-09 13:27:58 +02:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2018-01-02 18:12:05 +01:00
|
|
|
// Copyright (c) 2009-2017 The Bitcoin Core developers
|
2014-10-26 09:35:06 +01:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2012-05-18 16:02:28 +02:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2011-06-01 18:27:05 +02:00
|
|
|
|
2017-11-10 01:57:53 +01:00
|
|
|
#include <keystore.h>
|
2013-04-13 07:13:08 +02:00
|
|
|
|
2017-11-10 01:57:53 +01:00
|
|
|
#include <util.h>
|
2011-06-01 18:27:05 +02:00
|
|
|
|
2017-12-01 01:49:04 +01:00
|
|
|
void CBasicKeyStore::ImplicitlyLearnRelatedKeyScripts(const CPubKey& pubkey)
|
|
|
|
{
|
|
|
|
AssertLockHeld(cs_KeyStore);
|
|
|
|
CKeyID key_id = pubkey.GetID();
|
|
|
|
// We must actually know about this key already.
|
|
|
|
assert(HaveKey(key_id) || mapWatchKeys.count(key_id));
|
|
|
|
// This adds the redeemscripts necessary to detect P2WPKH and P2SH-P2WPKH
|
|
|
|
// outputs. Technically P2WPKH outputs don't have a redeemscript to be
|
|
|
|
// spent. However, our current IsMine logic requires the corresponding
|
|
|
|
// P2SH-P2WPKH redeemscript to be present in the wallet in order to accept
|
|
|
|
// payment even to P2WPKH outputs.
|
|
|
|
// Also note that having superfluous scripts in the keystore never hurts.
|
|
|
|
// They're only used to guide recursion in signing and IsMine logic - if
|
|
|
|
// a script is present but we can't do anything with it, it has no effect.
|
|
|
|
// "Implicitly" refers to fact that scripts are derived automatically from
|
|
|
|
// existing keys, and are present in memory, even without being explicitly
|
|
|
|
// loaded (e.g. from a file).
|
|
|
|
if (pubkey.IsCompressed()) {
|
|
|
|
CScript script = GetScriptForDestination(WitnessV0KeyHash(key_id));
|
|
|
|
// This does not use AddCScript, as it may be overridden.
|
|
|
|
CScriptID id(script);
|
|
|
|
mapScripts[id] = std::move(script);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-10 10:04:08 +02:00
|
|
|
bool CBasicKeyStore::GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const
|
2011-07-05 16:42:32 +02:00
|
|
|
{
|
|
|
|
CKey key;
|
2015-06-10 10:04:08 +02:00
|
|
|
if (!GetKey(address, key)) {
|
2016-04-19 23:03:42 +02:00
|
|
|
LOCK(cs_KeyStore);
|
2015-06-10 10:04:08 +02:00
|
|
|
WatchKeyMap::const_iterator it = mapWatchKeys.find(address);
|
|
|
|
if (it != mapWatchKeys.end()) {
|
|
|
|
vchPubKeyOut = it->second;
|
|
|
|
return true;
|
|
|
|
}
|
2011-07-05 16:42:32 +02:00
|
|
|
return false;
|
2015-06-10 10:04:08 +02:00
|
|
|
}
|
2011-07-05 16:42:32 +02:00
|
|
|
vchPubKeyOut = key.GetPubKey();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-05-01 06:52:05 +02:00
|
|
|
bool CBasicKeyStore::AddKeyPubKey(const CKey& key, const CPubKey &pubkey)
|
2011-06-01 18:27:05 +02:00
|
|
|
{
|
2013-05-01 06:52:05 +02:00
|
|
|
LOCK(cs_KeyStore);
|
|
|
|
mapKeys[pubkey.GetID()] = key;
|
2017-12-01 01:49:04 +01:00
|
|
|
ImplicitlyLearnRelatedKeyScripts(pubkey);
|
2011-06-25 14:57:32 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-09-07 22:24:20 +02:00
|
|
|
bool CBasicKeyStore::HaveKey(const CKeyID &address) const
|
|
|
|
{
|
|
|
|
LOCK(cs_KeyStore);
|
|
|
|
return mapKeys.count(address) > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::set<CKeyID> CBasicKeyStore::GetKeys() const
|
|
|
|
{
|
|
|
|
LOCK(cs_KeyStore);
|
|
|
|
std::set<CKeyID> set_address;
|
|
|
|
for (const auto& mi : mapKeys) {
|
|
|
|
set_address.insert(mi.first);
|
|
|
|
}
|
|
|
|
return set_address;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CBasicKeyStore::GetKey(const CKeyID &address, CKey &keyOut) const
|
|
|
|
{
|
2017-10-07 05:56:43 +02:00
|
|
|
LOCK(cs_KeyStore);
|
|
|
|
KeyMap::const_iterator mi = mapKeys.find(address);
|
|
|
|
if (mi != mapKeys.end()) {
|
|
|
|
keyOut = mi->second;
|
|
|
|
return true;
|
2017-09-07 22:24:20 +02:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-01-05 03:40:52 +01:00
|
|
|
bool CBasicKeyStore::AddCScript(const CScript& redeemScript)
|
2011-10-03 19:05:43 +02:00
|
|
|
{
|
2014-03-11 03:43:15 +01:00
|
|
|
if (redeemScript.size() > MAX_SCRIPT_ELEMENT_SIZE)
|
2015-01-08 11:44:25 +01:00
|
|
|
return error("CBasicKeyStore::AddCScript(): redeemScripts > %i bytes are invalid", MAX_SCRIPT_ELEMENT_SIZE);
|
2014-03-11 03:43:15 +01:00
|
|
|
|
2013-05-01 06:52:05 +02:00
|
|
|
LOCK(cs_KeyStore);
|
2014-09-25 04:24:46 +02:00
|
|
|
mapScripts[CScriptID(redeemScript)] = redeemScript;
|
2011-10-03 19:05:43 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-05-14 23:44:52 +02:00
|
|
|
bool CBasicKeyStore::HaveCScript(const CScriptID& hash) const
|
2011-10-03 19:05:43 +02:00
|
|
|
{
|
2013-05-01 06:52:05 +02:00
|
|
|
LOCK(cs_KeyStore);
|
|
|
|
return mapScripts.count(hash) > 0;
|
2011-10-03 19:05:43 +02:00
|
|
|
}
|
|
|
|
|
2017-11-11 08:49:27 +01:00
|
|
|
std::set<CScriptID> CBasicKeyStore::GetCScripts() const
|
|
|
|
{
|
|
|
|
LOCK(cs_KeyStore);
|
|
|
|
std::set<CScriptID> set_script;
|
|
|
|
for (const auto& mi : mapScripts) {
|
|
|
|
set_script.insert(mi.first);
|
|
|
|
}
|
|
|
|
return set_script;
|
|
|
|
}
|
|
|
|
|
2012-05-14 23:44:52 +02:00
|
|
|
bool CBasicKeyStore::GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const
|
2011-10-03 19:05:43 +02:00
|
|
|
{
|
2013-05-01 06:52:05 +02:00
|
|
|
LOCK(cs_KeyStore);
|
|
|
|
ScriptMap::const_iterator mi = mapScripts.find(hash);
|
|
|
|
if (mi != mapScripts.end())
|
2011-10-03 19:05:43 +02:00
|
|
|
{
|
2013-05-01 06:52:05 +02:00
|
|
|
redeemScriptOut = (*mi).second;
|
|
|
|
return true;
|
2011-10-03 19:05:43 +02:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-06-10 10:04:08 +02:00
|
|
|
static bool ExtractPubKey(const CScript &dest, CPubKey& pubKeyOut)
|
|
|
|
{
|
|
|
|
//TODO: Use Solver to extract this?
|
|
|
|
CScript::const_iterator pc = dest.begin();
|
|
|
|
opcodetype opcode;
|
|
|
|
std::vector<unsigned char> vch;
|
2018-02-16 21:28:03 +01:00
|
|
|
if (!dest.GetOp(pc, opcode, vch) || !CPubKey::ValidSize(vch))
|
2015-06-10 10:04:08 +02:00
|
|
|
return false;
|
|
|
|
pubKeyOut = CPubKey(vch);
|
|
|
|
if (!pubKeyOut.IsFullyValid())
|
|
|
|
return false;
|
|
|
|
if (!dest.GetOp(pc, opcode, vch) || opcode != OP_CHECKSIG || dest.GetOp(pc, opcode, vch))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-06-09 21:11:59 +02:00
|
|
|
bool CBasicKeyStore::AddWatchOnly(const CScript &dest)
|
2013-07-26 01:06:01 +02:00
|
|
|
{
|
|
|
|
LOCK(cs_KeyStore);
|
|
|
|
setWatchOnly.insert(dest);
|
2015-06-10 10:04:08 +02:00
|
|
|
CPubKey pubKey;
|
2017-12-01 01:49:04 +01:00
|
|
|
if (ExtractPubKey(dest, pubKey)) {
|
2015-06-10 10:04:08 +02:00
|
|
|
mapWatchKeys[pubKey.GetID()] = pubKey;
|
2017-12-01 01:49:04 +01:00
|
|
|
ImplicitlyLearnRelatedKeyScripts(pubKey);
|
|
|
|
}
|
2013-07-26 01:06:01 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-07-26 21:05:11 +02:00
|
|
|
bool CBasicKeyStore::RemoveWatchOnly(const CScript &dest)
|
|
|
|
{
|
|
|
|
LOCK(cs_KeyStore);
|
|
|
|
setWatchOnly.erase(dest);
|
2015-06-10 10:04:08 +02:00
|
|
|
CPubKey pubKey;
|
2017-12-01 01:49:04 +01:00
|
|
|
if (ExtractPubKey(dest, pubKey)) {
|
2015-06-10 10:04:08 +02:00
|
|
|
mapWatchKeys.erase(pubKey.GetID());
|
2017-12-01 01:49:04 +01:00
|
|
|
}
|
|
|
|
// Related CScripts are not removed; having superfluous scripts around is
|
|
|
|
// harmless (see comment in ImplicitlyLearnRelatedKeyScripts).
|
2014-07-26 21:05:11 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-06-09 21:11:59 +02:00
|
|
|
bool CBasicKeyStore::HaveWatchOnly(const CScript &dest) const
|
2013-07-26 01:06:01 +02:00
|
|
|
{
|
|
|
|
LOCK(cs_KeyStore);
|
|
|
|
return setWatchOnly.count(dest) > 0;
|
|
|
|
}
|
2014-07-26 21:05:11 +02:00
|
|
|
|
|
|
|
bool CBasicKeyStore::HaveWatchOnly() const
|
|
|
|
{
|
|
|
|
LOCK(cs_KeyStore);
|
|
|
|
return (!setWatchOnly.empty());
|
|
|
|
}
|
2017-12-01 01:48:41 +01:00
|
|
|
|
|
|
|
CKeyID GetKeyForDestination(const CKeyStore& store, const CTxDestination& dest)
|
|
|
|
{
|
|
|
|
// Only supports destinations which map to single public keys, i.e. P2PKH,
|
|
|
|
// P2WPKH, and P2SH-P2WPKH.
|
|
|
|
if (auto id = boost::get<CKeyID>(&dest)) {
|
|
|
|
return *id;
|
|
|
|
}
|
|
|
|
if (auto witness_id = boost::get<WitnessV0KeyHash>(&dest)) {
|
|
|
|
return CKeyID(*witness_id);
|
|
|
|
}
|
|
|
|
if (auto script_id = boost::get<CScriptID>(&dest)) {
|
|
|
|
CScript script;
|
|
|
|
CTxDestination inner_dest;
|
|
|
|
if (store.GetCScript(*script_id, script) && ExtractDestination(script, inner_dest)) {
|
|
|
|
if (auto inner_witness_id = boost::get<WitnessV0KeyHash>(&inner_dest)) {
|
|
|
|
return CKeyID(*inner_witness_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return CKeyID();
|
|
|
|
}
|