1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2017 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.
10 bool CKeyStore::AddKey(const CKey
&key
) {
11 return AddKeyPubKey(key
, key
.GetPubKey());
14 bool CBasicKeyStore::GetPubKey(const CKeyID
&address
, CPubKey
&vchPubKeyOut
) const
17 if (!GetKey(address
, key
)) {
19 WatchKeyMap::const_iterator it
= mapWatchKeys
.find(address
);
20 if (it
!= mapWatchKeys
.end()) {
21 vchPubKeyOut
= it
->second
;
26 vchPubKeyOut
= key
.GetPubKey();
30 bool CBasicKeyStore::AddKeyPubKey(const CKey
& key
, const CPubKey
&pubkey
)
33 mapKeys
[pubkey
.GetID()] = key
;
37 bool CBasicKeyStore::HaveKey(const CKeyID
&address
) const
40 return mapKeys
.count(address
) > 0;
43 std::set
<CKeyID
> CBasicKeyStore::GetKeys() const
46 std::set
<CKeyID
> set_address
;
47 for (const auto& mi
: mapKeys
) {
48 set_address
.insert(mi
.first
);
53 bool CBasicKeyStore::GetKey(const CKeyID
&address
, CKey
&keyOut
) const
56 KeyMap::const_iterator mi
= mapKeys
.find(address
);
57 if (mi
!= mapKeys
.end()) {
64 bool CBasicKeyStore::AddCScript(const CScript
& redeemScript
)
66 if (redeemScript
.size() > MAX_SCRIPT_ELEMENT_SIZE
)
67 return error("CBasicKeyStore::AddCScript(): redeemScripts > %i bytes are invalid", MAX_SCRIPT_ELEMENT_SIZE
);
70 mapScripts
[CScriptID(redeemScript
)] = redeemScript
;
74 bool CBasicKeyStore::HaveCScript(const CScriptID
& hash
) const
77 return mapScripts
.count(hash
) > 0;
80 std::set
<CScriptID
> CBasicKeyStore::GetCScripts() const
83 std::set
<CScriptID
> set_script
;
84 for (const auto& mi
: mapScripts
) {
85 set_script
.insert(mi
.first
);
90 bool CBasicKeyStore::GetCScript(const CScriptID
&hash
, CScript
& redeemScriptOut
) const
93 ScriptMap::const_iterator mi
= mapScripts
.find(hash
);
94 if (mi
!= mapScripts
.end())
96 redeemScriptOut
= (*mi
).second
;
102 static bool ExtractPubKey(const CScript
&dest
, CPubKey
& pubKeyOut
)
104 //TODO: Use Solver to extract this?
105 CScript::const_iterator pc
= dest
.begin();
107 std::vector
<unsigned char> vch
;
108 if (!dest
.GetOp(pc
, opcode
, vch
) || vch
.size() < 33 || vch
.size() > 65)
110 pubKeyOut
= CPubKey(vch
);
111 if (!pubKeyOut
.IsFullyValid())
113 if (!dest
.GetOp(pc
, opcode
, vch
) || opcode
!= OP_CHECKSIG
|| dest
.GetOp(pc
, opcode
, vch
))
118 bool CBasicKeyStore::AddWatchOnly(const CScript
&dest
)
121 setWatchOnly
.insert(dest
);
123 if (ExtractPubKey(dest
, pubKey
))
124 mapWatchKeys
[pubKey
.GetID()] = pubKey
;
128 bool CBasicKeyStore::RemoveWatchOnly(const CScript
&dest
)
131 setWatchOnly
.erase(dest
);
133 if (ExtractPubKey(dest
, pubKey
))
134 mapWatchKeys
.erase(pubKey
.GetID());
138 bool CBasicKeyStore::HaveWatchOnly(const CScript
&dest
) const
141 return setWatchOnly
.count(dest
) > 0;
144 bool CBasicKeyStore::HaveWatchOnly() const
147 return (!setWatchOnly
.empty());