SignatureBouncer.sol 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. pragma solidity ^0.4.24;
  2. import "../ownership/Ownable.sol";
  3. import "../access/rbac/RBAC.sol";
  4. import "../ECRecovery.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 _sig
  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 _sig data and the _sig 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 ECRecovery for bytes32;
  31. string public constant ROLE_BOUNCER = "bouncer";
  32. uint constant METHOD_ID_SIZE = 4;
  33. // signature size is 65 bytes (tightly packed v + r + s), but gets padded to 96 bytes
  34. uint constant SIGNATURE_SIZE = 96;
  35. /**
  36. * @dev requires that a valid signature of a bouncer was provided
  37. */
  38. modifier onlyValidSignature(bytes _sig)
  39. {
  40. require(isValidSignature(msg.sender, _sig));
  41. _;
  42. }
  43. /**
  44. * @dev requires that a valid signature with a specifed method of a bouncer was provided
  45. */
  46. modifier onlyValidSignatureAndMethod(bytes _sig)
  47. {
  48. require(isValidSignatureAndMethod(msg.sender, _sig));
  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 _sig)
  55. {
  56. require(isValidSignatureAndData(msg.sender, _sig));
  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. require(_bouncer != address(0));
  77. removeRole(_bouncer, ROLE_BOUNCER);
  78. }
  79. /**
  80. * @dev is the signature of `this + sender` from a bouncer?
  81. * @return bool
  82. */
  83. function isValidSignature(address _address, bytes _sig)
  84. internal
  85. view
  86. returns (bool)
  87. {
  88. return isValidDataHash(
  89. keccak256(abi.encodePacked(address(this), _address)),
  90. _sig
  91. );
  92. }
  93. /**
  94. * @dev is the signature of `this + sender + methodId` from a bouncer?
  95. * @return bool
  96. */
  97. function isValidSignatureAndMethod(address _address, bytes _sig)
  98. internal
  99. view
  100. returns (bool)
  101. {
  102. bytes memory data = new bytes(METHOD_ID_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), _address, data)),
  108. _sig
  109. );
  110. }
  111. /**
  112. * @dev is the signature of `this + sender + methodId + params(s)` from a bouncer?
  113. * @notice the _sig parameter of the method being validated must be the "last" parameter
  114. * @return bool
  115. */
  116. function isValidSignatureAndData(address _address, bytes _sig)
  117. internal
  118. view
  119. returns (bool)
  120. {
  121. require(msg.data.length > SIGNATURE_SIZE);
  122. bytes memory data = new bytes(msg.data.length - SIGNATURE_SIZE);
  123. for (uint i = 0; i < data.length; i++) {
  124. data[i] = msg.data[i];
  125. }
  126. return isValidDataHash(
  127. keccak256(abi.encodePacked(address(this), _address, data)),
  128. _sig
  129. );
  130. }
  131. /**
  132. * @dev internal function to convert a hash to an eth signed message
  133. * and then recover the signature and check it against the bouncer role
  134. * @return bool
  135. */
  136. function isValidDataHash(bytes32 _hash, bytes _sig)
  137. internal
  138. view
  139. returns (bool)
  140. {
  141. address signer = _hash
  142. .toEthSignedMessageHash()
  143. .recover(_sig);
  144. return hasRole(signer, ROLE_BOUNCER);
  145. }
  146. }