Preserve key compression attribute while initializing a new instance of CKey.
[novacoin.git] / src / key.cpp
index 20ea691..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 || fCompressedPubKey)
-        SetCompressedPubKey();
+    SetCompressedPubKey(fCompressed);
     return true;
 }
 
@@ -334,32 +332,12 @@ CSecret CKey::GetSecret(bool &fCompressed) const
 bool CKey::WritePEM(BIO *streamObj, const SecureString &strPassKey) const // dumppem 4KJLA99FyqMMhjjDe7KnRXK4sjtv9cCtNS /tmp/test.pem 123
 {
     EVP_PKEY *evpKey = EVP_PKEY_new();
-    bool result = true;
+    if (!EVP_PKEY_assign_EC_KEY(evpKey, pkey))
+        return error("CKey::WritePEM() : Error initializing EVP_PKEY instance.");
+    if(!PEM_write_bio_PKCS8PrivateKey(streamObj, evpKey, EVP_aes_256_cbc(), (char *)&strPassKey[0], strPassKey.size(), NULL, NULL))
+        return error("CKey::WritePEM() : Error writing private key data to stream object");
 
-    do
-    {
-        if (!EVP_PKEY_assign_EC_KEY(evpKey, pkey))
-        {
-            result = error("CKey::WritePEM() : Error initializing EVP_PKEY instance.");
-            break;
-        }
-
-        if(!PEM_write_bio_PKCS8PrivateKey(streamObj, evpKey, EVP_aes_256_cbc(), (char *)&strPassKey[0], strPassKey.size(), NULL, NULL))
-        {
-            result = error("CKey::WritePEM() : Error writing private key data to stream object");
-            break;
-        }
-
-        if(!PEM_write_bio_PUBKEY(streamObj, evpKey))
-        {
-            result = error("CKey::WritePEM() : Error writing public key data to stream object");
-            break;
-        }
-    }
-    while(false);
-
-    EVP_PKEY_free(evpKey);
-    return result;
+    return true;
 }
 
 CSecret CKey::GetSecret() const
@@ -449,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())
                 {
@@ -561,8 +538,6 @@ bool CPubKey::VerifyCompact(uint256 hash, const std::vector<unsigned char>& vchS
     CPubKey key;
     if (!key.SetCompactSignature(hash, vchSig))
         return false;
-    if ((*this) != key)
-        return false;
     return true;
 }
 
@@ -783,6 +758,8 @@ bool CMalleablePubKey::SetString(const std::string& strMalleablePubKey)
     if (!DecodeBase58Check(strMalleablePubKey, vchTemp)) {
         throw key_error("CMalleablePubKey::SetString() : Provided key data seems corrupted.");
     }
+    if (vchTemp.size() != 68)
+        return false;
 
     CDataStream ssKey(vchTemp, SER_NETWORK, PROTOCOL_VERSION);
     ssKey >> *this;
@@ -1050,7 +1027,8 @@ bool CMalleableKey::SetString(const std::string& strMutableKey)
     if (!DecodeBase58Check(strMutableKey, vchTemp)) {
         throw key_error("CMalleableKey::SetString() : Provided key data seems corrupted.");
     }
-
+    if (vchTemp.size() != 66)
+        return false;
     CDataStream ssKey(vchTemp, SER_NETWORK, PROTOCOL_VERSION);
     ssKey >> *this;
 
@@ -1194,6 +1172,9 @@ bool CMalleableKeyView::SetString(const std::string& strMutableKey)
         throw key_error("CMalleableKeyView::SetString() : Provided key data seems corrupted.");
     }
 
+    if (vchTemp.size() != 67)
+        return false;
+
     CDataStream ssKey(vchTemp, SER_NETWORK, PROTOCOL_VERSION);
     ssKey >> *this;