Author

Topic: Completed: [Bounty] PHP function to generate bitcoin addr from Electrum mpk (Read 5857 times)

sr. member
Activity: 441
Merit: 268
I created a minimalistic version of MPK address generator in PHP. It requires GMP extension to be installed on the system.

See this thread for more details: https://bitcointalksearch.org/topic/ann-deterministic-bitcoin-address-generator-for-php-python-and-ruby-240018
hero member
Activity: 784
Merit: 1009
firstbits:1MinerQ
Well, on Ubuntu I just have to install the php5-gmp package to get the extension working.  It installs a .so file and adds it to php.ini.  The package distributors may still have to compile the PHP core in a certain way beforehand to make this work, I don't know.  Similar convenience may be available in other Linux/PHP distributions.
I didn't realize that. I'm using Ubuntu as well but didn't see php5-gmp. That's definitely an option then.
sr. member
Activity: 448
Merit: 254
GMP is faster but it's not available in a default install of PHP. So you're saying you can use an extension for GMP? I would need to code the two base conversion routines in bcmath to support GMP as right now they aren't there. But if you can dynamically load GMP without recompiling PHP then this may be worthwhile as bcmath is painfully slow.

My PHP extension (using openssl) is very fast. Much faster than using GMP and it's a loadable extension so that's probably just as good or better a choice. But I likely wouldn't have written it if I could have used GMP easily.

Well, on Ubuntu I just have to install the php5-gmp package to get the extension working.  It installs a .so file and adds it to php.ini.  The package distributors may still have to compile the PHP core in a certain way beforehand to make this work, I don't know.  Similar convenience may be available in other Linux/PHP distributions.

Your extension looks nice and convenient as a one-step all-C function.  There is also a patch to expose some raw OpenSSL elliptic curve stuff to PHP, though I haven't tested it.
hero member
Activity: 784
Merit: 1009
firstbits:1MinerQ
3. PHP script (uses phpecc library and bcmath, quite slow)

from the github code:
Quote
// bcmath module in php seems to be very slow
// apparently the gmp module is much faster
// base2dec needs to be written for gmp as phpecc is missing it

This is all true, but if you define ('USE_EXT', 'GMP'); the EC math is still noticeably faster, in my experience, even if you're relying on the bcmath base2dec.  I don't know if doing a gmp implementation of the conversion utilities would make even more of a difference, but if you don't mind using both extensions, this "mixed" configuration was acceptable enough speed for my experiments.
GMP is faster but it's not available in a default install of PHP. So you're saying you can use an extension for GMP? I would need to code the two base conversion routines in bcmath to support GMP as right now they aren't there. But if you can dynamically load GMP without recompiling PHP then this may be worthwhile as bcmath is painfully slow.

My PHP extension (using openssl) is very fast. Much faster than using GMP and it's a loadable extension so that's probably just as good or better a choice. But I likely wouldn't have written it if I could have used GMP easily.
sr. member
Activity: 448
Merit: 254
3. PHP script (uses phpecc library and bcmath, quite slow)

from the github code:
Quote
// bcmath module in php seems to be very slow
// apparently the gmp module is much faster
// base2dec needs to be written for gmp as phpecc is missing it

This is all true, but if you define ('USE_EXT', 'GMP'); the EC math is still noticeably faster, in my experience, even if you're relying on the bcmath base2dec.  I don't know if doing a gmp implementation of the conversion utilities would make even more of a difference, but if you don't mind using both extensions, this "mixed" configuration was acceptable enough speed for my experiments.
hero member
Activity: 784
Merit: 1009
firstbits:1MinerQ
Thanks Gleb. Bounty received.

I've also now incorporated the mpkgen scripts into my misc repo on GitHub. I made a few small changes to function names and added an attribution to vanitygen for some code.

https://github.com/bkkcoins/misc

There are several separate things in the mpkgen update.

1. Python script
2. C program (uses openssl library, fast)
3. PHP script (uses phpecc library and bcmath, quite slow)
3. PHP extension (written in C, uses openssl library, fast)

Anyone generating addresses outside Electrum should be aware of the gap limit value and have it set higher than the number of addresses generated that could be discarded/unused when given out.

It would also be wise (more safe) to store the index number of generated addresses, or at least a count of addresses given out. Along with the seed this would allow creating the private key for any of these addresses even if they cannot be detected by Electrum in the wallet.

This also would allow using these scripts for non-Electrum based payment discovery and management. eg. store index values given out and then generate keys and sign transactions on a safe computer to combine payments to an offline wallet.

