3

I am trying to understand how HD wallets work in Ethereum. Currently I have managed to generate a child extended private and public key (xpub and xpriv) from my BIP32 Root Key. However, I can't find any documentation as to derive an Ethereum address/public/private key from my generated child xpbub and xpriv. Any is help is greatly appreciated !

3
  • So you want to get an address from a privateKey?
    – Sky
    Commented Apr 4, 2023 at 10:37
  • I want to eventually get an address from an extended private key Commented Apr 4, 2023 at 11:46
  • Getting address form a private key is not an issue. Any development tool has that functionality. Are you using Hardhat or Truffle or something like that? Example of getting address from PublicKey ethereum.stackexchange.com/questions/142683/…
    – Sky
    Commented Apr 4, 2023 at 14:05

1 Answer 1

1

Hope this script.js helps:

const Wallet = require('ethereumjs-wallet').default;

// Replace this with your child extended private key (xpriv)
const xpriv = 'xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi';

// Initialize the wallet from the extended private key
const wallet = Wallet.fromExtendedPrivateKey(xpriv);

// Get the Ethereum address
const address = wallet.getAddressString();
console.log('Address:', address);

// Get the private key
const privateKey = wallet.getPrivateKeyString();
console.log('Private Key:', privateKey);

// Get the public key
const publicKey = wallet.getPublicKeyString();
console.log('Public Key:', publicKey);

NOTE: Make sure you installed ethereumjs-wallet library

npm install ethereumjs-wallet

After that you just run the script:

node ./path_to_script/script.js

You should get this output:

Address: 0x056db290f8ba3250ca64a45d16284d04bc6f5fbf
Private Key: 0xe8f32e723decf4051aefac8e2c93c9c5b214313817cdb01a1494b917c8436b35
Public Key: 0x39a36013301597daef41fbe593a02cc513d0b55527ec2df1050e2e8ff49c85c23cbe7ded0e7ce6a594896b8f62888fdbc5c8821305e2ea42bf01e37300116281
2
  • Thank you this helps a lot, I was wondering you could point me to some resources to understand exactly how the private and public keys are derived from the xpub and xpriv, in terms of the hash functions used Commented Apr 5, 2023 at 9:37
  • I usually get my does of knowledge from medium articles, so much so that I even pay a subscription (5$ per month). But you can read a number of articles for free every month. wolovim.medium.com/ethereum-201-hd-wallets-11d0c93c87f7 I Hope this helps, as it indeed proved as an amazing tool for me.
    – Sky
    Commented Apr 6, 2023 at 6:35

Your Answer

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

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