bitcoin-php icon indicating copy to clipboard operation
bitcoin-php copied to clipboard

Generate multiple address types from a single key.

Open dan-da opened this issue 5 years ago • 2 comments

At present, it seems that a given HierarchicalKey instance can only be encoded as a single address type, eg p2pkh, p2wpkh, p2sh-p2wpkh, etc. AddressCreator takes no constructor arguments and internally generates an address object based on the key's outputscript.

However fundamentally a key is a numeric value that can be encoded into an address in different ways.

A use-case where this becomes important is when deriving wallet keys in the style of bitcoin-core HD wallets. This style of derivation uses a single xprv/xpub root key and derives hardened keys from it. For each derived key, either a "legacy", 'p2sh-segwit' or 'bech32' address can be created, at user's choice. A derivation tool might want to display multiple address-types for each derived key.

So I'm unsure how best to achieve this with the existing library. I supposed that for each derived key, the secret can be extracted and used to instantiate a new key specific to each desired address type. But it seems much cleaner/simpler something like:

$key = $root->derive('m/0');
$addrCreator = new AddressCreator($network);
echo $addrCreator->fromKey($key, 'p2pkh');
echo $addrCreator->fromKey($key, 'p2sh-p2wpkh');
echo $addrCreator->fromKey($key, 'p2wpkh');

It's quite possible there is some confusion on my part. If there is a simple way to achieve the above use-case already, please provide an example.

dan-da avatar Aug 02 '18 03:08 dan-da

For that case I'd probably use some of the KeyToScript things (though I've yet find a nice way to do that with multisig) or do it manually.

I wouldn't have exposed getAddress on HK were it not for the SLIP132 stuff. Before that, the address type wouldn't have been dynamic, but changing the default script type could cause problems for devs.. but since it varies with prefix, getAddress / getScriptAndSignData seemed reasonable.

For the addresses of such scripts, just use $addrCreator->fromOutputScript()

afk11 avatar Aug 02 '18 13:08 afk11

I wonder if a nice way to fix this would be withScriptFactory(ScriptDataFactory $factory): self?

afk11 avatar Dec 04 '18 23:12 afk11