Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
GNSPS committed Apr 12, 2021
2 parents 9cb61ce + 5d251ad commit 83a5468
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 35 deletions.
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 [email protected]
```
or
```
$ npm install [email protected]
```

* Version `v0.1.2` has a major bug fix.

* Version `v0.1.1` has a critical bug fix.
Expand Down
6 changes: 3 additions & 3 deletions contracts/AssertBytes.sol
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand Down
28 changes: 9 additions & 19 deletions contracts/BytesLib.sol
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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(
Expand Down Expand Up @@ -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`
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand All @@ -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;

Expand All @@ -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;

Expand All @@ -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;

Expand All @@ -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;

Expand All @@ -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;

Expand All @@ -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;

Expand All @@ -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;

Expand All @@ -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;

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions contracts/Migrations.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// SPDX-License-Identifier: Unlicense
pragma solidity >=0.5.0 <0.7.0;
pragma solidity >=0.8.0;




Expand All @@ -11,7 +12,7 @@ contract Migrations {
if (msg.sender == owner) _;
}

constructor () public {
constructor () {
owner = msg.sender;
}

Expand Down
2 changes: 1 addition & 1 deletion ethpm.json
Original file line number Diff line number Diff line change
@@ -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á <[email protected]>"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
4 changes: 1 addition & 3 deletions test/TestBytesLib1.sol
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -21,8 +21,6 @@ contract TestBytesLib1 {
bytes storageBytes65 = hex"f00d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000feed";
bytes storageBytes70 = hex"f00d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000feed";

event LogBytes(bytes loggedBytes);

/**
* Sanity Checks
*/
Expand Down
6 changes: 3 additions & 3 deletions test/TestBytesLib2.sol
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion truffle.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
};

0 comments on commit 83a5468

Please sign in to comment.