-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEscrow.sol
70 lines (57 loc) · 2.18 KB
/
Escrow.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
contract Escrow is ReentrancyGuard {
uint256 public balance;
address public receiver;
address private thirdParty;
address public sender;
address public owner;
bytes32 private encrypted_password;
uint256 public startBlock;
uint256 public endBlock;
event PasswordSet(string message);
event Timeout(string message);
event Transaction(string message);
modifier onlyOwner() {
require(msg.sender == owner, "Only the owner can call this function");
_;
}
modifier onlyThirdParty() {
require(msg.sender == thirdParty, "Only the trusted third party can call this function");
_;
}
constructor(address _receiver, address _thirdParty, uint256 _endBlock) {
require(_endBlock > block.timestamp, "End time must be in the future");
receiver = _receiver;
thirdParty = _thirdParty;
owner = msg.sender;
endBlock = _endBlock;
}
function setPassword(bytes32 password) public onlyThirdParty {
require(password != bytes32(0), "Password cannot be empty");
encrypted_password = password;
emit PasswordSet("Password successfully set");
}
function holdingBank() public payable {
balance += msg.value;
sender = msg.sender;
}
function timeRanOut() public onlyOwner nonReentrant {
require(block.timestamp >= endBlock, "Money is still in escrow until time runs out");
require(balance > 0, "No money sent to the contract yet");
payable(sender).transfer(balance);
emit Timeout("Time ran out");
}
function checkTimeLeft() public view returns (uint256) {
return (block.timestamp >= endBlock) ? 0 : endBlock - block.timestamp;
}
function successfulTransaction(string memory password) public onlyThirdParty nonReentrant {
require(
keccak256(abi.encodePacked(password)) == encrypted_password,
"Password doesn't match"
);
payable(receiver).transfer(balance);
emit Transaction("Password match, successful transaction");
}
}