I want to implement a brain wallet like address generator using web3.js -- I basically have a string of text and want to convert this into an Ethereum address. How can this be done?


Here is a snippet excerpted from ConsenSys's eth-lightwallet to generate the address from a arbitrary private key:

KeyStore._computeAddressFromPrivKey = function (privKey) {

  // Create the key pair container
  // `ec` is the secp256k1 utils
  var keyPair = ec.genKeyPair();

  // Set the privKey
  keyPair._importPrivate(privKey, 'hex');

  // Derive the pubKey
  var compact = false;
  var pubKey = keyPair.getPublic(compact, 'hex').slice(2);

  // pubKey -> address
  var pubKeyWordArray = CryptoJS.enc.Hex.parse(pubKey);
  var hash = CryptoJS.SHA3(pubKeyWordArray, { outputLength: 256 });
  var address = hash.toString(CryptoJS.enc.Hex).slice(24);

  return address;

I added some comments to facilitate comprehension. Hope this helps:)

  • lets say I have the following btc private key: 5KTch7mGRUmg7Ksgx9LyzDcqEBZVJcuogGkSBjtESmkzatbJZ3W --- How would I generate a corresponding eth address with it? thx. Apr 30 '17 at 16:23
  • 1
    I've made a simple repo here: github.com/yuanfeiz/simple-eth-addr-generator Apr 30 '17 at 16:37
  • 1
    @duckx let me know if any further help is needed:) May 1 '17 at 4:57
  • i just tried your small application to generate the ethereum address, but how do you output the ethereum private key? see: prntscr.com/fxt30y --- i need the private key as how am i going to send my ether back out? thanks. Jul 19 '17 at 21:30
  • how can i generate ethereum address using privatekey or mnemonic in flutter? Mar 26 '19 at 21:31

My understanding is that web3 is not intended for Ethereum address generation/management, and that this is usually left to the client (Mist, Metamask, Parity, etc).

However, there are some native js solutions that may provide the functionality you're looking for in your use case (mnemonic seed):

Note that I've used neither of these solutions myself.

Further reading:

  • 1
    But what library are those applications using to generate an address? I doubt they are writing the Ethereum address generator from scratch. thx. Apr 29 '17 at 21:20
  • From looking into the source a bit, it looks like they use the bitcore-mnemonic library, and bitcore-lib. Check out the constructor here for some sample code on how they are generating the wallets. Apr 29 '17 at 21:29
  • Keythereum seems to be well supported for JS!
    – SCBuergel
    Aug 30 '17 at 6:53

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.