SignatureBouncer.sol 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. pragma solidity ^0.4.24;
  2. import "../access/roles/SignerRole.sol";
  3. import "../cryptography/ECDSA.sol";
  4. /**
  5. * @title SignatureBouncer
  6. * @author PhABC, Shrugs and aflesher
  7. * @dev SignatureBouncer allows users to submit a signature as a permission to do an action.
  8. * If the signature is from one of the authorized signer addresses, the signature
  9. * is valid.
  10. * Signer addresses can be individual servers signing grants or different
  11. * users within a decentralized club that have permission to invite other members.
  12. * This technique is useful for whitelists and airdrops; instead of putting all
  13. * valid addresses on-chain, simply sign a grant of the form
  14. * keccak256(abi.encodePacked(`:contractAddress` + `:granteeAddress`)) using a valid signer address.
  15. * Then restrict access to your crowdsale/whitelist/airdrop using the
  16. * `onlyValidSignature` modifier (or implement your own using _isValidSignature).
  17. * In addition to `onlyValidSignature`, `onlyValidSignatureAndMethod` and
  18. * `onlyValidSignatureAndData` can be used to restrict access to only a given method
  19. * or a given method with given parameters respectively.
  20. * See the tests in SignatureBouncer.test.js for specific usage examples.
  21. * @notice A method that uses the `onlyValidSignatureAndData` modifier must make the _signature
  22. * parameter the "last" parameter. You cannot sign a message that has its own
  23. * signature in it so the last 128 bytes of msg.data (which represents the
  24. * length of the _signature data and the _signaature data itself) is ignored when validating.
  25. * Also non fixed sized parameters make constructing the data in the signature
  26. * much more complex. See https://ethereum.stackexchange.com/a/50616 for more details.
  27. */
  28. contract SignatureBouncer is SignerRole {
  29. using ECDSA for bytes32;
  30. // Function selectors are 4 bytes long, as documented in
  31. // https://solidity.readthedocs.io/en/v0.4.24/abi-spec.html#function-selector
  32. uint256 private constant _METHOD_ID_SIZE = 4;
  33. // Signature size is 65 bytes (tightly packed v + r + s), but gets padded to 96 bytes
  34. uint256 private constant _SIGNATURE_SIZE = 96;
  35. /**
  36. * @dev requires that a valid signature of a signer was provided
  37. */
  38. modifier onlyValidSignature(bytes signature)
  39. {
  40. require(_isValidSignature(msg.sender, signature));
  41. _;
  42. }
  43. /**
  44. * @dev requires that a valid signature with a specifed method of a signer was provided
  45. */
  46. modifier onlyValidSignatureAndMethod(bytes signature)
  47. {
  48. require(_isValidSignatureAndMethod(msg.sender, signature));
  49. _;
  50. }
  51. /**
  52. * @dev requires that a valid signature with a specifed method and params of a signer was provided
  53. */
  54. modifier onlyValidSignatureAndData(bytes signature)
  55. {
  56. require(_isValidSignatureAndData(msg.sender, signature));
  57. _;
  58. }
  59. /**
  60. * @dev is the signature of `this + sender` from a signer?
  61. * @return bool
  62. */
  63. function _isValidSignature(address account, bytes signature)
  64. internal
  65. view
  66. returns (bool)
  67. {
  68. return _isValidDataHash(
  69. keccak256(abi.encodePacked(address(this), account)),
  70. signature
  71. );
  72. }
  73. /**
  74. * @dev is the signature of `this + sender + methodId` from a signer?
  75. * @return bool
  76. */
  77. function _isValidSignatureAndMethod(address account, bytes signature)
  78. internal
  79. view
  80. returns (bool)
  81. {
  82. bytes memory data = new bytes(_METHOD_ID_SIZE);
  83. for (uint i = 0; i < data.length; i++) {
  84. data[i] = msg.data[i];
  85. }
  86. return _isValidDataHash(
  87. keccak256(abi.encodePacked(address(this), account, data)),
  88. signature
  89. );
  90. }
  91. /**
  92. * @dev is the signature of `this + sender + methodId + params(s)` from a signer?
  93. * @notice the signature parameter of the method being validated must be the "last" parameter
  94. * @return bool
  95. */
  96. function _isValidSignatureAndData(address account, bytes signature)
  97. internal
  98. view
  99. returns (bool)
  100. {
  101. require(msg.data.length > _SIGNATURE_SIZE);
  102. bytes memory data = new bytes(msg.data.length - _SIGNATURE_SIZE);
  103. for (uint i = 0; i < data.length; i++) {
  104. data[i] = msg.data[i];
  105. }
  106. return _isValidDataHash(
  107. keccak256(abi.encodePacked(address(this), account, data)),
  108. signature
  109. );
  110. }
  111. /**
  112. * @dev internal function to convert a hash to an eth signed message
  113. * and then recover the signature and check it against the signer role
  114. * @return bool
  115. */
  116. function _isValidDataHash(bytes32 hash, bytes signature)
  117. internal
  118. view
  119. returns (bool)
  120. {
  121. address signer = hash
  122. .toEthSignedMessageHash()
  123. .recover(signature);
  124. return signer != address(0) && isSigner(signer);
  125. }
  126. }