7

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?

Patoshi パトシ
  • 4,800
  • 7
  • 45
  • 93

2 Answers2

4

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:)

Yuanfei Zhu
  • 578
  • 4
  • 16
3

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:

Travis Jacobs
  • 1,535
  • 1
  • 13
  • 25