-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimelockprevention.sol
More file actions
53 lines (38 loc) · 1.32 KB
/
timelockprevention.sol
File metadata and controls
53 lines (38 loc) · 1.32 KB
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
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/math/SafeMath.sol";
contract TimeLock{
using SafeMath for uint;
mapping(address => uint) public balances;
mapping(address => uint) public lockTime;
function deposit() external payable{
balances[msg.sender] += msg.value;
lockTime[msg.sender] = block.timestamp + 1 weeks;
}
function increaselocktime(uint _time) public {
//lockTime[msg.sender] += _time;
lockTime[msg.sender] = lockTime[msg.sender].add(_time);
}
function withdraw() public {
require(balances[msg.sender] > 0, "You don't have enough ether");
require(block.timestamp > lockTime[msg.sender], "The time has not expired");
uint balance = balances[msg.sender]; //balance of msg.sender
balances[msg.sender] = 0;
(bool success,) = msg.sender.call{value: balance}("");
require(success, "It did not go through");
}
}
contract Attack {
TimeLock timeLock;
constructor(TimeLock _timeLock) {
timeLock = TimeLock(_timeLock);
}
fallback() external payable {}
function attack() public payable {
timeLock.deposit{value: msg.value}();
timeLock.increaselocktime(
type(uint).max + 1 - timeLock.lockTime(address(this))
);
timeLock.withdraw();
}
}