8

To keen downvoters and/or closers: If you think this is offtopic for SO, kindly point me out other StackExchange site where this question would be more appropriate.


How to implement ECDSA curve secp256k1 in PHP?

Or rather: Are there any solutions - ie. includable specialized classes - already done?

I can see there are plenty of opensource libraries, classes and stuff available for other languages (JavaScript, Python,...) but I've just spent whole afternoon googling for some/any PHP solution and ... nothing!.

This is for a bitcoin project of mine and I need a way how to generate public key from private key ... and then I want to generate the final bitcoin address.

I know how to generate private key (don't worry about it being random or not - not an issue here) and I have both 256bit hexadecimal and WIF notations. But the next step: coming up with a public key and then final bitcoin address, is kind of a problem to me, as I have literally zero cryptograph-ish background and I know the solution is to utilize secp256k1 somehow.

This is what I have so far:

// Random bytes
// $private_key = bin2hex(openssl_random_pseudo_bytes(32));
// But using brainwallet.org style to have easy comparison
$passphrase = "correct horse battery staple";
$private_key = hash('sha256', $passphrase);
var_dump ("PrivKey: $private_key");
// Bitcoin::privKeyToWIF from github.com/mikegogulski/bitcoin-php
$wif = Bitcoin::privKeyToWIF($private_key); 
var_dump ("WIF PrivKey: $wif");
// And now I don't know where to even start ...

tl;dr How to implement this in PHP? (..and privKey->pubKey conversion before that)

https://i.stack.imgur.com/U2neg.png

I know about...

  • http://github.com/mikegogulski/bitcoin-php .. Which is pretty neat and has lots of useful methods and ways how to control bitcoind via RPC, but unfortunately pure PHP method that could handle privKey->pubKey mechanism is missing.
  • http://bitcoinphp.com/ .. I couldn't find it in there.
  • openssl extension in PHP, but unfortunately the only digest method OPENSSL-PHP documentation is mentioning is 'ecdsa-with-SHA1', and correct me if I'm wrong, but I'd need 'ecdsa-with-SHA256', or something like that (?)
  • I even tried to convert the algorithm from bitcoinjs.js, but with my crypto-knowledge I was unable to extract the gist of anything. I simply don't understand those curves and their bit operations and other spooky stuff.

I'm looking for PURE PHP solution. I'm not looking for using shell running bitcoind and then parse JSON for key pairs and then...

Why there is no piece of code that could handle this entirely in PHP? OR IS THERE?! :)

Smuuf
  • 6,339
  • 3
  • 23
  • 35
  • 1
    And if this deserves a downvote, you could share with the rest why exactly. – Smuuf Dec 04 '13 at 21:29
  • I didn't downvote this, but you *do* ask for a solution in PHP without showing that you actually tried creating your own solution, which is sometimes frowned upon. – admdrew Dec 04 '13 at 21:33
  • ...that said, I think this is an interesting question (that I wouldn't mind knowing the answer to myself). Unfortunately, I'm out of votes, so I couldn't `+1` this for you. – admdrew Dec 04 '13 at 21:34
  • 1
    Oh, good point. But then again - I wouldn't know where to even start with implementing this magic. :) – Smuuf Dec 04 '13 at 21:36
  • OpenSSL needs a hack to support ecdsa-with-SHA256: https://forum.openvpn.net/topic8404-15.html – Vladislav Rastrusny Dec 04 '13 at 22:43
  • Constructive, thank you. – Smuuf Dec 04 '13 at 23:40
  • 3
    I started developing a PHP library that will achieve exactly this. It requires php >=5.4 and the php5-gmp extension to be installed. You can look at the code here https://github.com/rgex/BitcoinECDSA.php You can already provide a private key (256 hexa string) and it will generate for you a WIF, the public key and the uncompressed Bitcoin address corresponding. But please don't use it in production there is still a lot of things missing. – Jan Moritz May 12 '14 at 18:06
  • @JanMoritz thank you excellent! – Alan Deep Mar 25 '18 at 07:47

0 Answers0