Skip to content

Commit

Permalink
Distributor when doing checks now always infers real target address f…
Browse files Browse the repository at this point in the history
…rom config to accommodate for middlewares such as access manager intermediate contract
  • Loading branch information
peersky committed Sep 26, 2024
1 parent 0b61cff commit dbe87be
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/thick-poems-remember.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@peeramid-labs/eds": major
---

Distributor when doing checks now always infers real target address from config to accommodate for middlewares such as access manager intermediate contract
16 changes: 11 additions & 5 deletions src/abstracts/Distributor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,13 @@ abstract contract Distributor is IDistributor, CodeIndexer, ERC165 {
);
require(success, string(result));
}
numInstances++;
uint256 instanceId = numInstances;
for (uint256 i = 0; i < instances.length; i++) {
instanceIds[instances[i]] = instanceId;
distributionOf[instanceId] = distributorsId;
}
emit Instantiated(distributorsId, args, instances);
emit Instantiated(distributorsId, instanceId, args, instances);
return (instances, distributionName, distributionVersion);
}

Expand All @@ -91,16 +92,17 @@ abstract contract Distributor is IDistributor, CodeIndexer, ERC165 {
* it will revert if instanceId belongs to disactivated distribution
*/
function beforeCall(
bytes memory,
bytes memory config,
bytes4,
address maybeInstance,
uint256,
bytes memory
) public view virtual returns (bytes memory) {
(address target) = abi.decode(config, (address));
bytes32 distributorsId = distributionOf[getInstanceId(maybeInstance)];
if (
distributorsId != bytes32(0) &&
getInstanceId(msg.sender) == getInstanceId(maybeInstance) &&
getInstanceId(target) == getInstanceId(maybeInstance) &&
distirbutionsSet.contains(distributorsId) == true
) {
// ToDo: This check could be based on DistributionOf, hence allowing cross-instance calls
Expand All @@ -111,15 +113,19 @@ abstract contract Distributor is IDistributor, CodeIndexer, ERC165 {
}

function afterCall(
bytes memory,
bytes memory config,
bytes4,
address maybeInstance,
uint256,
bytes memory,
bytes memory
) public virtual {
(address target) = abi.decode(config, (address));
bytes32 distributorsId = distributionOf[getInstanceId(maybeInstance)];
if (getInstanceId(msg.sender) != getInstanceId(maybeInstance) && distirbutionsSet.contains(distributorsId) == true) {
if (
(getInstanceId(target) != getInstanceId(maybeInstance)) &&
distirbutionsSet.contains(distributorsId) == true
) {
revert InvalidInstance(maybeInstance);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/IDistributor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ interface IDistributor is IERC7746, IERC165 {
error DistributionExists(bytes32 id);
error InitializerNotFound(bytes32 id);
error InvalidInstance(address instance);
event Instantiated(bytes32 indexed distributionId, bytes indexed argsHash, address[] instances);
event Instantiated(bytes32 indexed distributionId,uint256 indexed instanceId, bytes indexed argsHash, address[] instances);
event DistributionRemoved(bytes32 indexed id);

event DistributionAdded(bytes32 indexed id, address indexed initializer);
Expand Down
9 changes: 4 additions & 5 deletions src/managers/SimpleAccessManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {IERC7746} from "../interfaces/IERC7746.sol";
import {IDistributor} from "../interfaces/IDistributor.sol";
import "@openzeppelin/contracts/utils/introspection/ERC165Checker.sol";
import "@openzeppelin/contracts/utils/introspection/ERC165.sol";

contract SimpleAccessManager is Initializable, IERC7746, ERC165 {
struct MethodSettings {
bool isDistributionOnly;
Expand Down Expand Up @@ -62,7 +61,7 @@ contract SimpleAccessManager is Initializable, IERC7746, ERC165 {
}

function beforeCall(
bytes memory configuration,
bytes memory ,
bytes4 selector,
address sender,
uint256 value,
Expand All @@ -76,14 +75,14 @@ contract SimpleAccessManager is Initializable, IERC7746, ERC165 {
revert("SimpleAccessManager: sender is not allowed to call this method");
} else {
if (s.methodSettings[selector].isDistributionOnly) {
return s.distributor.beforeCall(configuration, selector, sender, value, data);
return s.distributor.beforeCall(abi.encode(msg.sender), selector, sender, value, data);
}
return "";
}
}

function afterCall(
bytes memory configuration,
bytes memory ,
bytes4 selector,
address sender,
uint256 value,
Expand All @@ -95,7 +94,7 @@ contract SimpleAccessManager is Initializable, IERC7746, ERC165 {
revert("ERC20DistributorsManager: only target can call this function");
}
if (s.methodSettings[selector].isDistributionOnly) {
s.distributor.afterCall(configuration, selector, sender, value, data,beforeCallResult);
s.distributor.afterCall(abi.encode(msg.sender), selector, sender, value, data,beforeCallResult);
}
}

Expand Down

0 comments on commit dbe87be

Please sign in to comment.