Git Product home page Git Product logo

nem-php's Introduction

Build Status Total Downloads Latest Stable Version Latest Unstable Version License

This package aims to provide with an easy-to-use PHP Laravel Namespace helping developers to communicate with the NEM blockchain through its NIS API.

This package should be an aid to any developer working on Laravel/Lumen/Symfony applications with the NEM blockchain.

This package is currently still in development, please do not use in production.

The author of this package cannot be held responsible for any loss of money or any malintentioned usage forms of this package. Please use this package with caution.

Package licensed under MIT License.

Documentation

Reader-friendly Documentation will be added in development period and will be available on the Github Wiki at evias/nem-php Wiki.

Currently phpdocumentor is integrated to the project in order to automatically generate API documentation from the source code. You must run the phpdoc and phpdocmd command if you wish to generate the Documentation, the first stable release will include a generated Documentation version.

# First build the API documentation
./vendor/bin/phpdoc -d src/ -t build/ --template="xml"

# Then generate the Markdown
./vendor/bin/phpdocmd build/structure.xml docs/

Pot de vin

If you like the initiative, and for the sake of good mood, I recommend you take a few minutes to Donate a beer or Three [because belgians like that] by sending some XEM (or whatever Mosaic you think pays me a few beers someday!) to my Wallet:

NB72EM6TTSX72O47T3GQFL345AB5WYKIDODKPPYW
Username Role
eVias Project Lead
RobertoSnap Developer

Installation

You can install this package with Composer. You only need to require evias/nem-php.

composer require evias/nem-php

The package can also be downloaded manually by cloning this repository or by downloading the packagist archive:

Once you have required the package in your composer.json file (or using the command above), you can install the dependencies of this package:

composer install

Unit Tests

The library provides with a Unit Test Suite for the implemented SDK features.

The unit test suite is also configured on Travis-CI with the current Build Status:

  • Build Status

If you wish to run the unitary test suite, you can use the executable file provided by PHPUnit which is located under vendor/bin/phpunit.

Alernatively, you can create a symbolic link to this executable file in the nem-php clone root folder.

ln -s vendor/bin/phpunit 

Now you can simply run phpunit in the terminal and it will launch the Rocket.. meh, the Unit Tests Suite.

Laravel advanced features

Modify your config/app.php configuration file to include the NEM\ServiceProvider service provider. Look out for the providers configuration array and add our class as shown below:

'providers' => [
    NEM\ServiceProvider::class,
],

If you wish to make use of the Laravel Facades provided by this library, you will also need to list the alias in your config/app.php configuration file under the aliases configuration array as described below:

'aliases' => [
    'NemSDK' => NEM\Facades\NemSDK::class,
],

Usage / Examples

When you have installed the evias/nem-php package you will be able to use the API class to send API requests to the configured NIS. By default, the config/nem.php file defines the localhost NIS to be used, this can be changed.

If you are using Laravel or Lumen, you will need to register the Service Provider of this package into your app:

    // Laravel/Lumen registering the service provider
    $app = Laravel\Lumen\Application(realpath(__DIR__));
    $app->register(NEM\ServiceProvider::class);

Example 1: Using the Service Provider (Laravel only)

    // Example 1: Using the Service Provider
    // --------------------------------------
    // The Service Provider for Laravel/Lumen will bind "nem.config",
    // "nem" and "nem.ncc" in the IoC. "nem" and "nem.ncc" are pre-
    // configured instances of the API class using APP_ENV for the environment.
    $nemConfig = $app["nem.config"]
    $nemAPI = $app["nem"];
    $nccAPI = $app["nem.ncc"];

Example 2: Using the API wrapper

    // Example 2: Instantiating the API class
    // --------------------------------------
    // You can also create a new instance of the API
    $nemAPI = new NEM\API();
    $nemAPI->setOptions([
        "protocol" => "http",
        "use_ssl" => false,
        "host" 	  => "go.nem.ninja",
        "port"    => 7890,
        "endpoint" => "/",
    ]);

    // If you wish you can define your own RequestHandler, have a look at the
    // NEM\Contracts\RequestHandler interface.
    $nemAPI->setOptions(["handler_class" => Path\To\My\Handler::class]);

Example 3: Sending GET/POST request to the NIS API and getting back JSON

    // Example 3: Sending GET/POST JSON requests
    // -----------------------------------------
    // The API wrapper class can be used to send API requests to the
    // configured NIS host with following snippet:
    $response = $nemAPI->getJSON("heartbeat", "");

    // sending JSON through POST and receiving JSON back.
    $postData = ["myField" => "hasThisValue", "yourField" => "isNotEmpty"];
    $response = $nemAPI->postJSON("post/endpoint", json_encode($postData));

Example 4: Custom Headers and response callback configurations

    // Example 4: Custom headers and Callback configuration
    // -----------------------------------------------------
    // The 3rd parameter of the get() and post() methods lets you pass
    // an options array to the RequestHandler. To add specific headers for
    // example you would do as follows:
    $response = $nemAPI->getJSON("hearbeat", "", ["headers" => ["Content-Type" => "text/xml"]]);

    // You may also define onSuccess, onError and onReject callbacks to be executed
    // when the Guzzle Promises respectively complete, encounter an error or are denied.
    // @see Psr\Http\Message\ResponseInterface
    // @see GuzzleHttp\Exception\RequestException
    $response = $nemAPI->getJSON("heartbeat", "", [
        "onSuccess" => function(ResponseInterface $response) {
            echo $response->getBody();
        },
        "onError" => function(RequestException $exception) {
            echo "This is bad: " . $exception->getMessage();
        },
        "onReject" => function($reason) {
            echo "Request could not be completed: " . $reason;
        }
    ]);

Example 5: Use the SDK to create NEM NIS compliant Objects

    // Example 5: Use the SDK to create NEM *NIS compliant* Objects
    // ------------------------------------------------------------
    // You can create an instance and pass the *connection configuration*
    $sdk = new NEM\SDK([
        "protocol" => "http",
        "use_ssl" => false,
        "host" 	  => "go.nem.ninja",
        "port"    => 7890,
        "endpoint" => "/",
    ]);

    // Or you can use an already initialized API client
    $sdk = new NEM\SDK([], new NEM\API());
    $account = $sdk->models()->account(["address" => "TDWZ55R5VIHSH5WWK6CEGAIP7D35XVFZ3RU2S5UQ"]);

    // The \NEM\Contracts\DataTransferObject interface tells us that Models
    // always have a toDTO() method which will format the said object into
    // its *NIS compliant* object.

    // Dump [AccountMetaDataPair](https://bob.nem.ninja/docs/#accountMetaDataPair) object
    var_dump($account->toDTO());

Example 6: Use the SDK NIS Web Service implementations

    // Example 6: Use the SDK NIS Web Service implementations
    // ------------------------------------------------------------
    $sdk = new NEM\SDK();
    $service = $sdk->account();

    // Generate a new account
    $account = $service->generateAccount(); // $account is an instance of \NEM\Models\Account

    // Read account data *from the NEM blockchain* using the Address
    $account = $service->getFromAddress("TDWZ55R5VIHSH5WWK6CEGAIP7D35XVFZ3RU2S5UQ");

    // Read account data *from the NEM blockchain* using the Public Key
    $account = $service->getFromPublicKey("d90c08cfbbf918d9304ddd45f6432564c390a5facff3df17ed5c096c4ccf0d04");

Example 7: Use the SDK to read an account's transactions

    // Example 7: Use the SDK to read an account's transactions
    // ------------------------------------------------------------
    $sdk = new NEM\SDK();
    $service = $sdk->account();

    // Get incoming transaction for an account by its address
    // $incomings will be an instance of \NEM\Models\ModelCollection
    $incomings = $service->incomingTransactions("TDWZ55R5VIHSH5WWK6CEGAIP7D35XVFZ3RU2S5UQ");

    // Get outgoing transaction for an account by its address
    $outgoings = $service->outgoingTransactions("TDWZ55R5VIHSH5WWK6CEGAIP7D35XVFZ3RU2S5UQ");

    // Get unconfirmed transaction for an account by its address
    $unconfirmed = $service->unconfirmedTransactions("TDWZ55R5VIHSH5WWK6CEGAIP7D35XVFZ3RU2S5UQ");

