signTransaction method

Signs an Aptos raw transaction with the provided account.

  • account: The primary account responsible for signing the transaction.
  • transaction: The raw transaction to be signed.
  • feePayerAccount (optional): An additional account that covers the transaction fee, if applicable.
  • secondarySignerAccounts (optional): A list of secondary accounts for multi-agent transactions.

Returns an AptosSignedTransaction containing the raw transaction and its corresponding authenticator.

Implementation

AptosSignedTransaction signTransaction(
    {required AptosAccount account,
    required AptosRawTransaction transaction,
    AptosAccount? feePayerAccount,
    List<AptosAccount>? secondarySignerAccounts}) {
  final digest = transaction.signingSerialize(
      feePayerAddress: feePayerAccount?.toAddress(),
      secondarySignerAddresses:
          secondarySignerAccounts?.map((e) => e.toAddress()).toList());

  // Sign the transaction digest with the primary account
  final sender = account.signWithAuth(digest);

  // Sign with fee payer account if provided
  AptosAccountAuthenticator? feePayerAuthenticator =
      feePayerAccount?.signWithAuth(digest);

  // Sign with secondary signer accounts if provided
  List<AptosAccountAuthenticator>? secondarySignerAuthenticator =
      secondarySignerAccounts?.map((e) => e.signWithAuth(digest)).toList();

  // Build the transaction authenticator based on the provided signers
  final txAuthenticated = _toTransactionAuthenticated(
      sender: sender,
      feePayerAddress: feePayerAccount?.toAddress(),
      feePayerAuthenticator: feePayerAuthenticator,
      secondarySignerAddressess:
          secondarySignerAccounts?.map((e) => e.toAddress()).toList(),
      secondarySignerAuthenticated: secondarySignerAuthenticator);

  return AptosSignedTransaction(
      rawTransaction: transaction, authenticator: txAuthenticated);
}