Created
May 15, 2023 11:51
-
-
Save PaulRBerg/cfb21196db3c4d92d1a028589acb5b23 to your computer and use it in GitHub Desktop.
Reducing the control flow nesting depth of
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function verify(bytes calldata signature, bytes32 hash, address claimedSigner) internal view { | |
bytes32 r; | |
bytes32 s; | |
uint8 v; | |
if (claimedSigner.code.length > 0) { | |
bytes4 magicValue = IERC1271(claimedSigner).isValidSignature(hash, signature); | |
if (magicValue != IERC1271.isValidSignature.selector) revert InvalidContractSignature(); | |
return; | |
} | |
if (signature.length == 65) { | |
(r, s) = abi.decode(signature, (bytes32, bytes32)); | |
v = uint8(signature[64]); | |
} else if (signature.length == 64) { | |
// EIP-2098 | |
bytes32 vs; | |
(r, vs) = abi.decode(signature, (bytes32, bytes32)); | |
s = vs & UPPER_BIT_MASK; | |
v = uint8(uint256(vs >> 255)) + 27; | |
} else { | |
revert InvalidSignatureLength(); | |
} | |
address signer = ecrecover(hash, v, r, s); | |
if (signer == address(0)) revert InvalidSignature(); | |
if (signer != claimedSigner) revert InvalidSigner(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment