SignatureBouncer.sol 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. string public constant ROLE_BOUNCER = "bouncer";
  32. uint internal constant METHOD_ID_SIZE = 4;
  33. // signature size is 65 bytes (tightly packed v + r + s), but gets padded to 96 bytes
  34. uint internal constant SIGNATURE_SIZE = 96;
  35. /**
  36. * @dev requires that a valid signature of a bouncer 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 bouncer 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 bouncer was provided
  53. */
  54. modifier onlyValidSignatureAndData(bytes _signature)
  55. {
  56. require(_isValidSignatureAndData(msg.sender, _signature));
  57. _;
  58. }
  59. /**
  60. * @dev allows the owner to add additional bouncer addresses
  61. */
  62. function addBouncer(address _bouncer)
  63. public
  64. onlyOwner
  65. {
  66. require(_bouncer != address(0));
  67. _addRole(_bouncer, ROLE_BOUNCER);
  68. }
  69. /**
  70. * @dev allows the owner to remove bouncer addresses
  71. */
  72. function removeBouncer(address _bouncer)
  73. public
  74. onlyOwner
  75. {
  76. _removeRole(_bouncer, ROLE_BOUNCER);
  77. }
  78. /**
  79. * @dev is the signature of `this + sender` from a bouncer?
  80. * @return bool
  81. */
  82. function _isValidSignature(address _address, bytes _signature)
  83. internal
  84. view
  85. returns (bool)
  86. {
  87. return _isValidDataHash(
  88. keccak256(abi.encodePacked(address(this), _address)),
  89. _signature
  90. );
  91. }
  92. /**
  93. * @dev is the signature of `this + sender + methodId` from a bouncer?
  94. * @return bool
  95. */
  96. function _isValidSignatureAndMethod(address _address, bytes _signature)
  97. internal
  98. view
  99. returns (bool)
  100. {
  101. bytes memory data = new bytes(METHOD_ID_SIZE);
  102. for (uint i = 0; i < data.length; i++) {
  103. data[i] = msg.data[i];
  104. }
  105. return _isValidDataHash(
  106. keccak256(abi.encodePacked(address(this), _address, data)),
  107. _signature
  108. );
  109. }
  110. /**
  111. * @dev is the signature of `this + sender + methodId + params(s)` from a bouncer?
  112. * @notice the _signature parameter of the method being validated must be the "last" parameter
  113. * @return bool
  114. */
  115. function _isValidSignatureAndData(address _address, bytes _signature)
  116. internal
  117. view
  118. returns (bool)
  119. {
  120. require(msg.data.length > SIGNATURE_SIZE);
  121. bytes memory data = new bytes(msg.data.length - SIGNATURE_SIZE);
  122. for (uint i = 0; i < data.length; i++) {
  123. data[i] = msg.data[i];
  124. }
  125. return _isValidDataHash(
  126. keccak256(abi.encodePacked(address(this), _address, data)),
  127. _signature
  128. );
  129. }
  130. /**
  131. * @dev internal function to convert a hash to an eth signed message
  132. * and then recover the signature and check it against the bouncer role
  133. * @return bool
  134. */
  135. function _isValidDataHash(bytes32 _hash, bytes _signature)
  136. internal
  137. view
  138. returns (bool)
  139. {
  140. address signer = _hash
  141. .toEthSignedMessageHash()
  142. .recover(_signature);
  143. return hasRole(signer, ROLE_BOUNCER);
  144. }
  145. }