-
Notifications
You must be signed in to change notification settings - Fork 569
Expand file tree
/
Copy pathFiatTokenUtil.sol
More file actions
177 lines (160 loc) · 6.49 KB
/
Copy pathFiatTokenUtil.sol
File metadata and controls
177 lines (160 loc) · 6.49 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
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
/**
* Copyright 2023 Circle Internet Group, Inc. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
pragma solidity 0.6.12;
contract FiatTokenUtil {
// (address,address,uint256,uint256,uint256,bytes32) = 20*2 + 32*4 = 168
uint256 private constant _TRANSFER_PARAM_SIZE = 168;
// (uint8,bytes32,bytes32) = 1 + 32*2 = 65
uint256 private constant _SIGNATURE_SIZE = 65;
// keccak256("transferWithAuthorization(address,address,uint256,uint256,uint256,bytes32,uint8,bytes32,bytes32)")[0:4]
bytes4 private constant _TRANSFER_WITH_AUTHORIZATION_SELECTOR = 0xe3ee160e;
address private _fiatToken;
event TransferFailed(address indexed authorizer, bytes32 indexed nonce);
/**
* @notice Constructor
* @dev If FiatTokenProxy is used to hold state and delegate calls, the
* proxy's address should be provided, not the implementation address
* @param fiatToken Address of the FiatToken contract
*/
constructor(address fiatToken) public {
_fiatToken = fiatToken;
}
/**
* @notice Execute multiple authorized ERC20 Transfers
* @dev The length of params must be multiples of 168, each representing
* encode-packed data containing from[20] + to[20] + value[32] +
* validAfter[32] + validBefore[32] + nonce[32], and the length of
* signatures must be multiples of 65, each representing encode-packed data
* containing v[1] + r[32] + s[32].
* @param params Concatenated, encode-packed parameters
* @param signatures Concatenated, encode-packed signatures
* @param atomic If true, revert if any of the transfers fail
* @return True if every transfer was successful
*/
function transferWithMultipleAuthorizations(
bytes calldata params,
bytes calldata signatures,
bool atomic
) external returns (bool) {
uint256 num = params.length / _TRANSFER_PARAM_SIZE;
require(num > 0, "FiatTokenUtil: no transfer provided");
require(
num * _TRANSFER_PARAM_SIZE == params.length,
"FiatTokenUtil: length of params is invalid"
);
require(
signatures.length / _SIGNATURE_SIZE == num &&
num * _SIGNATURE_SIZE == signatures.length,
"FiatTokenUtil: length of signatures is invalid"
);
uint256 numSuccessful = 0;
for (uint256 i = 0; i < num; i++) {
uint256 paramsOffset = i * _TRANSFER_PARAM_SIZE;
uint256 sigOffset = i * _SIGNATURE_SIZE;
// extract from and to
bytes memory fromTo = _unpackAddresses(
abi.encodePacked(params[paramsOffset:paramsOffset + 40])
);
// extract value, validAfter, validBefore, and nonce
bytes memory other4 = abi.encodePacked(
params[paramsOffset + 40:paramsOffset + _TRANSFER_PARAM_SIZE]
);
// extract v
uint8 v = uint8(signatures[sigOffset]);
// extract r and s
bytes memory rs = abi.encodePacked(
signatures[sigOffset + 1:sigOffset + _SIGNATURE_SIZE]
);
// Call transferWithAuthorization with the extracted parameters
// solhint-disable-next-line avoid-low-level-calls
(bool success, bytes memory returnData) = _fiatToken.call(
abi.encodePacked(
_TRANSFER_WITH_AUTHORIZATION_SELECTOR,
fromTo,
other4,
abi.encode(v),
rs
)
);
// Revert if atomic is true, and the call was not successful
if (atomic && !success) {
_revertWithReasonFromReturnData(returnData);
}
// Increment the number of successful transfers
if (success) {
numSuccessful++;
} else {
// extract from
(address from, ) = abi.decode(fromTo, (address, address));
// extract nonce
(, , , bytes32 nonce) = abi.decode(
other4,
(uint256, uint256, uint256, bytes32)
);
emit TransferFailed(from, nonce);
}
}
// Return true if all transfers were successful
return numSuccessful == num;
}
/**
* @dev Converts encodePacked pair of addresses (20bytes + 20 bytes) to
* regular ABI-encoded pair of addresses (32bytes + 32bytes)
* @param packed Packed data (40 bytes)
* @return Unpacked data (64 bytes)
*/
function _unpackAddresses(bytes memory packed)
private
pure
returns (bytes memory)
{
address addr1;
address addr2;
assembly {
// Account for 32-byte length prefix in memory
// First address starts at offset 32 (after length field)
addr1 := mload(add(packed, 32))
// Second address starts at offset 52 (32 + 20 bytes of first address)
addr2 := mload(add(packed, 52))
}
return abi.encode(addr1, addr2);
}
/**
* @dev Revert with reason string extracted from the return data
* @param returnData Return data from a call
*/
function _revertWithReasonFromReturnData(bytes memory returnData)
private
pure
{
// Return data will be at least 100 bytes if it contains the reason
// string: Error(string) selector[4] + string offset[32] + string
// length[32] + string data[32] = 100
if (returnData.length < 100) {
revert("FiatTokenUtil: call failed");
}
// If the reason string exists, extract it, and bubble it up
string memory reason;
assembly {
// Skip over the bytes length[32] + Error(string) selector[4] +
// string offset[32] = 68 (0x44)
reason := add(returnData, 0x44)
}
revert(reason);
}
}