Skip to content

How to get private key of derived address? #905

Open

Description

@Schiocco

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!

Activity

  1. changed the title [-]How to get private key of derived address from?[/-] [+]How to get private key of derived address?[/+] on Jan 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions