addSignature method

void addSignature(
  1. SolAddress address,
  2. List<int> signature, {
  3. bool verifySignature = true,
})

Adds a signature to the transaction.

Implementation

void addSignature(SolAddress address, List<int> signature,
    {bool verifySignature = true}) {
  if (signature.length != SolanaTransactionConstant.signatureLengthInBytes) {
    throw const MessageException("Signature must be 64 bytes long");
  }
  final signerPubkeys =
      message.accountKeys.sublist(0, message.header.numRequiredSignatures);
  final signerIndex =
      signerPubkeys.indexWhere((pubkey) => pubkey.address == address.address);
  if (signerIndex < 0) {
    throw MessageException(
        "Cannot add signature, $address is not required to sign this transaction");
  }
  if (verifySignature &&
      !address
          .toPublicKey()
          .verify(message: serializeMessage(), signature: signature)) {
    throw const MessageException("Signature verification failed.");
  }
  List<List<int>> currentSigs = List.from(_signatures);
  currentSigs[signerIndex] = List<int>.unmodifiable(signature);
  _signatures = List<List<int>>.unmodifiable(currentSigs);
}