gatherSignatures method

Future<List<SignedTransaction>> gatherSignatures()

Obtain signatures for each transaction in this group. If signatures have already been obtained, this method will return cached versions of the signatures.

The composer's status will be at least SIGNED after executing this method.

An error will be thrown if signing any of the transactions fails.

@return an array of signed transactions.

Implementation

Future<List<SignedTransaction>> gatherSignatures() async {
  final compareTo = status.index - AtcStatus.SIGNED.index;
  if (compareTo >= 0) {
    return signedTxns;
  }

  final transactions = buildGroup();
  final txnSignerToIndices = <TxnSigner, List<int>>{};
  final tempSignedTxns =
      List<SignedTransaction?>.filled(transactions.length, null);
  for (var i = 0; i < transactions.length; i++) {
    final tws = transactions[i];
    if (!txnSignerToIndices.containsKey(tws.signer)) {
      txnSignerToIndices[tws.signer] = <int>[];
    }
    txnSignerToIndices[tws.signer]?.add(i);
  }

  final txnGroup = List<RawTransaction?>.filled(transactions.length, null);
  for (var i = 0; i < transactions.length; i++) {
    txnGroup[i] = transactions[i].transaction;
  }

  for (var txnSigner in txnSignerToIndices.keys) {
    final indices = txnSignerToIndices[txnSigner];
    if (indices != null) {
      final signed = await txnSigner.signTransactions(
        txnGroup.whereNotNull().toList(),
        indices,
      );

      for (var i = 0; i < indices.length; i++) {
        tempSignedTxns[indices[i]] = signed[i];
      }
    }
  }

  if (tempSignedTxns.contains(null)) {
    throw ArgumentError('Some signer did not sign the transaction');
  }

  signedTxns.clear();
  signedTxns.addAll(tempSignedTxns.whereNotNull().toList());
  _status = AtcStatus.SIGNED;
  return signedTxns;
}