Skip to content

Commit 2718072

Browse files
authored
✨ Add bytes32 to address utils (#1456)
1 parent a5bb996 commit 2718072

File tree

5 files changed

+46
-3
lines changed

5 files changed

+46
-3
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ jobs:
7878
forge test --fuzz-runs 5000
7979
)
8080
81-
tests-nightly:
81+
tests-ithaca:
8282
name: Forge Ithaca Testing
8383
runs-on: ubuntu-latest
8484
strategy:
@@ -89,7 +89,7 @@ jobs:
8989
- name: Install Foundry Nightly
9090
uses: foundry-rs/foundry-toolchain@v1
9191
with:
92-
version: nightly
92+
version: stable
9393
- name: Install Dependencies
9494
run: forge install
9595
- name: Run Tests with ${{ matrix.profile }}

docs/utils/libbytes.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,4 +455,20 @@ Returns bytes in calldata. Performs bounds checks.
455455
function emptyCalldata() internal pure returns (bytes calldata result)
456456
```
457457

458-
Returns empty calldata bytes. For silencing the compiler.
458+
Returns empty calldata bytes. For silencing the compiler.
459+
460+
### msbToAddress(bytes32)
461+
462+
```solidity
463+
function msbToAddress(bytes32 x) internal pure returns (address)
464+
```
465+
466+
Returns the most significant 20 bytes as an address.
467+
468+
### lsbToAddress(bytes32)
469+
470+
```solidity
471+
function lsbToAddress(bytes32 x) internal pure returns (address)
472+
```
473+
474+
Returns the least significant 20 bytes as an address.

src/utils/LibBytes.sol

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -849,4 +849,14 @@ library LibBytes {
849849
result.length := 0
850850
}
851851
}
852+
853+
/// @dev Returns the most significant 20 bytes as an address.
854+
function msbToAddress(bytes32 x) internal pure returns (address) {
855+
return address(bytes20(x));
856+
}
857+
858+
/// @dev Returns the least significant 20 bytes as an address.
859+
function lsbToAddress(bytes32 x) internal pure returns (address) {
860+
return address(uint160(uint256(x)));
861+
}
852862
}

src/utils/g/LibBytes.sol

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -853,4 +853,14 @@ library LibBytes {
853853
result.length := 0
854854
}
855855
}
856+
857+
/// @dev Returns the most significant 20 bytes as an address.
858+
function msbToAddress(bytes32 x) internal pure returns (address) {
859+
return address(bytes20(x));
860+
}
861+
862+
/// @dev Returns the least significant 20 bytes as an address.
863+
function lsbToAddress(bytes32 x) internal pure returns (address) {
864+
return address(uint160(uint256(x)));
865+
}
856866
}

test/LibBytes.t.sol

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,4 +366,11 @@ contract LibBytesTest is SoladyTest {
366366
return type(uint256).max;
367367
}
368368
}
369+
370+
function testBytes32ToAddress(bytes32 x) public {
371+
uint256 msb = uint256(x) >> 96;
372+
uint256 lsb = (uint256(x) << 96) >> 96;
373+
assertEq(uint160(LibBytes.msbToAddress(x)), msb);
374+
assertEq(uint160(LibBytes.lsbToAddress(x)), lsb);
375+
}
369376
}

0 commit comments

Comments
 (0)