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. – Patoshi パトシ Apr 30 '17 at 16:23
-
1I've made a simple repo here: github.com/yuanfeiz/simple-eth-addr-generator – Yuanfei Zhu Apr 30 '17 at 16:37
-
1
-
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. – Patoshi パトシ Jul 19 '17 at 21:30
-
how can i generate ethereum address using privatekey or mnemonic in flutter? – Ishwar Chandra Tiwari 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):
- EthereumJS-Accounts (seems deprecated)
- ConsenSys LightWallet
Note that I've used neither of these solutions myself.
Further reading:
-
1But what library are those applications using to generate an address? I doubt they are writing the Ethereum address generator from scratch. thx. – Patoshi パトシ 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. – Travis Jacobs Apr 29 '17 at 21:29
-