Solidity の abi.encode, abi.encodeXXX がよくわからなくなる
#Solidity #tips
https://medium.com/@libertylocked/what-are-abi-encoding-functions-in-solidity-0-4-24-c1a90b5ddce8
keccak256() には abi.encodePacked() を、adress.call() には abi.encodeWithSignature() を基本的には使う。
abi.encode(...) returns (bytes): ABI-encodes the given arguments
与えられた引数の型情報を意識して pad されてから連結した bytes を返却する
abi.encodePacked(...) returns (bytes): Performes packed encoding of the given arguments
もっともシンプルな動作。型情報を意識せず pad しないで連結する。
keccak256 が中でやって た のはコレ。v5.0.0 からシグネチャが変更になり、一つの encodePacked() されたのと同じ形式の bytes だけを受け付けるようになった https://solidity.readthedocs.io/en/v0.5.0/050-breaking-changes.html#semantic-and-syntactic-changes
abi.encodeWithSelector(bytes4 selector, ...) returns (bytes): ABI-encodes the given arguments starting from the second and prepends the given four-byte selector
selector とは関数のシグネチャを hash 化した時の先頭 4 bytes https://solidity.readthedocs.io/en/v0.7.1/abi-spec.html?highlight=function%20selector#function-selector
abi.encodeWithSignature(string signature, ...) returns (bytes): Equivalent to abi.encodeWithSelector(bytes4(keccak256(signature), ...)
x.call(bytes4(keccak256("f(uint256)"), a, b) と x.call(abi.encodeWithSignature("f(uint256)", a, b)) は同じで、その便利関数という感じ。
address.call(payload) に payload として渡すのはコレと覚えておくと良さそう
この signature は関数のシグネチャ。ボケーっとしてるとなんで署名が必要なんだっけ…?となる