If anyone is interested in such a sweep utility please contact me. I might work on that if useful.
member
Activity: 73
Merit: 10
Thanks to BkkCoins for great work - Bounty sent.

Gleb
http://www.bitcoinway.com/
hero member
Activity: 784
Merit: 1009
firstbits:1MinerQ
I've sent you an email. Have completed and tested code. Just waiting upon reply.
hero member
Activity: 784
Merit: 1009
firstbits:1MinerQ
I went ahead earlier and I've almost got this working now. I'm just debugging some issues with number conversion. I thought it would be fairly easy but turns out php and python offer very different ways of dealing with binary string data and hashing. So, just tracking the raw data thru and making sure it works right.

I expect in an hour or two to have a working function. It uses a PHP ECC library I found on GitHub. It says it is under the MIT license. It supports using bcmath or gmp for doing the bigint math. I have bcmath on my php install here and it's very slow. gmp is apparently much faster so that would be much preferable for doing this kind of math. I think openssl may have the abilities needed but the ssl functions in PHP did not seem to support ECC math.

I'll keep you posted shortly.
member
Activity: 73
Merit: 10
I can do this for you. I'm familiar with both languages.

I have the code already in Python distilled and simplified from the Electrum code (as a cmd line utility that gens address from mpk) and just need an ECDSA and Hash PHP lib to convert it for you. I can research and do this but don't want to bother if it's in vain because you already paid someone else.

No, i haven't paid anyone else yet - so if you want to go ahead - I reserve the bounty for you.
If you'd give me time estimate - that would be great.

BTW your function will need an increasing index value as well as MPK. The address cannot be gen'd from an MPK alone. A sequence value N is needed. But you just supply a static increasing counter value.

Also, note that using a wallet with these addresses you will need a high gap value to ensure that any addresses generated but never used do not stop the wallet from "seeing" past the empty addresses.

Sure - let just add an extra param - "$index" to it. Let assume calling code will take care of index tracking.
And no problem with high gap value at wallet (although I just personally need to learn what it really is).

Also, if you're ok with running a shell command from php then an easy way would be to call my python cmd util to provide the address. Slower no doubt, but easy.

I want PHP code to be compatible with hosting accounts that do not allow shell commands or do not have python installed.
So pure PHP would be preferred. But python utility would still be useful to have to test final PHP code against.

Feel free to email me directly at: gleb/at/bitcoinway.com

Gleb
hero member
Activity: 784
Merit: 1009
firstbits:1MinerQ
I can do this for you. I'm familiar with both languages.

I have the code already in Python distilled and simplified from the Electrum code (as a cmd line utility that gens address from mpk) and just need an ECDSA and Hash PHP lib to convert it for you. I can research and do this but don't want to bother if it's in vain because you already paid someone else.

BTW your function will need an increasing index value as well as MPK. The address cannot be gen'd from an MPK alone. A sequence value N is needed. But you just supply a static increasing counter value.

Also, note that using a wallet with these addresses you will need a high gap value to ensure that any addresses generated but never used do not stop the wallet from "seeing" past the empty addresses.

Also, if you're ok with running a shell command from php then an easy way would be to call my python cmd util to provide the address. Slower no doubt, but easy.
legendary
Activity: 1536
Merit: 1000
electronic [r]evolution
Got your message. I can't imagine it would be too hard to do what you require, but I don't really have a clue how Electrum generates the addresses from the "master public key". I assume the master pub key is basically created from the secret seed phrase is it? I don't really know Python at all but if there's some Python code I could refer to I could probably get the general idea of what needs to be done.
member
Activity: 73
Merit: 10
An easier way would be to run an electrum client on the server and use the command line interface to get addresses.

Or just use this - http://bitfreak.info/?page=tools&t=bitsci

Thanks, bitfreak.info link sounds interesting. I contacted their people for possible help.
sr. member
Activity: 330
Merit: 250
An easier way would be to run an electrum client on the server and use the command line interface to get addresses.

Or just use this - http://bitfreak.info/?page=tools&t=bitsci
member
Activity: 73
Merit: 10
-= Project completed =-

Current bounty: 1 BTC.

I need one standalone PHP function created that takes as a parameter Electrum wallet's Master Public Key to generate Bitcoin addresses:

$new_bitcoin_address = generate_bitcoin_address ($master_public_key);

There is an open source project - acceptbit.com (sources are here) that already utilizes this functionality, but it is written on Python, just like all other Electrum software.
Here are sources for Electrum client

I neither good at math nor at Python and need this function written in PHP to utilize it within Wordpress plugin.

So someone who knows both Python and PHP would likely be able to do that.

Here's more description of the project I working on.
Jump to: