Skip to content

Commit

Permalink
Nested calldata decoding ready
Browse files Browse the repository at this point in the history
  • Loading branch information
brickpop committed Dec 1, 2023
1 parent c27e97e commit 222f32e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
26 changes: 21 additions & 5 deletions packages/contracts/src/OnlyPluginUpgraderCondition.sol
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,26 @@ contract OnlyPluginUpgraderCondition is PermissionCondition {

function decodeApplyUpdateCalldata(
bytes memory _data
) public pure returns (bytes4 selector, address daoAddress) {
) public pure returns (bytes4 selector, address daoAddress, address targetPluginAddress) {
// Slicing is only supported for bytes calldata, not bytes memory
// Bytes memory requires an assembly block
assembly {
selector := mload(add(_data, 0x20)) // 32
daoAddress := mload(add(_data, 0x24)) // 32 + 4
selector := mload(add(_data, 0x20))

let _payloadStart := add(_data, 0x24)
daoAddress := mload(_payloadStart)

let _paramsStructOffset := mload(add(_payloadStart, 0x20))

// Extracting the first field of the struct at offset 0
// struct ApplyUpdateParams {
// address plugin;
// PluginSetupRef pluginSetupRef;
// bytes initData;
// PermissionLib.MultiTargetPermission[] permissions;
// bytes32 helpersHash;
// }
targetPluginAddress := mload(add(_payloadStart, _paramsStructOffset))
}
}

Expand Down Expand Up @@ -149,11 +163,13 @@ contract OnlyPluginUpgraderCondition is PermissionCondition {
}

function isValidApplyUpdateCalldata(bytes memory _data) private view returns (bool) {
(bytes4 _selector, address _dao) = decodeApplyUpdateCalldata(_data);
(bytes4 _selector, address _dao, address _targetPluginAddress) = decodeApplyUpdateCalldata(
_data
);

if (_selector != PluginSetupProcessor.applyUpdate.selector) return false;
else if (_dao != dao) return false;
// else if (!allowedPluginAddresses[_applyParams.plugin]) return false;
else if (!allowedPluginAddresses[_targetPluginAddress]) return false;

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1273,32 +1273,37 @@ describe('Only Plugin Upgrader Condition', function () {

it('Should decode decodeApplyUpdateCalldata properly', async () => {
applyUpdateParams.plugin = bob.address;
const applyUpdateParams2 = JSON.parse(JSON.stringify(applyUpdateParams));
applyUpdateParams2.plugin = alice.address;

const calldataList = [
pspInterface.encodeFunctionData('applyUpdate', [
dao.address,
applyUpdateParams,
]),
pspInterface.encodeFunctionData('applyUpdate', [
ADDRESS_THREE,
applyUpdateParams,
applyUpdateParams2,
]),
];

// 1
let [selector, decodedDaoAddress] =
let [selector, decodedDaoAddress, pluginAddress] =
await onlyPluginUpgraderCondition.decodeApplyUpdateCalldata(
calldataList[0]
);
expect(selector).to.eq(calldataList[0].slice(0, 10));
expect(decodedDaoAddress).to.eq(dao.address);
expect(pluginAddress).to.eq(bob.address);

// 2
[selector, decodedDaoAddress] =
[selector, decodedDaoAddress, pluginAddress] =
await onlyPluginUpgraderCondition.decodeApplyUpdateCalldata(
calldataList[1]
);
expect(selector).to.eq(calldataList[1].slice(0, 10));
expect(decodedDaoAddress).to.eq(ADDRESS_THREE);
expect(pluginAddress).to.eq(alice.address);
});
});
});

0 comments on commit 222f32e

Please sign in to comment.