Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retrieving the private/ public key for a specific path from truffle/hdwallet-provider #4112

Closed
Jdodo45 opened this issue Jun 16, 2021 · 2 comments

Comments

@Jdodo45
Copy link

Jdodo45 commented Jun 16, 2021

In brief

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

What I am trying to do in 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

Steps to Reproduce

This is my code which I run several time in different 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

Expected behavior and Actual behavior

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!

Environment

  • 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

P.S: sorry for posting here but I have already asked on stackoverflow but I didn't get any answer and I have been stuck on this for more than 2 weeks.

@Jdodo45
Copy link
Author

Jdodo45 commented Jun 23, 2021

I finally gave up using truffle and web3js and use ethereumjs-util instead.

Here is the code mainly from Harsha Goli

bip39 = require('bip39');
ethUtil = require('ethereumjs-util');
hdkey = require('hdkey');

const mnemonic = bip39.generateMnemonic(256); // if you already have a mnemonic you can put it here
const seed = bip39.mnemonicToSeedSync(mnemonic);
const root = hdkey.fromMasterSeed(seed);
const masterPrivateKey = root.privateKey.toString('hex');

var path = "m/44'/60'/0'/0/0"  // change path 
var addrNode = root.derive(path); 
var pubKey = ethUtil.privateToPublic(addrNode._privateKey);
var addr =  '0x' +  ethUtil.publicToAddress(pubKey).toString('hex');
var address = ethUtil.toChecksumAddress(addr);


console.log( "\n", "\n", "\n","ACCOUNT ", path, "\n", "private key : 0x", addrNode._privateKey.toString('hex'), "\n", "public key : ",pubKey.toString('hex'), "\n", "address : ", address)

@italoHonoratoSA
Copy link

Web3.js is much more complicated when it comes to handling accounts and extracting private keys through mnemonic.

Check my reviews here:
#1594 (comment)

Check a solution here:
https://ethereum.stackexchange.com/questions/102090/retrieving-the-private-public-key-with-web3js-for-a-specific-wallet-provided-by

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants