From: 0xDEADFACE Date: Sat, 13 Feb 2016 22:52:49 +0000 (+0300) Subject: Merge pull request #273 from svost/includes X-Git-Tag: nvc-v0.5.6~81^2~1 X-Git-Url: https://git.novaco.in/?p=novacoin.git;a=commitdiff_plain;h=39d23056c0cf13b3e59648c159f2d4aff190992b;hp=-c Merge pull request #273 from svost/includes Reorganize includes a bit. --- 39d23056c0cf13b3e59648c159f2d4aff190992b diff --combined src/key.cpp index dafffcf,39a7ebb..fe8573e --- a/src/key.cpp +++ b/src/key.cpp @@@ -6,12 -6,9 +6,9 @@@ #include #include #include "key.h" #include "base58.h" - #include "ies.h" // Generate a private key from just the secret parameter int EC_KEY_regenerate_key(EC_KEY *eckey, BIGNUM *priv_key) @@@ -592,12 -589,6 +589,12 @@@ bool CPoint::setBytes(const std::vector return true; } +// Initialize from octets stream +bool CPoint::setPubKey(const CPubKey &vchPubKey) +{ + return setBytes(vchPubKey.Raw()); +} + // Serialize to octets stream bool CPoint::getBytes(std::vector &vchBytes) { @@@ -672,7 -663,7 +669,7 @@@ void CMalleablePubKey::GetVariant(CPubK EC_KEY_free(eckey); CPoint point; - if (!point.setBytes(pubKeyL.Raw())) { + if (!point.setPubKey(pubKeyL)) { throw key_error("CMalleablePubKey::GetVariant() : Unable to decode L value"); } @@@ -689,7 -680,7 +686,7 @@@ bnHash.setuint160(Hash160(vchLr)); CPoint pointH; - pointH.setBytes(pubKeyH.Raw()); + pointH.setPubKey(pubKeyH); CPoint P; // Calculate P = Hash(L*r)*G + H @@@ -705,7 -696,7 +702,7 @@@ vchPubKeyVariant = CPubKey(vchResult); } -std::string CMalleablePubKey::ToString() +std::string CMalleablePubKey::ToString() const { CDataStream ssKey(SER_NETWORK, PROTOCOL_VERSION); ssKey << *this; @@@ -795,7 -786,7 +792,7 @@@ bool CMalleableKey::SetSecrets(const CS Reset(); CKey L, H; - if (!L.SetSecret(pvchSecretL, true) || !H.SetSecret(pvchSecretH, true)) + if (pvchSecretL.size() != 32 || !pvchSecretH.size() != 32 || !L.SetSecret(pvchSecretL, true) || !H.SetSecret(pvchSecretH, true)) { nVersion = 0; return false; @@@ -827,7 -818,7 +824,7 @@@ CMalleablePubKey CMalleableKey::GetMall } // Check ownership -bool CMalleableKey::CheckKeyVariant(const CPubKey &R, const CPubKey &vchPubKeyVariant) +bool CMalleableKey::CheckKeyVariant(const CPubKey &R, const CPubKey &vchPubKeyVariant) const { if (IsNull()) { throw key_error("CMalleableKey::CheckKeyVariant() : Attempting to run on NULL key object."); @@@ -842,7 -833,7 +839,7 @@@ } CPoint point_R; - if (!point_R.setBytes(R.Raw())) { + if (!point_R.setPubKey(R)) { throw key_error("CMalleableKey::CheckKeyVariant() : Unable to decode R value"); } @@@ -851,12 -842,12 +848,12 @@@ std::vector vchPubKeyH = H.GetPubKey().Raw(); CPoint point_H; - if (!point_H.setBytes(vchPubKeyH)) { + if (!point_H.setPubKey(vchPubKeyH)) { throw key_error("CMalleableKey::CheckKeyVariant() : Unable to decode H value"); } CPoint point_P; - if (!point_P.setBytes(vchPubKeyVariant.Raw())) { + if (!point_P.setPubKey(vchPubKeyVariant)) { throw key_error("CMalleableKey::CheckKeyVariant() : Unable to decode P value"); } @@@ -897,7 -888,7 +894,7 @@@ } // Check ownership and restore private key -bool CMalleableKey::CheckKeyVariant(const CPubKey &R, const CPubKey &vchPubKeyVariant, CKey &privKeyVariant) +bool CMalleableKey::CheckKeyVariant(const CPubKey &R, const CPubKey &vchPubKeyVariant, CKey &privKeyVariant) const { if (IsNull()) { throw key_error("CMalleableKey::CheckKeyVariant() : Attempting to run on NULL key object."); @@@ -912,7 -903,7 +909,7 @@@ } CPoint point_R; - if (!point_R.setBytes(R.Raw())) { + if (!point_R.setPubKey(R)) { throw key_error("CMalleableKey::CheckKeyVariant() : Unable to decode R value"); } @@@ -921,12 -912,12 +918,12 @@@ std::vector vchPubKeyH = H.GetPubKey().Raw(); CPoint point_H; - if (!point_H.setBytes(vchPubKeyH)) { + if (!point_H.setPubKey(vchPubKeyH)) { throw key_error("CMalleableKey::CheckKeyVariant() : Unable to decode H value"); } CPoint point_P; - if (!point_P.setBytes(vchPubKeyVariant.Raw())) { + if (!point_P.setPubKey(vchPubKeyVariant)) { throw key_error("CMalleableKey::CheckKeyVariant() : Unable to decode P value"); } @@@ -976,7 -967,7 +973,7 @@@ return true; } -std::string CMalleableKey::ToString() +std::string CMalleableKey::ToString() const { CDataStream ssKey(SER_NETWORK, PROTOCOL_VERSION); ssKey << *this; @@@ -1002,43 -993,28 +999,43 @@@ bool CMalleableKey::SetString(const std CMalleableKeyView::CMalleableKeyView(const CMalleableKey &b) { - assert(b.nVersion == CURRENT_VERSION); + if (b.vchSecretL.size() != 32) + throw key_error("CMalleableKeyView::CMalleableKeyView() : L size must be 32 bytes"); + + if (b.vchSecretH.size() != 32) + throw key_error("CMalleableKeyView::CMalleableKeyView() : L size must be 32 bytes"); + vchSecretL = b.vchSecretL; CKey H; H.SetSecret(b.vchSecretH, true); + vchPubKeyH = H.GetPubKey().Raw(); + nVersion = b.nVersion; +} + +CMalleableKeyView::CMalleableKeyView(const CMalleableKeyView &b) +{ + vchSecretL = b.vchSecretL; + vchPubKeyH = b.vchPubKeyH; + nVersion = CURRENT_VERSION; } CMalleableKeyView::CMalleableKeyView(const CSecret &L, const CPubKey &pvchPubKeyH) { vchSecretL = L; vchPubKeyH = pvchPubKeyH.Raw(); + nVersion = CURRENT_VERSION; } CMalleableKeyView& CMalleableKeyView::operator=(const CMalleableKey &b) { - assert(b.nVersion == CURRENT_VERSION); vchSecretL = b.vchSecretL; CKey H; H.SetSecret(b.vchSecretH, true); vchPubKeyH = H.GetPubKey().Raw(); + nVersion = b.nVersion; return (*this); } @@@ -1055,7 -1031,7 +1052,7 @@@ CMalleablePubKey CMalleableKeyView::Get } // Check ownership -bool CMalleableKeyView::CheckKeyVariant(const CPubKey &R, const CPubKey &vchPubKeyVariant) +bool CMalleableKeyView::CheckKeyVariant(const CPubKey &R, const CPubKey &vchPubKeyVariant) const { if (!R.IsValid()) { throw key_error("CMalleableKeyView::CheckKeyVariant() : R is invalid"); @@@ -1066,17 -1042,17 +1063,17 @@@ } CPoint point_R; - if (!point_R.setBytes(R.Raw())) { + if (!point_R.setPubKey(R)) { throw key_error("CMalleableKeyView::CheckKeyVariant() : Unable to decode R value"); } CPoint point_H; - if (!point_H.setBytes(vchPubKeyH)) { + if (!point_H.setPubKey(vchPubKeyH)) { throw key_error("CMalleableKeyView::CheckKeyVariant() : Unable to decode H value"); } CPoint point_P; - if (!point_P.setBytes(vchPubKeyVariant.Raw())) { + if (!point_P.setPubKey(vchPubKeyVariant)) { throw key_error("CMalleableKeyView::CheckKeyVariant() : Unable to decode P value"); } @@@ -1116,33 -1092,6 +1113,33 @@@ return true; } +std::string CMalleableKeyView::ToString() const +{ + CDataStream ssKey(SER_NETWORK, PROTOCOL_VERSION); + ssKey << *this; + std::vector vch(ssKey.begin(), ssKey.end()); + + return EncodeBase58Check(vch); +} + +bool CMalleableKeyView::SetString(const std::string& strMutableKey) +{ + std::vector vchTemp; + if (!DecodeBase58Check(strMutableKey, vchTemp)) { + throw key_error("CMalleableKeyView::SetString() : Provided key data seems corrupted."); + } + + CDataStream ssKey(vchTemp, SER_NETWORK, PROTOCOL_VERSION); + ssKey >> *this; + + return IsNull(); +} + +bool CMalleableKeyView::IsNull() const +{ + return nVersion != CURRENT_VERSION; +} + //// Asymmetric encryption void CPubKey::EncryptData(const std::vector& data, std::vector& encrypted)