{"library":"solidity-bytes-utils","title":"Solidity Bytes Utilities Library","type":"library","description":"This package, `solidity-bytes-utils`, provides a comprehensive utility library for manipulating tightly packed `bytes` arrays within Ethereum smart contracts written in Solidity. It offers functionalities such as concatenation, slicing, and various type casting methods for `bytes` arrays stored in both memory and storage, enhancing low-level byte manipulation capabilities. The current stable version is `0.8.4`, which is designed to be fully compatible with Solidity `v0.8.x` compilers. Since version `v0.8.0`, the library has adopted a specific versioning system where its major version directly aligns with the compatible Solidity compiler's major version, providing a clear indication of compatibility for developers. It is distributed as an npm package and primarily consumed by importing its `.sol` files into Solidity projects via paths like `bytes/BytesLib.sol`, making it suitable for development environments like Hardhat or Truffle. The library is not intended for deployment on mainnet itself, as its methods are all internal. Historically, the project has addressed several critical bugs, particularly concerning the `slice` method, which have involved vulnerabilities related to arbitrary memory writes and issues with zero-length slices, emphasizing the importance of using the latest patched versions for security. Its key differentiator lies in providing robust, audited low-level byte manipulation primitives for Solidity, which are crucial for efficient and secure smart contract development.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install solidity-bytes-utils"],"cli":null},"imports":["import \"bytes/BytesLib.sol\";","import \"bytes/AssertBytes.sol\";","myBytes.slice(start, length);"],"auth":{"required":false,"env_vars":[]},"links":{"homepage":null,"github":"https://github.com/GNSPS/solidity-bytes-utils","docs":null,"changelog":null,"pypi":null,"npm":"https://www.npmjs.com/package/solidity-bytes-utils","openapi_spec":null,"status_page":null,"smithery":null},"quickstart":{"code":"// SPDX-License-Identifier: MIT\npragma solidity ^0.8.0;\n\nimport \"bytes/BytesLib.sol\";\n\ncontract BytesUtilsExample {\n    using BytesLib for bytes;\n\n    function demonstrateUtils() public pure returns (\n        bytes memory concatenated,\n        bytes memory sliced,\n        uint256 castedUint256,\n        uint8 castedUint8\n    ) {\n        bytes memory a = hex\"01020304\";\n        bytes memory b = hex\"05060708\";\n\n        // Concatenate two bytes arrays\n        concatenated = a.concat(b); // Result: 0x0102030405060708\n\n        // Slice a portion: slice(start_index, length)\n        // Extracts 4 bytes starting from index 2\n        sliced = concatenated.slice(2, 4); // Result: 0x03040506\n\n        // Type cast bytes to uint256\n        bytes memory valBytes = hex\"000000000000000000000000000000000000000000000000000000000000000A\"; // Represents decimal 10\n        castedUint256 = valBytes.toUint(32); // Cast 32 bytes from position 0 to uint256\n\n        // Type cast bytes to uint8\n        bytes memory smallValBytes = hex\"0F\"; // Represents decimal 15\n        castedUint8 = smallValBytes.toUint8(0); // Cast 1 byte from position 0 to uint8\n    }\n}","lang":"typescript","description":"This Solidity smart contract demonstrates the core functionalities of `solidity-bytes-utils`, including concatenating bytes arrays, slicing a portion, and type casting bytes into different unsigned integer types (uint256 and uint8). The code is written in Solidity for a `^0.8.0` compiler.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}