-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update AddressAliasHelper library to work with solidity 0.8 version (#58
) * test: Add test that bricks the executor with an underflowing address * fix: Updates the AddressAliasHelper library with 0.8 solidity version
- Loading branch information
1 parent
fd3a14b
commit 8fa25b0
Showing
3 changed files
with
108 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,29 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// Copyright 2021-2022, Offchain Labs, Inc. | ||
// For license information, see https://github.com/nitro/blob/master/LICENSE | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
|
||
/* | ||
* Copyright 2019-2021, Offchain Labs, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
pragma solidity >=0.7.5; | ||
pragma solidity ^0.8.0; | ||
|
||
library AddressAliasHelper { | ||
uint160 constant offset = uint160(0x1111000000000000000000000000000000001111); | ||
uint160 internal constant OFFSET = uint160(0x1111000000000000000000000000000000001111); | ||
|
||
/// @notice Utility function that converts the address in the L1 that submitted a tx to | ||
/// the inbox to the msg.sender viewed in the L2 | ||
/// @param l1Address the address in the L1 that triggered the tx to L2 | ||
/// @return l2Address L2 address as viewed in msg.sender | ||
function applyL1ToL2Alias(address l1Address) internal pure returns (address l2Address) { | ||
l2Address = address(uint160(l1Address) + offset); | ||
} | ||
/// @notice Utility function that converts the address in the L1 that submitted a tx to | ||
/// the inbox to the msg.sender viewed in the L2 | ||
/// @param l1Address the address in the L1 that triggered the tx to L2 | ||
/// @return l2Address L2 address as viewed in msg.sender | ||
function applyL1ToL2Alias(address l1Address) internal pure returns (address l2Address) { | ||
unchecked { | ||
l2Address = address(uint160(l1Address) + OFFSET); | ||
} | ||
} | ||
|
||
/// @notice Utility function that converts the msg.sender viewed in the L2 to the | ||
/// address in the L1 that submitted a tx to the inbox | ||
/// @param l2Address L2 address as viewed in msg.sender | ||
/// @return l1Address the address in the L1 that triggered the tx to L2 | ||
function undoL1ToL2Alias(address l2Address) internal pure returns (address l1Address) { | ||
l1Address = address(uint160(l2Address) - offset); | ||
} | ||
} | ||
/// @notice Utility function that converts the msg.sender viewed in the L2 to the | ||
/// address in the L1 that submitted a tx to the inbox | ||
/// @param l2Address L2 address as viewed in msg.sender | ||
/// @return l1Address the address in the L1 that triggered the tx to L2 | ||
function undoL1ToL2Alias(address l2Address) internal pure returns (address l1Address) { | ||
unchecked { | ||
l1Address = address(uint160(l2Address) - OFFSET); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,18 @@ | ||
import { ethers, BigNumber } from 'ethers'; | ||
import { ZERO_ADDRESS } from './constants'; | ||
|
||
export const ALIASING_OFFSET = '0x1111000000000000000000000000000000001111'; | ||
|
||
export const applyL1ToL2Alias = (l1Address: string) => { | ||
const offset = BigNumber.from('0x1111000000000000000000000000000000001111'); | ||
return ethers.utils.getAddress( | ||
BigNumber.from(l1Address).add(offset).mod(BigNumber.from(2).pow(160)).toHexString() | ||
); | ||
const offset = BigNumber.from(ALIASING_OFFSET); | ||
const l2Address = BigNumber.from(l1Address).add(offset).mod(BigNumber.from(2).pow(160)); | ||
if (l2Address.eq(0)) return ZERO_ADDRESS; | ||
return ethers.utils.getAddress(l2Address.toHexString()); | ||
}; | ||
|
||
export const undoL1ToL2Alias = (l2Address: string) => { | ||
const offset = BigNumber.from(ALIASING_OFFSET); | ||
const l1Address = BigNumber.from(l2Address).sub(offset).mod(BigNumber.from(2).pow(160)); | ||
if (l1Address.eq(0)) return ZERO_ADDRESS; | ||
return ethers.utils.getAddress(l1Address.toHexString()); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters