Skip to content

Instantly share code, notes, and snippets.

@andreafspeziale
Last active July 3, 2019 22:39
Show Gist options
  • Save andreafspeziale/fbaa0b2d4ce1d3d354811e921c2b4480 to your computer and use it in GitHub Desktop.
Save andreafspeziale/fbaa0b2d4ce1d3d354811e921c2b4480 to your computer and use it in GitHub Desktop.
function isSignerWhitelisted(
    bytes32 _hashedMessage,
    bytes memory _signedHashedMessage
)
    internal
    view
    returns (bool)
{
    bytes32 r;
    bytes32 s;
    uint8 v;
    // Check the signature length
    if (_signedHashedMessage.length != 65) {
      return false;
    }
    // Divide the signature in r, s and v variables
    // ecrecover takes the signature parameters, and the only way to get them
    // currently is to use assembly.
    // solium-disable-next-line security/no-inline-assembly
    assembly {
      r := mload(add(_signedHashedMessage, 32))
      s := mload(add(_signedHashedMessage, 64))
      v := byte(0, mload(add(_signedHashedMessage, 96)))
    }
    // Version of signature should be 27 or 28, but 0 and 1 are also possible versions
    if (v < 27) {
      v += 27;
    }
    // If the version is correct return the signer address
    if (v != 27 && v != 28) {
      return false;
    } else {
      // solium-disable-next-line arg-overflow
      return whitelist[ecrecover(
        keccak256(abi.encodePacked("\x19Ethereum Signed Message:\n32", _hashedMessage)),
        v, r, s
      )];
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment