Skip to content

Commit 9776282

Browse files
authored
v0.0.6 - Addition of several other typecast methods (#20)
* 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
1 parent 2403414 commit 9776282

File tree

6 files changed

+141
-113
lines changed

6 files changed

+141
-113
lines changed

README.md

+18-2
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,28 @@ and use the library `AssertBytes` much like they use `Assert` in Truffle's [exam
3131

3232
## EthPM
3333

34-
This library is published at EPM under the alias `bytes`
34+
This library is published in EPM under the alias `bytes`
3535

3636
**Installing it with Truffle**
3737

3838
```
39-
truffle install bytes
39+
$ truffle install bytes
40+
```
41+
42+
## NPM
43+
44+
This library is published in NPM under the alias `solidity-bytes-utils`
45+
46+
**Installing it with NPM**
47+
48+
```
49+
$ npm install solidity-bytes-utils
50+
```
51+
52+
**Importing it in your Solidity contract**
53+
54+
```
55+
import "solidity-bytes-utils/BytesLib.sol";
4056
```
4157

4258
## Contributing

contracts/BytesLib.sol

+33
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,39 @@ library BytesLib {
286286
return tempAddress;
287287
}
288288

289+
function toUint8(bytes _bytes, uint _start) internal pure returns (uint8) {
290+
require(_bytes.length >= (_start + 1));
291+
uint8 tempUint;
292+
293+
assembly {
294+
tempUint := mload(add(add(_bytes, 0x1), _start))
295+
}
296+
297+
return tempUint;
298+
}
299+
300+
function toUint16(bytes _bytes, uint _start) internal pure returns (uint16) {
301+
require(_bytes.length >= (_start + 2));
302+
uint16 tempUint;
303+
304+
assembly {
305+
tempUint := mload(add(add(_bytes, 0x2), _start))
306+
}
307+
308+
return tempUint;
309+
}
310+
311+
function toUint32(bytes _bytes, uint _start) internal pure returns (uint32) {
312+
require(_bytes.length >= (_start + 4));
313+
uint32 tempUint;
314+
315+
assembly {
316+
tempUint := mload(add(add(_bytes, 0x4), _start))
317+
}
318+
319+
return tempUint;
320+
}
321+
289322
function toUint(bytes _bytes, uint _start) internal pure returns (uint256) {
290323
require(_bytes.length >= (_start + 32));
291324
uint256 tempUint;

ethpm.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"package_name": "bytes",
3-
"version": "0.0.5",
3+
"version": "0.0.6",
44
"description": "Solidity bytes tightly packed arrays utility library.",
55
"authors": [
66
"Gonçalo Sá <[email protected]>"

package-lock.json

+3-108
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "solidity-bytes-utils",
3-
"version": "0.0.5",
3+
"version": "0.0.6",
44
"description": "Solidity bytes tightly packed arrays utility library.",
55
"main": "truffle.js",
66
"repository": {

test/TestBytesLib2.sol

+85-1
Original file line numberDiff line numberDiff line change
@@ -126,14 +126,98 @@ contract TestBytesLib2 {
126126
// This should throw;
127127
}
128128

129+
function testToUint8() public {
130+
bytes memory memBytes = hex"f00d20feed";
131+
132+
uint8 testUint8 = 32; // 0x20 == 32
133+
uint8 resultUint8;
134+
135+
resultUint8 = memBytes.toUint8(2);
136+
Assert.equal(uint256(resultUint8), uint256(testUint8), "Typecast to 8-bit-wide unsigned integer failed.");
137+
138+
// Now we're going to test for slicing actions that throw present in the functions below
139+
// with a ThrowProxy contract
140+
// v. http://truffleframework.com/tutorials/testing-for-throws-in-solidity-tests
141+
ThrowProxy throwProxy = new ThrowProxy(address(this));
142+
143+
TestBytesLib2(address(throwProxy)).toUint8Throw();
144+
bool r = throwProxy.execute.gas(100000)();
145+
Assert.isFalse(r, "Typecasting with wrong index should throw");
146+
}
147+
148+
function toUint8Throw() public pure {
149+
bytes memory memBytes = hex"f00d42feed";
150+
151+
uint8 resultUint8;
152+
153+
resultUint8 = memBytes.toUint8(35);
154+
// This should throw;
155+
}
156+
157+
function testToUint16() public {
158+
bytes memory memBytes = hex"f00d0020feed";
159+
160+
uint16 testUint16 = 32; // 0x20 == 32
161+
uint16 resultUint16;
162+
163+
resultUint16 = memBytes.toUint16(2);
164+
Assert.equal(uint256(resultUint16), uint256(testUint16), "Typecast to 16-bit-wide unsigned integer failed.");
165+
166+
// Now we're going to test for slicing actions that throw present in the functions below
167+
// with a ThrowProxy contract
168+
// v. http://truffleframework.com/tutorials/testing-for-throws-in-solidity-tests
169+
ThrowProxy throwProxy = new ThrowProxy(address(this));
170+
171+
TestBytesLib2(address(throwProxy)).toUint16Throw();
172+
bool r = throwProxy.execute.gas(100000)();
173+
Assert.isFalse(r, "Typecasting with wrong index should throw");
174+
}
175+
176+
function toUint16Throw() public pure {
177+
bytes memory memBytes = hex"f00d0042feed";
178+
179+
uint16 resultUint16;
180+
181+
resultUint16 = memBytes.toUint16(35);
182+
// This should throw;
183+
}
184+
185+
function testToUint32() public {
186+
bytes memory memBytes = hex"f00d00000020feed";
187+
188+
uint32 testUint32 = 32; // 0x20 == 32
189+
uint32 resultUint32;
190+
191+
resultUint32 = memBytes.toUint32(2);
192+
Assert.equal(uint256(resultUint32), uint256(testUint32), "Typecast to 32-bit-wide unsigned integer failed.");
193+
194+
// Now we're going to test for slicing actions that throw present in the functions below
195+
// with a ThrowProxy contract
196+
// v. http://truffleframework.com/tutorials/testing-for-throws-in-solidity-tests
197+
ThrowProxy throwProxy = new ThrowProxy(address(this));
198+
199+
TestBytesLib2(address(throwProxy)).toUint32Throw();
200+
bool r = throwProxy.execute.gas(100000)();
201+
Assert.isFalse(r, "Typecasting with wrong index should throw");
202+
}
203+
204+
function toUint32Throw() public pure {
205+
bytes memory memBytes = hex"f00d00000042feed";
206+
207+
uint32 resultUint32;
208+
209+
resultUint32 = memBytes.toUint32(35);
210+
// This should throw;
211+
}
212+
129213
function testToUint() public {
130214
bytes memory memBytes = hex"f00d0000000000000000000000000000000000000000000000000000000000000020feed";
131215

132216
uint256 testUint = 32; // 0x20 == 32
133217
uint256 resultUint;
134218

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

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

0 commit comments

Comments
 (0)