Implementing secp256k1 (ECDSA) in PHP (for Bitcoin) [closed]

前提是你 提交于 2019-12-04 12:16:01

问题


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)

http://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?! :)

来源:https://stackoverflow.com/questions/20386230/implementing-secp256k1-ecdsa-in-php-for-bitcoin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!