genesis 1
genesis 2
genesis 3
genesis 4
genesis 5
genesis 6 #include "headers.h"
genesis 7 #include "db.h"
genesis 8 #include "crypter.h"
genesis 9
genesis 10 std::vector<unsigned char> CKeyStore::GenerateNewKey()
genesis 11 {
genesis 12 RandAddSeedPerfmon();
genesis 13 CKey key;
genesis 14 key.MakeNewKey();
genesis 15 if (!AddKey(key))
genesis 16 throw std::runtime_error("CKeyStore::GenerateNewKey() : AddKey failed");
genesis 17 return key.GetPubKey();
genesis 18 }
genesis 19
genesis 20 bool CKeyStore::GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char> &vchPubKeyOut) const
genesis 21 {
genesis 22 CKey key;
genesis 23 if (!GetKey(address, key))
genesis 24 return false;
genesis 25 vchPubKeyOut = key.GetPubKey();
genesis 26 return true;
genesis 27 }
genesis 28
genesis 29 bool CBasicKeyStore::AddKey(const CKey& key)
genesis 30 {
genesis 31 CRITICAL_BLOCK(cs_KeyStore)
mod6_privkey_tools 32 mapKeys[CBitcoinAddress(key.GetPubKey())] = key.GetSecret();
genesis 33 return true;
genesis 34 }
genesis 35
genesis 36 bool CCryptoKeyStore::SetCrypted()
genesis 37 {
genesis 38 CRITICAL_BLOCK(cs_KeyStore)
genesis 39 {
genesis 40 if (fUseCrypto)
genesis 41 return true;
genesis 42 if (!mapKeys.empty())
genesis 43 return false;
genesis 44 fUseCrypto = true;
genesis 45 }
genesis 46 return true;
genesis 47 }
genesis 48
genesis 49 std::vector<unsigned char> CCryptoKeyStore::GenerateNewKey()
genesis 50 {
genesis 51 RandAddSeedPerfmon();
genesis 52 CKey key;
genesis 53 key.MakeNewKey();
genesis 54 if (!AddKey(key))
genesis 55 throw std::runtime_error("CCryptoKeyStore::GenerateNewKey() : AddKey failed");
genesis 56 return key.GetPubKey();
genesis 57 }
genesis 58
genesis 59 bool CCryptoKeyStore::Unlock(const CKeyingMaterial& vMasterKeyIn)
genesis 60 {
genesis 61 CRITICAL_BLOCK(cs_KeyStore)
genesis 62 {
genesis 63 if (!SetCrypted())
genesis 64 return false;
genesis 65
genesis 66 CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
genesis 67 for (; mi != mapCryptedKeys.end(); ++mi)
genesis 68 {
genesis 69 const std::vector<unsigned char> &vchPubKey = (*mi).second.first;
genesis 70 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
genesis 71 CSecret vchSecret;
genesis 72 if(!DecryptSecret(vMasterKeyIn, vchCryptedSecret, Hash(vchPubKey.begin(), vchPubKey.end()), vchSecret))
genesis 73 return false;
genesis 74 CKey key;
genesis 75 key.SetSecret(vchSecret);
genesis 76 if (key.GetPubKey() == vchPubKey)
genesis 77 break;
genesis 78 return false;
genesis 79 }
genesis 80 vMasterKey = vMasterKeyIn;
genesis 81 }
genesis 82 return true;
genesis 83 }
genesis 84
genesis 85 bool CCryptoKeyStore::AddKey(const CKey& key)
genesis 86 {
genesis 87 CRITICAL_BLOCK(cs_KeyStore)
genesis 88 {
genesis 89 if (!IsCrypted())
genesis 90 return CBasicKeyStore::AddKey(key);
genesis 91
genesis 92 if (IsLocked())
genesis 93 return false;
genesis 94
genesis 95 std::vector<unsigned char> vchCryptedSecret;
genesis 96 std::vector<unsigned char> vchPubKey = key.GetPubKey();
genesis 97 if (!EncryptSecret(vMasterKey, key.GetSecret(), Hash(vchPubKey.begin(), vchPubKey.end()), vchCryptedSecret))
genesis 98 return false;
genesis 99
genesis 100 if (!AddCryptedKey(key.GetPubKey(), vchCryptedSecret))
genesis 101 return false;
genesis 102 }
genesis 103 return true;
genesis 104 }
genesis 105
genesis 106
genesis 107 bool CCryptoKeyStore::AddCryptedKey(const std::vector<unsigned char> &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret)
genesis 108 {
genesis 109 CRITICAL_BLOCK(cs_KeyStore)
genesis 110 {
genesis 111 if (!SetCrypted())
genesis 112 return false;
genesis 113
genesis 114 mapCryptedKeys[CBitcoinAddress(vchPubKey)] = make_pair(vchPubKey, vchCryptedSecret);
genesis 115 }
genesis 116 return true;
genesis 117 }
genesis 118
genesis 119 bool CCryptoKeyStore::GetKey(const CBitcoinAddress &address, CKey& keyOut) const
genesis 120 {
genesis 121 CRITICAL_BLOCK(cs_KeyStore)
genesis 122 {
genesis 123 if (!IsCrypted())
genesis 124 return CBasicKeyStore::GetKey(address, keyOut);
genesis 125
genesis 126 CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
genesis 127 if (mi != mapCryptedKeys.end())
genesis 128 {
genesis 129 const std::vector<unsigned char> &vchPubKey = (*mi).second.first;
genesis 130 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
genesis 131 CSecret vchSecret;
genesis 132 if (!DecryptSecret(vMasterKey, vchCryptedSecret, Hash(vchPubKey.begin(), vchPubKey.end()), vchSecret))
genesis 133 return false;
genesis 134 keyOut.SetSecret(vchSecret);
genesis 135 return true;
genesis 136 }
genesis 137 }
genesis 138 return false;
genesis 139 }
genesis 140
genesis 141 bool CCryptoKeyStore::GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char>& vchPubKeyOut) const
genesis 142 {
genesis 143 CRITICAL_BLOCK(cs_KeyStore)
genesis 144 {
genesis 145 if (!IsCrypted())
genesis 146 return CKeyStore::GetPubKey(address, vchPubKeyOut);
genesis 147
genesis 148 CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
genesis 149 if (mi != mapCryptedKeys.end())
genesis 150 {
genesis 151 vchPubKeyOut = (*mi).second.first;
genesis 152 return true;
genesis 153 }
genesis 154 }
genesis 155 return false;
genesis 156 }
genesis 157
genesis 158 bool CCryptoKeyStore::EncryptKeys(CKeyingMaterial& vMasterKeyIn)
genesis 159 {
genesis 160 CRITICAL_BLOCK(cs_KeyStore)
genesis 161 {
genesis 162 if (!mapCryptedKeys.empty() || IsCrypted())
genesis 163 return false;
genesis 164
genesis 165 fUseCrypto = true;
genesis 166 CKey key;
genesis 167 BOOST_FOREACH(KeyMap::value_type& mKey, mapKeys)
genesis 168 {
genesis 169 if (!key.SetSecret(mKey.second))
genesis 170 return false;
genesis 171 const std::vector<unsigned char> vchPubKey = key.GetPubKey();
genesis 172 std::vector<unsigned char> vchCryptedSecret;
genesis 173 if (!EncryptSecret(vMasterKeyIn, key.GetSecret(), Hash(vchPubKey.begin(), vchPubKey.end()), vchCryptedSecret))
genesis 174 return false;
genesis 175 if (!AddCryptedKey(vchPubKey, vchCryptedSecret))
genesis 176 return false;
genesis 177 }
genesis 178 mapKeys.clear();
genesis 179 }
genesis 180 return true;
genesis 181 }