@1inch/solidity-utils 2.0.19 → 2.0.22
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -3,18 +3,19 @@
|
|
|
3
3
|
pragma solidity ^0.8.0;
|
|
4
4
|
pragma abicoder v1;
|
|
5
5
|
|
|
6
|
+
import "./interfaces/IWETH.sol";
|
|
7
|
+
|
|
6
8
|
abstract contract OnlyWethReceiver {
|
|
7
9
|
error EthDepositRejected();
|
|
8
10
|
|
|
9
|
-
// solhint-disable-
|
|
10
|
-
address internal immutable _WETH;
|
|
11
|
+
IWETH internal immutable _WETH; // solhint-disable-line var-name-mixedcase
|
|
11
12
|
|
|
12
|
-
constructor(
|
|
13
|
+
constructor(IWETH weth) {
|
|
13
14
|
_WETH = weth;
|
|
14
15
|
}
|
|
15
16
|
|
|
16
17
|
receive() external payable {
|
|
17
18
|
// solhint-disable-next-line avoid-tx-origin
|
|
18
|
-
if (msg.sender != _WETH) revert EthDepositRejected();
|
|
19
|
+
if (msg.sender != address(_WETH)) revert EthDepositRejected();
|
|
19
20
|
}
|
|
20
21
|
}
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
|
|
3
|
+
pragma solidity ^0.8.0;
|
|
4
|
+
pragma abicoder v1;
|
|
5
|
+
|
|
6
|
+
import "@openzeppelin/contracts/interfaces/IERC1271.sol";
|
|
7
|
+
|
|
8
|
+
library ECDSA {
|
|
9
|
+
function recover(bytes32 hash, uint8 v, bytes32 r, bytes32 s) internal view returns(address signer) {
|
|
10
|
+
/// @solidity memory-safe-assembly
|
|
11
|
+
assembly { // solhint-disable-line no-inline-assembly
|
|
12
|
+
let ptr := mload(0x40)
|
|
13
|
+
|
|
14
|
+
mstore(ptr, hash)
|
|
15
|
+
mstore(add(ptr, 0x20), v)
|
|
16
|
+
mstore(add(ptr, 0x40), r)
|
|
17
|
+
mstore(add(ptr, 0x60), s)
|
|
18
|
+
if staticcall(gas(), 0x1, ptr, 0x80, 0, 0x20) {
|
|
19
|
+
signer := mload(0)
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function recover(bytes32 hash, bytes32 r, bytes32 vs) internal view returns(address signer) {
|
|
25
|
+
/// @solidity memory-safe-assembly
|
|
26
|
+
assembly { // solhint-disable-line no-inline-assembly
|
|
27
|
+
let ptr := mload(0x40)
|
|
28
|
+
|
|
29
|
+
mstore(ptr, hash)
|
|
30
|
+
mstore(add(ptr, 0x20), add(27, shr(255, vs)))
|
|
31
|
+
mstore(add(ptr, 0x40), r)
|
|
32
|
+
mstore(add(ptr, 0x60), shr(1, shl(1, vs)))
|
|
33
|
+
if staticcall(gas(), 0x1, ptr, 0x80, 0, 0x20) {
|
|
34
|
+
signer := mload(0)
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function recover(bytes32 hash, bytes calldata signature) internal view returns(address signer) {
|
|
40
|
+
/// @solidity memory-safe-assembly
|
|
41
|
+
assembly { // solhint-disable-line no-inline-assembly
|
|
42
|
+
let ptr := mload(0x40)
|
|
43
|
+
|
|
44
|
+
// memory[ptr:ptr+0x80] = (hash, v, r, s)
|
|
45
|
+
switch signature.length
|
|
46
|
+
case 65 {
|
|
47
|
+
// memory[ptr+0x20:ptr+0x80] = (v, r, s)
|
|
48
|
+
mstore(add(ptr, 0x20), byte(0, calldataload(add(signature.offset, 0x40))))
|
|
49
|
+
calldatacopy(add(ptr, 0x40), signature.offset, 0x40)
|
|
50
|
+
}
|
|
51
|
+
case 64 {
|
|
52
|
+
// memory[ptr+0x20:ptr+0x80] = (v, r, s)
|
|
53
|
+
let vs := calldataload(add(signature.offset, 0x20))
|
|
54
|
+
mstore(add(ptr, 0x20), add(27, shr(255, vs)))
|
|
55
|
+
calldatacopy(add(ptr, 0x40), signature.offset, 0x20)
|
|
56
|
+
mstore(add(ptr, 0x60), shr(1, shl(1, vs)))
|
|
57
|
+
}
|
|
58
|
+
default {
|
|
59
|
+
ptr := 0
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if ptr {
|
|
63
|
+
if gt(mload(add(ptr, 0x60)), 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0) {
|
|
64
|
+
ptr := 0
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if ptr {
|
|
68
|
+
// memory[ptr:ptr+0x20] = (hash)
|
|
69
|
+
mstore(ptr, hash)
|
|
70
|
+
|
|
71
|
+
if staticcall(gas(), 0x1, ptr, 0x80, 0, 0x20) {
|
|
72
|
+
signer := mload(0)
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function recoverOrIsValidSignature(address signer, bytes32 hash, bytes calldata signature) internal view returns(bool success) {
|
|
80
|
+
if ((signature.length == 64 || signature.length == 65) && recover(hash, signature) == signer) {
|
|
81
|
+
return true;
|
|
82
|
+
}
|
|
83
|
+
return isValidSignature(signer, hash, signature);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function recoverOrIsValidSignature(address signer, bytes32 hash, uint8 v, bytes32 r, bytes32 s) internal view returns(bool success) {
|
|
87
|
+
if (recover(hash, v, r, s) == signer) {
|
|
88
|
+
return true;
|
|
89
|
+
}
|
|
90
|
+
return isValidSignature(signer, hash, v, r, s);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function recoverOrIsValidSignature(address signer, bytes32 hash, bytes32 r, bytes32 vs) internal view returns(bool success) {
|
|
94
|
+
if (recover(hash, r, vs) == signer) {
|
|
95
|
+
return true;
|
|
96
|
+
}
|
|
97
|
+
return isValidSignature(signer, hash, r, vs);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function recoverOrIsValidSignature65(address signer, bytes32 hash, bytes32 r, bytes32 vs) internal view returns(bool success) {
|
|
101
|
+
if (recover(hash, r, vs) == signer) {
|
|
102
|
+
return true;
|
|
103
|
+
}
|
|
104
|
+
return isValidSignature65(signer, hash, r, vs);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function isValidSignature(address signer, bytes32 hash, bytes calldata signature) internal view returns(bool success) {
|
|
108
|
+
// (bool success, bytes memory data) = signer.staticcall(abi.encodeWithSelector(IERC1271.isValidSignature.selector, hash, signature));
|
|
109
|
+
// return success && data.length >= 4 && abi.decode(data, (bytes4)) == IERC1271.isValidSignature.selector;
|
|
110
|
+
bytes4 selector = IERC1271.isValidSignature.selector;
|
|
111
|
+
/// @solidity memory-safe-assembly
|
|
112
|
+
assembly { // solhint-disable-line no-inline-assembly
|
|
113
|
+
let ptr := mload(0x40)
|
|
114
|
+
let len := add(0x64, signature.length)
|
|
115
|
+
|
|
116
|
+
mstore(ptr, selector)
|
|
117
|
+
mstore(add(ptr, 0x04), hash)
|
|
118
|
+
mstore(add(ptr, 0x24), 0x40)
|
|
119
|
+
mstore(add(ptr, 0x44), signature.length)
|
|
120
|
+
calldatacopy(add(ptr, 0x64), signature.offset, signature.length)
|
|
121
|
+
mstore(0, 0)
|
|
122
|
+
if staticcall(gas(), signer, ptr, len, 0, 0x20) {
|
|
123
|
+
success := eq(selector, mload(0))
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function isValidSignature(address signer, bytes32 hash, uint8 v, bytes32 r, bytes32 s) internal view returns(bool success) {
|
|
129
|
+
bytes4 selector = IERC1271.isValidSignature.selector;
|
|
130
|
+
/// @solidity memory-safe-assembly
|
|
131
|
+
assembly { // solhint-disable-line no-inline-assembly
|
|
132
|
+
let ptr := mload(0x40)
|
|
133
|
+
let len := add(0x64, 65)
|
|
134
|
+
|
|
135
|
+
mstore(ptr, selector)
|
|
136
|
+
mstore(add(ptr, 0x04), hash)
|
|
137
|
+
mstore(add(ptr, 0x24), 0x40)
|
|
138
|
+
mstore(add(ptr, 0x44), 65)
|
|
139
|
+
mstore(add(ptr, 0x64), r)
|
|
140
|
+
mstore(add(ptr, 0x84), s)
|
|
141
|
+
mstore8(add(ptr, 0xa4), v)
|
|
142
|
+
mstore(0, 0)
|
|
143
|
+
if staticcall(gas(), signer, ptr, len, 0, 0x20) {
|
|
144
|
+
success := eq(selector, mload(0))
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function isValidSignature(address signer, bytes32 hash, bytes32 r, bytes32 vs) internal view returns(bool success) {
|
|
150
|
+
// (bool success, bytes memory data) = signer.staticcall(abi.encodeWithSelector(IERC1271.isValidSignature.selector, hash, abi.encodePacked(r, vs)));
|
|
151
|
+
// return success && data.length >= 4 && abi.decode(data, (bytes4)) == IERC1271.isValidSignature.selector;
|
|
152
|
+
bytes4 selector = IERC1271.isValidSignature.selector;
|
|
153
|
+
/// @solidity memory-safe-assembly
|
|
154
|
+
assembly { // solhint-disable-line no-inline-assembly
|
|
155
|
+
let ptr := mload(0x40)
|
|
156
|
+
let len := add(0x64, 64)
|
|
157
|
+
|
|
158
|
+
mstore(ptr, selector)
|
|
159
|
+
mstore(add(ptr, 0x04), hash)
|
|
160
|
+
mstore(add(ptr, 0x24), 0x40)
|
|
161
|
+
mstore(add(ptr, 0x44), 64)
|
|
162
|
+
mstore(add(ptr, 0x64), r)
|
|
163
|
+
mstore(add(ptr, 0x84), vs)
|
|
164
|
+
mstore(0, 0)
|
|
165
|
+
if staticcall(gas(), signer, ptr, len, 0, 0x20) {
|
|
166
|
+
success := eq(selector, mload(0))
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function isValidSignature65(address signer, bytes32 hash, bytes32 r, bytes32 vs) internal view returns(bool success) {
|
|
172
|
+
// (bool success, bytes memory data) = signer.staticcall(abi.encodeWithSelector(IERC1271.isValidSignature.selector, hash, abi.encodePacked(r, vs & ~uint256(1 << 255), uint8(vs >> 255))));
|
|
173
|
+
// return success && data.length >= 4 && abi.decode(data, (bytes4)) == IERC1271.isValidSignature.selector;
|
|
174
|
+
bytes4 selector = IERC1271.isValidSignature.selector;
|
|
175
|
+
/// @solidity memory-safe-assembly
|
|
176
|
+
assembly { // solhint-disable-line no-inline-assembly
|
|
177
|
+
let ptr := mload(0x40)
|
|
178
|
+
let len := add(0x64, 65)
|
|
179
|
+
|
|
180
|
+
mstore(ptr, selector)
|
|
181
|
+
mstore(add(ptr, 0x04), hash)
|
|
182
|
+
mstore(add(ptr, 0x24), 0x40)
|
|
183
|
+
mstore(add(ptr, 0x44), 65)
|
|
184
|
+
mstore(add(ptr, 0x64), r)
|
|
185
|
+
mstore(add(ptr, 0x84), shr(1, shl(1, vs)))
|
|
186
|
+
mstore8(add(ptr, 0xa4), add(27, shr(255, vs)))
|
|
187
|
+
mstore(0, 0)
|
|
188
|
+
if staticcall(gas(), signer, ptr, len, 0, 0x20) {
|
|
189
|
+
success := eq(selector, mload(0))
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function toEthSignedMessageHash(bytes32 hash) internal pure returns (bytes32 res) {
|
|
195
|
+
// 32 is the length in bytes of hash, enforced by the type signature above
|
|
196
|
+
// return keccak256(abi.encodePacked("\x19Ethereum Signed Message:\n32", hash));
|
|
197
|
+
/// @solidity memory-safe-assembly
|
|
198
|
+
assembly { // solhint-disable-line no-inline-assembly
|
|
199
|
+
mstore(0, 0x19457468657265756d205369676e6564204d6573736167653a0a333200000000) // "\x19Ethereum Signed Message:\n32"
|
|
200
|
+
mstore(28, hash)
|
|
201
|
+
res := keccak256(0, 60)
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
function toTypedDataHash(bytes32 domainSeparator, bytes32 structHash) internal pure returns (bytes32 res) {
|
|
206
|
+
// return keccak256(abi.encodePacked("\x19\x01", domainSeparator, structHash));
|
|
207
|
+
/// @solidity memory-safe-assembly
|
|
208
|
+
assembly { // solhint-disable-line no-inline-assembly
|
|
209
|
+
let ptr := mload(0x40)
|
|
210
|
+
mstore(ptr, 0x1901000000000000000000000000000000000000000000000000000000000000) // "\x19\x01"
|
|
211
|
+
mstore(add(ptr, 0x02), domainSeparator)
|
|
212
|
+
mstore(add(ptr, 0x22), structHash)
|
|
213
|
+
res := keccak256(ptr, 66)
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
@@ -69,13 +69,7 @@ library UniERC20 {
|
|
|
69
69
|
function uniApprove(IERC20 token, address to, uint256 amount) internal {
|
|
70
70
|
if (isETH(token)) revert ApproveCalledOnETH();
|
|
71
71
|
|
|
72
|
-
|
|
73
|
-
(bool success, bytes memory returndata) = address(token).call(abi.encodeWithSelector(token.approve.selector, to, amount));
|
|
74
|
-
|
|
75
|
-
if (!success || (returndata.length > 0 && !abi.decode(returndata, (bool)))) {
|
|
76
|
-
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, to, 0));
|
|
77
|
-
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, to, amount));
|
|
78
|
-
}
|
|
72
|
+
token.forceApprove(to, amount);
|
|
79
73
|
}
|
|
80
74
|
|
|
81
75
|
function _uniDecode(IERC20 token, string memory lowerCaseSignature, string memory upperCaseSignature) private view returns(string memory) {
|
|
@@ -100,34 +94,19 @@ library UniERC20 {
|
|
|
100
94
|
}
|
|
101
95
|
|
|
102
96
|
if (success && data.length == 32) {
|
|
103
|
-
|
|
97
|
+
uint256 len = 0;
|
|
104
98
|
while (len < data.length && data[len] >= 0x20 && data[len] <= 0x7E) {
|
|
105
99
|
len++;
|
|
106
100
|
}
|
|
107
101
|
|
|
108
102
|
if (len > 0) {
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
for (uint i = 0; i < len; i++) {
|
|
112
|
-
result[i] = data[i];
|
|
113
|
-
}
|
|
103
|
+
assembly { // solhint-disable-line no-inline-assembly
|
|
104
|
+
mstore(data, len)
|
|
114
105
|
}
|
|
115
|
-
return string(
|
|
106
|
+
return string(data);
|
|
116
107
|
}
|
|
117
108
|
}
|
|
118
109
|
|
|
119
110
|
return StringUtil.toHex(address(token));
|
|
120
111
|
}
|
|
121
|
-
|
|
122
|
-
function _callOptionalReturn(IERC20 token, bytes memory data) private {
|
|
123
|
-
// solhint-disable-next-line avoid-low-level-calls
|
|
124
|
-
(bool success, bytes memory result) = address(token).call(data);
|
|
125
|
-
if (!success) {
|
|
126
|
-
RevertReasonForwarder.reRevert();
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
if (result.length > 0) { // Return data is optional
|
|
130
|
-
if (!abi.decode(result, (bool))) revert ERC20OperationFailed();
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
112
|
}
|