forked from ProjectOpenSea/seaport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReferenceGettersAndDerivers.sol
245 lines (229 loc) · 7.83 KB
/
ReferenceGettersAndDerivers.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
import {
ConsiderationItem,
OfferItem,
OrderParameters
} from "../../contracts/lib/ConsiderationStructs.sol";
import { ReferenceConsiderationBase } from "./ReferenceConsiderationBase.sol";
import "contracts/lib/ConsiderationConstants.sol";
/**
* @title GettersAndDerivers
* @author 0age
* @notice ConsiderationInternal contains pure and internal view functions
* related to getting or deriving various values.
*/
contract ReferenceGettersAndDerivers is ReferenceConsiderationBase {
/**
* @dev Derive and set hashes, reference chainId, and associated domain
* separator during deployment.
*
* @param conduitController A contract that deploys conduits, or proxies
* that may optionally be used to transfer approved
* ERC20/721/1155 tokens.
*/
constructor(address conduitController)
ReferenceConsiderationBase(conduitController)
{}
/**
* @dev Internal view function to derive the EIP-712 hash for an offer item.
*
* @param offerItem The offered item to hash.
*
* @return The hash.
*/
function _hashOfferItem(OfferItem memory offerItem)
internal
view
returns (bytes32)
{
return
keccak256(
abi.encode(
_OFFER_ITEM_TYPEHASH,
offerItem.itemType,
offerItem.token,
offerItem.identifierOrCriteria,
offerItem.startAmount,
offerItem.endAmount
)
);
}
/**
* @dev Internal view function to derive the EIP-712 hash for a
* consideration item.
*
* @param considerationItem The consideration item to hash.
*
* @return The hash.
*/
function _hashConsiderationItem(ConsiderationItem memory considerationItem)
internal
view
returns (bytes32)
{
return
keccak256(
abi.encode(
_CONSIDERATION_ITEM_TYPEHASH,
considerationItem.itemType,
considerationItem.token,
considerationItem.identifierOrCriteria,
considerationItem.startAmount,
considerationItem.endAmount,
considerationItem.recipient
)
);
}
/**
* @dev Internal view function to derive the order hash for a given order.
* Note that only the original consideration items are included in the
* order hash, as additional consideration items may be supplied by the
* caller.
*
* @param orderParameters The parameters of the order to hash.
* @param counter The counter of the order to hash.
*
* @return orderHash The hash.
*/
function _deriveOrderHash(
OrderParameters memory orderParameters,
uint256 counter
) internal view returns (bytes32 orderHash) {
// Designate new memory regions for offer and consideration item hashes.
bytes32[] memory offerHashes = new bytes32[](
orderParameters.offer.length
);
bytes32[] memory considerationHashes = new bytes32[](
orderParameters.totalOriginalConsiderationItems
);
// Iterate over each offer on the order.
for (uint256 i = 0; i < orderParameters.offer.length; ++i) {
// Hash the offer and place the result into memory.
offerHashes[i] = _hashOfferItem(orderParameters.offer[i]);
}
// Iterate over each consideration on the order.
for (
uint256 i = 0;
i < orderParameters.totalOriginalConsiderationItems;
++i
) {
// Hash the consideration and place the result into memory.
considerationHashes[i] = _hashConsiderationItem(
orderParameters.consideration[i]
);
}
// Derive and return the order hash as specified by EIP-712.
return
keccak256(
abi.encode(
_ORDER_TYPEHASH,
orderParameters.offerer,
orderParameters.zone,
keccak256(abi.encodePacked(offerHashes)),
keccak256(abi.encodePacked(considerationHashes)),
orderParameters.orderType,
orderParameters.startTime,
orderParameters.endTime,
orderParameters.zoneHash,
orderParameters.salt,
orderParameters.conduitKey,
counter
)
);
}
/**
* @dev Internal pure function to efficiently derive an digest to sign for
* an order in accordance with EIP-712.
*
* @param domainSeparator The domain separator.
* @param orderHash The order hash.
*
* @return value The hash.
*/
function _deriveEIP712Digest(bytes32 domainSeparator, bytes32 orderHash)
internal
pure
returns (bytes32 value)
{
value = keccak256(
abi.encodePacked(uint16(0x1901), domainSeparator, orderHash)
);
}
/**
* @dev Internal view function to derive the address of a given conduit
* using a corresponding conduit key.
*
* @param conduitKey A bytes32 value indicating what corresponding conduit,
* if any, to source token approvals from. This value is
* the "salt" parameter supplied by the deployer (i.e. the
* conduit controller) when deploying the given conduit.
*
* @return conduit The address of the conduit associated with the given
* conduit key.
*/
function _deriveConduit(bytes32 conduitKey)
internal
view
returns (address conduit)
{
// Derive the address of the conduit.
conduit = address(
uint160(
uint256(
keccak256(
abi.encodePacked(
bytes1(0xff),
address(_CONDUIT_CONTROLLER),
conduitKey,
_CONDUIT_CREATION_CODE_HASH
)
)
)
)
);
}
/**
* @dev Internal view function to get the EIP-712 domain separator. If the
* chainId matches the chainId set on deployment, the cached domain
* separator will be returned; otherwise, it will be derived from
* scratch.
*/
function _domainSeparator() internal view returns (bytes32) {
// prettier-ignore
return block.chainid == _CHAIN_ID
? _DOMAIN_SEPARATOR
: _deriveDomainSeparator(_EIP_712_DOMAIN_TYPEHASH,
_NAME_HASH,
_VERSION_HASH);
}
/**
* @notice Retrieve configuration information for this contract.
*
* @return version The contract version.
* @return domainSeparator The domain separator for this contract.
* @return conduitController The conduit Controller set for this contract.
*/
function _information()
internal
view
returns (
string memory version,
bytes32 domainSeparator,
address conduitController
)
{
version = _VERSION;
domainSeparator = _domainSeparator();
conduitController = address(_CONDUIT_CONTROLLER);
}
/**
* @notice Retrieve the name of this contract.
*
* @return The name of this contract.
*/
function _name() internal pure returns (string memory) {
// Return the name of the contract.
return _NAME;
}
}