alias.ts 1.53 KB
Newer Older
1 2
import { isAddress } from '@ethersproject/address'
import { BigNumber } from '@ethersproject/bignumber'
3

4
import { bnToAddress } from '../common'
5

6 7 8 9 10 11
// Constant representing the alias to apply to the msg.sender when a contract sends an L1 => L2
// message. We need this aliasing scheme because a contract can be deployed to the same address
// on both L1 and L2 but with different bytecode (address is not dependent on bytecode when using
// the standard CREATE opcode). We want to treat L1 contracts as having a different address while
// still making it possible for L2 contracts to easily reverse the aliasing scheme and figure out
// the real address of the contract that sent the L1 => L2 message.
12 13 14
export const L1_TO_L2_ALIAS_OFFSET =
  '0x1111000000000000000000000000000000001111'

15 16 17 18 19 20
/**
 * Applies the L1 => L2 aliasing scheme to an address.
 *
 * @param address Address to apply the scheme to.
 * @returns Address with the scheme applied.
 */
21
export const applyL1ToL2Alias = (address: string): string => {
22
  if (!isAddress(address)) {
23 24 25
    throw new Error(`not a valid address: ${address}`)
  }

26
  return bnToAddress(BigNumber.from(address).add(L1_TO_L2_ALIAS_OFFSET))
27 28
}

29 30 31 32 33 34
/**
 * Reverses the L1 => L2 aliasing scheme from an address.
 *
 * @param address Address to reverse the scheme from.
 * @returns Alias with the scheme reversed.
 */
35
export const undoL1ToL2Alias = (address: string): string => {
36
  if (!isAddress(address)) {
37 38 39
    throw new Error(`not a valid address: ${address}`)
  }

40
  return bnToAddress(BigNumber.from(address).sub(L1_TO_L2_ALIAS_OFFSET))
41
}