-
-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathOwnershipTransferEnforcer.sol
More file actions
87 lines (75 loc) · 3.43 KB
/
OwnershipTransferEnforcer.sol
File metadata and controls
87 lines (75 loc) · 3.43 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// SPDX-License-Identifier: MIT AND Apache-2.0
pragma solidity ^0.8.23;
import { ExecutionLib } from "@erc7579/lib/ExecutionLib.sol";
import { CaveatEnforcer } from "./CaveatEnforcer.sol";
import { ModeCode } from "../utils/Types.sol";
import { IERC173 } from "../interfaces/IERC173.sol";
/**
* @title OwnershipTransferEnforcer
* @dev This contract enforces the ownership transfer of ERC-173 compliant contracts.
* @dev This enforcer operates only in single execution call type and with default execution mode.
*/
contract OwnershipTransferEnforcer is CaveatEnforcer {
using ExecutionLib for bytes;
////////////////////////////// Events //////////////////////////////
event OwnershipTransferEnforced(
address indexed sender, address indexed redeemer, bytes32 indexed delegationHash, address newOwner
);
////////////////////////////// Public Methods //////////////////////////////
/**
* @notice Enforces the ownership transfer of an ERC-173 compliant contract.
* @dev This function enforces the ownership transfer before the transaction is performed.
* @param _terms The address of the contract whose ownership is being transferred.
* @param _mode The execution mode. (Must be Single callType, Default execType)
* @param _executionCallData The transaction the delegate might try to perform.
* @param _delegationHash The hash of the delegation being operated on.
*/
function beforeHook(
bytes calldata _terms,
bytes calldata,
ModeCode _mode,
bytes calldata _executionCallData,
bytes32 _delegationHash,
address,
address _redeemer
)
public
override
onlySingleCallTypeMode(_mode)
onlyDefaultExecutionMode(_mode)
{
address newOwner = _validateAndEnforce(_terms, _executionCallData);
emit OwnershipTransferEnforced(msg.sender, _redeemer, _delegationHash, newOwner);
}
/**
* @notice Decodes the terms used in this CaveatEnforcer.
* @param _terms encoded data that is used during the execution hooks.
* @return targetContract_ The address of the ERC-173 compliant contract.
*/
function getTermsInfo(bytes calldata _terms) public pure returns (address targetContract_) {
require(_terms.length == 20, "OwnershipTransferEnforcer:invalid-terms-length");
targetContract_ = address(bytes20(_terms));
}
/**
* @notice Validates the ownership transfer and enforces the terms.
* @param _terms The address of the contract whose ownership is being transferred.
* @param _executionCallData The transaction the delegate might try to perform.
* @return newOwner_ The address of the new owner.
*/
function _validateAndEnforce(
bytes calldata _terms,
bytes calldata _executionCallData
)
internal
pure
returns (address newOwner_)
{
(address target_,, bytes calldata callData_) = _executionCallData.decodeSingle();
require(callData_.length == 36, "OwnershipTransferEnforcer:invalid-execution-length");
bytes4 selector_ = bytes4(callData_[0:4]);
require(selector_ == IERC173.transferOwnership.selector, "OwnershipTransferEnforcer:invalid-method");
address targetContract_ = getTermsInfo(_terms);
require(targetContract_ == target_, "OwnershipTransferEnforcer:invalid-contract");
newOwner_ = address(uint160(uint256(bytes32(callData_[4:36]))));
}
}