You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

keystore.cpp 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright (c) 2009-2010 Satoshi Nakamoto
  2. // Copyright (c) 2009-2016 The Starwels developers
  3. // Distributed under the MIT software license, see the accompanying
  4. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  5. #include "keystore.h"
  6. #include "key.h"
  7. #include "pubkey.h"
  8. #include "util.h"
  9. bool CKeyStore::AddKey(const CKey &key) {
  10. return AddKeyPubKey(key, key.GetPubKey());
  11. }
  12. bool CBasicKeyStore::GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const
  13. {
  14. CKey key;
  15. if (!GetKey(address, key)) {
  16. LOCK(cs_KeyStore);
  17. WatchKeyMap::const_iterator it = mapWatchKeys.find(address);
  18. if (it != mapWatchKeys.end()) {
  19. vchPubKeyOut = it->second;
  20. return true;
  21. }
  22. return false;
  23. }
  24. vchPubKeyOut = key.GetPubKey();
  25. return true;
  26. }
  27. bool CBasicKeyStore::AddKeyPubKey(const CKey& key, const CPubKey &pubkey)
  28. {
  29. LOCK(cs_KeyStore);
  30. mapKeys[pubkey.GetID()] = key;
  31. return true;
  32. }
  33. bool CBasicKeyStore::AddCScript(const CScript& redeemScript)
  34. {
  35. if (redeemScript.size() > MAX_SCRIPT_ELEMENT_SIZE)
  36. return error("CBasicKeyStore::AddCScript(): redeemScripts > %i bytes are invalid", MAX_SCRIPT_ELEMENT_SIZE);
  37. LOCK(cs_KeyStore);
  38. mapScripts[CScriptID(redeemScript)] = redeemScript;
  39. return true;
  40. }
  41. bool CBasicKeyStore::HaveCScript(const CScriptID& hash) const
  42. {
  43. LOCK(cs_KeyStore);
  44. return mapScripts.count(hash) > 0;
  45. }
  46. bool CBasicKeyStore::GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const
  47. {
  48. LOCK(cs_KeyStore);
  49. ScriptMap::const_iterator mi = mapScripts.find(hash);
  50. if (mi != mapScripts.end())
  51. {
  52. redeemScriptOut = (*mi).second;
  53. return true;
  54. }
  55. return false;
  56. }
  57. static bool ExtractPubKey(const CScript &dest, CPubKey& pubKeyOut)
  58. {
  59. //TODO: Use Solver to extract this?
  60. CScript::const_iterator pc = dest.begin();
  61. opcodetype opcode;
  62. std::vector<unsigned char> vch;
  63. if (!dest.GetOp(pc, opcode, vch) || vch.size() < 33 || vch.size() > 65)
  64. return false;
  65. pubKeyOut = CPubKey(vch);
  66. if (!pubKeyOut.IsFullyValid())
  67. return false;
  68. if (!dest.GetOp(pc, opcode, vch) || opcode != OP_CHECKSIG || dest.GetOp(pc, opcode, vch))
  69. return false;
  70. return true;
  71. }
  72. bool CBasicKeyStore::AddWatchOnly(const CScript &dest)
  73. {
  74. LOCK(cs_KeyStore);
  75. setWatchOnly.insert(dest);
  76. CPubKey pubKey;
  77. if (ExtractPubKey(dest, pubKey))
  78. mapWatchKeys[pubKey.GetID()] = pubKey;
  79. return true;
  80. }
  81. bool CBasicKeyStore::RemoveWatchOnly(const CScript &dest)
  82. {
  83. LOCK(cs_KeyStore);
  84. setWatchOnly.erase(dest);
  85. CPubKey pubKey;
  86. if (ExtractPubKey(dest, pubKey))
  87. mapWatchKeys.erase(pubKey.GetID());
  88. return true;
  89. }
  90. bool CBasicKeyStore::HaveWatchOnly(const CScript &dest) const
  91. {
  92. LOCK(cs_KeyStore);
  93. return setWatchOnly.count(dest) > 0;
  94. }
  95. bool CBasicKeyStore::HaveWatchOnly() const
  96. {
  97. LOCK(cs_KeyStore);
  98. return (!setWatchOnly.empty());
  99. }