-
Notifications
You must be signed in to change notification settings - Fork 0
/
Escrow.sol
85 lines (65 loc) · 3.19 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
pragma solidity >=0.4.22 <0.6.0;
contract Escrow {
/*static variables after contract construction*/
uint public funds;
uint created = block.number;
uint expiration;
address payable seller;
address payable buyer;
/*state of the escrow*/
bytes32 buyerHash;
string expeditionNumber;
bool debug;
bool paid;
bool refunded;
constructor(address payable _seller, address payable _buyer, bool _debug, uint _expiration) public payable {
require(_seller != _buyer);
require(msg.value > 0);
seller = _seller;
buyer = _buyer;
debug = _debug;
expiration = created+_expiration;
}
function setHash(bytes32 hash) public {
require(block.number <= expiration, "Buyer cannot set hash if auction has expired");
require(msg.sender == buyer, "only the buyer can communicate the hash");
buyerHash = hash;
}
function setExpeditionNumber(string memory en) public {
require(bytes(expeditionNumber).length == 0, "Expedition number can be set only once");
require(bytes(en).length != 0, "Expedition number cannot have zero length");
require(msg.sender == seller, "Only the seller can communicate expedition number");
require(block.number <= expiration, "Expedition number cannot be communicate after escrow expiration");
expeditionNumber = en; /// suppose it is valid
}
function verifyHash(uint nonce) public {
require(!paid, "The good has been already paid");
require(msg.sender == seller && buyerHash != "", "Only the seller can verify the hash of the buyer");
require(block.number <= expiration, "hash can be verified only before expiration");
bytes32 sellerHash = keccak256(abi.encode(nonce));
require(sellerHash == buyerHash);
paid = true;
seller.transfer(funds);
}
function refundSeller() public {
require(block.number > expiration, "Seller can ask refund only after escrow expiration");
require(bytes(expeditionNumber).length > 0, "Seller must have provided the expedition number before asking a refund");
require(msg.sender == seller, "Only the sller can call this function");
require(!paid && !refunded, "seller can be refunded only once and only if he has not been paid yet");
paid = true;
seller.transfer(funds);
}
function refundBuyer() public {
require(block.number > expiration, "buyer can ask refund only after escrow expiration");
require(bytes(expeditionNumber).length == 0, "buyer can be refunded only if the sleer has not provided a valid expedition number");
require(msg.sender == buyer, "Only the buyer canc all this function");
require(buyerHash != "", "The buyer must have provided an hash in order to be refunded");
require(!refunded && !paid, "The buyer can be refunded only once and only if the good has not been paid yet");
refunded = true;
buyer.transfer(funds);
}
function balance () public view returns (uint){
require(debug);
return address(this).balance;
}
}