getMessageComponents method

Tuple<MessageHeader, List<SolAddress>> getMessageComponents()

Implementation

Tuple<MessageHeader, List<SolAddress>> getMessageComponents() {
  final mapEntries = _keyMetaMap.entries.toList();
  if (mapEntries.length > SolanaTransactionConstant.maximumAccountKeys) {
    throw MessageException("Max static account keys length exceeded");
  }
  final writableSigners = mapEntries
      .where((entry) => entry.value.isSigner && entry.value.isWritable)
      .toList();
  final readonlySigners = mapEntries
      .where((entry) => entry.value.isSigner && !entry.value.isWritable)
      .toList();
  final writableNonSigners = mapEntries
      .where((entry) => !entry.value.isSigner && entry.value.isWritable)
      .toList();
  final readonlyNonSigners = mapEntries
      .where((entry) => !entry.value.isSigner && !entry.value.isWritable)
      .toList();

  final MessageHeader header = MessageHeader(
      numRequiredSignatures: writableSigners.length + readonlySigners.length,
      numReadonlySignedAccounts: readonlySigners.length,
      numReadonlyUnsignedAccounts: readonlyNonSigners.length);
  if (writableSigners.isEmpty) {
    throw MessageException("Expected at least one writable signer key");
  }
  final payerAddress = writableSigners[0].key;
  if (payerAddress != payer.address) {
    throw MessageException(
        "Expected first writable signer key to be the fee payer");
  }
  final List<SolAddress> staticAccountKeys = List<SolAddress>.unmodifiable([
    ...writableSigners.map((entry) => SolAddress(entry.key)),
    ...readonlySigners.map((entry) => SolAddress(entry.key)),
    ...writableNonSigners.map((entry) => SolAddress(entry.key)),
    ...readonlyNonSigners.map((entry) => SolAddress(entry.key)),
  ]);

  return Tuple(header, staticAccountKeys);
}