Skip to content

Instantly share code, notes, and snippets.

@MCarlomagno
Last active February 20, 2022 18:32
Show Gist options
  • Save MCarlomagno/0fc345e2a75dbd04cc51855f34068b91 to your computer and use it in GitHub Desktop.
Save MCarlomagno/0fc345e2a75dbd04cc51855f34068b91 to your computer and use it in GitHub Desktop.
pragma solidity >=0.7.0 <0.9.0;
import "github.com/OpenZeppelin/openzeppelin-contracts/blob/release-v3.3/contracts/cryptography/ECDSA.sol";
import "github.com/OpenZeppelin/openzeppelin-contracts/blob/release-v3.3/contracts/utils/ReentrancyGuard.sol";
contract UniDirectionalPaymentChannel is ReentrancyGuard {
using ECDSA for bytes32;
address payable public sender;
address payable public receiver;
constructor(address payable _receiver) payable {
require(_receiver != address(0), "receiver = zero address");
sender = msg.sender;
receiver = _receiver;
}
function _getHash(uint _amount) private view returns (bytes32) {
return keccak256(abi.encodePacked(address(this), _amount));
}
function getHash(uint _amount) external view returns (bytes32) {
return _getHash(_amount);
}
function _getEthSignedHash(uint _amount) private view returns (bytes32) {
return _getHash(_amount).toEthSignedMessageHash();
}
function getEthSignedHash(uint _amount) external view returns (bytes32) {
return _getEthSignedHash(_amount);
}
function _verify(uint _amount, bytes memory _sig) private view returns (bool) {
return _getEthSignedHash(_amount).recover(_sig) == sender;
}
function verify(uint _amount, bytes memory _sig) external view returns (bool) {
return _verify(_amount, _sig);
}
function send(uint _amount, bytes memory _sig) external nonReentrant {
// verifing signature and sender != receiver
require(msg.sender != receiver, "sender must be different than receiver");
require(_verify(_amount, _sig), "invalid signature");
(bool sent, ) = receiver.call{value: _amount}("");
require(sent, "Failed to send Ether");
selfdestruct(sender);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment