Gathering detailed insights and metrics for @sendnodes/pocket-js
Gathering detailed insights and metrics for @sendnodes/pocket-js
Gathering detailed insights and metrics for @sendnodes/pocket-js
Gathering detailed insights and metrics for @sendnodes/pocket-js
npm install @sendnodes/pocket-js
Typescript
Module System
Node Version
NPM Version
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
23
Official Javascript client for connecting an application to the Pocket Network of decentralized nodes.
Pocket-JS is the core client used for sending relays to any network that is currently supported on the Pocket Network.
These instructions will outline how to start developing with the Pocket-JS SDK.
You should have a basic knowledge of blockchain technology and JavaScript. You will also need to install the NPM tool.
npm install --save @pokt-network/pocket-js
Visit our docs site for tutorials and information about the Pocket Network or get started with the examples below:
1const pocketJS = require('@pokt-network/pocket-js') 2const { Pocket, Configuration, HttpRpcProvider, PocketAAT } = pocketJS; 3 4// The dispatcher provides information about your Pocket session so that your 5// application can then connect to the decentralized network of nodes. 6// You can use one of our dispatchers or any node connected to the Pocket blockchain. 7const dispatchURL = new URL("https://node1.mainnet.pokt.network:443") 8const rpcProvider = new HttpRpcProvider(dispatchURL) 9const configuration = new Configuration(5, 1000, 0, 40000) 10const pocketInstance = new Pocket([dispatchURL], rpcProvider, configuration) 11 12// See https://docs.pokt.network/home/resources/references/supported-blockchains for blockchain choices 13const blockchain = "0021" // Ethereum mainnet
An Application Authentication Token is a token signed by an account that has staked for bandwidth as an App on the Pocket blockchain. You can create an Application Authentication Token (AAT) for multiple clients using the Pocket Core CLI.
An example of a properly-formed AAT:
1{ 2 "version": "0.0.1", 3 "clientPublicKey": "78219c51f6157e629948166d3af8c90cf4c4f5b245513b47806ed4dbdb28d0b6", 4 "applicationPublicKey": "a85ffc9026d9c9f7e302785f3f9ddd15c85ddc85eeaa3b24e23b9e736d66361d", 5 "applicationSignature": "727d8bb9167861413b5c85a7f220b7464f05e3740d6f8dc78734fa764a3093ba7b84e81fae4e5574e300177564d93a1ca5b6f0e2bf594367fa39e99510bf800f" 6}
Once you have your AAT, include it with your project as a JSON file.
1const aat = require('./aat.json')
To unlock the AAT for use in your application, you must first import and unlock the AAT's client account indicated by the clientPublicKey
field. The PPK file is obtained through the Pocket Core CLI with pocket accounts export
.
A properly-formed ppk.json file will start with {"kdf":"scrypt"
. Include it with your project as a JSON file along with the passphrase used when creating it:
1const accountPPK = require('./ppk.json') 2const accountPassphrase = 'Qwerty1234!'
Once unlocked, your app can use the AAT to send relayed RPC calls to the external blockchain:
1// This is only called once to setup the Pocket Instance and AAT 2async function unlockAAT(aat, accountPPK, accountPassphrase) { 3 try { 4 const account = await pocketInstance.keybase.importPPKFromJSON( 5 accountPassphrase, 6 JSON.stringify(accountPPK), 7 accountPassphrase 8 ) 9 await pocketInstance.keybase.unlockAccount(account.addressHex, accountPassphrase, 0) 10 return await PocketAAT.fromSignature( 11 aat.version, 12 account.publicKey.toString('hex'), 13 aat.applicationPublicKey, 14 aat.applicationSignature 15 ) 16 } catch(e) { 17 console.log(e) 18 } 19} 20 21// Call this every time you want to fetch RPC data 22async function sendRelay(rpcQuery, blockchain, pocketAAT) { 23 try { 24 return await pocketInstance.sendRelay(rpcQuery, blockchain, pocketAAT) 25 } catch (e) { 26 console.log(e) 27 } 28} 29 30unlockAAT(aat, accountPPK, accountPassphrase).then(pocketAAT => { 31 rpcQuery = '{"jsonrpc":"2.0","id":1,"method":"net_version","params":[]}' 32 sendRelay(rpcQuery, blockchain, pocketAAT).then(result => { 33 console.log(result.payload); 34 }) 35})
If you instead include the staked application's public and private keys, you can generate the AAT on-the-fly:
1const accountPrivateKey = '25a42ad8ef4b5...' 2const accountPublicKey = '6e2cda5a6b6709...' 3const accountPassphrase = 'Qwerty1234!' 4 5// This is only called once to setup the Pocket Instance and AAT 6async function unlockAccount(accountPrivateKey, accountPublicKey, accountPassphrase) { 7 try { 8 const account = await pocketInstance.keybase.importAccount( 9 Buffer.from(accountPrivateKey, 'hex'), 10 accountPassphrase 11 ) 12 await pocketInstance.keybase.unlockAccount(account.addressHex, accountPassphrase, 0) 13 return await PocketAAT.from( 14 "0.0.1", 15 accountPublicKey, 16 accountPublicKey, 17 accountPrivateKey 18 ) 19 } catch(e) { 20 console.log(e) 21 } 22} 23 24// Call this every time you want to fetch RPC data 25async function sendRelay(rpcQuery, blockchain, pocketAAT) { 26 try { 27 return await pocketInstance.sendRelay(rpcQuery, blockchain, pocketAAT) 28 } catch (e) { 29 console.log(e) 30 } 31} 32 33unlockAccount(accountPrivateKey, accountPublicKey, accountPassphrase).then(pocketAAT => { 34 rpcQuery = '{"jsonrpc":"2.0","id":1,"method":"net_version","params":[]}' 35 sendRelay(rpcQuery, blockchain, pocketAAT).then(result => { 36 console.log(result.payload); 37 }) 38})
1const accountAddress = "36b783a1189f605969f438dfaece2a4b38c65752" 2const balance = await pocketInstance.rpc().query.getBalance(accountAddress) 3console.log("Account Balance: " + balance)
npm run test
Please read CONTRIBUTING.md for details on contributions and the process of submitting pull requests.
Join us on Discord for immediate assistance directly from the Pocket Team.
This project is licensed under the MIT License; see the LICENSE.md file for details.
No vulnerabilities found.
No security vulnerabilities found.