22pragma solidity ^ 0.7.6 ;
33
44import "../interfaces/GPv2EIP1271.sol " ;
5- import "./GPv2Order.sol " ;
6- import "./GPv2Trade.sol " ;
5+ import "../libraries /GPv2Order.sol " ;
6+ import "../libraries /GPv2Trade.sol " ;
77
88/// @title Gnosis Protocol v2 Signing Library.
99/// @author Gnosis Developers
10- library GPv2Signing {
10+ abstract contract GPv2Signing {
1111 using GPv2Order for GPv2Order.Data;
1212 using GPv2Order for bytes ;
1313
@@ -22,6 +22,46 @@ library GPv2Signing {
2222 /// @dev Signing scheme used for recovery.
2323 enum Scheme {Eip712, EthSign, Eip1271}
2424
25+ /// @dev The EIP-712 domain type hash used for computing the domain
26+ /// separator.
27+ bytes32 private constant DOMAIN_TYPE_HASH =
28+ keccak256 (
29+ "EIP712Domain(string name,string version,uint256 chainId,address verifyingContract) "
30+ );
31+
32+ /// @dev The EIP-712 domain name used for computing the domain separator.
33+ bytes32 private constant DOMAIN_NAME = keccak256 ("Gnosis Protocol " );
34+
35+ /// @dev The EIP-712 domain version used for computing the domain separator.
36+ bytes32 private constant DOMAIN_VERSION = keccak256 ("v2 " );
37+
38+ /// @dev The domain separator used for signing orders that gets mixed in
39+ /// making signatures for different domains incompatible. This domain
40+ /// separator is computed following the EIP-712 standard and has replay
41+ /// protection mixed in so that signed orders are only valid for specific
42+ /// GPv2 contracts.
43+ bytes32 public immutable domainSeparator;
44+
45+ constructor () {
46+ // NOTE: Currently, the only way to get the chain ID in solidity is
47+ // using assembly.
48+ uint256 chainId;
49+ // solhint-disable-next-line no-inline-assembly
50+ assembly {
51+ chainId := chainid ()
52+ }
53+
54+ domainSeparator = keccak256 (
55+ abi.encode (
56+ DOMAIN_TYPE_HASH,
57+ DOMAIN_NAME,
58+ DOMAIN_VERSION,
59+ chainId,
60+ address (this )
61+ )
62+ );
63+ }
64+
2565 /// @dev Returns an empty recovered order with a pre-allocated buffer for
2666 /// packing the unique identifier.
2767 ///
@@ -38,27 +78,19 @@ library GPv2Signing {
3878 /// trade.
3979 ///
4080 /// @param recoveredOrder Memory location used for writing the recovered order data.
41- /// @param domainSeparator The domain separator used for signing the order.
4281 /// @param tokens The list of tokens included in the settlement. The token
4382 /// indices in the trade parameters map to tokens in this array.
4483 /// @param trade The trade data to recover the order data from.
4584 function recoverOrderFromTrade (
4685 RecoveredOrder memory recoveredOrder ,
47- bytes32 domainSeparator ,
4886 IERC20 [] calldata tokens ,
4987 GPv2Trade.Data calldata trade
5088 ) internal view {
5189 GPv2Order.Data memory order = recoveredOrder.data;
5290
53- GPv2Signing.Scheme signingScheme =
54- GPv2Trade.extractOrder (trade, tokens, order);
91+ Scheme signingScheme = GPv2Trade.extractOrder (trade, tokens, order);
5592 (bytes32 orderDigest , address owner ) =
56- recoverOrderSigner (
57- order,
58- domainSeparator,
59- signingScheme,
60- trade.signature
61- );
93+ recoverOrderSigner (order, signingScheme, trade.signature);
6294
6395 recoveredOrder.uid.packOrderUidParams (
6496 orderDigest,
@@ -74,36 +106,22 @@ library GPv2Signing {
74106 /// @dev Recovers an order's signer from the specified order and signature.
75107 ///
76108 /// @param order The order to recover a signature for.
77- /// @param domainSeparator The domain separator used for signing the order.
78109 /// @param signingScheme The signing scheme.
79110 /// @param signature The signature bytes.
80111 /// @return orderDigest The computed order hash.
81112 /// @return owner The recovered address from the specified signature.
82113 function recoverOrderSigner (
83114 GPv2Order.Data memory order ,
84- bytes32 domainSeparator ,
85115 Scheme signingScheme ,
86116 bytes calldata signature
87117 ) internal view returns (bytes32 orderDigest , address owner ) {
88118 orderDigest = order.hash ();
89119 if (signingScheme == Scheme.Eip712) {
90- owner = recoverEip712Signer (
91- signature,
92- domainSeparator,
93- orderDigest
94- );
120+ owner = recoverEip712Signer (signature, orderDigest);
95121 } else if (signingScheme == Scheme.EthSign) {
96- owner = recoverEthsignSigner (
97- signature,
98- domainSeparator,
99- orderDigest
100- );
122+ owner = recoverEthsignSigner (signature, orderDigest);
101123 } else if (signingScheme == Scheme.Eip1271) {
102- owner = recoverEip1271Signer (
103- signature,
104- domainSeparator,
105- orderDigest
106- );
124+ owner = recoverEip1271Signer (signature, orderDigest);
107125 }
108126 }
109127
@@ -169,15 +187,13 @@ library GPv2Signing {
169187 ///
170188 /// @param encodedSignature Calldata pointing to tightly packed signature
171189 /// bytes.
172- /// @param domainSeparator The domain separator used for signing the order.
173190 /// @param orderDigest The EIP-712 signing digest derived from the order
174191 /// parameters.
175192 /// @return owner The address of the signer.
176193 function recoverEip712Signer (
177194 bytes calldata encodedSignature ,
178- bytes32 domainSeparator ,
179195 bytes32 orderDigest
180- ) internal pure returns (address owner ) {
196+ ) internal view returns (address owner ) {
181197 (bytes32 r , bytes32 s , uint8 v ) =
182198 decodeEcdsaSignature (encodedSignature);
183199
@@ -209,15 +225,13 @@ library GPv2Signing {
209225 ///
210226 /// @param encodedSignature Calldata pointing to tightly packed signature
211227 /// bytes.
212- /// @param domainSeparator The domain separator used for signing the order.
213228 /// @param orderDigest The EIP-712 signing digest derived from the order
214229 /// parameters.
215230 /// @return owner The address of the signer.
216231 function recoverEthsignSigner (
217232 bytes calldata encodedSignature ,
218- bytes32 domainSeparator ,
219233 bytes32 orderDigest
220- ) internal pure returns (address owner ) {
234+ ) internal view returns (address owner ) {
221235 (bytes32 r , bytes32 s , uint8 v ) =
222236 decodeEcdsaSignature (encodedSignature);
223237
@@ -258,7 +272,6 @@ library GPv2Signing {
258272 /// cover the full length of the decoded signature.
259273 function recoverEip1271Signer (
260274 bytes calldata encodedSignature ,
261- bytes32 domainSeparator ,
262275 bytes32 orderDigest
263276 ) internal view returns (address owner ) {
264277 // NOTE: Use assembly to read the verifier address from the encoded
0 commit comments