1

When I try to run this module, it giveas an error "TypeError: provider.getAddress is not a function".

const fs = require('fs')
const Web3 = require('web3')


function loadAccount (privateKeyFileName) {
  const providerUrl = 'wss://eth-goerli.g.alchemy.com/v2/<key>'
  const chainId = 5 // Goerli testnet chain ID
  const privateKey = fs.readFileSync(privateKeyFileName, 'utf-8')
  const provider = new Web3.providers.WebsocketProvider(providerUrl)

  return {
    ownerAddress: provider.getAddress(),
    web3js: new Web3(provider),
    provider
  }
}

module.exports = {
  loadAccount,
}

1 Answer 1

0

It is necessary to create a account using the private key first, then is possible to obtain the public address.

const fs = require('fs')
const Web3 = require('web3')


function loadAccount (privateKeyFileName) {
  const providerUrl = 'wss://eth-goerli.g.alchemy.com/v2/<key>'
  const chainId = 5 // Goerli testnet chain ID
  const privateKey = fs.readFileSync(privateKeyFileName, 'utf-8')
  const provider = new Web3.providers.WebsocketProvider(providerUrl)
  const web3js= new Web3(provider);

  return {
    ownerAddress: web3js.eth.accounts.privateKeyToAccount(privateKey).address,
    web3js: web3js,
    provider
  }
}

module.exports = {
  loadAccount,
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.