7

I am aware of several tools and websites that generate addresses for you, but I'm looking for an actual JavaScript code snippet to demonstrate the steps plainly.

5

Take a look at :

https://github.com/kvhnuke/etherwallet

( and mainly, this file, search for generateSingleWallet : https://github.com/kvhnuke/etherwallet/blob/gh-pages/js/source/01_global.js )

It is the source code for the https://www.myetherwallet.com/ that provides in-browser generation of ethereum addresses.

A simple javascript snippet (using CryptoJS and staticJS/01_ethereumjs-accounts.js libraries from the forementioned repository):

function generateSingleWallet(password) {
  // reference from staticJS/01_ethereumjs-accounts.js
  var acc = new Accounts();
  var newAccountEnc = acc.new(password);
  var addressHash =   
    cryptoJSToHex(CryptoJS.SHA3(newAccountEnc.address));
  addressHash = addressHash.substr(addressHash.length - 4);

  var newAccountUnEnc = acc.get(newAccountEnc.address, password);
  // now: newAccountUnEnc.private is unencoded private key
  newAccountEnc.private = newAccountEnc.private + addressHash;
  // now: newAccountEnc.private is encoded private key

  // always clear the Accounts object after generation
  acc.clear();
}

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.