0

I have implemented address generation using HDWallet as shown in the snippet below:

let Seed = generateMnemonic();
const wallet = EthHdWallet.fromMnemonic(Seed);
console.log("Wallet" + JSON.stringify(wallet));
console.log(typeof wallet);
let address = wallet.generateAddresses(3);
console.log("Wallet after address generation: " + JSON.stringify(wallet));
console.log("Address generated: " + address);

Wallet gives one object which contains:

  • _hdKey (Has parameter _hdKey which further have two parameter xpriv & xpub )
  • _root (Also Has parameter _hdKey which further have two parameter xpriv & xpub different from hd key's parameter value)
  • _children (Generated addresses with its own private and public key pairs)

Difficulty about:

  1. What does that two key pairs indicates? which we get it under the _hdKey and _root?
  2. How to recover my address only using privateKey?
| improve this question | | | | |
0

I'd suggest to check BIP 32 documentation, the objects you are looking at can be traced to concepts from the specification.

The underscore may indicate that they are an implementation detail of EthHdWallet so for a detailed answer someone will have to look at the source code.

From the EthHdWallet's constructor we can determine the meaning:

  constructor (xPrivKey) {
    this._hdKey = fromExtendedKey(xPrivKey)
    this._root = this._hdKey.derivePath(BIP44_PATH)
    this._children = []
  }
  • _hdKey Is the master node for the HD wallet
  • _root Is the node at Ethereum derivation path m/44'/60'/0'/0
  • _children Children keys derived from _root

It is possible to calculate address from private key (see for example https://ethereum.stackexchange.com/a/27187), but it involves some low level calculation. An easier solution is to use a library like https://github.com/ethereumjs/ethereumjs-wallet and import the private key into a wallet and obtain the address.

const wallet = HDWallet.fromPrivateKey(privateKeyBuffer)
const address = wallet.getChecksumAddressString()
| improve this answer | | | | |
  • thank you for perfect and simple explanation, From the EthHDWallet constructor, everything has cleared.! As you've said that we can get our address back by using private key. But I would like to ask that is that possible using Seed to? – Rudrika Jan 29 at 5:09
  • 1
    @Rudrika EthHdWallet have a function getAddresses() that returns all addresses previously generated. For Ethereum you can use the first one. – Ismael Jan 29 at 15:18

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.