add more contracts from openzeppelin-solidity
This commit is contained in:
97
contracts/access/SignatureBouncer.sol
Normal file
97
contracts/access/SignatureBouncer.sol
Normal file
@ -0,0 +1,97 @@
|
||||
pragma solidity ^0.4.18;
|
||||
|
||||
import "../ownership/Ownable.sol";
|
||||
import "../ownership/rbac/RBAC.sol";
|
||||
import "../ECRecovery.sol";
|
||||
|
||||
/**
|
||||
* @title SignatureBouncer
|
||||
* @author PhABC and Shrugs
|
||||
* @dev Bouncer allows users to submit a signature as a permission to do an action.
|
||||
* @dev If the signature is from one of the authorized bouncer addresses, the signature
|
||||
* @dev is valid. The owner of the contract adds/removes bouncers.
|
||||
* @dev Bouncer addresses can be individual servers signing grants or different
|
||||
* @dev users within a decentralized club that have permission to invite other members.
|
||||
* @dev
|
||||
* @dev This technique is useful for whitelists and airdrops; instead of putting all
|
||||
* @dev valid addresses on-chain, simply sign a grant of the form
|
||||
* @dev keccak256(`:contractAddress` + `:granteeAddress`) using a valid bouncer address.
|
||||
* @dev Then restrict access to your crowdsale/whitelist/airdrop using the
|
||||
* @dev `onlyValidSignature` modifier (or implement your own using isValidSignature).
|
||||
* @dev
|
||||
* @dev See the tests Bouncer.test.js for specific usage examples.
|
||||
*/
|
||||
contract SignatureBouncer is Migratable, Ownable, RBAC {
|
||||
using ECRecovery for bytes32;
|
||||
|
||||
string public constant ROLE_BOUNCER = "bouncer";
|
||||
|
||||
function initialize(address _sender)
|
||||
isInitializer("SignatureBouncer", "1.9.0-beta")
|
||||
public
|
||||
{
|
||||
Ownable.initialize(_sender);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev requires that a valid signature of a bouncer was provided
|
||||
*/
|
||||
modifier onlyValidSignature(bytes _sig)
|
||||
{
|
||||
require(isValidSignature(msg.sender, _sig));
|
||||
_;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev allows the owner to add additional bouncer addresses
|
||||
*/
|
||||
function addBouncer(address _bouncer)
|
||||
onlyOwner
|
||||
public
|
||||
{
|
||||
require(_bouncer != address(0));
|
||||
addRole(_bouncer, ROLE_BOUNCER);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev allows the owner to remove bouncer addresses
|
||||
*/
|
||||
function removeBouncer(address _bouncer)
|
||||
onlyOwner
|
||||
public
|
||||
{
|
||||
require(_bouncer != address(0));
|
||||
removeRole(_bouncer, ROLE_BOUNCER);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev is the signature of `this + sender` from a bouncer?
|
||||
* @return bool
|
||||
*/
|
||||
function isValidSignature(address _address, bytes _sig)
|
||||
internal
|
||||
view
|
||||
returns (bool)
|
||||
{
|
||||
return isValidDataHash(
|
||||
keccak256(address(this), _address),
|
||||
_sig
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev internal function to convert a hash to an eth signed message
|
||||
* @dev and then recover the signature and check it against the bouncer role
|
||||
* @return bool
|
||||
*/
|
||||
function isValidDataHash(bytes32 hash, bytes _sig)
|
||||
internal
|
||||
view
|
||||
returns (bool)
|
||||
{
|
||||
address signer = hash
|
||||
.toEthSignedMessageHash()
|
||||
.recover(_sig);
|
||||
return hasRole(signer, ROLE_BOUNCER);
|
||||
}
|
||||
}
|
||||
29
contracts/mocks/BouncerMock.sol
Normal file
29
contracts/mocks/BouncerMock.sol
Normal file
@ -0,0 +1,29 @@
|
||||
pragma solidity ^0.4.18;
|
||||
|
||||
import "../access/SignatureBouncer.sol";
|
||||
|
||||
|
||||
contract SignatureBouncerMock is SignatureBouncer {
|
||||
function initialize(address _sender)
|
||||
isInitializer("SignatureBouncerMock", "1.9.0-beta")
|
||||
public
|
||||
{
|
||||
SignatureBouncer.initialize(_sender);
|
||||
}
|
||||
|
||||
function checkValidSignature(address _address, bytes _sig)
|
||||
public
|
||||
view
|
||||
returns (bool)
|
||||
{
|
||||
return isValidSignature(_address, _sig);
|
||||
}
|
||||
|
||||
function onlyWithValidSignature(bytes _sig)
|
||||
onlyValidSignature(_sig)
|
||||
public
|
||||
view
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
76
contracts/payment/SplitPayment.sol
Normal file
76
contracts/payment/SplitPayment.sol
Normal file
@ -0,0 +1,76 @@
|
||||
pragma solidity ^0.4.21;
|
||||
|
||||
import "../math/SafeMath.sol";
|
||||
import "zos-lib/contracts/migrations/Migratable.sol";
|
||||
|
||||
|
||||
/**
|
||||
* @title SplitPayment
|
||||
* @dev Base contract that supports multiple payees claiming funds sent to this contract
|
||||
* according to the proportion they own.
|
||||
*/
|
||||
contract SplitPayment is Migratable {
|
||||
using SafeMath for uint256;
|
||||
|
||||
uint256 public totalShares = 0;
|
||||
uint256 public totalReleased = 0;
|
||||
|
||||
mapping(address => uint256) public shares;
|
||||
mapping(address => uint256) public released;
|
||||
address[] public payees;
|
||||
|
||||
/**
|
||||
* @dev Constructor
|
||||
*/
|
||||
function initialize(address[] _payees, uint256[] _shares)
|
||||
isInitializer("SplitPayment", "1.9.0-beta")
|
||||
public
|
||||
payable
|
||||
{
|
||||
require(_payees.length == _shares.length);
|
||||
|
||||
for (uint256 i = 0; i < _payees.length; i++) {
|
||||
addPayee(_payees[i], _shares[i]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev payable fallback
|
||||
*/
|
||||
function () public payable {}
|
||||
|
||||
/**
|
||||
* @dev Claim your share of the balance.
|
||||
*/
|
||||
function claim() public {
|
||||
address payee = msg.sender;
|
||||
|
||||
require(shares[payee] > 0);
|
||||
|
||||
uint256 totalReceived = address(this).balance.add(totalReleased);
|
||||
uint256 payment = totalReceived.mul(shares[payee]).div(totalShares).sub(released[payee]);
|
||||
|
||||
require(payment != 0);
|
||||
require(address(this).balance >= payment);
|
||||
|
||||
released[payee] = released[payee].add(payment);
|
||||
totalReleased = totalReleased.add(payment);
|
||||
|
||||
payee.transfer(payment);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Add a new payee to the contract.
|
||||
* @param _payee The address of the payee to add.
|
||||
* @param _shares The number of shares owned by the payee.
|
||||
*/
|
||||
function addPayee(address _payee, uint256 _shares) internal {
|
||||
require(_payee != address(0));
|
||||
require(_shares > 0);
|
||||
require(shares[_payee] == 0);
|
||||
|
||||
payees.push(_payee);
|
||||
shares[_payee] = _shares;
|
||||
totalShares = totalShares.add(_shares);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user