3

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')
2
  • Is there a particular tool you're trying to match? I.e. do you want to create the same address(es) as some other tool given the same seed phrase?
    – user19510
    Feb 12, 2018 at 15:35
  • Or is your goal just to produce a random private key (and address)?
    – user19510
    Feb 12, 2018 at 15:36

3 Answers 3

10

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}`);
1
  • Nice! Was hoping someone would come up with a better answer than mine :-) +1 Feb 13, 2018 at 22:37
2

If you've already generated a seed equating to a private key, then you can probably get the rest of what you want from Ethereum's GitHub repo. (The same functionality is probably provided in the BIP-39 JS code.)

Something along the lines of:

var utils = require('ethereumjs-util')
var privateKey = new Buffer(<from_seed>)
var publicKey = utils.privateToPublic(privateKey).toString('hex')
var address = utils.privateToAddress(privateKey).toString('hex')

Edit:

The above assumes you've already generated some derived private keys from your mnemonic. To get from the raw BIP-39 mnemonic to the above is a bit more involved. (I'm probably not best qualified to give a decent answer on JS code... Someone may have a better method than the below. As @smarx says, it's also not clear what your ultimate aims are.)

In short - from index.js in the BIP-39 JS code - you'll need to:

  • Get the derivation path (getDerivationPath())
  • Calculate the BIP32 Extended Key (calcBip32ExtendedKey())
  • Get the key (bip32ExtendedKey.derive())
  • From this, get the keypair (key.keyPair)

Then you can get your values directly:

var privKeyBuffer = keyPair.d.toBuffer(32);
privkey = privKeyBuffer.toString('hex');
var addressBuffer = ethUtil.privateToAddress(privKeyBuffer);
var hexAddress = addressBuffer.toString('hex');
var checksumAddress = ethUtil.toChecksumAddress(hexAddress);
address = ethUtil.addHexPrefix(checksumAddress);
privkey = ethUtil.addHexPrefix(privkey);
pubkey = ethUtil.addHexPrefix(pubkey);

The above might at least give you an idea of where to look to get further.

5
  • Okay, which is basically this answer...: ethereum.stackexchange.com/a/1063/52 Feb 12, 2018 at 14:48
  • its giving me the error that "private key length is invalid"
    – Kravitz
    Feb 12, 2018 at 15:21
  • What are you feeding into the the privateKey buffer? (You'll need to use one of the derived private keys.) Feb 12, 2018 at 15:35
  • im feeding in the mneumonic seed phrase, i thought the privated key was derived from it ? so this is basically what i feed into buffer "vacant element sleep harsh stick else salt great kitten clutch salad subway"
    – Kravitz
    Feb 12, 2018 at 15:38
  • I use bip39 for generating the seed
    – Kravitz
    Feb 12, 2018 at 15:38
0

If you use it in browser or React Native with Expo, try ethers.js, otherwise, you will introduce a lot of works here, since you have to access native code for using react-native-randomBytes

with ethers.js, just run:

const wallet = Wallet.fromMnemonic(mnemonic);

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.