chequebook.sol 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. pragma solidity ^0.4.18;
  2. import "./mortal.sol";
  3. /// @title Chequebook for Ethereum micropayments
  4. /// @author Daniel A. Nagy <daniel@ethereum.org>
  5. contract chequebook is mortal {
  6. // Cumulative paid amount in wei to each beneficiary
  7. mapping (address => uint256) public sent;
  8. /// @notice Overdraft event
  9. event Overdraft(address deadbeat);
  10. // Allow sending ether to the chequebook.
  11. function() public payable { }
  12. /// @notice Cash cheque
  13. ///
  14. /// @param beneficiary beneficiary address
  15. /// @param amount cumulative amount in wei
  16. /// @param sig_v signature parameter v
  17. /// @param sig_r signature parameter r
  18. /// @param sig_s signature parameter s
  19. /// The digital signature is calculated on the concatenated triplet of contract address, beneficiary address and cumulative amount
  20. function cash(address beneficiary, uint256 amount, uint8 sig_v, bytes32 sig_r, bytes32 sig_s) public {
  21. // Check if the cheque is old.
  22. // Only cheques that are more recent than the last cashed one are considered.
  23. require(amount > sent[beneficiary]);
  24. // Check the digital signature of the cheque.
  25. bytes32 hash = keccak256(address(this), beneficiary, amount);
  26. require(owner == ecrecover(hash, sig_v, sig_r, sig_s));
  27. // Attempt sending the difference between the cumulative amount on the cheque
  28. // and the cumulative amount on the last cashed cheque to beneficiary.
  29. uint256 diff = amount - sent[beneficiary];
  30. if (diff <= this.balance) {
  31. // update the cumulative amount before sending
  32. sent[beneficiary] = amount;
  33. beneficiary.transfer(diff);
  34. } else {
  35. // Upon failure, punish owner for writing a bounced cheque.
  36. // owner.sendToDebtorsPrison();
  37. Overdraft(owner);
  38. // Compensate beneficiary.
  39. selfdestruct(beneficiary);
  40. }
  41. }
  42. }