Coder Social home page Coder Social logo

decred-php-api's Introduction

Decred PHP API

Build Status

PHP API for the Decred Cryptocurrency

Installation

Add composer package to your project

composer require decred/decred-php-api

Make sure GMP PHP extesion is installed. In ubuntu:

sudo apt install php7.0-gmp

From repository

You also can clone git package with

git clone https://github.com/decred/decred-php-api.git

But still you will need to fetch library dependencies with composer.

composer install --no-dev

Don't forget to include composer autoloader.

include __DIR__.'/../vendor/autoload.php';

Usage examples

Library have wide functionality, so you could find usage examples in examples library or looking into PHPUnit tests.

Generating seed

First of all we need to get Network intance to start working with library.

$testnet = \Decred\TestNet::instance();

And mainnet accordingly

$mainnet = \Decred\MainNet::instance();

Now lets generate a seed, that will be also verified for usage on testnet. Defaut account and branch address will be derivded to verify the seed.

$seed = \Decred\Crypto\ExtendedKey::generateSeed($testnet);

HD Wallets

When we have usable seed we can create HD master key.

$master = \Decred\Crypto\ExtendedKey::newMaster($seed, $testnet);

newMaster method will return ExtendedKey object, that have variant API for working with HD wallets.

ExtendedKey::privateChildKey($index)

Derives HD private child key from parent HD private key, returns ExtendedKey object.

ExtendedKey::hardenedChildKey($index)

Derives HD hardened child key from parent HD private key, returns ExtendedKey object.

Can't be dervied from HD public key.

ExtendedKey::publicChildKey($index)

Derives HD public child key from parent HD private or public key, returns ExtendedKey object.

ExtendedKey::neuter

Verify that extended key is public, returns ExtendedKey object.

Default account

Using this basic methods we can derive default account HD private and public keys accourding to BIP44.

HD path (m\44'\42'\0')

$defaultAccountPrivateKey = $master
    ->hardenedChildKey(44)
    ->hardenedChildKey(42)
    ->hardenedChildKey(0);

$defaultAccountPublicKey = $master->neuter();

ExtendedKey implements __toString() method, so you can easily get Base58 representation of HD key.

echo sprintf("Default account HD private key: %s\n", $defaultAccountPrivateKey);
echo sprintf("Default account HD public key: %s\n", $defaultAccountPublicKey);

From default account we can derive 0 branch and 0 index and the get default address.

$defaultAddress = $defaultAccountPublicKey
    ->publicChildKey(0)
    ->publicChildKey(0)
    ->getAddress();

echo sprintf("Default address: %s\n", $defaultAddress);

decred-php-api's People

Contributors

r3volut1oner avatar dajohi avatar sup3rwet avatar degeri avatar

Watchers

James Cloos avatar

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.