SignatureBouncer.sol 5.3 KB

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