Assert CPubKey::ValidLength to the pubkey's header-relevent size

Previously this was an inline test where the specificity was probably judged
overly specific. As a class method it makes sense to maintain consistency.

And replace some magic values with their constant equivalents.

Zcash: Excludes changes to the following functions we don't have:

- ExtractPubKey (bitcoin/bitcoin#6415)
- IsCompressedPubKey (bitcoin/bitcoin#8499)
This commit is contained in:
Ben Woosley 2018-02-16 20:28:03 +00:00 committed by Jack Grigg
parent 3c8c8009db
commit 82beb18901
No known key found for this signature in database
GPG Key ID: 9E8255172BBF9898
3 changed files with 10 additions and 5 deletions

View File

@ -70,6 +70,11 @@ private:
}
public:
bool static ValidSize(const std::vector<unsigned char> &vch) {
return vch.size() > 0 && GetLen(vch[0]) == vch.size();
}
//! Construct an invalid public key.
CPubKey()
{

View File

@ -65,17 +65,17 @@ static inline void popstack(vector<valtype>& stack)
}
bool static IsCompressedOrUncompressedPubKey(const valtype &vchPubKey) {
if (vchPubKey.size() < 33) {
if (vchPubKey.size() < CPubKey::COMPRESSED_PUBLIC_KEY_SIZE) {
// Non-canonical public key: too short
return false;
}
if (vchPubKey[0] == 0x04) {
if (vchPubKey.size() != 65) {
if (vchPubKey.size() != CPubKey::PUBLIC_KEY_SIZE) {
// Non-canonical public key: invalid length for uncompressed key
return false;
}
} else if (vchPubKey[0] == 0x02 || vchPubKey[0] == 0x03) {
if (vchPubKey.size() != 33) {
if (vchPubKey.size() != CPubKey::COMPRESSED_PUBLIC_KEY_SIZE) {
// Non-canonical public key: invalid length for compressed key
return false;
}

View File

@ -110,7 +110,7 @@ bool Solver(const CScript& scriptPubKey, txnouttype& typeRet, std::vector<std::v
// 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))
@ -124,7 +124,7 @@ bool Solver(const CScript& scriptPubKey, txnouttype& typeRet, std::vector<std::v
if (opcode2 == OP_PUBKEY)
{
if (vch1.size() < 33 || vch1.size() > 65)
if (!CPubKey::ValidSize(vch1))
break;
vSolutionsRet.push_back(vch1);
}