diff --git a/README.md b/README.md index e1e8e82..7c764f0 100644 --- a/README.md +++ b/README.md @@ -2,11 +2,11 @@ # Solidity Bytes Arrays Utils -Bytes tightly packed arrays utility library for ethereum contracts written in Solidity. +Bytes tightly packed arrays' utility library for ethereum contracts written in Solidity. The library lets you concatenate, slice and type cast bytes arrays both in memory and storage. -Given this library has an all-internal collection of methods it doesn't make sense having it reside in the mainnet. Instead it will only be available in EPM as an installable package. +Given this library has an all-internal collection of methods it doesn't make sense to have it reside in the mainnet. Instead it will only be available on EPM as an installable package. ## Important Fixes Changelog @@ -34,6 +34,17 @@ This made me realize that in permissioned blockchains where gas is also not a li ## _Version Notes_: +* Starting from version `v0.8.0` the versioning will change to follow compatible Solidity's compiler versions. +This means that now the library will only compile on Solidity versions `>=0.8.0` so, if you need `<0.8.0` support for your project just use `v0.1.2` of the library with: + +``` +$ truffle install bytes@0.0.6 +``` +or +``` +$ npm install solidity-bytes-utils@0.0.6 +``` + * Version `v0.1.2` has a major bug fix. * Version `v0.1.1` has a critical bug fix. diff --git a/contracts/AssertBytes.sol b/contracts/AssertBytes.sol old mode 100755 new mode 100644 index 9d436f7..9965677 --- a/contracts/AssertBytes.sol +++ b/contracts/AssertBytes.sol @@ -7,7 +7,7 @@ * This library is compliant with the test event convention that the Truffle suite uses. */ -pragma solidity >=0.5.0 <0.7.0; +pragma solidity >=0.8.0 <0.9.0; library AssertBytes { @@ -104,7 +104,7 @@ library AssertBytes { assembly { // we know _a_offset is 0 - let fslot := sload(_a_slot) + let fslot := sload(_a.slot) let slength := div(and(fslot, sub(mul(0x100, iszero(and(fslot, 1))), 1)), 2) let mlength := mload(_b) @@ -133,7 +133,7 @@ library AssertBytes { let cb := 1 // get the keccak hash to get the contents of the array - mstore(0x0, _a_slot) + mstore(0x0, _a.slot) let sc := keccak256(0x0, 0x20) let mc := add(_b, 0x20) diff --git a/contracts/BytesLib.sol b/contracts/BytesLib.sol old mode 100755 new mode 100644 index 90a3894..532897a --- a/contracts/BytesLib.sol +++ b/contracts/BytesLib.sol @@ -6,7 +6,7 @@ * @dev Bytes tightly packed arrays utility library for ethereum contracts written in Solidity. * The library lets you concatenate, slice and type cast bytes arrays both in memory and storage. */ -pragma solidity >=0.5.0 <0.7.0; +pragma solidity >=0.8.0 <0.9.0; library BytesLib { @@ -93,7 +93,7 @@ library BytesLib { // Read the first 32 bytes of _preBytes storage, which is the length // of the array. (We don't need to use the offset into the slot // because arrays use the entire slot.) - let fslot := sload(_preBytes_slot) + let fslot := sload(_preBytes.slot) // Arrays of 31 bytes or less have an even value in their slot, // while longer arrays have an odd value. The actual length is // the slot divided by two for odd values, and the lowest order @@ -113,7 +113,7 @@ library BytesLib { // update the contents of the slot. // uint256(bytes_storage) = uint256(bytes_storage) + uint256(bytes_memory) + new_length sstore( - _preBytes_slot, + _preBytes.slot, // all the modifications to the slot are inside this // next block add( @@ -143,11 +143,11 @@ library BytesLib { // The stored value fits in the slot, but the combined value // will exceed it. // get the keccak hash to get the contents of the array - mstore(0x0, _preBytes_slot) + mstore(0x0, _preBytes.slot) let sc := add(keccak256(0x0, 0x20), div(slength, 32)) // save new length - sstore(_preBytes_slot, add(mul(newlength, 2), 1)) + sstore(_preBytes.slot, add(mul(newlength, 2), 1)) // The contents of the _postBytes array start 32 bytes into // the structure. Our first read should obtain the `submod` @@ -190,12 +190,12 @@ library BytesLib { } default { // get the keccak hash to get the contents of the array - mstore(0x0, _preBytes_slot) + mstore(0x0, _preBytes.slot) // Start copying to the last used word of the stored array. let sc := add(keccak256(0x0, 0x20), div(slength, 32)) // save new length - sstore(_preBytes_slot, add(mul(newlength, 2), 1)) + sstore(_preBytes.slot, add(mul(newlength, 2), 1)) // Copy over the first `submod` bytes of the new data as in // case 1 above. @@ -235,7 +235,6 @@ library BytesLib { returns (bytes memory) { require(_length + 31 >= _length, "slice_overflow"); - require(_start + _length >= _start, "slice_overflow"); require(_bytes.length >= _start + _length, "slice_outOfBounds"); bytes memory tempBytes; @@ -296,7 +295,6 @@ library BytesLib { } function toAddress(bytes memory _bytes, uint256 _start) internal pure returns (address) { - require(_start + 20 >= _start, "toAddress_overflow"); require(_bytes.length >= _start + 20, "toAddress_outOfBounds"); address tempAddress; @@ -308,7 +306,6 @@ library BytesLib { } function toUint8(bytes memory _bytes, uint256 _start) internal pure returns (uint8) { - require(_start + 1 >= _start, "toUint8_overflow"); require(_bytes.length >= _start + 1 , "toUint8_outOfBounds"); uint8 tempUint; @@ -320,7 +317,6 @@ library BytesLib { } function toUint16(bytes memory _bytes, uint256 _start) internal pure returns (uint16) { - require(_start + 2 >= _start, "toUint16_overflow"); require(_bytes.length >= _start + 2, "toUint16_outOfBounds"); uint16 tempUint; @@ -332,7 +328,6 @@ library BytesLib { } function toUint32(bytes memory _bytes, uint256 _start) internal pure returns (uint32) { - require(_start + 4 >= _start, "toUint32_overflow"); require(_bytes.length >= _start + 4, "toUint32_outOfBounds"); uint32 tempUint; @@ -344,7 +339,6 @@ library BytesLib { } function toUint64(bytes memory _bytes, uint256 _start) internal pure returns (uint64) { - require(_start + 8 >= _start, "toUint64_overflow"); require(_bytes.length >= _start + 8, "toUint64_outOfBounds"); uint64 tempUint; @@ -356,7 +350,6 @@ library BytesLib { } function toUint96(bytes memory _bytes, uint256 _start) internal pure returns (uint96) { - require(_start + 12 >= _start, "toUint96_overflow"); require(_bytes.length >= _start + 12, "toUint96_outOfBounds"); uint96 tempUint; @@ -368,7 +361,6 @@ library BytesLib { } function toUint128(bytes memory _bytes, uint256 _start) internal pure returns (uint128) { - require(_start + 16 >= _start, "toUint128_overflow"); require(_bytes.length >= _start + 16, "toUint128_outOfBounds"); uint128 tempUint; @@ -380,7 +372,6 @@ library BytesLib { } function toUint256(bytes memory _bytes, uint256 _start) internal pure returns (uint256) { - require(_start + 32 >= _start, "toUint256_overflow"); require(_bytes.length >= _start + 32, "toUint256_outOfBounds"); uint256 tempUint; @@ -392,7 +383,6 @@ library BytesLib { } function toBytes32(bytes memory _bytes, uint256 _start) internal pure returns (bytes32) { - require(_start + 32 >= _start, "toBytes32_overflow"); require(_bytes.length >= _start + 32, "toBytes32_outOfBounds"); bytes32 tempBytes32; @@ -458,7 +448,7 @@ library BytesLib { assembly { // we know _preBytes_offset is 0 - let fslot := sload(_preBytes_slot) + let fslot := sload(_preBytes.slot) // Decode the length of the stored array like in concatStorage(). let slength := div(and(fslot, sub(mul(0x100, iszero(and(fslot, 1))), 1)), 2) let mlength := mload(_postBytes) @@ -488,7 +478,7 @@ library BytesLib { let cb := 1 // get the keccak hash to get the contents of the array - mstore(0x0, _preBytes_slot) + mstore(0x0, _preBytes.slot) let sc := keccak256(0x0, 0x20) let mc := add(_postBytes, 0x20) diff --git a/contracts/Migrations.sol b/contracts/Migrations.sol index 64d5cf5..23883af 100755 --- a/contracts/Migrations.sol +++ b/contracts/Migrations.sol @@ -1,5 +1,6 @@ // SPDX-License-Identifier: Unlicense -pragma solidity >=0.5.0 <0.7.0; +pragma solidity >=0.8.0; + @@ -11,7 +12,7 @@ contract Migrations { if (msg.sender == owner) _; } - constructor () public { + constructor () { owner = msg.sender; } diff --git a/ethpm.json b/ethpm.json index ab70837..daabb78 100644 --- a/ethpm.json +++ b/ethpm.json @@ -1,6 +1,6 @@ { "package_name": "bytes", - "version": "0.1.2", + "version": "0.8.0", "description": "Solidity bytes tightly packed arrays utility library.", "authors": [ "Gonçalo Sá " diff --git a/package.json b/package.json index 81be712..218a4d9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "solidity-bytes-utils", - "version": "0.1.2", + "version": "0.8.0", "description": "Solidity bytes tightly packed arrays utility library.", "main": "truffle.js", "repository": { diff --git a/test/TestBytesLib1.sol b/test/TestBytesLib1.sol index 94c37b7..24cca58 100755 --- a/test/TestBytesLib1.sol +++ b/test/TestBytesLib1.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: Unlicense -pragma solidity >=0.5.0 <0.7.0; +pragma solidity >=0.8.0 <0.9.0; import "truffle/Assert.sol"; import "../contracts/AssertBytes.sol"; @@ -21,8 +21,6 @@ contract TestBytesLib1 { bytes storageBytes65 = hex"f00d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000feed"; bytes storageBytes70 = hex"f00d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000feed"; - event LogBytes(bytes loggedBytes); - /** * Sanity Checks */ diff --git a/test/TestBytesLib2.sol b/test/TestBytesLib2.sol index fc617f5..65b1cad 100755 --- a/test/TestBytesLib2.sol +++ b/test/TestBytesLib2.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: Unlicense -pragma solidity >=0.5.0 <0.7.0; +pragma solidity >=0.8.0 <0.9.0; import "truffle/Assert.sol"; import "../contracts/AssertBytes.sol"; @@ -11,8 +11,8 @@ contract TestBytesLib2 { bytes storageCheckBytes = hex"aabbccddeeff"; bytes storageCheckBytesZeroLength = hex""; - - uint256 constant MAX_UINT = uint256(-1); + + uint256 constant MAX_UINT = type(uint256).max; /** * Sanity Checks diff --git a/truffle.js b/truffle.js index 071b4c4..529d99f 100755 --- a/truffle.js +++ b/truffle.js @@ -43,7 +43,7 @@ module.exports = { }, compilers: { solc: { - version: "0.6.10", // A version or constraint - Ex. "^0.5.0" + version: "0.8.3", // A version or constraint - Ex. "^0.5.0" } } };