Integration with NEM using PHP

0
3792

We will learn how to use NEM blockchain to create wallets. We will integrate NEM with Laravel Framework and build the web app.

You should be familiar with making apps with Laravel framework and you need a fresh Laravel Installation.

We will utilize NIS API. By this, we will fully understand how API works and how to use it.

Introduction

This platform allows the users to create their own virtual wallets and store their XEM there.

While the platform has quite a few features, the main features include:

  1. Creating new wallet accounts
  2. Sending and Receiving XEMs
  3. View transaction history

Now we know that our points will be based on the blockchain implementation by NEM, the app will be based on Laravel framework.

 

Architecture:

Let’s review the architecture of the system we’re going to build. The whole system consists of a few parts:

  • Laravel app
    It is a website with HTML pages and a business logic which controls users accounts, notifications, requests to send/receive payments or requests for new addresses. It resides on server 1, has public access to the Internet and handles user’s requests.
  • Co-signer Laravel app
    This is an important additional security measure. It is used to co-sign valid transactions. The app resides on a dedicated server 2 with no direct connection with the actual wallet App. If the wallet App is hacked, this server will help prevent the stealing of user’s funds.
  • The co-signer listens to pending transactions, reviews them, and validates if they look legitimate and then signs a transaction on the NEM blockchain.
  • NEM Blockchain
    It offers HTTP API and we consume it as any other service.
    To use the public blockchain we need XEMs to be loaded on our account because each transaction on the chain requires a fee being paid. This is why we need to protect our funds.

 

Private Key Generation:

There are two accounts being used to manage the platform (each has private and public keys). All of them are spread across two servers:

  • Wallet App, Server 1
    This server is publicly available.
  • Co-signer App, Server 2
    This server is isolated and has public access. It can only interact with NEM blockchain and shared data storage (also accessible by Wallet App).

4 accounts have different purposes:

  • Account_1. Keys are stored on server 1.
    Initiates XEM transfers
  • Account_2. Keys are stored on server 2.
    This account is a cosignatory for Account_2

 

Creating NEM Node:

To interact with a NEM blockchain you need a running node which has APIs and responds to your requests.

You will need your own node running somewhere on your server, to learn how to launch a node.
What you need to do:

  1. Install 64-bit version of JAVA.
  2. Download and extract the latest release from http://bob.nem.ninja:
  3. wget http://bob.nem.ninja/nis-0.6.95.tgz
  4. sudo tar xzf nis-0.6.95.tgz
  5. Edit config file at nis/config.properties.
  6. Run the NIS node:
  7. ./nix.runNis.sh
  8. Test it is accessible:
  9. http -b https://your-domain:7891/status
  10. {
  11.    “code”: 5,
  12.    “message”: “status”,
  13.    “type”: 4
  14. }

Code 5 means node is still syncing. In a while it will be fully operational.

 

Generating Accounts:

We need to make 2 accounts as explained above. It is simple to generate a new account by just sending a GET request:

http -b https://your-domain:7891/account/generate

{

   “address”: “TDBEUBX6SJDSO64NDAU3QMHOCAPWLFGYD4KCOV3E”,

   “privateKey”: “54cce7c2422c8307488a4dd2ae6ac22fb0e2e2c9d3ca05081312020fb7882a68”,

   “publicKey”: “705803b12a02b8e32a4d4f1d8c8c3d0ddb0bd87fbc1dca30ebbd6cc12f59530d”

}

Store 2 generated keys and addresses:

Account_1

{“privateKey”:“…”,“address”:“…”,“publicKey”:“…”}

Account_2

{“privateKey”:“…”,“address”:“…”,“publicKey”:“…”}

Converting to a multisig account

Now, Account_1 must be converted to a multisig account. These are the steps to do this:

  1. Load account with 34 XEMs (this is a fee )
  2. Initiate a conversion transaction.

Now let’s send multisig convertion request. Call the command like this:

“account1_publickKey” “account1_privateKey” “account2_publickKey”

Now pass the output of this file to the NIS like this:

“account1_publickKey” \

“account1_privateKey” “account2_publickKey”

| http https://your-domain:7891/transaction/prepare-announce –json

 

# if went well, the result should look like this

{

   “code”: 1,

   “innerTransactionHash”: {},

   “message”: “SUCCESS”,

   “transactionHash”: {

       “data”: “1a9…”

   },

   “type”: 1

}

Now we have one multisig account and 1 co-signers in place.
Here is the perfect time to send to this fresh account more XEMs, so it can send assets to others.

Now we prepared our NEM infrastructure:

  • we have live running NIS node
  • we have created and secured multisig account for storing assets

NEM offers unique APIs which are easily integrated into any existing app. It is a matter of reading a documentation to start using NEM. We have implemented this in a secured manner and powered it by blockchain technology.

We hope you learned about NEM and now ready to dive deeper into it.