I can't find a way to retrieve the private key / public key / public address for a web3 account loaded via a provider. I have tried the web3.eth.accounts command but I am not getting the private key / public key / public address .

const web3 = new Web3(provider);

In more details, I would like to have a HD wallet for receiving ethers on different public keys (and later selling them) Following the advice in the doc, I am using truffle/hdwallet-provider for the HD wallet. So now I am trying to retrieve the private key, the public key and possibly the public address via web3js for the provider created via truffle/hdwallet-provider wallet

This is my code which I run several time each time in a new node console in a cmd.

Before starting the node console, I start a cmd with geth --rpc --rpcport 8545 while internet is turned off so that I don't have to download/sync the blockchain

const mnemonicPhrase = 'some words ... '

const HDWalletProvider = require("@truffle/hdwallet-provider");
Web3 = require('web3');

provider = new HDWalletProvider({
  mnemonic: mnemonicPhrase,
  providerOrUrl: "http://localhost:8545",
  numberOfAddresses: 1,
  shareNonce: true,
  derivationPath: "m/44'/0'/0'/0/"

const web3 = new Web3(provider);

There should be a way to retrieve the private/public key via web3.eth.accounts but I am only getting a single address

Thanks a lof for your help!

  • Operating System: windows
  • Ethereum client:
  • Truffle version (truffle version): @truffle/hdwallet-provider@1.4.0 (web3@1.3.6)
  • node version (node --version): v14.16.1
  • npm version (npm --version): 6.14.12

Sorry, web3 does not retrieve private/public keys

However, you can generate an account object that contains a private/public key.

> {
address: "0xb8CE9ab6943e0eCED004cDe8e3bBed6568B2Fa01",
privateKey: "0x348ce564d427a3311b6536bbcff9390d69395b06ed6c486954e971d960fe8709",
signTransaction: function(tx){...},
sign: function(data){...},
encrypt: function(password){...}


  • Thanks for your answer. Yes that is possible but then this address is not part of my truffle wallet/mnemonic. Is there another way/package that I can use to retrieve the private/public key from truffle?
    – J. Does
    Jun 18 at 15:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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