Skip to content

Commit

Permalink
v0.0.6 - Addition of several other typecast methods (#20)
Browse files Browse the repository at this point in the history
* Vanity updates

* Add type cast method to `bytes32`

* Update to the natSpec comment on top of both libraries 😄

Though I really like Nick Johnson's string library this has been built entirely by me (as opposed to what the NatSpec comment in the top said since the last commit 😄) I just copied the comment layout on top of his library and forgot to change the author 😂

* Adding Solidity highlight to gitattribute

🎉 😄

* Update package versions & prepare for NPM publish

* Create a NPM ignore file to keep `truffle.js` from being published

* Syntax & pragmas updates for v0.5.0

* Update README to reflect NPM publish

* Another README update

* Support for typecasting into uint8, uint16, uint32 (#19)

* Add uint8, uint16, uint32

* Unit test for uint8, uint16, uint32

* Removing Truffle as a dependency

* Bump version to v0.0.6
  • Loading branch information
GNSPS authored Feb 11, 2019
1 parent 2403414 commit 9776282
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 113 deletions.
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,28 @@ and use the library `AssertBytes` much like they use `Assert` in Truffle's [exam

## EthPM

This library is published at EPM under the alias `bytes`
This library is published in EPM under the alias `bytes`

**Installing it with Truffle**

```
truffle install bytes
$ truffle install bytes
```

## NPM

This library is published in NPM under the alias `solidity-bytes-utils`

**Installing it with NPM**

```
$ npm install solidity-bytes-utils
```

**Importing it in your Solidity contract**

```
import "solidity-bytes-utils/BytesLib.sol";
```

## Contributing
Expand Down
33 changes: 33 additions & 0 deletions contracts/BytesLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,39 @@ library BytesLib {
return tempAddress;
}

function toUint8(bytes _bytes, uint _start) internal pure returns (uint8) {
require(_bytes.length >= (_start + 1));
uint8 tempUint;

assembly {
tempUint := mload(add(add(_bytes, 0x1), _start))
}

return tempUint;
}

function toUint16(bytes _bytes, uint _start) internal pure returns (uint16) {
require(_bytes.length >= (_start + 2));
uint16 tempUint;

assembly {
tempUint := mload(add(add(_bytes, 0x2), _start))
}

return tempUint;
}

function toUint32(bytes _bytes, uint _start) internal pure returns (uint32) {
require(_bytes.length >= (_start + 4));
uint32 tempUint;

assembly {
tempUint := mload(add(add(_bytes, 0x4), _start))
}

return tempUint;
}

function toUint(bytes _bytes, uint _start) internal pure returns (uint256) {
require(_bytes.length >= (_start + 32));
uint256 tempUint;
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.0.5",
"version": "0.0.6",
"description": "Solidity bytes tightly packed arrays utility library.",
"authors": [
"Gonçalo Sá <goncalo.sa@consensys.net>"
Expand Down
111 changes: 3 additions & 108 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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.0.5",
"version": "0.0.6",
"description": "Solidity bytes tightly packed arrays utility library.",
"main": "truffle.js",
"repository": {
Expand Down
86 changes: 85 additions & 1 deletion test/TestBytesLib2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,98 @@ contract TestBytesLib2 {
// This should throw;
}

function testToUint8() public {
bytes memory memBytes = hex"f00d20feed";

uint8 testUint8 = 32; // 0x20 == 32
uint8 resultUint8;

resultUint8 = memBytes.toUint8(2);
Assert.equal(uint256(resultUint8), uint256(testUint8), "Typecast to 8-bit-wide unsigned integer failed.");

// Now we're going to test for slicing actions that throw present in the functions below
// with a ThrowProxy contract
// v. http://truffleframework.com/tutorials/testing-for-throws-in-solidity-tests
ThrowProxy throwProxy = new ThrowProxy(address(this));

TestBytesLib2(address(throwProxy)).toUint8Throw();
bool r = throwProxy.execute.gas(100000)();
Assert.isFalse(r, "Typecasting with wrong index should throw");
}

function toUint8Throw() public pure {
bytes memory memBytes = hex"f00d42feed";

uint8 resultUint8;

resultUint8 = memBytes.toUint8(35);
// This should throw;
}

function testToUint16() public {
bytes memory memBytes = hex"f00d0020feed";

uint16 testUint16 = 32; // 0x20 == 32
uint16 resultUint16;

resultUint16 = memBytes.toUint16(2);
Assert.equal(uint256(resultUint16), uint256(testUint16), "Typecast to 16-bit-wide unsigned integer failed.");

// Now we're going to test for slicing actions that throw present in the functions below
// with a ThrowProxy contract
// v. http://truffleframework.com/tutorials/testing-for-throws-in-solidity-tests
ThrowProxy throwProxy = new ThrowProxy(address(this));

TestBytesLib2(address(throwProxy)).toUint16Throw();
bool r = throwProxy.execute.gas(100000)();
Assert.isFalse(r, "Typecasting with wrong index should throw");
}

function toUint16Throw() public pure {
bytes memory memBytes = hex"f00d0042feed";

uint16 resultUint16;

resultUint16 = memBytes.toUint16(35);
// This should throw;
}

function testToUint32() public {
bytes memory memBytes = hex"f00d00000020feed";

uint32 testUint32 = 32; // 0x20 == 32
uint32 resultUint32;

resultUint32 = memBytes.toUint32(2);
Assert.equal(uint256(resultUint32), uint256(testUint32), "Typecast to 32-bit-wide unsigned integer failed.");

// Now we're going to test for slicing actions that throw present in the functions below
// with a ThrowProxy contract
// v. http://truffleframework.com/tutorials/testing-for-throws-in-solidity-tests
ThrowProxy throwProxy = new ThrowProxy(address(this));

TestBytesLib2(address(throwProxy)).toUint32Throw();
bool r = throwProxy.execute.gas(100000)();
Assert.isFalse(r, "Typecasting with wrong index should throw");
}

function toUint32Throw() public pure {
bytes memory memBytes = hex"f00d00000042feed";

uint32 resultUint32;

resultUint32 = memBytes.toUint32(35);
// This should throw;
}

function testToUint() public {
bytes memory memBytes = hex"f00d0000000000000000000000000000000000000000000000000000000000000020feed";

uint256 testUint = 32; // 0x20 == 32
uint256 resultUint;

resultUint = memBytes.toUint(2);
Assert.equal(resultUint, testUint, "Typecast to unsigned integer failed.");
Assert.equal(resultUint, testUint, "Typecast to 256-bit-wide unsigned integer failed.");

// Now we're going to test for slicing actions that throw present in the functions below
// with a ThrowProxy contract
Expand Down

0 comments on commit 9776282

Please sign in to comment.