-
Notifications
You must be signed in to change notification settings - Fork 19
/
AccountVerifier.sol
165 lines (150 loc) · 7.01 KB
/
AccountVerifier.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
// SPDX-License-Identifier: Apache-2.0
pragma solidity 0.8.24;
import { EIP712 } from "@openzeppelin/contracts/utils/cryptography/EIP712.sol";
import { SignatureChecker } from "@openzeppelin/contracts/utils/cryptography/SignatureChecker.sol";
import { VerifierBase } from "@equilibria/root/verifier/VerifierBase.sol";
import { IMarketFactory } from "@perennial/v2-core/contracts/interfaces/IMarketFactory.sol";
import { IAccountVerifier } from "./interfaces/IAccountVerifier.sol";
import { IRelayVerifier } from "./interfaces/IRelayVerifier.sol"; // only needed for docstrings
import { Action, ActionLib } from "./types/Action.sol";
import { DeployAccount, DeployAccountLib } from "./types/DeployAccount.sol";
import { MarketTransfer, MarketTransferLib } from "./types/MarketTransfer.sol";
import { RebalanceConfigChange, RebalanceConfigChangeLib } from "./types/RebalanceConfigChange.sol";
import { Withdrawal, WithdrawalLib } from "./types/Withdrawal.sol";
import { RelayedNonceCancellation, RelayedNonceCancellationLib } from "./types/RelayedNonceCancellation.sol";
import { RelayedGroupCancellation, RelayedGroupCancellationLib } from "./types/RelayedGroupCancellation.sol";
import { RelayedOperatorUpdate, RelayedOperatorUpdateLib } from "./types/RelayedOperatorUpdate.sol";
import { RelayedSignerUpdate, RelayedSignerUpdateLib } from "./types/RelayedSignerUpdate.sol";
import { RelayedAccessUpdateBatch, RelayedAccessUpdateBatchLib } from "./types/RelayedAccessUpdateBatch.sol";
/// @title Verifier
/// @notice ERC712 signed message verifier for the Perennial V2 Collateral Accounts package.
contract AccountVerifier is VerifierBase, IAccountVerifier {
/// @dev market factory to check authorization
IMarketFactory internal immutable marketFactory;
/// @dev Initializes the domain separator and parameter caches
constructor(IMarketFactory _marketFactory) EIP712("Perennial V2 Collateral Accounts", "1.0.0") {
marketFactory = _marketFactory;
}
/// @inheritdoc IAccountVerifier
function verifyAction(Action calldata action, bytes calldata signature)
external
validateAndCancel(action.common, signature)
{
if (!SignatureChecker.isValidSignatureNow(
action.common.signer,
_hashTypedDataV4(ActionLib.hash(action)),
signature
)) revert VerifierInvalidSignerError();
}
/// @inheritdoc IAccountVerifier
function verifyDeployAccount(DeployAccount calldata deployAccount, bytes calldata signature)
external
validateAndCancel(deployAccount.action.common, signature)
{
if (!SignatureChecker.isValidSignatureNow(
deployAccount.action.common.signer,
_hashTypedDataV4(DeployAccountLib.hash(deployAccount)),
signature
)) revert VerifierInvalidSignerError();
}
/// @inheritdoc IAccountVerifier
function verifyMarketTransfer(MarketTransfer calldata marketTransfer, bytes calldata signature)
external
validateAndCancel(marketTransfer.action.common, signature)
{
if (!SignatureChecker.isValidSignatureNow(
marketTransfer.action.common.signer,
_hashTypedDataV4(MarketTransferLib.hash(marketTransfer)),
signature
)) revert VerifierInvalidSignerError();
}
/// @inheritdoc IAccountVerifier
function verifyRebalanceConfigChange(RebalanceConfigChange calldata change, bytes calldata signature)
external
validateAndCancel(change.action.common, signature)
{
if (!SignatureChecker.isValidSignatureNow(
change.action.common.signer,
_hashTypedDataV4(RebalanceConfigChangeLib.hash(change)),
signature
)) revert VerifierInvalidSignerError();
}
/// @inheritdoc IAccountVerifier
function verifyWithdrawal(Withdrawal calldata withdrawal, bytes calldata signature)
external
validateAndCancel(withdrawal.action.common, signature)
{
if (!SignatureChecker.isValidSignatureNow(
withdrawal.action.common.signer,
_hashTypedDataV4(WithdrawalLib.hash(withdrawal)),
signature
)) revert VerifierInvalidSignerError();
}
/// @inheritdoc IRelayVerifier
function verifyRelayedNonceCancellation(
RelayedNonceCancellation calldata message,
bytes calldata outerSignature
) external validateAndCancel(message.action.common, outerSignature)
{
if (!SignatureChecker.isValidSignatureNow(
message.action.common.signer,
_hashTypedDataV4(RelayedNonceCancellationLib.hash(message)),
outerSignature
)) revert VerifierInvalidSignerError();
}
/// @inheritdoc IRelayVerifier
function verifyRelayedGroupCancellation(
RelayedGroupCancellation calldata message,
bytes calldata outerSignature
) external validateAndCancel(message.action.common, outerSignature)
{
if (!SignatureChecker.isValidSignatureNow(
message.action.common.signer,
_hashTypedDataV4(RelayedGroupCancellationLib.hash(message)),
outerSignature
)) revert VerifierInvalidSignerError();
}
/// @inheritdoc IRelayVerifier
function verifyRelayedOperatorUpdate(
RelayedOperatorUpdate calldata message,
bytes calldata outerSignature
) external validateAndCancel(message.action.common, outerSignature)
{
if (!SignatureChecker.isValidSignatureNow(
message.action.common.signer,
_hashTypedDataV4(RelayedOperatorUpdateLib.hash(message)),
outerSignature
)) revert VerifierInvalidSignerError();
}
/// @inheritdoc IRelayVerifier
function verifyRelayedSignerUpdate(
RelayedSignerUpdate calldata message,
bytes calldata outerSignature
) external validateAndCancel(message.action.common, outerSignature)
{
if (!SignatureChecker.isValidSignatureNow(
message.action.common.signer,
_hashTypedDataV4(RelayedSignerUpdateLib.hash(message)),
outerSignature
)) revert VerifierInvalidSignerError();
}
/// @inheritdoc IRelayVerifier
function verifyRelayedAccessUpdateBatch(
RelayedAccessUpdateBatch calldata message,
bytes calldata outerSignature
) external validateAndCancel(message.action.common, outerSignature)
{
if (!SignatureChecker.isValidSignatureNow(
message.action.common.signer,
_hashTypedDataV4(RelayedAccessUpdateBatchLib.hash(message)),
outerSignature
)) revert VerifierInvalidSignerError();
}
/// @notice Checks whether signer is allowed to sign a message for account
/// @param account user to check authorization for (not the collateral account)
/// @param signer address which signed a message for the account
/// @return true if signer is authorized, otherwise false
function _authorized(address account, address signer) internal view override returns (bool) {
return super._authorized(account, signer) || marketFactory.signers(account, signer);
}
}