-
Notifications
You must be signed in to change notification settings - Fork 0
/
BuggyEscrow.sol
314 lines (278 loc) · 8.01 KB
/
BuggyEscrow.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.11;
// Can use console.log() for debugging.
import "hardhat/console.sol";
import { ECDSA } from "./openzeppelin/ECDSA.sol";
import { IERC20 } from "./openzeppelin/IERC20.sol";
import { BytecodeSalt } from "./BytecodeSalt.sol";
import { Rescuable } from "./Rescuable.sol";
import { SecretKeyOwnable } from "./SecretKeyOwnable.sol";
/**
* @title BuggyEscrow
* @author Royal
*
* @notice A buggy contract that attempts to implement an escrow use case.
*
* THIS CODE CONTAINS VULNERABILITIES. DO NOT USE THIS CODE IN PRODUCTION.
*
* ---------------------------------------------------------------------------
*
* Escrow contract that supports atomic swaps of any two ERC-20 tokens between two parties.
*
* This contract inherits from Rescuable.sol and SecretKeyOwnable.sol.
*
* Within BuggyEscrow.sol we expose 5 external state-changing functions to interact with swaps:
* - initiateSwap
* - initiateSwapWithSig
* - completeSwap
* - completeSwapBySig
* - cancelSwap
*
* CONTRACT LIFECYCLE:
*
* 1. Initiate a swap:
*
* A swap is initiated by specifying the following:
* - The address of each party to the swap
* - The addresses of the tokens to be paid by each party
* - The amount of token to be paid by each party
*
* Upon initiating a swap, party A's tokens are transfered to this contract and held in escrow.
*
* 2. After initiating a swap, one of two things can happen:
*
* - Canceled by party A: Funds are returned to party A from escrow.
*
* - Completed by party B: Party B transfers funds to party A according to the agreed terms.
* Funds are released from escrow to party B.
*/
contract BuggyEscrow is
Rescuable,
BytecodeSalt
{
// ==================== Structs ==================== //
/// @notice The info for one side of a swap.
struct SwapInfo {
address party;
IERC20 token;
uint256 tokenAmount;
}
// ==================== Storage ==================== //
/// @dev Mapping from keccak256(partyA, tokenA, tokenAmountA) => (partyB, tokenB, tokenAmount)
mapping(bytes32 => SwapInfo) internal _PENDING_SWAPS_;
// ==================== Constructor ==================== //
constructor(
string memory secretKey,
bytes32 secretKeyHash
)
SecretKeyOwnable(secretKeyHash)
{
require(
_isSecretKey(secretKey),
"sanity check failed"
);
}
// ==================== External Functions ==================== //
/**
* @notice Initiate a swap.
*
* Party A is authorized by checking msg.sender.
*
* Will revert if party A does not have an ERC-20 allowance set on this contract for token A.
*/
function initiateSwap(
SwapInfo calldata sideA,
SwapInfo calldata sideB
)
external
{
require(
msg.sender == sideA.party,
"sender must be party A"
);
_initiateSwap(sideA, sideB);
}
/**
* @notice Initiate a swap.
*
* Party A is authorized by checking the provided signature.
*
* Will revert if party A does not have an ERC-20 allowance set on this contract for token A.
*/
function initiateSwapWithSig(
SwapInfo calldata sideA,
SwapInfo calldata sideB,
bytes calldata partyASignature
)
external
{
bytes memory message = abi.encode(
sideA.token,
sideA.tokenAmount,
sideB.token,
sideB.tokenAmount
);
_validateSignature(message, partyASignature, sideA.party);
_initiateSwap(sideA, sideB);
}
/**
* @notice Complete a swap.
*
* Party B is authorized by checking msg.sender.
*
* Will revert if party B does not have an ERC-20 allowance set on this contract for token B.
*/
function completeSwap(
SwapInfo calldata sideA
)
external
{
SwapInfo memory sideB = getPendingSwap(sideA);
require(
msg.sender == sideB.party,
"sender must be party B"
);
_completeSwap(sideA, sideB);
}
/**
* @notice Complete a swap.
*
* Party B is authorized by checking the provided signature.
*
* Will revert if party B does not have an ERC-20 allowance set on this contract for token B.
*/
function completeSwapBySig(
SwapInfo calldata sideA,
bytes calldata partyBSignature
)
external
{
SwapInfo memory sideB = getPendingSwap(sideA);
bytes memory message = abi.encode(
sideA.token,
sideA.tokenAmount,
sideB.token,
sideB.tokenAmount
);
_validateSignature(message, partyBSignature, sideB.party);
_completeSwap(sideA, sideB);
}
/**
* @notice Cancel a swap.
*
* Party A is authorized by checking msg.sender.
*/
function cancelSwap(
SwapInfo calldata sideA
)
external
{
require(
msg.sender == sideA.party,
"sender must be party A"
);
_cancelSwap(sideA);
}
// ==================== Public Functions ==================== //
/**
* @notice Get info about a pending swap.
*/
function getPendingSwap(
SwapInfo memory sideA
)
public
view
returns (SwapInfo memory)
{
return _PENDING_SWAPS_[keccak256(abi.encode(sideA))];
}
// ==================== Internal Functions ==================== //
/**
* @notice Set a pending swap.
*/
function _setPendingSwap(
SwapInfo memory sideA,
SwapInfo memory sideB
)
internal
{
_PENDING_SWAPS_[keccak256(abi.encode(sideA))] = sideB;
}
/**
* @notice Delete a pending swap.
*/
function _deletePendingSwap(
SwapInfo memory sideA
)
internal
{
delete _PENDING_SWAPS_[keccak256(abi.encode(sideA))];
}
function _initiateSwap(
SwapInfo memory sideA,
SwapInfo memory sideB
)
internal
{
require(
sideB.party != address(0),
"party B cannot be zero"
);
// Store information about the pending swap.
_setPendingSwap(sideA, sideB);
// Escrow an amount of token A to be transfered to party B upon completion of the swap.
sideA.token.transferFrom(sideA.party, address(this), sideA.tokenAmount);
}
function _completeSwap(
SwapInfo memory sideA,
SwapInfo memory sideB
)
internal
{
// Delete the pending swap.
_deletePendingSwap(sideA);
// Transfer from B -> A.
sideB.token.transferFrom(sideB.party, sideA.party, sideB.tokenAmount);
// Complete the escrowed transfer from A -> B.
sideA.token.transfer(sideB.party, sideA.tokenAmount);
}
function _cancelSwap(
SwapInfo memory sideA
)
internal
{
SwapInfo memory sideB = getPendingSwap(sideA);
require(
sideB.party != address(0),
"swap is not pending"
);
// Delete the pending swap.
_deletePendingSwap(sideA);
// Return funds from escrow to party A.
sideA.token.transferFrom(address(this), sideA.party, sideA.tokenAmount);
}
function _validateSignature(
bytes memory message,
bytes memory signature,
address expectedSigner
)
internal
pure
{
bytes32 digest = ECDSA.toEthSignedMessageHash(keccak256(message));
address recoveredSigner = ECDSA.recover(digest, signature);
require(
recoveredSigner == expectedSigner,
"invalid signer"
);
}
function _getExcessEthAmount()
internal
view
override
returns (uint256)
{
// Any ETH balance is excess since the contract does not expect to hold any ETH.
return address(this).balance;
}
}