-
Notifications
You must be signed in to change notification settings - Fork 17
/
rockpaperscissors.sol
231 lines (195 loc) · 8.32 KB
/
rockpaperscissors.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
contract RockPaperScissors {
uint constant public BET_MIN = 1e16; // The minimum bet (1 finney)
uint constant public REVEAL_TIMEOUT = 10 minutes; // Max delay of revelation phase
uint public initialBet; // Bet of first player
uint private firstReveal; // Moment of first reveal
enum Moves {None, Rock, Paper, Scissors}
enum Outcomes {None, PlayerA, PlayerB, Draw} // Possible outcomes
// Players' addresses
address payable playerA;
address payable playerB;
// Encrypted moves
bytes32 private encrMovePlayerA;
bytes32 private encrMovePlayerB;
// Clear moves set only after both players have committed their encrypted moves
Moves private movePlayerA;
Moves private movePlayerB;
/**************************************************************************/
/*************************** REGISTRATION PHASE ***************************/
/**************************************************************************/
// Bet must be greater than a minimum amount and greater than bet of first player
modifier validBet() {
require(msg.value >= BET_MIN);
require(initialBet == 0 || msg.value >= initialBet);
_;
}
modifier notAlreadyRegistered() {
require(msg.sender != playerA && msg.sender != playerB);
_;
}
// Register a player.
// Return player's ID upon successful registration.
function register() public payable validBet notAlreadyRegistered returns (uint) {
if (playerA == address(0x0)) {
playerA = payable(msg.sender);
initialBet = msg.value;
return 1;
} else if (playerB == address(0x0)) {
playerB = payable(msg.sender);
return 2;
}
return 0;
}
/**************************************************************************/
/****************************** COMMIT PHASE ******************************/
/**************************************************************************/
modifier isRegistered() {
require (msg.sender == playerA || msg.sender == playerB);
_;
}
// Save player's encrypted move.
// Return 'true' if move was valid, 'false' otherwise.
function play(bytes32 encrMove) public isRegistered returns (bool) {
if (msg.sender == playerA && encrMovePlayerA == 0x0) {
encrMovePlayerA = encrMove;
} else if (msg.sender == playerB && encrMovePlayerB == 0x0) {
encrMovePlayerB = encrMove;
} else {
return false;
}
return true;
}
/**************************************************************************/
/****************************** REVEAL PHASE ******************************/
/**************************************************************************/
modifier commitPhaseEnded() {
require(encrMovePlayerA != 0x0 && encrMovePlayerB != 0x0);
_;
}
// Compare clear move given by the player with saved encrypted move.
// Return clear move upon success, 'Moves.None' otherwise.
function reveal(string memory clearMove) public isRegistered commitPhaseEnded returns (Moves) {
bytes32 encrMove = sha256(abi.encodePacked(clearMove)); // Hash of clear input (= "move-password")
Moves move = Moves(getFirstChar(clearMove)); // Actual move (Rock / Paper / Scissors)
// If move invalid, exit
if (move == Moves.None) {
return Moves.None;
}
// If hashes match, clear move is saved
if (msg.sender == playerA && encrMove == encrMovePlayerA) {
movePlayerA = move;
} else if (msg.sender == playerB && encrMove == encrMovePlayerB) {
movePlayerB = move;
} else {
return Moves.None;
}
// Timer starts after first revelation from one of the player
if (firstReveal == 0) {
firstReveal = block.timestamp;
}
return move;
}
// Return first character of a given string.
function getFirstChar(string memory str) private pure returns (uint) {
bytes1 firstByte = bytes(str)[0];
if (firstByte == 0x31) {
return 1;
} else if (firstByte == 0x32) {
return 2;
} else if (firstByte == 0x33) {
return 3;
} else {
return 0;
}
}
/**************************************************************************/
/****************************** RESULT PHASE ******************************/
/**************************************************************************/
modifier revealPhaseEnded() {
require((movePlayerA != Moves.None && movePlayerB != Moves.None) ||
(firstReveal != 0 && block.timestamp > firstReveal + REVEAL_TIMEOUT));
_;
}
// Compute the outcome and pay the winner(s).
// Return the outcome.
function getOutcome() public revealPhaseEnded returns (Outcomes) {
Outcomes outcome;
if (movePlayerA == movePlayerB) {
outcome = Outcomes.Draw;
} else if ((movePlayerA == Moves.Rock && movePlayerB == Moves.Scissors) ||
(movePlayerA == Moves.Paper && movePlayerB == Moves.Rock) ||
(movePlayerA == Moves.Scissors && movePlayerB == Moves.Paper) ||
(movePlayerA != Moves.None && movePlayerB == Moves.None)) {
outcome = Outcomes.PlayerA;
} else {
outcome = Outcomes.PlayerB;
}
address payable addrA = playerA;
address payable addrB = playerB;
uint betPlayerA = initialBet;
reset(); // Reset game before paying to avoid reentrancy attacks
pay(addrA, addrB, betPlayerA, outcome);
return outcome;
}
// Pay the winner(s).
function pay(address payable addrA, address payable addrB, uint betPlayerA, Outcomes outcome) private {
// Uncomment lines below if you need to adjust the gas limit
if (outcome == Outcomes.PlayerA) {
addrA.transfer(address(this).balance);
// addrA.call.value(address(this).balance).gas(1000000)("");
} else if (outcome == Outcomes.PlayerB) {
addrB.transfer(address(this).balance);
// addrB.call.value(address(this).balance).gas(1000000)("");
} else {
addrA.transfer(betPlayerA);
addrB.transfer(address(this).balance);
// addrA.call.value(betPlayerA).gas(1000000)("");
// addrB.call.value(address(this).balance).gas(1000000)("");
}
}
// Reset the game.
function reset() private {
initialBet = 0;
firstReveal = 0;
playerA = payable(address(0x0));
playerB = payable(address(0x0));
encrMovePlayerA = 0x0;
encrMovePlayerB = 0x0;
movePlayerA = Moves.None;
movePlayerB = Moves.None;
}
/**************************************************************************/
/**************************** HELPER FUNCTIONS ****************************/
/**************************************************************************/
// Return contract balance
function getContractBalance() public view returns (uint) {
return address(this).balance;
}
// Return player's ID
function whoAmI() public view returns (uint) {
if (msg.sender == playerA) {
return 1;
} else if (msg.sender == playerB) {
return 2;
} else {
return 0;
}
}
// Return 'true' if both players have commited a move, 'false' otherwise.
function bothPlayed() public view returns (bool) {
return (encrMovePlayerA != 0x0 && encrMovePlayerB != 0x0);
}
// Return 'true' if both players have revealed their move, 'false' otherwise.
function bothRevealed() public view returns (bool) {
return (movePlayerA != Moves.None && movePlayerB != Moves.None);
}
// Return time left before the end of the revelation phase.
function revealTimeLeft() public view returns (int) {
if (firstReveal != 0) {
return int((firstReveal + REVEAL_TIMEOUT) - block.timestamp);
}
return int(REVEAL_TIMEOUT);
}
}