import { Pocket } from'@pokt-network/pocket-js'constRECEIVER_ADDRESS='...'constSENDER_ADDRESS='...'constSENDER_PRIVATE_KEY='...'// The passphrase used to encrypt the private key while in memory:constPASSPHRASE='foobar'// PocketJS must always be initialized with at least one dispatcher.constPOCKET_DISPATCHER='https://dispatch-1.nodes.pokt.network:4201'// To send a transaction, you can use one of the public RPCs or// your own Pocket node.constPOCKET_RPC='https://mainnet-1.nodes.pokt.network:4201'constpocket=newPocket(POCKET_DISPATCHER,POCKET_RPC)// If you are using Pocket Mainnet, make sure to disable legacyCodecpocket.configuration.useLegacyTxCodec =false;// Create a transaction signer using the `withPrivateKey` method:consttxSigner=pocket.withPrivateKey(SENDER_PRIVATE_KEY)consttransactionResponse=awaittxSigner.send(// Origin address for the sendSENDER_ADDRESS,// Receiver addressRECEIVER_ADDRESS,// 10 POKT'10000000' ).submit('mainnet',// The transaction fee is always 10,000 uPOKT'10000' )// Check if the transaction returned an error:if (typeGuard(transactionResponse, RpcError)) {thrownewError(transactionResponse.message)}// You will be able to look up this transaction through this hash after the // next block clears.const { hash } = transactionResponseconsole.log(hash)
Creating a Signed SEND Transaction Offline
To create a signed transaction that can be sent immediately or stored:
import { Pocket } from'@pokt-network/pocket-js'constRECEIVER_ADDRESS='...'constSENDER_ADDRESS='...'constSENDER_PRIVATE_KEY='...'// The passphrase used to encrypt the private key while in memory:constPASSPHRASE='foobar'// PocketJS must always be initialized with at least one dispatcher.constPOCKET_DISPATCHER='https://dispatch-1.nodes.pokt.network:4201'constpocket=newPocket(POCKET_DISPATCHER)// If you are using Pocket Mainnet, make sure to disable legacyCodecpocket.configuration.useLegacyTxCodec =false;// Create a transaction signer using the `withPrivateKey` method:consttxSigner=pocket.withPrivateKey(SENDER_PRIVATE_KEY)// Now use the transaction signer to create a signed SEND transactionconsttxSignerWithSendTransaction=txSigner.send(// Origin address for the sendSENDER_ADDRESS,// Receiver addressRECEIVER_ADDRESS,// 10 POKT'10000000')// Generate offline signed send transactionconstsendTx=awaittxSignerWithSendTransaction.createTransaction('mainnet',// The transaction fee is always 10,000 uPOKT'10000')console.log('Offline signed send transaction:', sendTx)
After calling .sendTransaction(), you will get back a response with this format:
Calculate transaction hash from raw transaction bytes
constcrypto=require('crypto');// This is the raw transaction bytes obtained from offline signed transactionconsttxHex='d1010a4....bf8970d'consttxHash=crypto.createHash('sha256').update(Buffer.from(txHex,'hex')).digest('hex');console.log(txHash)
Deserialize offline signed SEND transaction
You can also decode the raw transaction bytes generated offline (only works for SEND transactions):
// Only supported for versions >= 0.7.1const { ProtoTxDecoder } =require('@pokt-network/pocket-js')constENCODED_TX_BYTES=Buffer.from('d1010a4....bf8970d','hex')constprotoTxDecoder=awaitpocket.withProtoTxDecoder()constprotoStdTx=awaitprotoTxDecoder.unmarshalStdTx(ENCODED_TX_BYTES)constdata=awaitprotoTxDecoder.decodeStdTxData(protoStdTx)console.log('Deserialized transaction:', data)