Example 8: Derive a Public Key with a hexadecimal or binary Private Key

    // Example 8: Derive a Public Key with a hexadecimal or binary Private Key
    // --------------------------------------------------------------------------
    $privateKey = "e77c84331edbfa3d209c4e68809c98a634ad6e8891e4174455c33be9dd25fce5";
    $publicKey  = "d90c08cfbbf918d9304ddd45f6432564c390a5facff3df17ed5c096c4ccf0d04";
    $keypair = new NEM\Core\KeyPair($privateKey);
    var_dump($keypair->getPublicKey("hex")); // will output: 

    // Create with *provided public key* (no derivation - faster)
    $keypair = new NEM\Core\KeyPair($privateKey, $publicKey);

Example 9: Create New KeyPair and Address (randomly)

    // Example 9: Create New KeyPair and Address (randomly)
    // --------------------------------------------------------------------------
    $keypair = new NEM\Core\KeyPair();
    $address = NEM\Models\Address::fromPublicKey($keypair->getPublicKey());

    var_dump($keypair->getPrivateKey("hex"), $address->toClean());

Example 10: Sign Data with a NEM KeyPair

    // Example 10: Sign Data with a NEM KeyPair
    // --------------------------------------------------------------------------
    $keypair = new NEM\Core\KeyPair("abf4cf55a2b3f742d7543d9cc17f50447b969e6e06f5ea9195d428ab12b7318d");

    var_dump($keypair->sign("nem-php by eVias!")->getHex());
    // this will show you the following hexadecimal signature:
    // 177908f0cb5e56a0da11bfc3b38f6d749c4c870c9b356313db6460925e4584a9304e6aa1a5ba50ec2f773bbdfbfc03285a35d986d056df27b7d05a74f6c9b501

    // you can now use this signature and verify it using the Public Key

Troubleshoot / Issues Resolution

Installing dependencies with MacOS MAMP or MAMP Pro

It may happen that your PHP server is not installed in the correct version or does not load the right extensions so here is a little snippet that will provide you with the exact PHP version needed (you also need to compile httpd).

First you will need to rebuilt the httpd service :

mkdir ~/httpd-2.2.34/

# Download httpd source code
cd ~/Downloads
wgets http://apache.belnet.be//httpd/httpd-2.2.34.tar.bz2
tar xvzf httpd-2.2.34.tar.bz2

# We now have the HTTPD source code unarchived
cd httpd-2.2.34
./configure
make
make install

The above step is only because MAMP does not include the Apache build/ folder (which you can now find under ~/httpd-2.2.34/). Ok, next step is to recompile PHP as an Apache Module as required by MAMP. Following snippet will let you download and compile the PHP package with MacOS and MAMP, enabling GMP, GD, MySQL, XML, CURL, GETTEXT, MBSTRING and BCMATH modules.

# This is where the *built* PHP will be installed.
mkdir ~/php-7.1.8/

# Download php source code
cd ~/Downloads
wgets http://de2.php.net/get/php-7.1.8.tar.bz2/from/this/mirror
tar xvzf php-7.1.8.tar.bz2

# We now have the PHP source code unarchived
cd php-7.1.8

# MacOS
brew install intltool icu4c gettext
brew link icu4c gettext
./configure --with-apxs2=/Applications/MAMP/Library/bin/apxs --prefix=/Users/greg/php-7.1.8 --enable-intl --with-gmp --with-xmlrpc --enable-bcmath --with-curl=/usr --with-gettext=/usr/local/Cellar/gettext/ --with-gd --with-pdo-mysql --with-openssl=/usr/local/Cellar/openssl/1.0.2n/ --enable-mbstring
make
make install
# End-MacOS

# Linux
./configure --prefix=/home/greg/php-7.1.8 --enable-intl --with-gmp --with-xmlrpc --enable-bcmath --with-curl=/usr --with-gettext --with-gd --with-pdo-mysql --with-openssl
make
make install
# End-Linux

After building the PHP source code you will also need to link to that file in your system. Following is an easy workaround, but please, make sure to backup any file before you overwrite executables.

The compilation of PHP will have installed a file: ~/httpd-2.2.34/modules/libphp7.so, you must link your system to that file in order to use the correct PHP modules. Use following snippet to link both, the PHP apache module and the PHP executable in your MAMP installation.

mkdir /Applications/MAMP/Library/build
cp -R /Users/greg/httpd-2.2.34/build/* /Applications/MAMP/Library/build/
sudo ln -s /Users/greg/httpd-2.2.34/modules/libphp7.so /Applications/MAMP/bin/php/php7.1.8/modules/
sudo ln -s /Users/greg/php-7.1.8/bin/php /usr/local/bin/php

Changelog

Important versions listed below. Refer to the Changelog for a full history of the project.

License

This software is released under the MIT License.

© 2017-2018 Grégory Saive [email protected], All rights reserved.

nem-php's People

Contributors

cs76 avatar evias avatar mxk1011 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

nem-php's Issues

private key from randomly new keypair

Hi, i tried out your 9th example to create a randomly keypair and i want to get clean private key from it, thus i assume

$keypair->getPrivateKey("hex")

this line of code would give me the private key i want, but somehow i try import the result in nano wallet, it shows different address from what your code generated

can you advice me?

Composer Dependency Conflict with Laravel 7

Using version ^0.2.14 for evias/nem-php
./composer.json has been updated
Running composer update evias/nem-php
Loading composer repositories with package information
Updating dependencies
Your requirements could not be resolved to an installable set of packages.

Problem 1
- Root composer.json requires evias/nem-php ^0.2.14 -> satisfiable by evias/nem-php[v0.2.14].
- evias/nem-php v0.2.14 requires illuminate/contracts 5.0.33|5.1.|5.2.|5.3.|5.4.|5.5.|5.6.|5.7.* -> found illuminate/contracts[v5.0.33, ..., 5.7.x-dev] but it conflicts with another require.

Installation failed, reverting ./composer.json and ./composer.lock to their original content.

Error in sending transaction.

here is my code

$sdk = new \NEM\SDK([
"protocol" => "http",
"use_ssl" => false,
"host" => "52.197.57.86",
"port" => 7890,
"endpoint" => "/",
]);

$message = $sdk->models()->Message(['type' => 1,'plain'=>'test123']);
$messageHash = $message->toDTO();

$keypair = new \NEM\Core\KeyPair("4444353a76a97fd65d67d0d0b5bae23069ca753d8a0793f01091e8051992d9b5","a01ca52e998f17804adfc193782a338b50a8f0476649dd4025fc3d2096cda5d9");

$data['transaction']['signer'] = $keypair->getPublicKey("hex");
$data['transaction']['amount'] = 3;
$data['transaction']['recipient'] = 'TAAM36DYU5GEUR6PBQG74TXCH63FDLF6Y2WXQOE3';
$data['transaction']['type'] = 257;
$data['transaction']['version'] = -1744830463;

$data['transaction']['message'] = $message;
$data['transaction']['message']['payload'] = $messageHash['payload'];
$data['transaction']['message']['type'] = $messageHash['type'];
$transactionM = $sdk->models()->Transaction($data);

$transaction = $service->announce($transactionM, $keypair);

errorphpnem

ModelMutator mutate argument miss match error

Hi! Here is the code snippet.

App::register('NEM\ServiceProvider');
$nemConfig = app('nem.config');
$nemAPI = app("nem");
$nemSDK = app("nem.sdk");
$service = $nemSDK->account();
$account = $service->getFromAddress("TBORJWW4BRZBZ2NHX5QKDHLZSG6LQZG23DYES3Z2");`

"Type error: Argument 2 passed to NEM\Models\Mutators\ModelMutator::mutate() must be of the type array, object given, called in /Users/CS76Bot/Development/web3/nem/nem-ecom/vendor/evias/nem-php/src/Infrastructure/Service.php on line 127"

If I change the argument type to object the model generated is empty.
- setAttributes in the model class is failing to map the attributes

Any help would be much appreciated :)
Thank you

[Proposal] Conflicting dependency

kornrunner/keccak requires php: >=7.1 though evias/nem-php requires php: >=5.5.9.

Use mine (just a copy of kornrunner/keccak for php: >=5.3), fix dependency of evias/nem-php, or take other way instead please.

Replace mdanter/ecc with paragonie/ecc

The PHPECC project has been abandoned for years, despite numerous attempts to fix security issues in the code. Paragon Initiative Enterprises has opted to fork PHPECC in order to provide a more secure implementation of ECDH and ECDSA for PHP projects.

Please refer to the Release Notes for our fork, which contains the specifics about the security issues we fixed and how to ensure your code is safe.

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.