Connect to Lightstreams

This tutorial is to walk you through connecting to the Lightstreams network using the Pocket-Web3-Provider.

Initial Setup/ Installation

Before we get started, you will need to have a build of the Pocket Core CLI and a generated Application Authentication Token before you can use Pocket-web3-provider.

To download the packages, we will use:

Download the Pocket Provider:

The Pocket-web3-provider package is hosted in NPM, to install open up a terminal and enter:

npm install --save @pokt-network/web3-provider
npm install --save lightstreams-network/lightstreams-js-sdk#master

Setting Up Web3 Environment

You can use the call the Pocket Provider in any file that needs to connect to the Pocket Network using JavaScript, but to do that, we need to create an instance that contains all the data your application needs to have in order to send the request.

Import libraries

/* 
 Import and declare these libraries: 
    - LsLib: (required)- Lightstreams library used to interact with the lightstreams network and creating a wallet
    - LsWallet:(required) used to create a Lightstream wallet
    - PocketLib: (required) Used to call multiple Pocket libraries.
    - Web3: (required) to send a transaction to the desired blockchain. 
    - Pocket: (required) to create a pocket instance to send a relay to the Pocket Network 
    - PocketAAT: (required) Required to allow the client to access the network on the applications behalf. 
    - Configuration: (required) Storing network settings like: timeout, blockchainID, SSL, ect. 
    - PocketProvider:(required) Used to create the Pocket Provider
    - HttpProvider: (required) Used to create RPC provider      
*/

const LsLib = require('lightstreams-js-sdk');
const LsWallet = LsLib.EthersWallet
const PocketLib= require('@pokt-network/web3-provider');
const web3 = LsLib.Web3;
const Configuration = PocketLib.Configuration;
const PocketProvider = PocketLib.PocketProvider;
const HttpRpcProvider = PocketLib.HttpRpcProvider;
const PocketAAT = PocketLib.PocketAAT;
const Pocket = PocketLib.Pocket;

Preconfiguration:

We are going to define a few variables that will help up identity the ICON network we are trying to connect to, create a pocket instance, and identify our application with our staked account on the Pocket Network

// An array holding the initial dispatcher url(s). You can use our known dispatcher list found here(https://docs.pokt.network/docs/known-dispatcher-list) 
const dispatchers = [new URL("http://node1.testnet.pokt.network:8081"), new URL("http://node2.testnet.pokt.network:8081")];

/*
- Configuration: Stores multiple properties used to interact with the Pocket Network.
     - maxDispatchers - (optional) Maximun amount of dispatchers urls to stored in rounting table, default 0.
   - maxSessions - (optional) Maximun amount of sessions to stored for the session manager, default 0.
   - maxConsensusNodes - (optional) Maximun amount of nodes for local consensus, mandatory ODD number, default 0.
   - requestTimeOut - (optional) Maximun timeout for every request in miliseconds, default 0.
   - acceptDisputedResponses - (optional) Accept or reject responses based on having a full consensus, default false.
  
*/
const configuration = new Configuration(5, 100, undefined, 40000);

// application PrivateKey Hex
const appPrivKeyHex = "6401..."

// blockchain: The specified blockchian ID
const blockchain = "dcc98e38e1edb55a97265efca6c34f21e55f683abdded0aa71df3958a49c8b69"; //LS (Sirius)testnet 

// create a randomWallet- Returns a encrypted JSON
const lsWallet = LsWallet.Keystore.createRandomWallet();

//store new wallet into an account(this will be used to sign transactions)
const lsAcct = LsWallet.Account.newAccount(lsWallet);

/*
Creates a Pocket relay that takes the following params:
    - dispatchers: Array holding the initial dispatcher url(s).
  - rpcProvider: (optional) Provider which will be used to reach out to the Pocket Core RPC interface.
  - configuration: (optional) Configuration object.
  - store — (optional) Save data using a Key/Value relationship. This object save information in memory.
*/
const httpProvider = new HttpProvider(dispatchers);
const pocket = new Pocket(dispatchers, httpProvider, configuration);

Getting Network Hash:

View our supported networks page to see other networks we support along with their network hash .

Creating AAT Token

The following section is to show you how to create an AAT token using the Pocket JS library.

async function sendRelay() {
  
    // generate a new client account.
    const clientPassphrase = "1234";
    const clientAccount = await pocket.keybase.createAccount(clientPassphrase);

    /*
      Import application acct: 
        - privateKey: (required) The application accounts private key
        - passphrase: (required) A passphrase to encrypt the private key iin the keybase 
    */
    const importacct = await pocket.keybase.importAccount(appPrivKeyHex,"Diglett");
    const unlockAcct =  await pocket.keybase.unlockAccount(clientAccount.addressHex,clientPassphrase,0);

    //optional test to check if it has been unlocked returns true or false.
    const isUnlocked = await pocket.keybase.isUnlocked(clientAccount.addressHex);

    /* 
       pocketAAT: Creates a PocketAAT object, and creates the signature using the provided parameters:
        - version: The spec version under which this ATT needs to be interpreted.
        - clientPublicKey: The client wallets address
        - applicationPublicKey: The hexadecimal publicKey of the Application
        - privateKey: private key of the application    
   */
  
    const pocketAAT = PocketAAT.from("0.0.1", clientAccount.publicKey.toString("hex"),appPubKeyHex,appPrivKeyHex);

Create Provider and Send a Relay:

Now that we have everything set up, it's time to put everything together.

Create Pocket Provider:

//**** Still in sendRelay()**** 

/*
  Create the Pocket Provider instance:
     - activeBlockchain: Target blockchain hash
     - pocketAAT: Pocket Authentication Token object.
     - pocket: Pocket instance
     - transactionSigner: Object containing the TransactionSigner interface methods.
 */  
const pocketProvider = new PocketProvider(blockchain, pocketAAT, pocket,transactionSigner);


// inject into Lightstreams Web3:
const lsWeb3 = await web3.newEngine(pocketProvider)

Query Balance:

To get a wallet balance:

const lsBal = await web3.getBalance(lsWeb3,addr);
 
 } // end of async function sendRelay()

Updated 3 months ago


Connect to Lightstreams


Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.