Registry / web-framework / solidity-bytes-utils

solidity-bytes-utils

JSON →
library0.8.4jsnpmunverified

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.

npm install solidity-bytes-utils
INSTALL
IMPORT
SIG · SOLIDITY-BYTES-UTI
S
solidity-bytes-utils
web-frameworkjavascriptv0.8.4
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

BytesLib
import "bytes/BytesLib.sol";
import { BytesLib } from 'bytes/BytesLib.sol';
This package provides Solidity library contracts. This `import` statement is used *within Solidity source files* to make the `BytesLib` functions available. It is typically consumed via a `using BytesLib for bytes;` directive. The path `bytes/BytesLib.sol` is standard when the library is installed via npm or EPM. Named imports like `import { BytesLib }` are not idiomatic for Solidity libraries with internal functions.
AssertBytes
import "bytes/AssertBytes.sol";
import { AssertBytes } from 'bytes/AssertBytes.sol';
This import is for the auxiliary `AssertBytes` library, also used within Solidity source files, primarily for testing and assertion purposes. Similar to `BytesLib`, it is typically imported directly by path.
bytes.slice()
myBytes.slice(start, length);
BytesLib.slice(myBytes, start, length);
After importing `BytesLib.sol` and declaring `using BytesLib for bytes;` within your Solidity contract, functions like `slice` are called directly on `bytes` variables as member functions. While direct calls using `BytesLib.slice(...)` are technically possible (without `using for`), the member function syntax is the idiomatic and recommended pattern in modern Solidity.

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.

// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; import "bytes/BytesLib.sol"; contract BytesUtilsExample { using BytesLib for bytes; function demonstrateUtils() public pure returns ( bytes memory concatenated, bytes memory sliced, uint256 castedUint256, uint8 castedUint8 ) { bytes memory a = hex"01020304"; bytes memory b = hex"05060708"; // Concatenate two bytes arrays concatenated = a.concat(b); // Result: 0x0102030405060708 // Slice a portion: slice(start_index, length) // Extracts 4 bytes starting from index 2 sliced = concatenated.slice(2, 4); // Result: 0x03040506 // Type cast bytes to uint256 bytes memory valBytes = hex"000000000000000000000000000000000000000000000000000000000000000A"; // Represents decimal 10 castedUint256 = valBytes.toUint(32); // Cast 32 bytes from position 0 to uint256 // Type cast bytes to uint8 bytes memory smallValBytes = hex"0F"; // Represents decimal 15 castedUint8 = smallValBytes.toUint8(0); // Cast 1 byte from position 0 to uint8 } }
Debug
Known issues
breakingVersion `v0.8.0` introduced breaking changes to support Solidity `v0.8.x` syntax and a new versioning scheme. Projects using Solidity compilers older than `0.8.0` must use an older version of the library (e.g., `v0.1.2`). Attempting to compile `v0.8.x` of the library with an older Solidity compiler will result in compilation errors.
fix
Upgrade your Solidity compiler to `^0.8.0` or ensure your `solidity-bytes-utils` version matches your compiler's major version. For older Solidity projects, install `solidity-bytes-utils@0.1.2`.
affects: <0.8.0
breakingThe `equal_nonAligned` function, which was introduced in an earlier version (e.g., `v0.9.0` of the old versioning system), was removed in `v0.8.3` due to identified issues and unnecessary complexity. Any codebase relying on this specific comparison method will encounter compilation or runtime errors upon upgrading.
fix
Review your code for usages of `equal_nonAligned` and remove or replace them. Consider standard Solidity equality checks or alternative byte comparison logic if non-aligned comparisons are critical.
affects: <=0.8.2
breakingVersion `v0.0.7` refactored the codebase to support Solidity `v0.5.x` breaking syntax changes, thereby dropping support for Solidity compilers `<0.4.22`. Projects requiring compatibility with very old Solidity versions must remain on `solidity-bytes-utils@0.0.6` or earlier.
fix
If your project targets Solidity `<0.4.22`, downgrade `solidity-bytes-utils` to `0.0.6`. Otherwise, upgrade your Solidity compiler to `0.4.22` or later.
affects: <0.0.7
breakingMultiple critical vulnerabilities were discovered in the `slice` method across early versions of the library. Versions `<=0.1.1` were vulnerable to arbitrary memory writes when the `_start` and `_length` parameters were user-supplied inputs. Additionally, versions `<=0.1.1` were affected by a bug where zero-length slices could return arbitrary memory content if memory slots were tainted. Users must upgrade to `v0.1.2` or later to mitigate these critical security issues and ensure correct, safe memory handling.
fix
Immediately update `solidity-bytes-utils` to version `0.1.2` or any subsequent version (e.g., `0.8.x`). Recompile and redeploy affected contracts.
affects: <=0.1.1
Errors
Common errors & fixes
Compiler error: Source file requires different compiler version
Using `solidity-bytes-utils` v0.8.x with a Solidity compiler version less than 0.8.0 (e.g., 0.6.x or 0.7.x). The library's major version now aligns with the compatible Solidity compiler version.
fix
Upgrade your Solidity compiler to `^0.8.0` or downgrade the library version to one compatible with your compiler, for example, `npm install solidity-bytes-utils@0.1.2` for older Solidity projects.
TypeError: Undeclared identifier. Did you mean to import it? (e.g. 'BytesLib') OR Source not found: bytes/BytesLib.sol
This typically occurs in Solidity when the library's `.sol` files are not found by the compiler. Reasons include an incorrect import path in your Solidity file or the `solidity-bytes-utils` package not being correctly installed or linked in your development environment (e.g., Hardhat, Truffle).
fix
Ensure `solidity-bytes-utils` is installed in your project's `node_modules` directory (`npm install solidity-bytes-utils` or `yarn add solidity-bytes-utils`). Verify that your Solidity file uses the correct import path: `import "bytes/BytesLib.sol";` and that your compiler configuration includes `node_modules` as a source mapping.
Upgrade
Version history
0.8.4latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
8 hits · last 30 days
node
6
OpenAI (training)
2
Resources
solidity-bytes-utils — npm install solidity-bytes-utils · libregistry