-
Notifications
You must be signed in to change notification settings - Fork 246
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add ConversionHelpers lib for converting memory types
- Loading branch information
Showing
4 changed files
with
38 additions
and
35 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
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
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,26 @@ | ||
pragma solidity ^0.4.24; | ||
|
||
|
||
library ConversionHelpers { | ||
function castUintArrayToBytes(uint256[] memory _input) internal pure returns (bytes memory output) { | ||
// Force cast the uint256[] into a bytes array, by overwriting its length | ||
// Note that the bytes array doesn't need to be initialized as we immediately overwrite it | ||
// with the input and a new length. The input becomes invalid from this point forward. | ||
uint256 byteLength = _input.length * 32; | ||
assembly { | ||
output := _input | ||
mstore(output, byteLength) | ||
} | ||
} | ||
|
||
function castBytesToUintArray(bytes memory _input) internal pure returns (uint256[] memory output) { | ||
// Force cast the bytes array into a uint256[], by overwriting its length | ||
// Note that the uint256[] doesn't need to be initialized as we immediately overwrite it | ||
// with the input and a new length. The input becomes invalid from this point forward. | ||
uint256 intsLength = _input.length / 32; | ||
assembly { | ||
output := _input | ||
mstore(output, intsLength) | ||
} | ||
} | ||
} |
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