What is @ethersproject/solidity?
@ethersproject/solidity is a part of the ethers.js library, which provides utilities for interacting with the Ethereum blockchain. This specific package focuses on Solidity-related functionalities, such as hashing and encoding data according to Solidity's rules.
What are @ethersproject/solidity's main functionalities?
Solidity Keccak256
This feature allows you to compute the Keccak256 hash of Solidity-encoded data. The example demonstrates hashing a string 'Hello, world!' using Solidity's encoding rules.
const { keccak256 } = require('@ethersproject/solidity');
const hash = keccak256(['string'], ['Hello, world!']);
console.log(hash);
Solidity Pack
This feature allows you to pack multiple Solidity types into a single byte array. The example shows how to pack a uint256 and an address into a byte array.
const { pack } = require('@ethersproject/solidity');
const packedData = pack(['uint256', 'address'], [12345, '0x742d35Cc6634C0532925a3b844Bc454e4438f44e']);
console.log(packedData);
Solidity AbiCoder
This feature provides an ABI coder for encoding and decoding Solidity data types. The example demonstrates encoding a uint256 and a string into ABI format.
const { AbiCoder } = require('@ethersproject/solidity');
const abiCoder = new AbiCoder();
const encoded = abiCoder.encode(['uint256', 'string'], [12345, 'Hello, world!']);
console.log(encoded);
Other packages similar to @ethersproject/solidity
web3-utils
web3-utils is a utility library that is part of the web3.js library. It provides similar functionalities for hashing, encoding, and decoding data according to Solidity's rules. Compared to @ethersproject/solidity, web3-utils is part of a larger library that includes many other utilities for interacting with the Ethereum blockchain.
ethjs-abi
ethjs-abi is a standalone library for encoding and decoding Ethereum ABI data. It offers similar functionalities to @ethersproject/solidity's AbiCoder but is more focused on ABI encoding/decoding. It is lightweight and can be used independently of other libraries.
solidity-sha3
solidity-sha3 is a library specifically designed for computing Solidity-compatible SHA3 (Keccak256) hashes. It provides a simple interface for hashing data according to Solidity's rules. While it focuses solely on hashing, it offers a similar feature to @ethersproject/solidity's keccak256 function.
Solidity Packed-Encoding Utilities
This sub-module is part of the ethers project.
It contains functions to perform Solidity-specific packed (i.e. non-standard)
encoding operations.
For more information, see the documentation.
Importing
Most users will prefer to use the umbrella package,
but for those with more specific needs, individual components can be imported.
const {
pack,
keccak256,
sha256
} = require("@ethersproject/solidity");
License
MIT License