Preserve key compression attribute while initializing a new instance of CKey.
authorCryptoManiac <balthazar@yandex.ru>
Sat, 2 Apr 2016 20:55:12 +0000 (23:55 +0300)
committerCryptoManiac <balthazar@yandex.ru>
Sat, 2 Apr 2016 20:55:12 +0000 (23:55 +0300)
THis bug was originally reported by Alex AXe: https://bitcointalk.org/index.php?topic=704756.msg14401298#msg14401298

src/key.cpp
src/key.h
src/keystore.cpp
src/walletdb.cpp

index 1adeab2..d5caca9 100644 (file)
@@ -165,10 +165,10 @@ const unsigned char vchMaxModHalfOrder[32] = {
 
 const unsigned char *vchZero = NULL;
 
-void CKey::SetCompressedPubKey()
+void CKey::SetCompressedPubKey(bool fCompressed)
 {
-    EC_KEY_set_conv_form(pkey, POINT_CONVERSION_COMPRESSED);
-    fCompressedPubKey = true;
+    EC_KEY_set_conv_form(pkey, fCompressed ? POINT_CONVERSION_COMPRESSED : POINT_CONVERSION_UNCOMPRESSED);
+    fCompressedPubKey = fCompressed;
 }
 
 void CKey::Reset()
@@ -264,8 +264,7 @@ void CKey::MakeNewKey(bool fCompressed)
 {
     if (!EC_KEY_generate_key(pkey))
         throw key_error("CKey::MakeNewKey() : EC_KEY_generate_key failed");
-    if (fCompressed)
-        SetCompressedPubKey();
+    SetCompressedPubKey(fCompressed);
     fSet = true;
 }
 
@@ -311,8 +310,7 @@ bool CKey::SetSecret(const CSecret& vchSecret, bool fCompressed)
     }
     BN_clear_free(bn);
     fSet = true;
-    if (fCompressed)
-        SetCompressedPubKey();
+    SetCompressedPubKey(fCompressed);
     return true;
 }
 
@@ -429,8 +427,7 @@ bool CKey::SignCompact(uint256 hash, std::vector<unsigned char>& vchSig)
         {
             CKey keyRec;
             keyRec.fSet = true;
-            if (fCompressedPubKey)
-                keyRec.SetCompressedPubKey();
+            keyRec.SetCompressedPubKey(fCompressedPubKey);
             if (ECDSA_SIG_recover_key_GFp(keyRec.pkey, sig, (unsigned char*)&hash, sizeof(hash), i, 1) == 1)
                 if (keyRec.GetPubKey() == this->GetPubKey())
                 {
index 2489dfc..45ce39b 100644 (file)
--- a/src/key.h
+++ b/src/key.h
@@ -240,10 +240,10 @@ public:
     bool IsNull() const;
     bool IsCompressed() const;
 
-    void SetCompressedPubKey();
+    void SetCompressedPubKey(bool fCompressed=true);
     void MakeNewKey(bool fCompressed=true);
     bool SetPrivKey(const CPrivKey& vchPrivKey);
-    bool SetSecret(const CSecret& vchSecret, bool fCompressed = true);
+    bool SetSecret(const CSecret& vchSecret, bool fCompressed = false);
     CSecret GetSecret(bool &fCompressed) const;
     CSecret GetSecret() const;
     CPrivKey GetPrivKey() const;
index 33b8eb1..0aca325 100644 (file)
@@ -158,8 +158,7 @@ bool CCryptoKeyStore::Unlock(const CKeyingMaterial& vMasterKeyIn)
                 return false;
             CKey key;
             key.SetSecret(vchSecret);
-            if (vchPubKey.size() == 33)
-                key.SetCompressedPubKey();
+            key.SetCompressedPubKey(vchPubKey.IsCompressed());
             if (key.GetPubKey() == vchPubKey)
                 break;
             return false;
@@ -318,8 +317,7 @@ bool CCryptoKeyStore::GetKey(const CKeyID &address, CKey& keyOut) const
             if (vchSecret.size() != 32)
                 return false;
             keyOut.SetSecret(vchSecret);
-            if (vchPubKey.size() == 33)
-                keyOut.SetCompressedPubKey();
+            keyOut.SetCompressedPubKey(vchPubKey.IsCompressed());
             return true;
         }
     }
@@ -399,8 +397,7 @@ bool CCryptoKeyStore::DecryptKeys(const CKeyingMaterial& vMasterKeyIn)
                 return false;
             CKey key;
             key.SetSecret(vchSecret);
-            if (vchPubKey.size() == 33)
-                key.SetCompressedPubKey();
+            key.SetCompressedPubKey(vchPubKey.IsCompressed());
             if (!CBasicKeyStore::AddKey(key))
                 return false;
         }
index 3880a93..0f39cdf 100644 (file)
@@ -352,9 +352,7 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue,
                     strErr = "Error reading wallet database: CPrivKey pubkey inconsistency";
                     return false;
                 }
-                if (vchPubKey.size() == 33) {
-                    key.SetCompressedPubKey();
-                }
+                key.SetCompressedPubKey(vchPubKey.IsCompressed());
                 if (!key.IsValid())
                 {
                     strErr = "Error reading wallet database: invalid CPrivKey";
@@ -375,9 +373,7 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue,
                     strErr = "Error reading wallet database: CWalletKey pubkey inconsistency";
                     return false;
                 }
-                if (vchPubKey.size() == 33) {
-                    key.SetCompressedPubKey();
-                }
+                key.SetCompressedPubKey(vchPubKey.IsCompressed());
                 if (!key.IsValid())
                 {
                     strErr = "Error reading wallet database: invalid CWalletKey";