Generate a TxID Using DijetsJS
Introduction#
A transaction id, or TxID, is a string of numbers and letters which identifies a specific transaction on the blockchain. TXIDs are one of the key components that developers interact with when working on a blockchain system. They're deterministic and can be generated using DijetsNodeGo or DijetsJS.
On Dijets network a TxID is a CB58 encoded string which is created by
sha256
hashing the transaction.
CB58 is a base58
encoding with a checksum. Below are the steps for deterministically generating a
TxID using DijetsJS.
Creating a Signed Transaction#
Let's start by creating a
BaseTx.
Once you have a BaseTx
then
create an UnsignedTx
and sign
it. Lastly convert the Tx
to a Buffer
by
calling tx.toBuffer()
.
// Create an UnsignedTx with the BaseTx const unsignedTx: UnsignedTx = new UnsignedTx(baseTx) // Sign the UnsignedTx to create a Tx const tx: Tx = unsignedTx.sign(xKeychain) // Convert the Tx to a Buffer const txBuf: Buffer = tx.toBuffer()
Generate the TxID#
Next, create a sha256
hash of the Buffer
from the previous step.
// Create sha256 hash of the Tx Buffer const sha256Hash: Buffer = Buffer.from( createHash("sha256").update(txBuf).digest().buffer )
As mentioned in the Introduction, a TxID is a CB58 encoded
string which is created by sha256
hashing the transaction. To create the TxID
now CB58 encode the newly created sha256
hash.
// cb58 the sha256 hash const generatedTxID: string = bintools.cb58Encode(sha256Hash) console.log(`Generated TxID: ${generatedTxID}`)
The generatedTxID
will be a CB58 encoded string similar to eLXEKFFMgGmK7ZLokCFjppdBfGy5hDuRqh5uJVyXXPaRErpAX
.
Confirm TxID Is Correct#
To confirm that the generatedTxID
is correct issue the BaseTx
to DijetsNodeGo
and compare the TxID which is returned with the recently created TxID.
// get the actual txID from the full node const actualTxID: string = await xchain.issueTx(tx) console.log(`Success! TxID: ${actualTxID}`) // Note the generated TxID and the returned TxID match Generated TXID: eLXEKFFMgGmK7ZLokCFjppdBfGy5hDuRqh5uJVyXXPaRErpAX Returned TXID: eLXEKFFMgGmK7ZLokCFjppdBfGy5hDuRqh5uJVyXXPaRErpAX
Summary#
TXIDs are a core component of any blockchain system. They are used extensively
in Dijets when creating transactions, issuing new assets and creating enterprise chains or subnetworks.
TXIDs are deterministically created by sha256
hashing
a Buffer
of the transaction and then CB58 encoding the hash.