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::AddCScript(const CScript
& redeemScript
)
41 if (redeemScript
.size() > MAX_SCRIPT_ELEMENT_SIZE
)
42 return error("CBasicKeyStore::AddCScript(): redeemScripts > %i bytes are invalid", MAX_SCRIPT_ELEMENT_SIZE
);
45 mapScripts
[CScriptID(redeemScript
)] = redeemScript
;
49 bool CBasicKeyStore::HaveCScript(const CScriptID
& hash
) const
52 return mapScripts
.count(hash
) > 0;
55 bool CBasicKeyStore::GetCScript(const CScriptID
&hash
, CScript
& redeemScriptOut
) const
58 ScriptMap::const_iterator mi
= mapScripts
.find(hash
);
59 if (mi
!= mapScripts
.end())
61 redeemScriptOut
= (*mi
).second
;
67 static bool ExtractPubKey(const CScript
&dest
, CPubKey
& pubKeyOut
)
69 //TODO: Use Solver to extract this?
70 CScript::const_iterator pc
= dest
.begin();
72 std::vector
<unsigned char> vch
;
73 if (!dest
.GetOp(pc
, opcode
, vch
) || vch
.size() < 33 || vch
.size() > 65)
75 pubKeyOut
= CPubKey(vch
);
76 if (!pubKeyOut
.IsFullyValid())
78 if (!dest
.GetOp(pc
, opcode
, vch
) || opcode
!= OP_CHECKSIG
|| dest
.GetOp(pc
, opcode
, vch
))
83 bool CBasicKeyStore::AddWatchOnly(const CScript
&dest
)
86 setWatchOnly
.insert(dest
);
88 if (ExtractPubKey(dest
, pubKey
))
89 mapWatchKeys
[pubKey
.GetID()] = pubKey
;
93 bool CBasicKeyStore::RemoveWatchOnly(const CScript
&dest
)
96 setWatchOnly
.erase(dest
);
98 if (ExtractPubKey(dest
, pubKey
))
99 mapWatchKeys
.erase(pubKey
.GetID());
103 bool CBasicKeyStore::HaveWatchOnly(const CScript
&dest
) const
106 return setWatchOnly
.count(dest
) > 0;
109 bool CBasicKeyStore::HaveWatchOnly() const
112 return (!setWatchOnly
.empty());