Create Relay With Pocket JS

The Pocket-JS is the core client used for sending relays to any network that is running on the Pocket Network.

Prerequisites:

Installing Pocket-JS

To install the Pocket-JS library in your project, run the following NPM command or add pocket-js in your package.json:

npm install --save @pokt-network/pocket-js

Creating a Relay

Import Pocket Library

To create a relay we have to import our @pokt-network/pocket-js module and declare some variables.

// Import PocketJS and declare the following variables
const PocketJS = require('@pokt-network/pocket-js');
const Pocket = PocketJS.Pocket;
const Configuration = PocketJS.Configuration;
const Provider = PocketJS.HttpRpcProvider;
const PocketAAT = PocketJS.PocketAAT;

Create Pocket Instance With Base Configurations:

Below are the required instances that need to be set up in order to send a relay.

/*
Create an array of dispatchers that will be connecting you to a Pocket Node. A list of Dispatchers can be found here: https://docs.pokt.network/v2.1/docs/known-dispatcher-list
*/

const dispatchers = [new URL("http://node1.testnet.pokt.network:8081"), new URL("http://node2.testnet.pokt.network:8081")];

/* 
The blockchain hash needed to identify the blockchain you wish to connect to. See Supported Networks(https://docs.pokt.network/v2.1/docs/supported-networks) 
*/
const blockchain = "8cf7f8799c5b30d36c86d18f0f4ca041cf1803e0414ed9e9fd3a19ba2f0938ff";


/*
(optional)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(1,5,3,30000,false)

// create RPC provider 
const rpcProvider = new Provider(dispatchers)

/*
 create a pocket instance and stores muliple configuration options for your node
	- 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 pocket = new Pocket(dispatchers, rpcProvider, configuration)

Unlock Application Account and Create AAT

Before we can start relaying application data to your desired blockchain, you will need to import your application account that has already been staked on the network.

const appPubKeyHex = "ab4e...";
const appPrivKeyHex = "4128...";
const passphrase = "pocket";

async function sendRelay() {

    /*
    create a client account that will be used to relay requests on the application account behalf. All you will need is: 
    - passphrase: a passphrase that will be used to encrypt the account
    
    */
    const clientPassphrase = "1234";
    const clientAccount = await pocket.keybase.createAccount(clientPassphrase);


    // import Application acct
    const importacct = await pocket.keybase.importAccount(appPrivKeyHex,passphrase);
  /*
  Unlock client account with:
  - addressHex: address of the account that will be unlocked in hex string format
  - passphrase: passphrase of the account to unlock
  - unlockPeriod: The amount of time (in ms) the account is going to be unlocked
  */
    const unlockAcct =  await pocket.keybase.unlockAccount(clientAccount.addressHex,clientPassphrase,0);

    //(optional) test to see if the client account has been sucessfully unlocked. It will return: True or False 
    const isUnlocked = await pocket.keybase.isUnlocked(clientAccount.addressHex);
  
    /*
    Create AAT Token with the following arguments:
    - version: Version information
    - clientPublicKey:Client Public Key
    - applicationPublicKey: Application Public Key
    - privateKey: Application Private Key
    */
    const pocketAAT = PocketAAT.from("0.0.1", clientAccount.publicKey.toString("hex"),appPubKeyHex.toString("hex"),appPrivKeyHex);

  
	...  
    
}

Sending the Relay:

To send the relay, we need to create a payload with the data we are querying(or writing) to the blockchain.

/*
	continuing inside the sendRelay() function 
*/

//Create the payload for the transaction  

var payload = {
        "jsonrpc": "2.0",
        "method": "eth_getBalance",
        "params": ["0xf892400Dc3C5a5eeBc96070ccd575D6A720F0F9f", "latest"],
        "id": (new Date()).getTime()
    }

//send the relay to the pocket network
const relayResponse = await pocket.sendRelay(payload, blockchain, pocketAAT);

console.log(relayResponse);


    

Updated 4 days ago



Create Relay With Pocket JS


Suggested Edits are limited on API Reference Pages

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