-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
17db5e0
commit fa72e22
Showing
2 changed files
with
35 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |