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 extended public keys from an extended key #29

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions hd-wallet-derive.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ function main()
}
}
$key = @$params['key'] ?: $walletDerive->mnemonicToKey($params['coin'], $params['mnemonic'], $params['key-type'], $params['mnemonic-pw']);

if (@$params['gen-extended']) {
$result = $walletDerive->getExtendedPublicKeys($key);
WalletDeriveReport::printResults($params, $result, true);
return 0;
}

$addrs = $walletDerive->derive_keys($key);

// Prints result
Expand Down
4 changes: 3 additions & 1 deletion src/Utils/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static function getCliParams()
'list-cols',
'bch-format:',
'alt-extended:',
'gen-key', 'gen-key-all',
'gen-key', 'gen-key-all', 'gen-extended',
'gen-words:',
'version', 'help', 'help-coins',
'preset:', 'path-change', 'path-account:', 'help-presets',
Expand Down Expand Up @@ -95,6 +95,7 @@ public static function processCliParams()

$params['gen-key'] = isset($params['gen-key']) || isset($params['gen-words']);
$params['gen-key-all'] = isset($params['gen-key-all']); // hidden param, for the truly worthy who read the code.
$params['gen-extended'] = isset($params['gen-extended']);
$key = @$params['key'];
$mnemonic = @$params['mnemonic'];

Expand Down Expand Up @@ -284,6 +285,7 @@ public static function printHelp()

--includeroot include root key as first element of report.
--gen-key generates a new key.
--gen-extended generate the coresponding x, y or z public keys from a provided key
--gen-words=<n> num words to generate. implies --gen-key.
one of: [$allowed_numwords]
default = 24.
Expand Down
47 changes: 47 additions & 0 deletions src/WalletDerive.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,53 @@ function serializePrivKey($symbol, $network, $key) {
return $hex ? '0x' . $key->getHex() : $key->toWif($network);
}

/**
* Get extended public keys from a given key in all available formats
*/
function getExtendedPublicKeys($key) {
$params = $this->get_params();
$coin = $params['coin'];
list($symbol) = explode('-', $coin);

$networkCoinFactory = new NetworkCoinFactory();
$network = $networkCoinFactory->getNetworkCoinInstance($coin);
Bitcoin::setNetwork($network);

// get initial key type for the coin
$initial_key_type = $this->getKeyTypeFromCoinAndKey($coin, $key);

// store results here
$extkeys = array();

Copy link
Contributor

@melaxon melaxon Oct 15, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May I suggest you arrange these pieces of code as foreach somehow like this:

    public function getExtendedPublicKeys($key) {
        $params = $this->get_params();
        $coin = $params['coin'];
        list($symbol) = explode('-', $coin);
        $networkCoinFactory = new NetworkCoinFactory();
        $network = $networkCoinFactory->getNetworkCoinInstance($coin);
        Bitcoin::setNetwork($network);
        // get initial key type for the coin
        $initial_key_type = $this->getKeyTypeFromCoinAndKey($coin, $key);
        $addrTypes = [
            'x' => 'legacy',
            'y' => 'p2sh-segwit',
            'z' => 'bech32'
        ];
        $types = ['x', 'y', 'z'];
        $extkeys = array();
        foreach ($types as $type) {
            $this->params['addr-type'] = $addrTypes[$type];
            $key_type = $type;
            $xyzPub = $type . 'pub';
            $master = $this->fromExtended($coin, $key, $network, $initial_key_type);
            if ( $this->networkSupportsKeyType($network, $key_type, $coin ) && method_exists($master, 'getPublicKey') ) {
                $extkeys[] = [ $xyzPub => $this->toExtendedKey($coin, $master->withoutPrivateKey(), $network, $key_type)];
            }
        }
        return $extkeys;

    }


I would also suggest you squash all the commits into a single one to keep nice clean history

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes I concur with @melaxon's suggestions.

// try to generate xpub address
$key_type = 'x';
$this->params['addr-type'] = 'legacy';
// generate the master key for the given type from the initial type
$master = $this->fromExtended($coin, $key, $network, $initial_key_type);
if ( $this->networkSupportsKeyType($network, $key_type, $coin ) && method_exists($master, 'getPublicKey') ) {
$extkeys[] = array( 'xpub' => $this->toExtendedKey($coin, $master->withoutPrivateKey(), $network, $key_type));
}

// try to generate ypub address
$key_type = 'y';
$this->params['addr-type'] = 'p2sh-segwit';
// regenerate the master key for the given type from the initial type
$master = $this->fromExtended($coin, $key, $network, $initial_key_type);
if ( $this->networkSupportsKeyType($network, $key_type, $coin ) && method_exists($master, 'getPublicKey') ) {
$extkeys[] = array( 'ypub' => $this->toExtendedKey($coin, $master->withoutPrivateKey(), $network, $key_type));
}

// try to generate zpub address
$key_type = 'z';
$this->params['addr-type'] = 'bech32';
// regenerate the master key for the given type from the initial type
$master = $this->fromExtended($coin, $key, $network, $initial_key_type);
if ( $this->networkSupportsKeyType($network, $key_type, $coin ) && method_exists($master, 'getPublicKey') ) {
$extkeys[] = array( 'zpub' => $this->toExtendedKey($coin, $master->withoutPrivateKey(), $network, $key_type));
}

return $extkeys;
}

private function address($key, $network) {
$addrCreator = new AddressCreator();
Expand Down