ABI 編碼和解碼函數(shù)

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

筆記

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

有關(guān)編碼的詳細(xì)信息,請(qǐng)參閱有關(guān)ABI和 緊密打包編碼的文檔。


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)