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')
Kravitz
  • 447
  • 1
  • 5
  • 14
  • 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 '18 at 15:35
  • Or is your goal just to produce a random private key (and address)? – user19510 Feb 12 '18 at 15:36

3 Answers3

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}`);
Ismael
  • 30,570
  • 21
  • 53
  • 96
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.

Richard Horrocks
  • 37,835
  • 13
  • 87
  • 144
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);
Haven
  • 101