Code Monkey home page Code Monkey logo

php-xpub's Introduction

Nimiq XPub

Build Status

A simple class to derive BTC and ETH extended public keys and addresses without GMP. Only the BCMath extension is required (but GMP is still used for faster calculations when available).

Supports the following formats:

Type Mainnet Testnet
BIP44 (standard) xpub tpub
BIP49 (nested segwit) ypub upub
BIP84 (native segwit) zpub vpub

Installation

The Nimiq XPub package is availabe via the Packagist package registry and can be installed with Composer:

composer require nimiq/xpub

Requirements

  • PHP >= 7.1
  • BCMath or GMP extension

Usage

# 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' );

See the tests for further example usage.

Conversion

You can use this library to convert between xpub formats, for example:

// 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...'

Helpers

The XPub class also exposes two common hashing methods:

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

# Get a double sha256
$hashed_hex = XPub::doubleSha256( $input_hex );

Development

Testing

To execute the test suite run:

composer run-script test
# or
php test/test.php

php-xpub's People

Contributors

dependabot[bot] avatar sisou avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

php-xpub's Issues

Question about Testnet Native Segwit Addresses

Is there any reason why testnet Native Segwit addresses derived from a vpub start with "tc1.." instead of "tb1.."? If there is a reason behind it i don't mind, just wanted the explanation.

Ypub support

Thanks for this awesome library, Could you add ypub support to this library?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.