Signing Transactions

The useWallet hook/composable/primitive provides two methods for signing Algorand transactions:

  • signTransactions - For directly signing transactions

  • transactionSigner - For use with transaction composers that accept an algosdk.TransactionSigner

circle-exclamation

signTransactions

The signTransactions method accepts either:

  • An array of algosdk.Transaction objects

  • An array of encoded transactions (Uint8Array)

  • An array of arrays of either of the above (for transaction groups)

It returns an array of Uint8Array | null, where null indicates an unsigned transaction.

import { useWallet } from '@txnlab/use-wallet-react'
import algosdk from 'algosdk'

function SendTransaction() {
  const {
    activeAddress,
    signTransactions,
    algodClient
  } = useWallet()

  const handleSend = async () => {
    if (!activeAddress) return

    try {
      // Create transaction
      const suggestedParams = await algodClient.getTransactionParams().do()
      const transaction = algosdk.makePaymentTxnWithSuggestedParamsFromObject({
        sender: activeAddress,
        receiver: activeAddress,
        amount: 0,
        suggestedParams
      })

      // Sign transaction
      const signedTxns = await signTransactions([transaction])

      // Send transaction
      const { txid } = await algodClient.sendRawTransaction(signedTxns).do()
      
      // Wait for confirmation
      const result = await algosdk.waitForConfirmation(algodClient, txid, 4)
      console.log(`Transaction confirmed at round ${result['confirmed-round']}`)
    } catch (error) {
      console.error('Error:', error)
    }
  }

  return (
    <button onClick={handleSend}>Send Transaction</button>
  )
}

For more information about constructing and sending transactions with the Algorand JavaScript SDK, see the JS SDK: Your First Transactionarrow-up-right guide in the Algorand Developer Portal.

transactionSigner

The transactionSigner provides a typed algosdk.TransactionSigner that can be used with transaction composers. This is particularly useful when working with ABI method calls or when you need to compose multiple transactions.

With Atomic Transaction Composer

The AtomicTransactionComposer (ATC) is the recommended way to build and execute groups of transactions, especially when working with smart contracts.

Here's an example using ATC to combine an ABI method call with a payment transaction:

For more information, see the Atomic Transaction Composer guidearrow-up-right in the Algorand Developer Portal.

With AlgoKit Utils

The most powerful way to interact with Algorand is using AlgoKit Utilsarrow-up-right' AlgorandClient. This class provides a unified interface for all Algorand functionality, including:

  • Smart contract interactions via typed clients

  • Transaction creation and composition

  • Fee calculations and simulation

Here's the same transaction group (ABI method call + payment) composed using AlgoKit Utils:

For more information, see:

Last updated