ABI 編碼和解碼函數

2022-05-12 17:02 更新
  • abi.decode(bytes memory encodedData, (...)) returns (...): ABI 解碼給定的數據,而類型在括號中作為第二個參數給出。例子:(uint a, uint[2] memory b, bytes memory c) = abi.decode(data, (uint, uint[2], bytes))
  • abi.encode(...) returns (bytes memory): ABI 編碼給定的參數
  • abi.encodePacked(...) returns (bytes memory):對給定參數執(zhí)行打包編碼。請注意,打包編碼可能不明確!
  • abi.encodeWithSelector(bytes4 selector, ...) returns (bytes memory): ABI 從第二個開始對給定的參數進行編碼,并將給定的四字節(jié)選擇器放在前面
  • abi.encodeWithSignature(string memory signature, ...) returns (bytes memory): 相當于abi.encodeWithSelector(bytes4(keccak256(bytes(signature))), ...)
  • abi.encodeCall(function functionPointer, (...)) returns (bytes memory)functionPointer: ABI使用元組中的參數對調用進行編碼。執(zhí)行完整的類型檢查,確保類型與函數簽名匹配。結果等于abi.encodeWithSelector(functionPointer.selector, (...))

筆記

這些編碼函數可用于為外部函數調用制作數據,而無需實際調用外部函數。此外,它是一種計算結構化數據散列的方法(盡管請注意,可以使用不同的函數參數類型來制造“散列沖突”)。keccak256(abi.encodePacked(a, b))

有關編碼的詳細信息,請參閱有關ABI和 緊密打包編碼的文檔。


以上內容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號