Crates.io | alloy-encode-packed |
lib.rs | alloy-encode-packed |
version | 0.1.1 |
source | src |
created_at | 2024-12-05 12:52:34.722652 |
updated_at | 2024-12-05 13:11:24.240273 |
description | Easy to use replica of Solidity abi.encodePacked. |
homepage | https://github.com/ennea8/alloy-encode-packed |
repository | |
max_upload_size | |
id | 1473148 |
size | 9,813 |
Ethereum abi.encodePacked() in Rust
alloy-encode-packed
is a Rust library that provides an implementation of Ethereum's abi.encodePacked()
function. This crate is an adaptation of the eth-encode-packed library, replacing the ethers.rs
dependency with alloy
, offering improved performance and compatibility with the latest Rust ecosystem.
The library is designed to facilitate encoding of Ethereum contract data in a packed format, which is essential for certain cryptographic operations and smart contract interactions.
let input = vec![
// this is supposed to be uint24 variable in solidity
SolidityDataType::NumberWithShift(U256::from(4001), TakeLastXBytes(24))
];
let (bytes, hash) = abi::encode_packed(&input);
let hash = format!("0x{:}", hash);
let expected = "0x000fa1";
assert_eq!(hash, expected);
Solidity
function packer(
uint24 uint24_data,
uint256 tokenId,
string calldata ipfsURI,
address sample,
uint256 id
) public pure returns (bytes memory ){
bytes memory res = abi.encodePacked(uint24_data, tokenId, ipfsURI, sample, id);
return res;
}
Rust
let address = hex::decode("d8b934580fcE35a11B58C6D73aDeE468a2833fa8").unwrap();
let address: [u8; 20] = address.try_into().unwrap();
let input = vec![
SolidityDataType::NumberWithShift(U256::from(3838), TakeLastXBytes(24)),
SolidityDataType::Number(U256::from(4001)),
SolidityDataType::String("this-is-a-sample-string"),
SolidityDataType::Address(Address::from(address)),
SolidityDataType::Number(U256::from(1)),
];
let (_bytes, hash) = abi::encode_packed(&input);
let hash = format!("0x{:}", hash);
let expected = "0x000efe0000000000000000000000000000000000000000000000000000000000000fa1746869732d69732d612d73616d706c652d737472696e67d8b934580fce35a11b58c6d73adee468a2833fa80000000000000000000000000000000000000000000000000000000000000001";
assert_eq!(hash, expected);