-
-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathNonceEnforcer.sol
More file actions
69 lines (60 loc) · 2.48 KB
/
NonceEnforcer.sol
File metadata and controls
69 lines (60 loc) · 2.48 KB
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
// SPDX-License-Identifier: MIT AND Apache-2.0
pragma solidity ^0.8.23;
import { CaveatEnforcer } from "./CaveatEnforcer.sol";
import { ModeCode } from "../utils/Types.sol";
/**
* @title Nonce Enforcer Contract
* @dev This contract extends the CaveatEnforcer contract. It provides functionality to add an nonce to a delegation and enable
* multi delegation revocation based on that nonce by incrementing the current nonce.
* @dev This enforcer operates only in default execution mode.
*/
contract NonceEnforcer is CaveatEnforcer {
////////////////////// State //////////////////////
mapping(address delegationManager => mapping(address delegator => uint256 nonce)) public currentNonce;
////////////////////////////// Events //////////////////////////////
event UsedNonce(address indexed delegationManager, address indexed delegator, uint256 nonce);
////////////////////////////// Public Methods //////////////////////////////
/**
* @notice
* @param _terms A uint256 representing the nonce used in the delegation.
* @param _mode The execution mode. (Must be Default execType)
*/
function beforeHook(
bytes calldata _terms,
bytes calldata,
ModeCode _mode,
bytes calldata,
bytes32,
address _delegator,
address
)
public
view
override
onlyDefaultExecutionMode(_mode)
{
uint256 nonce_ = getTermsInfo(_terms);
require(currentNonce[msg.sender][_delegator] == nonce_, "NonceEnforcer:invalid-nonce");
}
/**
* @notice Decodes the terms used in this CaveatEnforcer.
* @param _terms encoded data that is used during the execution hooks.
* @return nonce_ The ID used in the delegation.
*/
function getTermsInfo(bytes calldata _terms) public pure returns (uint256 nonce_) {
require(_terms.length == 32, "NonceEnforcer:invalid-terms-length");
nonce_ = uint256(bytes32(_terms));
}
/**
* @notice Increments the nonce of a delegator. This invalidates all previous delegations with the old nonce.
* @dev The message sender must be the delegator.
* @param _delegationManager the address of the delegation manager that the user is using.
*/
function incrementNonce(address _delegationManager) external {
uint256 oldNonce_;
unchecked {
oldNonce_ = currentNonce[_delegationManager][msg.sender]++;
}
emit UsedNonce(_delegationManager, msg.sender, oldNonce_);
}
}