whitelist.ts 3.72 KB
Newer Older
1 2
'use strict'

3
import fs from 'fs'
4

5 6 7 8
import { ethers } from 'ethers'
import { task } from 'hardhat/config'
import * as types from 'hardhat/internal/core/params/argumentTypes'
import { LedgerSigner } from '@ethersproject/hardware-wallets'
9

10 11 12 13 14 15
import { getContractFactory } from '../src/contract-defs'
import { predeploys } from '../src/predeploys'

// Add accounts the the OVM_DeployerWhitelist
// npx hardhat whitelist --address 0x..
task('whitelist')
16 17 18 19 20 21 22 23 24 25 26 27 28
  .addOptionalParam('address', 'Address to whitelist', undefined, types.string)
  .addOptionalParam(
    'addressFile',
    'File containing addresses to whitelist separated by a newline',
    undefined,
    types.string
  )
  .addOptionalParam(
    'whitelistMode',
    '"enable" if you want to add the address(es) from the whitelist, "disable" if you want remove the address(es) from the whitelist',
    'enable',
    types.string
  )
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
  .addOptionalParam('transactionGasPrice', 'tx.gasPrice', undefined, types.int)
  .addOptionalParam(
    'useLedger',
    'use a ledger for signing',
    false,
    types.boolean
  )
  .addOptionalParam(
    'ledgerPath',
    'ledger key derivation path',
    ethers.utils.defaultPath,
    types.string
  )
  .addOptionalParam(
    'contractsRpcUrl',
    'Sequencer HTTP Endpoint',
    process.env.CONTRACTS_RPC_URL,
    types.string
  )
  .addOptionalParam(
    'contractsDeployerKey',
    'Private Key',
    process.env.CONTRACTS_DEPLOYER_KEY,
    types.string
  )
  .addOptionalParam(
    'contractAddress',
56
    'Address of DeployerWhitelist contract',
57 58 59
    predeploys.OVM_DeployerWhitelist,
    types.string
  )
60
  .setAction(async (args) => {
61 62 63 64 65 66 67 68 69 70 71 72
    if (args.whitelistMode !== 'enable' && args.whitelistMode !== 'disable') {
      throw new Error(`Whitelist mode must be either "enable" or "disable"`)
    }

    if (args.address === undefined && args.addressPath === undefined) {
      throw new Error(`Must provide either address or address-path`)
    }

    if (args.address !== undefined && args.addressPath !== undefined) {
      throw new Error(`Cannot provide both address and address-path`)
    }

73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
    const provider = new ethers.providers.JsonRpcProvider(args.contractsRpcUrl)
    let signer: ethers.Signer
    if (!args.useLedger) {
      if (!args.contractsDeployerKey) {
        throw new Error('Must pass --contracts-deployer-key')
      }
      signer = new ethers.Wallet(args.contractsDeployerKey).connect(provider)
    } else {
      signer = new LedgerSigner(provider, 'default', args.ledgerPath)
    }

    const deployerWhitelist = getContractFactory('OVM_DeployerWhitelist')
      .connect(signer)
      .attach(args.contractAddress)

    const addr = await signer.getAddress()
    console.log(`Using signer: ${addr}`)
90
    const owner = await deployerWhitelist.owner()
91
    if (owner === '0x0000000000000000000000000000000000000000') {
92 93 94 95
      console.log(`Whitelist is disabled. Exiting early.`)
      return
    } else {
      console.log(`OVM_DeployerWhitelist owner: ${owner}`)
96 97 98 99 100 101
    }

    if (addr !== owner) {
      throw new Error(`Incorrect key. Owner ${owner}, Signer ${addr}`)
    }

102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
    const addresses = []
    if (args.address !== undefined) {
      addresses.push(args.address)
    } else {
      const addressFile = fs.readFileSync(args.addressPath, 'utf8')
      for (const line of addressFile.split('\n')) {
        if (line !== '') {
          addresses.push(line)
        }
      }
    }

    for (const address of addresses) {
      console.log(`Changing whitelist status for address: ${address}`)
      console.log(`New whitelist status: ${args.whitelistMode}`)
      const res = await deployerWhitelist.setWhitelistedDeployer(
        address,
        args.whitelistMode === 'enable' ? true : false,
        { gasPrice: args.transactionGasPrice }
      )
      await res.wait()
    }
124
  })