Bit-Wasp/bitcoin-lib-php

Feature Request: capability to derive all (used) wallet keys from public master key.

dan-da opened this issue · 3 comments

Hi, I've written a tool (https://github.com/dan-da/bitprices) that generates a historic transaction report including USD price/value/balance at the time of each transaction. The tool works with any bitcoin address. I'm also working on a website frontend (http://mybitprices.info/) that calls the tool, where people can enter their wallet addresses and easily generate a report.

Now that HD wallets are becoming popular, I would like to add the capability to input a single master public key/address and derive all the wallet addresses ever used from that.

SInce my tool is written in PHP, I'd like to find a PHP library that facilitates this.

In reviewing the Bip32.php in this project, I didn't see any way to derive addresses from a master public key. but maybe I missed it? If so, an example would be much appreciated.

The other piece I need besides derivation is checking if the address has ever been used. bip44 defines a gap of 20 unused addresses as indicating the end of a wallet.

What I would really love to see is a simple to use API like:
::derive_used_wallet_addresses_from_pubkey();

I've no idea how hard that would be to implement. ;-)

In general this capability should be useful to anyone writing an app for auditing purposes where private keys are not needed/desired.

See Vitalik's article which mentions that usage:
https://bitcoinmagazine.com/8396/deterministic-wallets-advantages-flaw/

afk11 commented

AFAIK, the BIP32 class returns an address whenever a new key is generated. However, one thing to note is this library does NOT have p2p or networking abilities - so there's no way to learn about unspent bitcoins on an address, you need to use an API like blocktrail/blockchain.info for that.

Oh - since you probably already have bitcoind somewhere in your stack, perhaps you could look into https://github.com/btcdrak/bitcoin/tree/addrindex-0.11

Hmm, I just saw this was marked 'feature request'.. Unfortunately there won't be new features being added to this library. Please look at https://github.com/Bit-Wasp/bitcoin-php - it has everything and more.

afk11 commented

I just reread everything and understand what you need.

"In reviewing the Bip32.php in this project, I didn't see any way to derive addresses from a master public key. but maybe I missed it? If so, an example would be much appreciated."

Start with the examples tests folders - You use Bip32::import() on your extended public key, and then derive() until finished.

"The other piece I need besides derivation is checking if the address has ever been used. bip44 defines a gap of 20 unused addresses as indicating the end of a wallet."

There's no universal means of querying an address - whether it's by relying on a full node, or asking blockchain.info.. It's normally one of those design aspects you end up building your whole project around, not something that can be encapsulated in one small function :)

thanks for your comments.

I did see the example. It wasn't originally clear to me that it would work just the same for the public key but that makes sense. I will give it a try.

yes it makes sense the app would need to figure out whether each address has been used or not on its own since that depends on reading the blockchain.

closing this issue.