Solidity

encodeWithSignature 結果後的多個 0 和 2,1 是什麼?

  • September 25, 2022
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;

contract testABI {
   function getEncodePacked(string calldata _func,bytes calldata _data) public pure     returns(bytes memory) {
       return abi.encodePacked(bytes4(keccak256(bytes(_func))), _data);
   }

   function getEncodeWithSignature(string calldata _func,bytes calldata _data) public pure returns(bytes memory) {
       return abi.encodeWithSignature(_func, _data);
   }
}

在此處輸入圖像描述

我正在研究 abi.encodePacked 和 abi.encodeWithSignature。

我想知道什麼“ 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000DABINENTIRENTENTYYENTINTYETTER”

這只是數據開始的偏移量和bytes動態“數組”中元素的計數及其值(在這種情況下為零)。

看看文件:https ://docs.soliditylang.org/en/v0.8.16/abi-spec.html#use-of-dynamic-types

2是數據的起始偏移量。1bytes數組中的元素(唯一的值是一個空字節,即0x00)。

1我們找到值之後,即00,但是那裡有很多零,因為所有這些值都用零填充到 32 個字節。

對於這樣的輸入baz(uint32,bool),0x05

這將導致:

0xcdcd77c0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000010500000000000000000000000000000000000000000000000000000000000000

對於baz(uint32,bool),0x0503

會是(請注意,現在沒有1,但是2因為我發送了 2 個參數。動態數據偏移量仍然相同:

0xcdcd77c0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000020503000000000000000000000000000000000000000000000000000000000000

引用自:https://ethereum.stackexchange.com/questions/136332