• Kelvin Fichter's avatar
    refactor: improve core-utils folder structure · 3c2acd91
    Kelvin Fichter authored
    Improves the folder structure within the core-utils to make it more
    intuitive. core-utils is now split into three primary folders, "common",
    "external", and "optimism". "common" includes TypeScript/JavaScript
    utilities not necessarily meant to be used with any particular package
    (like hex string utilities). "external" includes utilities that are
    meant to be used in conjunction with some other particular piece of
    tooling (ethers provider utilities, geth typings). "optimism" includes
    any utilities that are specifically meant to be used in the context of
    Optimism (like address aliasing utils, batch encoding utils).
    3c2acd91
bn.ts 1.26 KB
import { ethers } from 'ethers'

import { remove0x, add0x } from './hex-strings'

/**
 * Converts an ethers BigNumber into an equivalent Ethereum address representation.
 *
 * @param bn BigNumber to convert to an address.
 * @return BigNumber converted to an address, represented as a hex string.
 */
export const bnToAddress = (bn: ethers.BigNumber | number): string => {
  // Coerce numbers into a BigNumber.
  bn = ethers.BigNumber.from(bn)

  // Negative numbers are converted to addresses by adding MAX_ADDRESS + 1.
  // TODO: Explain this in more detail, it's basically just matching the behavior of doing
  // addr(uint256(addr) - some_number) in Solidity where some_number > uint256(addr).
  if (bn.isNegative()) {
    bn = ethers.BigNumber.from('0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF')
      .add(bn)
      .add(1)
  }

  // Convert to a hex string
  let addr = bn.toHexString()
  // Remove leading 0x so we can mutate the address a bit
  addr = remove0x(addr)
  // Make sure it's 40 characters (= 20 bytes)
  addr = addr.padStart(40, '0')
  // Only take the last 40 characters (= 20 bytes)
  addr = addr.slice(addr.length - 40, addr.length)
  // Add 0x again
  addr = add0x(addr)
  // Convert into a checksummed address
  addr = ethers.utils.getAddress(addr)

  return addr
}