Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Misc lib w/ stripSelector #94

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
Loading