Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: kjj on April 10, 2012, 04:30:23 PM



Title: WIF to address?
Post by: kjj on April 10, 2012, 04:30:23 PM
So, what is the best way to turn a WIF privkey into a public key / address?

Should I convert the WIF to DER or PEM and let openssl make the public key?  Or does anyone have some code that can do it directly?

My target is PHP on Linux, or shell, but I can probably use (or translate) from perl or python.


Title: Re: WIF to address?
Post by: kjj on April 10, 2012, 06:13:34 PM
No, in that thread they are using openSSL to generate the private and public keys as a random pair.  I need to create a public key to match a premade private key.


Title: Re: WIF to address?
Post by: coretechs on April 10, 2012, 06:16:31 PM
http://bitaddress.org

Wallet Details tab

It runs in the browser (javascript) so you can save a local copy and run it offline for added security.  It supports compressed pubkeys too.  ;)

I know you didn't mention java but bitcoinj is an option too - http://code.google.com/p/bitcoinj/


Title: Re: WIF to address?
Post by: kjj on April 10, 2012, 06:39:54 PM
http://bitaddress.org

Wallet Details tab

It runs in the browser (javascript) so you can save a local copy and run it offline for added security.  It supports compressed pubkeys too.  ;)


Yeah, I've seen that.  Of course, my script doesn't have a browser to paste keys into.  I'm hoping that I don't need to translate that monstrosity myself, but that might just be my next step.

It seems odd that there aren't any command line utilities to package a hex or binary private key into a PEM or DER for openssl to use, and no RPC method to find an address to match an imported key.


Title: Re: WIF to address?
Post by: kjj on April 16, 2012, 04:54:02 AM
Woohoo.  I got it.  I started with this paper and software (http://matejdanter.com/2010/12/elliptic-curve-php-oop-dsa-and-diffie-hellman/) and patched it to support the secp256k1 curve.  It is slow as hell, so I think it is doing the multiplication the hard way, but it works.


Title: Re: WIF to address?
Post by: pointbiz on April 17, 2012, 04:06:26 AM
http://bitaddress.org

Wallet Details tab

It runs in the browser (javascript) so you can save a local copy and run it offline for added security.  It supports compressed pubkeys too.  ;)


Yeah, I've seen that.  Of course, my script doesn't have a browser to paste keys into.  I'm hoping that I don't need to translate that monstrosity myself, but that might just be my next step.

It seems odd that there aren't any command line utilities to package a hex or binary private key into a PEM or DER for openssl to use, and no RPC method to find an address to match an imported key.

I know you have found a PHP solution but in case others are interested in the javascript command line solution I have provided instructions.

Here is an article on running javascript on the command line:
http://ajaxian.com/archives/javascript-on-the-command-line-or-as-a-shell-scripting-language

You can create a new file called bitaddress.js by copying and pasting the contents of each <script> tag that is inside the <head> of the bitaddress.org HTML document. Only take the JavaScript from the <head> of the document as those are the libraries.

Then at the bottom of that file you can add this function (not tested):
Code:
function wifToAddress(wifKey){
  var bytes = Bitcoin.Base58.decode(wifKey);
  bytes.shift();
  bytes = bytes.slice(0, bytes.length - 4);
  if (bytes.length == 32) {
    var btcKey = new Bitcoin.ECKey(bytes);
    return btcKey.getBitcoinAddress();
  }
}

// call function like this
var myBitcoinAddress = wifToAddress('5ThisIsMyWifAddressLaLaLaLa');