Git Product home page Git Product logo

solidity-bytes-utils's Introduction

Codeship Status for GNSPS/solidity-bytes-utils

Solidity Bytes Arrays Utils

Bytes tightly packed arrays utility library for ethereum contracts written.

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.

Usage

You can use the library here present by direct download and importing with:

import "BytesLib.sol";

or, if you have installed it from EPM (see below), with Truffle's specific paths:

import "bytes/BytesLib.sol";`

Usage examples and API are more thoroughly explained below.

Also there's an extra library in there called AssertBytes (inside the same named file) which is compatible with Truffle's Solidity testing library Assert.sol event firing and so lets you now test bytes equalities/inequalities in your Solidity tests by just importing it in your .sol test files:

import "bytes/AssertBytes.sol";

and use the library AssertBytes much like they use Assert in Truffle's example.

EthPM

This library is published at EPM under the alias bytes

Installing it with Truffle

truffle install bytes

Contributing

Contributions are more than welcome in any way shape or form! ๐Ÿ˜„

TODOs:

  • Two storage bytes arrays concatenation
  • Two storage bytes arrays concatenation
  • Slicing directly from storage
  • Implement inline assembly functions for better readability

Testing

This project uses Truffle for tests. Truffle's version of solc needs to be at least 0.4.19 for the contracts to compile. If you encounter compilation errors, try:

$ cd /usr/local/lib/node_modules/truffle
$ npm install solc@latest

To run the tests, start a testrpc instance, then run truffle test.

API

  • function concat(bytes memory _preBytes, bytes memory _postBytes) internal pure returns (bytes)

Concatenates two bytes arrays in memory and returns the concatenation result as another bytes array in memory.

  • function concatStorage(bytes storage _preBytes, bytes memory _postBytes) internal pure

Concatenates a bytes array present in memory directly into the given storage location addressed by the _preBytes storage pointer.

  • function slice(bytes _bytes, uint _start, uint _length) internal pure returns (bytes)

Takes a slice from a bytes array in memory of given length starting from _startth byte counting from the left-most one (0-based).

  • function toAddress(bytes _bytes, uint _start) internal pure returns (address)

Takes a 20-byte-long sequence present in a bytes array in memory and returns that as an address (also checks for sufficient length).

  • function toUint(bytes _bytes, uint _start) internal pure returns (uint256)

Takes a 32-byte-long sequence present in a bytes array in memory and returns that as an unsigned integer (also checks for sufficient length).

  • function equal(bytes memory _preBytes, bytes memory _postBytes) internal view returns (bool)

Compares two bytes arrays in memory and returns the comparison result as a bool variable.

  • function equalStorage(bytes storage _preBytes, bytes memory _postBytes) internal view returns (bool)

Compares a bytes array in storage against another bytes array in memory and returns the comparison result as a bool variable.

Examples

Ordered to mimic the above API section ordering:

contract MyContract {
	using BytesLib for bytes;

	function myFunc() {
		bytes memory _preBytes = hex"f00dfeed";
		bytes memory _postBytes = hex"f00dfeed";

		bytes memory concatBytes = _preBytes.concat(_postBytes);

		// concatBytes == 0xf00dfeedf00dfeed
	}
}
contract MyContract {
	using BytesLib for bytes;

	bytes storageBytes = hex"f00dfeed";

	function myFunc() {
		bytes memory _postBytes = hex"f00dfeed";

		storageBytes.concatStorage(_postBytes);

		// storageBytes == 0xf00dfeedf00dfeed
	}
}
contract MyContract {
	using BytesLib for bytes;

	function myFunc() {
		bytes memory memBytes = hex"f00dfeedaabbccddeeff";

		bytes memory slice1 = memBytes.slice(0, 2);
		bytes memory slice2 = memBytes.slice(2, 2);

		// slice1 == 0xf00d
		// slice2 == 0xfeed
	}
}
contract MyContract {
	using BytesLib for bytes;

	function myFunc() {
		bytes memory memBytes = hex"f00dfeed383Fa3B60f9B4AB7fBf6835d3c26C3765cD2B2e2f00dfeed";

		address addrFromBytes = memBytes.toAddress(4);

		// addrFromBytes == 0x383Fa3B60f9B4AB7fBf6835d3c26C3765cD2B2e2
	}
}
contract MyContract {
	using BytesLib for bytes;

	function myFunc() {
		bytes memory memBytes = hex"f00d0000000000000000000000000000000000000000000000000000000000000042feed";

		uint256 uintFromBytes = memBytes.toUint(2);

		// uintFromBytes == 42
	}
}
contract MyContract {
	using BytesLib for bytes;

	function myFunc() {
		bytes memory memBytes = hex"f00dfeed";
		bytes memory checkBytesTrue = hex"f00dfeed";
		bytes memory checkBytesFalse = hex"00000000";

		bool check1 = memBytes.equal(checkBytesTrue);
		bool check2 = memBytes.equal(checkBytesFalse);

		// check1 == true
		// check2 == false
	}
}
contract MyContract {
	using BytesLib for bytes;

	bytes storageBytes = hex"f00dfeed";

	function myFunc() {
		bytes memory checkBytesTrue = hex"f00dfeed";
		bytes memory checkBytesFalse = hex"00000000";

		bool check1 = storageBytes.equalStorage(checkBytesTrue);
		bool check2 = storageBytes.equalStorage(checkBytesFalse);

		// check1 == true
		// check2 == false
	}
}

solidity-bytes-utils's People

Contributors

gnsps avatar niran avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.