-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBytesHelper.sol
68 lines (60 loc) · 2.01 KB
/
BytesHelper.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
pragma solidity 0.8.25;
library BytesHelper {
/// @notice function to grab the first 4 bytes of calldata payload
/// returns the function selector
/// @param toSlice the calldata payload
function getFunctionSignature(bytes memory toSlice)
public
pure
returns (bytes4 functionSignature)
{
require(toSlice.length >= 4, "No function signature");
functionSignature = bytes4(toSlice);
}
/// @notice function to grab the first 32 bytes of returned memory
/// @param toSlice the calldata payload
function getFirstWord(bytes memory toSlice)
public
pure
returns (uint256 value)
{
require(toSlice.length >= 32, "Length less than 32 bytes");
assembly ("memory-safe") {
value := mload(add(toSlice, 0x20))
}
}
/// @notice function to grab a slice of bytes out of a byte string
/// returns the slice
/// @param toSlice the byte string to slice
/// @param start the start index of the slice
/// @param end the end index of the slice
function sliceBytes(bytes memory toSlice, uint256 start, uint256 end)
public
pure
returns (bytes memory)
{
require(
start < toSlice.length,
"Start index is greater than the length of the byte string"
);
require(
end <= toSlice.length,
"End index is greater than the length of the byte string"
);
require(start < end, "Start index not less than end index");
uint256 length = end - start;
bytes memory sliced = new bytes(length);
for (uint256 i = 0; i < length; i++) {
sliced[i] = toSlice[i + start];
}
return sliced;
}
/// @notice function to get the hash of a slice of bytes
function getSlicedBytesHash(
bytes memory toSlice,
uint256 start,
uint256 end
) public pure returns (bytes32) {
return keccak256(sliceBytes(toSlice, start, end));
}
}