Skip to content

Commit 12a1a97

Browse files
[protocol36] Improve ERC1271 impl (#1745)
1 parent aedd3da commit 12a1a97

File tree

2 files changed

+16
-87
lines changed

2 files changed

+16
-87
lines changed

packages/loopring_v3/contracts/lib/ERC1271.sol

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,8 @@
33
pragma solidity ^0.7.0;
44

55
abstract contract ERC1271 {
6-
7-
// bytes4(keccak256("isValidSignature(bytes,bytes)")
8-
bytes4 constant internal ERC1271_MAGICVALUE_BS = 0x20c13b0b;
9-
106
// bytes4(keccak256("isValidSignature(bytes32,bytes)")
11-
bytes4 constant internal ERC1271_MAGICVALUE_B32 = 0x1626ba7e;
12-
13-
bytes4 constant internal ERC1271_SELECTOR_BS = bytes4(
14-
keccak256(bytes("isValidSignature(bytes,bytes)"))
15-
);
16-
17-
bytes4 constant internal ERC1271_SELECTOR_B32 = bytes4(
18-
keccak256(bytes("isValidSignature(bytes32,bytes)"))
19-
);
20-
21-
function isValidSignature(
22-
bytes memory _data,
23-
bytes memory _signature)
24-
public
25-
view
26-
virtual
27-
returns (bytes4 magicValueBS);
7+
bytes4 constant internal ERC1271_MAGICVALUE = 0x1626ba7e;
288

299
function isValidSignature(
3010
bytes32 _hash,
@@ -33,4 +13,5 @@ abstract contract ERC1271 {
3313
view
3414
virtual
3515
returns (bytes4 magicValueB32);
16+
3617
}

packages/loopring_v3/contracts/lib/SignatureUtil.sol

Lines changed: 14 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ pragma experimental ABIEncoderV2;
66
import "../thirdparty/BytesUtil.sol";
77
import "./AddressUtil.sol";
88
import "./MathUint.sol";
9+
import "./ERC1271.sol";
910

1011

1112
/// @title SignatureUtil
@@ -26,16 +27,7 @@ library SignatureUtil
2627
WALLET // deprecated
2728
}
2829

29-
bytes4 constant internal ERC1271_MAGICVALUE_BS = 0x20c13b0b;
30-
bytes4 constant internal ERC1271_MAGICVALUE_B32 = 0x1626ba7e;
31-
32-
bytes4 constant internal ERC1271_SELECTOR_BS = bytes4(
33-
keccak256(bytes("isValidSignature(bytes,bytes)"))
34-
);
35-
36-
bytes4 constant internal ERC1271_SELECTOR_B32 = bytes4(
37-
keccak256(bytes("isValidSignature(bytes32,bytes)"))
38-
);
30+
bytes4 constant internal ERC1271_MAGICVALUE = 0x1626ba7e;
3931

4032
function verifySignatures(
4133
bytes32 signHash,
@@ -45,45 +37,19 @@ library SignatureUtil
4537
internal
4638
view
4739
returns (bool)
48-
{
49-
return verifySignatures(abi.encodePacked(signHash), signers, signatures);
50-
}
51-
52-
function verifySignatures(
53-
bytes memory data,
54-
address[] memory signers,
55-
bytes[] memory signatures
56-
)
57-
internal
58-
view
59-
returns (bool)
6040
{
6141
require(signers.length == signatures.length, "BAD_SIGNATURE_DATA");
6242
address lastSigner;
6343
for (uint i = 0; i < signers.length; i++) {
6444
require(signers[i] > lastSigner, "INVALID_SIGNERS_ORDER");
6545
lastSigner = signers[i];
66-
if (!verifySignature(data, signers[i], signatures[i])) {
46+
if (!verifySignature(signHash, signers[i], signatures[i])) {
6747
return false;
6848
}
6949
}
7050
return true;
7151
}
7252

73-
function verifySignature(
74-
bytes memory data,
75-
address signer,
76-
bytes memory signature
77-
)
78-
internal
79-
view
80-
returns (bool)
81-
{
82-
return signer.isContract() ?
83-
verifyERC1271WithBytes(data, signer, signature) :
84-
verifyEOASignature(keccak256(data), signer, signature);
85-
}
86-
8753
function verifySignature(
8854
bytes32 signHash,
8955
address signer,
@@ -93,8 +59,12 @@ library SignatureUtil
9359
view
9460
returns (bool)
9561
{
96-
return signer.isContract() ?
97-
verifyERC1271WithBytes32(signHash, signer, signature) :
62+
if (signer == address(0)) {
63+
return false;
64+
}
65+
66+
return signer.isContract()?
67+
verifyERC1271Signature(signHash, signer, signature):
9868
verifyEOASignature(signHash, signer, signature);
9969
}
10070

@@ -172,30 +142,8 @@ library SignatureUtil
172142
return success;
173143
}
174144

175-
function verifyERC1271WithBytes(
176-
bytes memory data,
177-
address signer,
178-
bytes memory signature
179-
)
180-
private
181-
view
182-
returns (bool)
183-
{
184-
bytes memory callData = abi.encodeWithSelector(
185-
ERC1271_SELECTOR_BS,
186-
data,
187-
signature
188-
);
189-
(bool success, bytes memory result) = signer.staticcall(callData);
190-
return (
191-
success &&
192-
result.length == 32 &&
193-
result.toBytes4(0) == ERC1271_MAGICVALUE_BS
194-
);
195-
}
196-
197-
function verifyERC1271WithBytes32(
198-
bytes32 hash,
145+
function verifyERC1271Signature(
146+
bytes32 signHash,
199147
address signer,
200148
bytes memory signature
201149
)
@@ -204,15 +152,15 @@ library SignatureUtil
204152
returns (bool)
205153
{
206154
bytes memory callData = abi.encodeWithSelector(
207-
ERC1271_SELECTOR_B32,
208-
hash,
155+
ERC1271.isValidSignature.selector,
156+
signHash,
209157
signature
210158
);
211159
(bool success, bytes memory result) = signer.staticcall(callData);
212160
return (
213161
success &&
214162
result.length == 32 &&
215-
result.toBytes4(0) == ERC1271_MAGICVALUE_B32
163+
result.toBytes4(0) == ERC1271_MAGICVALUE
216164
);
217165
}
218166
}

0 commit comments

Comments
 (0)