SignatureBouncer.sol 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. pragma solidity ^0.4.23;
  2. import "../ownership/Ownable.sol";
  3. import "../ownership/rbac/RBAC.sol";
  4. import "../ECRecovery.sol";
  5. /**
  6. * @title SignatureBouncer
  7. * @author PhABC and Shrugs
  8. * @dev Bouncer allows users to submit a signature as a permission to do an action.
  9. * @dev If the signature is from one of the authorized bouncer addresses, the signature
  10. * @dev is valid. The owner of the contract adds/removes bouncers.
  11. * @dev Bouncer addresses can be individual servers signing grants or different
  12. * @dev users within a decentralized club that have permission to invite other members.
  13. * @dev
  14. * @dev This technique is useful for whitelists and airdrops; instead of putting all
  15. * @dev valid addresses on-chain, simply sign a grant of the form
  16. * @dev keccak256(`:contractAddress` + `:granteeAddress`) using a valid bouncer address.
  17. * @dev Then restrict access to your crowdsale/whitelist/airdrop using the
  18. * @dev `onlyValidSignature` modifier (or implement your own using isValidSignature).
  19. * @dev
  20. * @dev See the tests Bouncer.test.js for specific usage examples.
  21. */
  22. contract SignatureBouncer is Ownable, RBAC {
  23. using ECRecovery for bytes32;
  24. string public constant ROLE_BOUNCER = "bouncer";
  25. /**
  26. * @dev requires that a valid signature of a bouncer was provided
  27. */
  28. modifier onlyValidSignature(bytes _sig)
  29. {
  30. require(isValidSignature(msg.sender, _sig));
  31. _;
  32. }
  33. /**
  34. * @dev allows the owner to add additional bouncer addresses
  35. */
  36. function addBouncer(address _bouncer)
  37. onlyOwner
  38. public
  39. {
  40. require(_bouncer != address(0));
  41. addRole(_bouncer, ROLE_BOUNCER);
  42. }
  43. /**
  44. * @dev allows the owner to remove bouncer addresses
  45. */
  46. function removeBouncer(address _bouncer)
  47. onlyOwner
  48. public
  49. {
  50. require(_bouncer != address(0));
  51. removeRole(_bouncer, ROLE_BOUNCER);
  52. }
  53. /**
  54. * @dev is the signature of `this + sender` from a bouncer?
  55. * @return bool
  56. */
  57. function isValidSignature(address _address, bytes _sig)
  58. internal
  59. view
  60. returns (bool)
  61. {
  62. return isValidDataHash(
  63. keccak256(address(this), _address),
  64. _sig
  65. );
  66. }
  67. /**
  68. * @dev internal function to convert a hash to an eth signed message
  69. * @dev and then recover the signature and check it against the bouncer role
  70. * @return bool
  71. */
  72. function isValidDataHash(bytes32 hash, bytes _sig)
  73. internal
  74. view
  75. returns (bool)
  76. {
  77. address signer = hash
  78. .toEthSignedMessageHash()
  79. .recover(_sig);
  80. return hasRole(signer, ROLE_BOUNCER);
  81. }
  82. }