Skip to content

Commit

Permalink
add Misc lib w/ stripSelector
Browse files Browse the repository at this point in the history
  • Loading branch information
zeroXbrock committed Jul 18, 2024
1 parent 17db5e0 commit fa72e22
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/utils/Misc.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

library Misc {
/**
* @notice Returns the given bytes with the first 4 bytes removed.
*/
function stripSelector(bytes memory data) internal pure returns (bytes memory trimmedData) {
trimmedData = new bytes(data.length - 4);
assembly {
mstore(add(trimmedData, 0x20), sub(mload(add(data, 0x20)), 0x04))
mstore(add(trimmedData, 0x20), mload(add(data, 0x24)))
}
}
}
20 changes: 20 additions & 0 deletions test/utils/Misc.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.13;

import "forge-std/Test.sol";
import "src/Test.sol";
import "src/utils/Misc.sol";

contract MiscTest is Test {
function x(uint256 n) public pure returns (uint256) {
return n;
}

function testStripSelector() public pure {
bytes memory suaveCalldata = abi.encodeWithSelector(this.x.selector, 123);
bytes memory strippedCalldata = Misc.stripSelector(suaveCalldata);
assertEq(strippedCalldata.length, 32);
uint256 num = abi.decode(strippedCalldata, (uint256));
assertEq(num, 123);
}
}

0 comments on commit fa72e22

Please sign in to comment.