Author

Topic: Derive in batches, addresses of an xpub programmatically. (Read 1059 times)

legendary
Activity: 3682
Merit: 1580
For a given HD wallets public xpub (Extended public key), derive the public child addresses, in bulk.
This PHP script : https://github.com/dan-da/hd-wallet-addrs , derives public addresses, but does so one by one, which is sloww.
I am pretty sure it is possible to derive say 10 to 100 child addresses at one go. But how ?

I believe the library that bitcoin payments for woocommerce uses is faster than the bitwasp ones. I use it for my bitcoin address plugin too. You can grab it from either plugin.

In my plugin you'll find it in includes/libs. The ElectrumHelper file contains the static function mpk_to_bc_address that you can use.

The downside is that this is only for electrum. You'll have to modify it to use another address derivation.
sr. member
Activity: 412
Merit: 287
Quote
You could always check out the extension for libsecp256k1 https://github.com/bit-wasp/secp256k1-php
(Note for googlers: the default branch does NOT compile for PHP5. Run
Code:
git checkout v0.0.8
in directory - secp256k1-php , before ./configure)

Thanks for this, but I can't figure out how to use or even understand it. Plus the examples in the README are for the latest branch which doesn't work with PHP5. So have no examples or documentation Sad.
Anyhow, this looks like what I want? :
Quote
Public Key tweak by addition

secp256k1_ec_pubkey_tweak_add(resource $context, resource $publicKey, string $tweak32) takes the given $tweak value, converts it to a point, and adds the point to the $publicKey point. The result is written to the provided $publicKey memory location.

This function is useful for deterministic key derivation.


btw What is 'tweak' here  ? Googling doesn't seem to help.

My Goal is to just generate public keys from an xpub. any guidance would be much appreciated

ty


> no examples or documentation.

Did you miss the tests directory Tongue Don't install it too use its functions directly - bitcoin-php will adapt between it and PHPECC, so you don't write code any differently.
newbie
Activity: 39
Merit: 0
Quote
You could always check out the extension for libsecp256k1 https://github.com/bit-wasp/secp256k1-php
(Note for googlers: the default branch does NOT compile for PHP5. Run
Code:
git checkout v0.0.8
in directory - secp256k1-php , before ./configure)

Thanks for this, but I can't figure out how to use or even understand it. Plus the examples in the README are for the latest branch which doesn't work with PHP5. So have no examples or documentation Sad.
Anyhow, this looks like what I want? :
Quote
Public Key tweak by addition

secp256k1_ec_pubkey_tweak_add(resource $context, resource $publicKey, string $tweak32) takes the given $tweak value, converts it to a point, and adds the point to the $publicKey point. The result is written to the provided $publicKey memory location.

This function is useful for deterministic key derivation.


btw What is 'tweak' here  ? Googling doesn't seem to help.

My Goal is to just generate public keys from an xpub. any guidance would be much appreciated

ty


sr. member
Activity: 412
Merit: 287
hd-wallet-addrs seems to wrap `bitwasp/bitcoin` which has advanced several versions since then (bitcoin-lib-php has been retired), including one that sped up elliptic curve operations a bit. You could always check out the extension for libsecp256k1 https://github.com/bit-wasp/secp256k1-php
staff
Activity: 3458
Merit: 6793
Just writing some code
newbie
Activity: 39
Merit: 0
Sorry, should have been clearer.
The library I am using takes about 9 seconds to derive 100 addresses. I am guessing its wayyy too slow and can be potentially much faster.

My question:
Am i right in assuming its too slow ?
If it is can someone point me to an alternative library/script ?


staff
Activity: 3458
Merit: 6793
Just writing some code
So what's the problem? What is your question? That the given code is too slow? If so, then go find one that is faster.

IIRC the actual key derivation algorithm should not be that slow.
newbie
Activity: 39
Merit: 0
That's what the current code seems to do as well

Code:
 for( $i = $start-1; $i < $end; $i ++ ) {
        $relpath = $relpath_base . (strlen($relpath_base) ? '/' : '') . "$type/$i";
        $abspath = strlen($abspath_base) ? $abspath_base . "/$type/$i" : '';
        $key = $master->derivePath($relpath);

        $address =  $key->getPublicKey()->getAddress()->getAddress(); // statement in the for loop.

        //stuff (store address in array) etc.
      
   }


The above code takes about 9 seconds to generate 100 keys. is this normal ?
staff
Activity: 3458
Merit: 6793
Just writing some code
Just make a for loop that increments the derivation path and derives another key.
newbie
Activity: 39
Merit: 0
For a given HD wallets public xpub (Extended public key), derive the public child addresses, in bulk.
This PHP script : https://github.com/dan-da/hd-wallet-addrs , derives public addresses, but does so one by one, which is sloww.
I am pretty sure it is possible to derive say 10 to 100 child addresses at one go. But how ? I tried digging through the above scripts code, but am unable to figure this out.

Help please!

Just to be clear, public chlid addresses  equals an address such as "14dXQGMby9q3TBAYFurAThvHBK29vr2ymg"

Thanks

btw I know I will have to check if the derived addresses have been used, but I got that part figured out! (For googlers: just send  a JSON-RPC request to the method getaddressdeltas, to get info about multiple addresses)
Jump to: