-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathVerifierBase.sol
96 lines (81 loc) · 3.91 KB
/
VerifierBase.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
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.13;
import { EIP712 } from "@openzeppelin/contracts/utils/cryptography/EIP712.sol";
import { SignatureChecker } from "@openzeppelin/contracts/utils/cryptography/SignatureChecker.sol";
import { Common, CommonLib } from "./types/Common.sol";
import { GroupCancellation, GroupCancellationLib } from "./types/GroupCancellation.sol";
import { IVerifierBase } from "./interfaces/IVerifierBase.sol";
abstract contract VerifierBase is IVerifierBase, EIP712 {
/// @inheritdoc IVerifierBase
mapping(address => mapping(uint256 => bool)) public nonces;
/// @inheritdoc IVerifierBase
mapping(address => mapping(uint256 => bool)) public groups;
/// @inheritdoc IVerifierBase
function verifyCommon(Common calldata common, bytes calldata signature)
external
validateAndCancel(common, signature)
{
if (!SignatureChecker.isValidSignatureNow(common.signer, _hashTypedDataV4(CommonLib.hash(common)), signature))
revert VerifierInvalidSignerError();
}
/// @inheritdoc IVerifierBase
function verifyGroupCancellation(GroupCancellation calldata groupCancellation, bytes calldata signature)
external
validateAndCancel(groupCancellation.common, signature)
{
if (!SignatureChecker.isValidSignatureNow(
groupCancellation.common.signer,
_hashTypedDataV4(GroupCancellationLib.hash(groupCancellation)),
signature
)) revert VerifierInvalidSignerError();
}
/// @inheritdoc IVerifierBase
function cancelNonce(uint256 nonce) external {
_cancelNonce(msg.sender, nonce);
}
/// @inheritdoc IVerifierBase
function cancelGroup(uint256 group) external {
_cancelGroup(msg.sender, group);
}
/// @inheritdoc IVerifierBase
function cancelNonceWithSignature(Common calldata common, bytes calldata signature) external {
IVerifierBase(this).verifyCommon(common, signature); // cancels nonce
}
/// @inheritdoc IVerifierBase
function cancelGroupWithSignature(GroupCancellation calldata groupCancellation, bytes calldata signature) external {
IVerifierBase(this).verifyGroupCancellation(groupCancellation, signature);
_cancelGroup(groupCancellation.common.account, groupCancellation.group);
}
/// @notice Checks account authorization
/// @param account the account to check authorization for
/// @param signer the signer of the account
/// @return whether the signer is authorized
function _authorized(address account, address signer) internal view virtual returns (bool) {
return account == signer;
}
/// @notice Cancels a nonce
/// @param account The account to cancel the nonce for
/// @param nonce The nonce to cancel
function _cancelNonce(address account, uint256 nonce) private {
nonces[account][nonce] = true;
emit NonceCancelled(account, nonce);
}
/// @notice Cancels a group nonce
/// @param account The account to cancel the group nonce for
/// @param group The group nonce to cancel
function _cancelGroup(address account, uint256 group) private {
groups[account][group] = true;
emit GroupCancelled(account, group);
}
/// @dev Validates the common data of a message
modifier validateAndCancel(Common calldata common, bytes calldata signature) {
if (!_authorized(common.account, common.signer)) revert VerifierInvalidSignerError();
if (common.domain != msg.sender) revert VerifierInvalidDomainError();
if (signature.length != 65) revert VerifierInvalidSignatureError();
if (nonces[common.account][common.nonce]) revert VerifierInvalidNonceError();
if (groups[common.account][common.group]) revert VerifierInvalidGroupError();
if (block.timestamp >= common.expiry) revert VerifierInvalidExpiryError();
_cancelNonce(common.account, common.nonce);
_;
}
}