diff --git a/src/script/sigencoding.cpp b/src/script/sigencoding.cpp --- a/src/script/sigencoding.cpp +++ b/src/script/sigencoding.cpp @@ -134,33 +134,33 @@ static bool IsCompressedOrUncompressedPubKey(const valtype &vchPubKey) { if (vchPubKey.size() < 33) { - // Non-canonical public key: too short + // Non-canonical public key: too short return false; } - if (vchPubKey[0] == 0x04) { - if (vchPubKey.size() != 65) { - // Non-canonical public key: invalid length for uncompressed key - return false; - } - } else if (vchPubKey[0] == 0x02 || vchPubKey[0] == 0x03) { - if (vchPubKey.size() != 33) { - // Non-canonical public key: invalid length for compressed key + + switch (vchPubKey[0]) { + case 0x04: + // Non-compressed public key: must be 65 bytes in size. + return vchPubKey.size() == 65; + + case 0x02: + case 0x03: + // Compressed public key: must be 33 bytes in size. + return vchPubKey.size() == 33; + + default: + // Non-canonical public key: neither compressed nor uncompressed return false; - } - } else { - // Non-canonical public key: neither compressed nor uncompressed - return false; } - return true; } static bool IsCompressedPubKey(const valtype &vchPubKey) { if (vchPubKey.size() != 33) { - // Non-canonical public key: invalid length for compressed key + // Non-canonical public key: invalid length for compressed key return false; } if (vchPubKey[0] != 0x02 && vchPubKey[0] != 0x03) { - // Non-canonical public key: invalid prefix for compressed key + // Non-canonical public key: invalid prefix for compressed key return false; } return true; @@ -168,7 +168,7 @@ bool CheckPubKeyEncoding(const valtype &vchPubKey, uint32_t flags, ScriptError *serror) { - if ((flags & SCRIPT_VERIFY_STRICTENC) != 0 && + if ((flags & SCRIPT_VERIFY_STRICTENC) && !IsCompressedOrUncompressedPubKey(vchPubKey)) { return set_error(serror, SCRIPT_ERR_PUBKEYTYPE); } diff --git a/src/test/sigencoding_tests.cpp b/src/test/sigencoding_tests.cpp --- a/src/test/sigencoding_tests.cpp +++ b/src/test/sigencoding_tests.cpp @@ -32,6 +32,11 @@ // Degenerate keys. {}, {0x00}, + {0x01}, + {0x02}, + {0x03}, + {0x04}, + {0x05}, {0x42}, {0xff}, // Invalid first byte 0x00.