Skip to content

Commit

Permalink
Documenting bytes to fixed bytes conversion.
Browse files Browse the repository at this point in the history
Co-authored-by: chriseth <[email protected]>

Co-authored-by: Alex Beregszaszi <[email protected]>
  • Loading branch information
mijovic and axic committed Apr 23, 2021
1 parent eb45706 commit 337adee
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
### 0.8.5 (unreleased)

Language Features:
* Allowing conversion from ``bytes`` and ``bytes`` slices to ``bytes1``/.../``bytes32``.


Compiler Features:
Expand Down
21 changes: 21 additions & 0 deletions docs/types/conversion.rst
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,27 @@ rules explicit::
uint8 d = uint8(uint16(a)); // d will be 0x34
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:

Conversions between Literals and Elementary Types
Expand Down
12 changes: 4 additions & 8 deletions docs/types/reference-types.rst
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ Array slices are useful to ABI-decode secondary data passed in function paramete
::

// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
pragma solidity >0.8.4 <0.9.0;
contract Proxy {
/// @dev Address of the client contract managed by proxy i.e., this contract
address client;
Expand All @@ -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
/// after doing basic validation on the address argument.
function forward(bytes calldata _payload) external {
// Since ABI decoding requires padded data, we cannot
// use abi.decode(_payload[:4], (bytes4)).
bytes4 sig =
_payload[0] |
(bytes4(_payload[1]) >> 8) |
(bytes4(_payload[2]) >> 16) |
(bytes4(_payload[3]) >> 24);
bytes4 sig = bytes4(_payload[:4]);
// Due to truncating behaviour, bytes4(_payload) performs identically.
// bytes4 sig = bytes4(_payload);
if (sig == bytes4(keccak256("setOwner(address)"))) {
address owner = abi.decode(_payload[4:], (address));
require(owner != address(0), "Address of owner cannot be zero.");
Expand Down

0 comments on commit 337adee

Please sign in to comment.