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

How to get private key of derived address? #905

Open
Schiocco opened this issue Jan 12, 2023 · 0 comments
Open

How to get private key of derived address? #905

Schiocco opened this issue Jan 12, 2023 · 0 comments

Comments

@Schiocco
Copy link

I'm trying to get the private key of the derived address generated from the code below.
I know I must pass my private key somewhere, but I do not know where. This problem is the function $child_key->getPrivateKey(). It returns an error: Unable to get private key, not known.

    $xpub = '...........';
    $pubkeytype = substr($xpub, 0, 4);
    $bitcoin_prefixes = new BitcoinRegistry();
    $adapter = Bitcoin::getEcAdapter();
    $slip132 = new Slip132(new KeyToScriptHelper($adapter));
    if ($pubkeytype == 'xpub') $pubPrefix = $slip132->p2pkh($bitcoin_prefixes);
    if ($pubkeytype == 'ypub') $pubPrefix = $slip132->p2shP2wpkh($bitcoin_prefixes);
    if ($pubkeytype == 'zpub') $pubPrefix = $slip132->p2wpkh($bitcoin_prefixes);
    $config = new GlobalPrefixConfig([new NetworkConfig(NetworkFactory::bitcoin(), [$pubPrefix])]);
    $serializer = new Base58ExtendedKeySerializer(new ExtendedKeySerializer($adapter, $config));
    $key = $serializer->parse(NetworkFactory::bitcoin(), $xpub);
    $child_key = $key->derivePath($path);
    return [$child_key->getPrivateKey(), $child_key->getAddress(new AddressCreator())->getAddress()];

Thank you!

@Schiocco Schiocco changed the title How to get private key of derived address from? How to get private key of derived address? Jan 12, 2023
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

1 participant