Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate multiple address types from a single key. #720

Open
dan-da opened this issue Aug 2, 2018 · 2 comments
Open

Generate multiple address types from a single key. #720

dan-da opened this issue Aug 2, 2018 · 2 comments

Comments

@dan-da
Copy link

dan-da commented Aug 2, 2018

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.

@afk11
Copy link
Member

afk11 commented Aug 2, 2018

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
Copy link
Member

afk11 commented Dec 4, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants