github.com/ethereum-optimism/optimism@v1.7.2/packages/core-utils/src/optimism/alias.ts (about) 1 import { isAddress } from '@ethersproject/address' 2 import { BigNumber } from '@ethersproject/bignumber' 3 4 import { bnToAddress } from '../common' 5 6 // Constant representing the alias to apply to the msg.sender when a contract sends an L1 => L2 7 // message. We need this aliasing scheme because a contract can be deployed to the same address 8 // on both L1 and L2 but with different bytecode (address is not dependent on bytecode when using 9 // the standard CREATE opcode). We want to treat L1 contracts as having a different address while 10 // still making it possible for L2 contracts to easily reverse the aliasing scheme and figure out 11 // the real address of the contract that sent the L1 => L2 message. 12 export const L1_TO_L2_ALIAS_OFFSET = 13 '0x1111000000000000000000000000000000001111' 14 15 /** 16 * Applies the L1 => L2 aliasing scheme to an address. 17 * 18 * @param address Address to apply the scheme to. 19 * @returns Address with the scheme applied. 20 */ 21 export const applyL1ToL2Alias = (address: string): string => { 22 if (!isAddress(address)) { 23 throw new Error(`not a valid address: ${address}`) 24 } 25 26 return bnToAddress(BigNumber.from(address).add(L1_TO_L2_ALIAS_OFFSET)) 27 } 28 29 /** 30 * Reverses the L1 => L2 aliasing scheme from an address. 31 * 32 * @param address Address to reverse the scheme from. 33 * @returns Alias with the scheme reversed. 34 */ 35 export const undoL1ToL2Alias = (address: string): string => { 36 if (!isAddress(address)) { 37 throw new Error(`not a valid address: ${address}`) 38 } 39 40 return bnToAddress(BigNumber.from(address).sub(L1_TO_L2_ALIAS_OFFSET)) 41 }