[Ethereum] How to generate Private key, public key and address


I can't find any documentation online about doing this in Javascript other than this.

I want to know how does authentication work in a ethereum dApp's.. As per my understanding user seed somehow gets generated into a private key, then using that private key to derive the public key and from that you derive the address.. does anyone have any further information on how this achieved with javascript or what is the architecture for this kind of setup.. any help on understanding it would be great.

i know how to generate the seed phrase etc with the js bip39 library after that im a bit stuck.

this is basically what i am attempting so far:

    const mnemonic = "vacant element sleep harsh stick else salt great kitten clutch salad subway"
    const privateKey = new Buffer( mnemonic )
    const publicKey = ethUtil.privateToPublic(privateKey).toString('hex')
    const address = ethUtil.privateToAddress(privateKey).toString('hex')

Best Answer

I have extracted this code from HD Wallet Provider

const bip39 = require('bip39');
const hdkey = require('ethereumjs-wallet/hdkey');

const mnemonic = '..';
const hdwallet = hdkey.fromMasterSeed(bip39.mnemonicToSeed(mnemonic));
const path = "m/44'/60'/0'/0/0";
const wallet = hdwallet.derivePath(path).getWallet();
const address = `0x${wallet.getAddress().toString('hex')}`;

console.log(`Address: ${address}`);
Related Topic