PHP code example of nimiq / xpub

1. Go to this page and download the library: Download nimiq/xpub library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

nimiq / xpub example snippets


# PSR-4 autoloading with composer
use Nimiq\XPub;

# Create an XPub class instance from an xpub/tpub/ypub/upub/zpub/vpub string.
$xpub = XPub::fromString( 'xpub...' ); // => BIP44 Original
$xpub = XPub::fromString( 'ypub...' ); // => BIP49 Nested SegWit
$xpub = XPub::fromString( 'zpub...' ); // => BIP84 Native SegWit

# You can also specify the address scheme to override auto-detection.
$xpub = XPub::fromString( 'xpub...', XPub::BIP84 );
$xpub = XPub::fromString( 'xpub...', XPub::BIP49 );
$xpub = XPub::fromString( 'zpub...', XPub::BIP44 );

# Derive a child extended public key from it.
$xpub_i = $xpub->derive( $i );
# You can also pass an array to derive a path.
$xpub_i_k = $xpub->derive( [$i, $k]);

# An XPub can be serialized back into a string.
# Pass $asHex = true to serialize into a HEX string, base58 is the default.
$xpub_string = $xpub_i->toString( $asHex = false );

# An XPub can be converted into an address.
# Pass $coin = 'eth' to convert into an ETH address.
#
# By default, xpubs are converted into standard addresses,
# ypubs are converted into nested segwit addresses,
# and zpubs are converted into native segwit addresses.
$address = $xpub_i->toAddress( $coin = 'btc' );

// Parse any extended public key string
$xpub = XPub::fromString( 'xpub...' );

// Change the version on the instance
$xpub->version = 'zpub'; // Use any of the supported formats from the table at the top of the README

// Stringify to the target format
$zpub = $xpub->toString(); // 'zpub...'

# Get a hash160
$hashed_hex = XPub::hash160( $input_hex );

# Get a double sha256
$hashed_hex = XPub::doubleSha256( $input_hex );
bash
composer run-script test
# or
php test/test.php