mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Documenting bytes to fixed bytes conversion.
Co-authored-by: chriseth <chris@ethereum.org> Co-authored-by: Alex Beregszaszi <alex@rtfs.hu>
This commit is contained in:
parent
eb457064b1
commit
337adee395
@ -1,6 +1,7 @@
|
|||||||
### 0.8.5 (unreleased)
|
### 0.8.5 (unreleased)
|
||||||
|
|
||||||
Language Features:
|
Language Features:
|
||||||
|
* Allowing conversion from ``bytes`` and ``bytes`` slices to ``bytes1``/.../``bytes32``.
|
||||||
|
|
||||||
|
|
||||||
Compiler Features:
|
Compiler Features:
|
||||||
|
@ -99,6 +99,27 @@ rules explicit::
|
|||||||
uint8 d = uint8(uint16(a)); // d will be 0x34
|
uint8 d = uint8(uint16(a)); // d will be 0x34
|
||||||
uint8 e = uint8(bytes1(a)); // e will be 0x12
|
uint8 e = uint8(bytes1(a)); // e will be 0x12
|
||||||
|
|
||||||
|
``bytes`` arrays and ``bytes`` calldata slices can be converted explicitly to fixed bytes types (``bytes1``/.../``bytes32``).
|
||||||
|
In case the array is longer than the target fixed bytes type, truncation at the end will happen.
|
||||||
|
If the array is shorter than the target type, it will be padded with zeros at the end.
|
||||||
|
|
||||||
|
::
|
||||||
|
|
||||||
|
// SPDX-License-Identifier: GPL-3.0
|
||||||
|
pragma solidity ^0.8.5;
|
||||||
|
|
||||||
|
contract C {
|
||||||
|
bytes s = "abcdefgh";
|
||||||
|
function f(bytes calldata c, bytes memory m) public view returns (bytes16, bytes3) {
|
||||||
|
require(c.length == 16, "");
|
||||||
|
bytes16 b = bytes16(m); // if length of m is greater than 16, truncation will happen
|
||||||
|
b = bytes16(s); // padded on the right, so result is "abcdefgh\0\0\0\0\0\0\0\0"
|
||||||
|
bytes3 b1 = bytes3(s); // truncated, b1 equals to "abc"
|
||||||
|
b = bytes16(c[:8]); // also padded with zeros
|
||||||
|
return (b, b1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
.. _types-conversion-literals:
|
.. _types-conversion-literals:
|
||||||
|
|
||||||
Conversions between Literals and Elementary Types
|
Conversions between Literals and Elementary Types
|
||||||
|
@ -492,7 +492,7 @@ Array slices are useful to ABI-decode secondary data passed in function paramete
|
|||||||
::
|
::
|
||||||
|
|
||||||
// SPDX-License-Identifier: GPL-3.0
|
// SPDX-License-Identifier: GPL-3.0
|
||||||
pragma solidity >=0.7.0 <0.9.0;
|
pragma solidity >0.8.4 <0.9.0;
|
||||||
contract Proxy {
|
contract Proxy {
|
||||||
/// @dev Address of the client contract managed by proxy i.e., this contract
|
/// @dev Address of the client contract managed by proxy i.e., this contract
|
||||||
address client;
|
address client;
|
||||||
@ -504,13 +504,9 @@ Array slices are useful to ABI-decode secondary data passed in function paramete
|
|||||||
/// Forward call to "setOwner(address)" that is implemented by client
|
/// Forward call to "setOwner(address)" that is implemented by client
|
||||||
/// after doing basic validation on the address argument.
|
/// after doing basic validation on the address argument.
|
||||||
function forward(bytes calldata _payload) external {
|
function forward(bytes calldata _payload) external {
|
||||||
// Since ABI decoding requires padded data, we cannot
|
bytes4 sig = bytes4(_payload[:4]);
|
||||||
// use abi.decode(_payload[:4], (bytes4)).
|
// Due to truncating behaviour, bytes4(_payload) performs identically.
|
||||||
bytes4 sig =
|
// bytes4 sig = bytes4(_payload);
|
||||||
_payload[0] |
|
|
||||||
(bytes4(_payload[1]) >> 8) |
|
|
||||||
(bytes4(_payload[2]) >> 16) |
|
|
||||||
(bytes4(_payload[3]) >> 24);
|
|
||||||
if (sig == bytes4(keccak256("setOwner(address)"))) {
|
if (sig == bytes4(keccak256("setOwner(address)"))) {
|
||||||
address owner = abi.decode(_payload[4:], (address));
|
address owner = abi.decode(_payload[4:], (address));
|
||||||
require(owner != address(0), "Address of owner cannot be zero.");
|
require(owner != address(0), "Address of owner cannot be zero.");
|
||||||
|
Loading…
Reference in New Issue
Block a user