keystore.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. // ECOin - Copyright (c) - 2014/2022 - GPLv3 - epsylon@riseup.net (https://03c8.net)
  2. #include "keystore.h"
  3. #include "script.h"
  4. bool CKeyStore::GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const
  5. {
  6. CKey key;
  7. if (!GetKey(address, key))
  8. return false;
  9. vchPubKeyOut = key.GetPubKey();
  10. return true;
  11. }
  12. bool CBasicKeyStore::AddKey(const CKey& key)
  13. {
  14. bool fCompressed = false;
  15. CSecret secret = key.GetSecret(fCompressed);
  16. {
  17. LOCK(cs_KeyStore);
  18. mapKeys[key.GetPubKey().GetID()] = make_pair(secret, fCompressed);
  19. }
  20. return true;
  21. }
  22. bool CBasicKeyStore::AddCScript(const CScript& redeemScript)
  23. {
  24. {
  25. LOCK(cs_KeyStore);
  26. mapScripts[redeemScript.GetID()] = redeemScript;
  27. }
  28. return true;
  29. }
  30. bool CBasicKeyStore::HaveCScript(const CScriptID& hash) const
  31. {
  32. bool result;
  33. {
  34. LOCK(cs_KeyStore);
  35. result = (mapScripts.count(hash) > 0);
  36. }
  37. return result;
  38. }
  39. bool CBasicKeyStore::GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const
  40. {
  41. {
  42. LOCK(cs_KeyStore);
  43. ScriptMap::const_iterator mi = mapScripts.find(hash);
  44. if (mi != mapScripts.end())
  45. {
  46. redeemScriptOut = (*mi).second;
  47. return true;
  48. }
  49. }
  50. return false;
  51. }
  52. bool CCryptoKeyStore::SetCrypted()
  53. {
  54. {
  55. LOCK(cs_KeyStore);
  56. if (fUseCrypto)
  57. return true;
  58. if (!mapKeys.empty())
  59. return false;
  60. fUseCrypto = true;
  61. }
  62. return true;
  63. }
  64. bool CCryptoKeyStore::Lock()
  65. {
  66. if (!SetCrypted())
  67. return false;
  68. {
  69. LOCK(cs_KeyStore);
  70. vMasterKey.clear();
  71. }
  72. NotifyStatusChanged(this);
  73. return true;
  74. }
  75. bool CCryptoKeyStore::Unlock(const CKeyingMaterial& vMasterKeyIn)
  76. {
  77. {
  78. LOCK(cs_KeyStore);
  79. if (!SetCrypted())
  80. return false;
  81. CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
  82. for (; mi != mapCryptedKeys.end(); ++mi)
  83. {
  84. const CPubKey &vchPubKey = (*mi).second.first;
  85. const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
  86. CSecret vchSecret;
  87. if(!DecryptSecret(vMasterKeyIn, vchCryptedSecret, vchPubKey.GetHash(), vchSecret))
  88. return false;
  89. if (vchSecret.size() != 32)
  90. return false;
  91. CKey key;
  92. key.SetPubKey(vchPubKey);
  93. key.SetSecret(vchSecret);
  94. if (key.GetPubKey() == vchPubKey)
  95. break;
  96. return false;
  97. }
  98. vMasterKey = vMasterKeyIn;
  99. }
  100. NotifyStatusChanged(this);
  101. return true;
  102. }
  103. bool CCryptoKeyStore::AddKey(const CKey& key)
  104. {
  105. {
  106. LOCK(cs_KeyStore);
  107. if (!IsCrypted())
  108. return CBasicKeyStore::AddKey(key);
  109. if (IsLocked())
  110. return false;
  111. std::vector<unsigned char> vchCryptedSecret;
  112. CPubKey vchPubKey = key.GetPubKey();
  113. bool fCompressed;
  114. if (!EncryptSecret(vMasterKey, key.GetSecret(fCompressed), vchPubKey.GetHash(), vchCryptedSecret))
  115. return false;
  116. if (!AddCryptedKey(key.GetPubKey(), vchCryptedSecret))
  117. return false;
  118. }
  119. return true;
  120. }
  121. bool CCryptoKeyStore::AddCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret)
  122. {
  123. {
  124. LOCK(cs_KeyStore);
  125. if (!SetCrypted())
  126. return false;
  127. mapCryptedKeys[vchPubKey.GetID()] = make_pair(vchPubKey, vchCryptedSecret);
  128. }
  129. return true;
  130. }
  131. bool CCryptoKeyStore::GetKey(const CKeyID &address, CKey& keyOut) const
  132. {
  133. {
  134. LOCK(cs_KeyStore);
  135. if (!IsCrypted())
  136. return CBasicKeyStore::GetKey(address, keyOut);
  137. CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
  138. if (mi != mapCryptedKeys.end())
  139. {
  140. const CPubKey &vchPubKey = (*mi).second.first;
  141. const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
  142. CSecret vchSecret;
  143. if (!DecryptSecret(vMasterKey, vchCryptedSecret, vchPubKey.GetHash(), vchSecret))
  144. return false;
  145. if (vchSecret.size() != 32)
  146. return false;
  147. keyOut.SetPubKey(vchPubKey);
  148. keyOut.SetSecret(vchSecret);
  149. return true;
  150. }
  151. }
  152. return false;
  153. }
  154. bool CCryptoKeyStore::GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const
  155. {
  156. {
  157. LOCK(cs_KeyStore);
  158. if (!IsCrypted())
  159. return CKeyStore::GetPubKey(address, vchPubKeyOut);
  160. CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
  161. if (mi != mapCryptedKeys.end())
  162. {
  163. vchPubKeyOut = (*mi).second.first;
  164. return true;
  165. }
  166. }
  167. return false;
  168. }
  169. bool CCryptoKeyStore::EncryptKeys(CKeyingMaterial& vMasterKeyIn)
  170. {
  171. {
  172. LOCK(cs_KeyStore);
  173. if (!mapCryptedKeys.empty() || IsCrypted())
  174. return false;
  175. fUseCrypto = true;
  176. BOOST_FOREACH(KeyMap::value_type& mKey, mapKeys)
  177. {
  178. CKey key;
  179. if (!key.SetSecret(mKey.second.first, mKey.second.second))
  180. return false;
  181. const CPubKey vchPubKey = key.GetPubKey();
  182. std::vector<unsigned char> vchCryptedSecret;
  183. bool fCompressed;
  184. if (!EncryptSecret(vMasterKeyIn, key.GetSecret(fCompressed), vchPubKey.GetHash(), vchCryptedSecret))
  185. return false;
  186. if (!AddCryptedKey(vchPubKey, vchCryptedSecret))
  187. return false;
  188. }
  189. mapKeys.clear();
  190. }
  191. return true;
  192. }