diff --git a/src/keystore.cpp b/src/keystore.cpp --- a/src/keystore.cpp +++ b/src/keystore.cpp @@ -118,7 +118,7 @@ CScript::const_iterator pc = dest.begin(); opcodetype opcode; std::vector vch; - if (!dest.GetOp(pc, opcode, vch) || vch.size() < 33 || vch.size() > 65) { + if (!dest.GetOp(pc, opcode, vch) || !CPubKey::ValidSize(vch)) { return false; } pubKeyOut = CPubKey(vch); diff --git a/src/pubkey.h b/src/pubkey.h --- a/src/pubkey.h +++ b/src/pubkey.h @@ -66,6 +66,10 @@ void Invalidate() { vch[0] = 0xFF; } public: + bool static ValidSize(const std::vector &vch) { + return vch.size() > 0 && GetLen(vch[0]) == vch.size(); + } + //! Construct an invalid public key. CPubKey() { Invalidate(); } diff --git a/src/script/sigencoding.cpp b/src/script/sigencoding.cpp --- a/src/script/sigencoding.cpp +++ b/src/script/sigencoding.cpp @@ -288,11 +288,11 @@ static bool IsCompressedOrUncompressedPubKey(const valtype &vchPubKey) { switch (vchPubKey.size()) { - case 33: + case CPubKey::COMPRESSED_PUBLIC_KEY_SIZE: // Compressed public key: must start with 0x02 or 0x03. return vchPubKey[0] == 0x02 || vchPubKey[0] == 0x03; - case 65: + case CPubKey::PUBLIC_KEY_SIZE: // Non-compressed public key: must start with 0x04. return vchPubKey[0] == 0x04; @@ -303,7 +303,7 @@ } static bool IsCompressedPubKey(const valtype &vchPubKey) { - if (vchPubKey.size() != 33) { + if (vchPubKey.size() != CPubKey::COMPRESSED_PUBLIC_KEY_SIZE) { // Non-canonical public key: invalid length for compressed key return false; } diff --git a/src/script/standard.cpp b/src/script/standard.cpp --- a/src/script/standard.cpp +++ b/src/script/standard.cpp @@ -116,7 +116,7 @@ // Template matching opcodes: if (opcode2 == OP_PUBKEYS) { - while (vch1.size() >= 33 && vch1.size() <= 65) { + while (CPubKey::ValidSize(vch1)) { vSolutionsRet.push_back(vch1); if (!script1.GetOp(pc1, opcode1, vch1)) { break; @@ -130,7 +130,7 @@ } if (opcode2 == OP_PUBKEY) { - if (vch1.size() < 33 || vch1.size() > 65) { + if (!CPubKey::ValidSize(vch1)) { break; } vSolutionsRet.push_back(vch1);