Class Transaction

    Transaction class representing a Klever blockchain transaction Extends the protobuf Transaction class with convenience methods

    This class wraps the proto-generated Transaction and provides:

    • Easy signing with private keys
    • Serialization to bytes/hex
    • Fee calculations
    • Transaction state management

    This is a pure data class - no network dependencies. Use provider.sendRawTransaction(tx.toHex()) to broadcast.

    Hierarchy
    • Transaction
      • Transaction

    Constructors

    Properties

    Block: number

    Transaction Block.

    GasLimit: number

    Transaction GasLimit.

    GasMultiplier: number

    Transaction GasMultiplier.

    RawData?: null | IRaw

    Transaction RawData.

    Receipts: IReceipt[]

    Transaction Receipts.

    Result: TXResult

    Transaction Result.

    ResultCode: TXResultCode

    Transaction ResultCode.

    Signature: Uint8Array<ArrayBufferLike>[]

    Transaction Signature.

    Methods

    • Get the transaction hash Computes blake2b hash of the RawData proto bytes

      Returns string

      Transaction hash as hex string

      const tx = new Transaction(txData)
      const hash = tx.getHash()
      // hash: "a3f2e8d9..."
    • Get the transaction hash bytes Computes blake2b hash of the RawData proto bytes

      Returns Uint8Array

      Transaction hash as Uint8Array

      const tx = new Transaction(txData)
      const hashBytes = tx.getHashBytes()
      // Use for signing or other operations
    • Get total fee (KAppFee + BandwidthFee) Returns the combined fee for the transaction in smallest KLV units

      Returns bigint

      Total fee amount in KLV smallest units (1 KLV = 1,000,000 units)

      const tx = new Transaction(txData)
      const totalFee = tx.getTotalFee()
      console.log(`Total fee: ${totalFee} units`) // e.g., "600000 units"

      // Convert to human-readable KLV
      const feeInKLV = Number(totalFee) / 1_000_000
      console.log(`Fee: ${feeInKLV} KLV`) // e.g., "0.6 KLV"
    • Check if transaction is signed Verifies whether the transaction has been signed and is ready for broadcast

      Returns boolean

      true if transaction has at least one signature, false otherwise

      const tx = new Transaction(txData)
      console.log(tx.isSigned()) // false

      await tx.sign(privateKey)
      console.log(tx.isSigned()) // true

      // Only broadcast if signed
      if (tx.isSigned()) {
      await provider.sendRawTransaction(tx.toHex())
      }
    • Sign the transaction with a private key Signs the transaction hash (blake2b of RawData)

      Parameters

      • privateKey: PrivateKey

        Private key to sign with

      Returns Promise<Transaction>

      This transaction instance with signature added

      const tx = new Transaction(txData)
      await tx.sign(privateKey)
      // tx now has Signature field populated
    • Get raw proto bytes for the transaction This is what gets signed and broadcast to the network

      Returns Uint8Array

      Proto bytes as Uint8Array

      const tx = new Transaction(txData)
      const bytes = tx.toBytes()
      // bytes: Uint8Array [10, 32, 65, ...]
      // Use for signing or network transmission
    • Get proto bytes as hex string Useful for broadcasting to node via HTTP or storing in databases

      Returns string

      Hex encoded proto bytes (without 0x prefix)

      const tx = new Transaction(txData)
      await tx.sign(privateKey)
      const hex = tx.toHex()
      // hex: "0a20418f2b3c..."

      // Broadcast to network
      const hash = await provider.sendRawTransaction(hex)
    • Creates a new Transaction instance using the specified properties.

      Parameters

      • Optionalproperties: ITransaction

        Properties to set

      Returns Transaction

      Transaction instance

    • Decodes a Transaction message from the specified reader or buffer.

      Parameters

      • reader: Uint8Array<ArrayBufferLike> | Reader

        Reader or buffer to decode from

      • Optionallength: number

        Message length if known beforehand

      Returns Transaction

      Transaction

      If the payload is not a reader or valid buffer

      If required fields are missing

    • Decodes a Transaction message from the specified reader or buffer, length delimited.

      Parameters

      • reader: Uint8Array<ArrayBufferLike> | Reader

        Reader or buffer to decode from

      Returns Transaction

      Transaction

      If the payload is not a reader or valid buffer

      If required fields are missing

    • Encodes the specified Transaction message. Does not implicitly verify messages.

      Parameters

      • message: ITransaction

        Transaction message or plain object to encode

      • Optionalwriter: Writer

        Writer to encode to

      Returns Writer

      Writer

    • Encodes the specified Transaction message, length delimited. Does not implicitly verify messages.

      Parameters

      • message: ITransaction

        Transaction message or plain object to encode

      • Optionalwriter: Writer

        Writer to encode to

      Returns Writer

      Writer

    • Create a Transaction from raw proto bytes Decodes protobuf bytes back into a Transaction object

      Parameters

      • bytes: Uint8Array

        Proto-encoded transaction bytes

      Returns Transaction

      Transaction instance

      // Decode bytes from storage or network
      const bytes = new Uint8Array([10, 32, 65, ...])
      const tx = Transaction.fromBytes(bytes)

      // Verify and use
      console.log(tx.getHash())
      console.log(tx.isSigned())
    • Create a Transaction from hex-encoded proto bytes Decodes a hex string back into a Transaction object

      Parameters

      • hex: string

        Hex string of proto-encoded transaction (with or without 0x prefix)

      Returns Transaction

      Transaction instance

      // Decode hex string from storage or API
      const hex = "0a20418f2b3c..."
      const tx = Transaction.fromHex(hex)

      // Verify signature
      console.log(tx.isSigned()) // true or false

      // Get transaction hash
      console.log(tx.getHash())
    • Create a Transaction from a plain JSON object (from API) Properly converts base64 strings to Uint8Array for proto fields. This is typically used when receiving transaction data from the node's API.

      Parameters

      • obj: { [k: string]: unknown }

        Plain object with base64-encoded byte fields

      Returns Transaction

      Transaction instance

      // Response from node's /transaction/build endpoint
      const response = await fetch('/transaction/build', {
      method: 'POST',
      body: JSON.stringify(buildRequest)
      })
      const data = await response.json()

      // Convert API response to Transaction
      const tx = Transaction.fromObject(data.result)

      // Sign and broadcast
      await tx.sign(privateKey)
      await provider.sendRawTransaction(tx.toHex())
    • Create a new Transaction from an existing Transaction Creates a deep copy of the transaction

      Parameters

      Returns Transaction

      New Transaction instance

      const originalTx = new Transaction(txData)
      const copiedTx = Transaction.fromTransaction(originalTx)

      // Modifications to copiedTx won't affect originalTx
      await copiedTx.sign(privateKey)
    • Creates a plain object from a Transaction message. Also converts values to other types if specified.

      Parameters

      • message: Transaction

        Transaction

      • Optionaloptions: IConversionOptions

        Conversion options

      Returns { [k: string]: any }

      Plain object

    • Verifies a Transaction message.

      Parameters

      • message: { [k: string]: any }

        Plain object to verify

      Returns null | string

      null if valid, otherwise the reason why it is not