#solidity #abi #ethereum #eth

eth-encode-packed

Solidity abi.encodePacked()的简单复制品

1个不稳定版本

0.1.0 2021年8月26日

#19 in #eth

Download history 95/week @ 2024-03-13 88/week @ 2024-03-20 120/week @ 2024-03-27 124/week @ 2024-04-03 108/week @ 2024-04-10 56/week @ 2024-04-17 70/week @ 2024-04-24 79/week @ 2024-05-01 98/week @ 2024-05-08 85/week @ 2024-05-15 112/week @ 2024-05-22 97/week @ 2024-05-29 465/week @ 2024-06-05 526/week @ 2024-06-12 647/week @ 2024-06-19 589/week @ 2024-06-26

2,245 个月下载量

MIT 许可证

11KB
140

Rust中的Ethereum abi.encodePacked()

该项目允许在Rust中以Solidity中的abi.encodePacked()的方式进行数据序列化和打包

示例用法

打包uint24

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);

依赖关系

~2.5–3.5MB
~58K SLoC