I have a 256-bit private key and I want to get its public address without having to install Geth (which is quite heavy) on the machine. Is there any small JS lib that does that?
1 Answer
You want lightweight and Javascript... how's this? https://github.com/ethereumjs/ethereumjs-util
npm install ethereumjs-util
Example:
var ethUtils = require('ethereumjs-util')
var privateKey = new Buffer([234, 84, 189, 197, 45, 22, 63, 136, 201, 58, 176, 97, 87, 130, 207, 113, 138, 46, 251, 158, 81, 167, 152, 154, 171, 27, 8, 6, 126, 156, 28, 95])
var address = ethUtils.privateToAddress(privateKey).toString('hex')
// address will be 2f015c60e0be116b1f0cd534704db9c92118fb6a
If you have the private key in hex, then you can the use Buffer constructor with second argument as 'hex', example:
var privateKey = new Buffer('ea54bdc52d163f88c93ab0615782cf718a2efb9e51a7989aab1b08067e9c1c5f', 'hex')
It has few more utility functions than you asked for, but you will need at least a crypto library (and in turn a bignumber library).
-
Hmm not so awesome. That file seems to depend on installing C libraries (wtf?) that do not install at all. I need something browser compatible... could you browserify the relevant parts? Feb 6, 2016 at 13:39
-
-
ethereumjs-tx may actually be a middle-ground you are looking for, browser compatible and lighter than ethereumjs-accounts.– eth ♦Feb 7, 2016 at 0:27
-
I'm not sure how that helps, I'm supposed to create a fake transaction and get the public address of the private key from it? :| Feb 7, 2016 at 17:31
-
ethereumjs-tx says it creates an
ethUtil
global, so should be able to useprivateToAddress
like answer.– eth ♦Feb 8, 2016 at 5:48