0

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);
web3.eth.accounts

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);
web3.eth.accounts

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/[email protected] ([email protected])
  • node version (node --version): v14.16.1
  • npm version (npm --version): 6.14.12

2 Answers 2

2

hd-wallet stores all keys inside wallet variable, you can access then by following example.

provider = new HDWalletProvider({
  mnemonic: mnemonicPhrase,
  providerOrUrl: "http://localhost:8545",
  numberOfAddresses: 2,
  shareNonce: true,
  derivationPath: "m/44'/0'/0'/0/"
});
const web3 = new Web3(provider);
const wallets = provider.wallets;
var accounts = [];
for (const wallet in wallets) {
  let account = wallets[wallet];
  accounts.push({
    privateKey: account.privateKey.toString("hex"),
    publicKey: account.publicKey.toString("hex"),
    publicAddress: wallet,
  });
}

Result

[
  {
    privateKey: '8bdde4f02d6d6736bebd8s277ec89893be7be4c02e9ad5da38a8fca619986311',
    publicKey: '0414351013efd92a4aa932443548160bdeb1d56553efe5c50f8a75342ea2a71a96a12b5000a4b4ad3fe51d2075142cf8d7dce51a1a17b93eee447389ba0fc136d8',
    publicAddress: '0xE839836eC6Ef7Fac9f52e460B0A9DBD9342D806F'
  },
  {
    privateKey: '1745940f309220276d2e974415daf7ad08ca172ef3922bd241327b9cbc866f32',
    publicKey: '04ca9c29801acc74b5a9925efc47b590e1807f68c79eecacdc1f1c4e0e1b3af4717537f0ac7dbaa79569a4382943e03f8149f96a3198b18ce58593e132f399b659',
    publicAddress: '0xdB5C44A94705664904c2dB120A61345bEf76027e'
  },
]
0

Sorry, web3 does not retrieve private/public keys

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

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

}

1
  • 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, 2021 at 15:51

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.