Generate and validate accounts on the native POKT blockchain.
The simplest way to generate new accounts, or addresses, on the POKT blockchain is to use the official Pocket client, PocketJS.
Creating a New Account
To create a new account on the POKT blockchain and export it:
import { Pocket } from'@pokt-network/pocket-js'// PocketJS must always be initialized with at least one dispatcher.constPOCKET_DISPATCHER='https://dispatch-1.nodes.pokt.network:4201'constpocket=newPocket(POCKET_DISPATCHER)// The passphrase used to encrypt the private key while in memory:constPASSPHRASE='foobar'constaccount=awaitpocket.keybase.createAccount(PASSPHRASE)// The result of successful account creation:console.log(account)// Using the exportAccount function, you can obtain a plaintext private key.constexportedAccountPrivateKey=awaitpocket.keybase.exportAccount(account.address.toString('hex'),PASSPHRASE)// This plaintext private key should be encrypted before storage.console.log(exportedAccountPrivateKey.toString('hex'))// You can also export an encrypted JSON version of the same private key.// The passphrase used to encrypt this JSON file is separate from the // previous PASSPHRASE.constexportedPPK=awaitpocket.keybase.exportPPK( exportedAccountPrivateKey,// The PPK passphrase used to encrypt the JSON file'foo',// A hint for the PPK passphrase'what comes before bar')console.log(exportedPPK)
Importing an Existing Account
To import an existing account using either the raw private key or the encrypted JSON PPK:
import { Pocket } from'@pokt-network/pocket-js'// PocketJS must always be initialized with at least one dispatcher.constPOCKET_DISPATCHER='https://dispatch-1.nodes.pokt.network:4201'constpocket=newPocket(POCKET_DISPATCHER)// The passphrase used to encrypt the private key while in memory:constPASSPHRASE='foobar'constPRIVATE_KEY='...'// Import an existing account using the raw private key:constimportedAccount=awaitpocket.keybase.importAccount(PRIVATEKEY,// The passphrase to encrypt the private key while in memoryPASSPHRASE)// Import an account using the encrypted JSON PPK:constimportedPPKAccount=awaitpocket.keybase.importPPK(// The PPK passphrase used when the key was exported'foo',exportedPPK.salt,exportedPPK.secParam,exportedPPK.hint,exportedPPK.cypherText,// The passphrase to encrypt the private key while in memoryPASSPHRASE,)
Verifying an Address
To verify a POKT blockchain address, public key, or raw private key:
import { validateAddressHex, validatePrivateKey, validatePublicKey } from'@pokt-network/pocket-js'// Validate a POKT blockchain address: returns undefined if valid.// This should be wrapped in a try / catch block as it will throw the // appropriate error if the address is not valid.try {constisValidAddress=!(validateAddressHex(account.addressHex) instanceofError )} catch (e) {// Handle the error}// Validate a public key: returns true or false.constisValidPublicKey=validatePublicKey(account.publicKey.toString('hex'))// Validate a private key: returns true or false.constisValidPrivateKey=validatePrivateKey(privateKey)