2017-11-10 03:19:51 -06:00
|
|
|
pragma solidity ^0.4.18;
|
|
|
|
|
2018-01-08 06:15:57 -06:00
|
|
|
import "./mortal.sol";
|
2016-08-28 06:34:59 -05:00
|
|
|
|
|
|
|
/// @title Chequebook for Ethereum micropayments
|
2017-11-10 03:19:51 -06:00
|
|
|
/// @author Daniel A. Nagy <daniel@ethereum.org>
|
2016-08-28 06:34:59 -05:00
|
|
|
contract chequebook is mortal {
|
|
|
|
// Cumulative paid amount in wei to each beneficiary
|
|
|
|
mapping (address => uint256) public sent;
|
|
|
|
|
|
|
|
/// @notice Overdraft event
|
|
|
|
event Overdraft(address deadbeat);
|
|
|
|
|
2018-01-08 06:15:57 -06:00
|
|
|
// Allow sending ether to the chequebook.
|
|
|
|
function() public payable { }
|
|
|
|
|
2016-08-28 06:34:59 -05:00
|
|
|
/// @notice Cash cheque
|
|
|
|
///
|
|
|
|
/// @param beneficiary beneficiary address
|
|
|
|
/// @param amount cumulative amount in wei
|
|
|
|
/// @param sig_v signature parameter v
|
|
|
|
/// @param sig_r signature parameter r
|
|
|
|
/// @param sig_s signature parameter s
|
|
|
|
/// The digital signature is calculated on the concatenated triplet of contract address, beneficiary address and cumulative amount
|
2018-01-08 06:15:57 -06:00
|
|
|
function cash(address beneficiary, uint256 amount, uint8 sig_v, bytes32 sig_r, bytes32 sig_s) public {
|
2016-08-28 06:34:59 -05:00
|
|
|
// Check if the cheque is old.
|
|
|
|
// Only cheques that are more recent than the last cashed one are considered.
|
2017-11-10 03:19:51 -06:00
|
|
|
require(amount > sent[beneficiary]);
|
2016-08-28 06:34:59 -05:00
|
|
|
// Check the digital signature of the cheque.
|
2017-11-10 03:19:51 -06:00
|
|
|
bytes32 hash = keccak256(address(this), beneficiary, amount);
|
|
|
|
require(owner == ecrecover(hash, sig_v, sig_r, sig_s));
|
2016-08-28 06:34:59 -05:00
|
|
|
// Attempt sending the difference between the cumulative amount on the cheque
|
|
|
|
// and the cumulative amount on the last cashed cheque to beneficiary.
|
2017-09-25 03:36:13 -05:00
|
|
|
uint256 diff = amount - sent[beneficiary];
|
|
|
|
if (diff <= this.balance) {
|
2018-01-08 06:15:57 -06:00
|
|
|
// update the cumulative amount before sending
|
2016-08-30 08:53:14 -05:00
|
|
|
sent[beneficiary] = amount;
|
2017-11-10 03:19:51 -06:00
|
|
|
beneficiary.transfer(diff);
|
2016-08-28 06:34:59 -05:00
|
|
|
} else {
|
|
|
|
// Upon failure, punish owner for writing a bounced cheque.
|
|
|
|
// owner.sendToDebtorsPrison();
|
|
|
|
Overdraft(owner);
|
|
|
|
// Compensate beneficiary.
|
2017-11-10 03:19:51 -06:00
|
|
|
selfdestruct(beneficiary);
|
2016-08-28 06:34:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|