/* * 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 . */ #include "keystore.h" #include // for MAX_SCRIPT_ELEMENT_SIZE bool CKeyStore::AddKey(const PrivateKey &key) { return AddKeyPubKey(key, key.getPubKey()); } bool CBasicKeyStore::GetPubKey(const KeyId &address, PublicKey &vchPubKeyOut) const { PrivateKey key; if (!GetKey(address, key)) { WatchKeyMap::const_iterator it = mapWatchKeys.find(address); if (it != mapWatchKeys.end()) { vchPubKeyOut = it->second; return true; } return false; } vchPubKeyOut = key.getPubKey(); return true; } bool CBasicKeyStore::AddKeyPubKey(const PrivateKey& key, const PublicKey &pubkey) { LOCK(cs_KeyStore); mapKeys[pubkey.getKeyId()] = key; return true; } bool CBasicKeyStore::AddCScript(const CScript& redeemScript) { if (redeemScript.size() > MAX_SCRIPT_ELEMENT_SIZE) { logCritical(Log::Internals) << "CBasicKeyStore::AddCScript(): redeemScripts >" << MAX_SCRIPT_ELEMENT_SIZE << "bytes are invalid"; return false; } LOCK(cs_KeyStore); mapScripts[CScriptID(redeemScript)] = redeemScript; return true; } bool CBasicKeyStore::HaveCScript(const CScriptID& hash) const { LOCK(cs_KeyStore); return mapScripts.count(hash) > 0; } bool CBasicKeyStore::GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const { LOCK(cs_KeyStore); ScriptMap::const_iterator mi = mapScripts.find(hash); if (mi != mapScripts.end()) { redeemScriptOut = (*mi).second; return true; } return false; } static bool ExtractPubKey(const CScript &dest, PublicKey& pubKeyOut) { //TODO: Use Solver to extract this? CScript::const_iterator pc = dest.begin(); opcodetype opcode; std::vector vch; if (!dest.GetOp(pc, opcode, vch) ||!PublicKeyUtils::isValidSize(vch)) return false; pubKeyOut = PublicKey(vch); if (!pubKeyOut.isFullyValid()) return false; if (!dest.GetOp(pc, opcode, vch) || opcode != OP_CHECKSIG || dest.GetOp(pc, opcode, vch)) return false; return true; } bool CBasicKeyStore::AddWatchOnly(const CScript &dest) { LOCK(cs_KeyStore); setWatchOnly.insert(dest); PublicKey pubKey; if (ExtractPubKey(dest, pubKey)) mapWatchKeys[pubKey.getKeyId()] = pubKey; return true; } bool CBasicKeyStore::RemoveWatchOnly(const CScript &dest) { LOCK(cs_KeyStore); setWatchOnly.erase(dest); PublicKey pubKey; if (ExtractPubKey(dest, pubKey)) mapWatchKeys.erase(pubKey.getKeyId()); return true; } bool CBasicKeyStore::HaveWatchOnly(const CScript &dest) const { LOCK(cs_KeyStore); return setWatchOnly.count(dest) > 0; } bool CBasicKeyStore::HaveWatchOnly() const { LOCK(cs_KeyStore); return (!setWatchOnly.empty()); }