1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
12 bool CKeyStore::AddKey(const CKey
&key
) {
13 return AddKeyPubKey(key
, key
.GetPubKey());
16 bool CBasicKeyStore::GetPubKey(const CKeyID
&address
, CPubKey
&vchPubKeyOut
) const
19 if (!GetKey(address
, key
)) {
21 WatchKeyMap::const_iterator it
= mapWatchKeys
.find(address
);
22 if (it
!= mapWatchKeys
.end()) {
23 vchPubKeyOut
= it
->second
;
28 vchPubKeyOut
= key
.GetPubKey();
32 bool CBasicKeyStore::AddKeyPubKey(const CKey
& key
, const CPubKey
&pubkey
)
35 mapKeys
[pubkey
.GetID()] = key
;
39 bool CBasicKeyStore::HaveKey(const CKeyID
&address
) const
42 return mapKeys
.count(address
) > 0;
45 std::set
<CKeyID
> CBasicKeyStore::GetKeys() const
48 std::set
<CKeyID
> set_address
;
49 for (const auto& mi
: mapKeys
) {
50 set_address
.insert(mi
.first
);
55 bool CBasicKeyStore::GetKey(const CKeyID
&address
, CKey
&keyOut
) const
58 KeyMap::const_iterator mi
= mapKeys
.find(address
);
59 if (mi
!= mapKeys
.end()) {
66 bool CBasicKeyStore::AddCScript(const CScript
& redeemScript
)
68 if (redeemScript
.size() > MAX_SCRIPT_ELEMENT_SIZE
)
69 return error("CBasicKeyStore::AddCScript(): redeemScripts > %i bytes are invalid", MAX_SCRIPT_ELEMENT_SIZE
);
72 mapScripts
[CScriptID(redeemScript
)] = redeemScript
;
76 bool CBasicKeyStore::HaveCScript(const CScriptID
& hash
) const
79 return mapScripts
.count(hash
) > 0;
82 bool CBasicKeyStore::GetCScript(const CScriptID
&hash
, CScript
& redeemScriptOut
) const
85 ScriptMap::const_iterator mi
= mapScripts
.find(hash
);
86 if (mi
!= mapScripts
.end())
88 redeemScriptOut
= (*mi
).second
;
94 static bool ExtractPubKey(const CScript
&dest
, CPubKey
& pubKeyOut
)
96 //TODO: Use Solver to extract this?
97 CScript::const_iterator pc
= dest
.begin();
99 std::vector
<unsigned char> vch
;
100 if (!dest
.GetOp(pc
, opcode
, vch
) || vch
.size() < 33 || vch
.size() > 65)
102 pubKeyOut
= CPubKey(vch
);
103 if (!pubKeyOut
.IsFullyValid())
105 if (!dest
.GetOp(pc
, opcode
, vch
) || opcode
!= OP_CHECKSIG
|| dest
.GetOp(pc
, opcode
, vch
))
110 bool CBasicKeyStore::AddWatchOnly(const CScript
&dest
)
113 setWatchOnly
.insert(dest
);
115 if (ExtractPubKey(dest
, pubKey
))
116 mapWatchKeys
[pubKey
.GetID()] = pubKey
;
120 bool CBasicKeyStore::RemoveWatchOnly(const CScript
&dest
)
123 setWatchOnly
.erase(dest
);
125 if (ExtractPubKey(dest
, pubKey
))
126 mapWatchKeys
.erase(pubKey
.GetID());
130 bool CBasicKeyStore::HaveWatchOnly(const CScript
&dest
) const
133 return setWatchOnly
.count(dest
) > 0;
136 bool CBasicKeyStore::HaveWatchOnly() const
139 return (!setWatchOnly
.empty());