deserializeV0 static method

MessageV0 deserializeV0(
  1. List<int> serializedMessage
)

convert bytes to Message V0

Implementation

static MessageV0 deserializeV0(List<int> serializedMessage) {
  List<int> byteArray = List<int>.from(serializedMessage, growable: true);
  final prefix = byteArray.removeAt(0);
  final maskedPrefix = prefix & SolanaTransactionConstant.versionPrefixMask;
  assert(prefix != maskedPrefix,
      'Expected versioned message but received legacy message');
  final version = maskedPrefix;
  assert(version == 0,
      'Expected versioned message with version 0 but found version $version');
  final MessageHeader header = MessageHeader(
      numRequiredSignatures: byteArray.removeAt(0),
      numReadonlySignedAccounts: byteArray.removeAt(0),
      numReadonlyUnsignedAccounts: byteArray.removeAt(0));

  final staticAccountKeys = <SolAddress>[];
  final staticAccountKeysLength =
      SolanaTransactionUtils._decodeLength(byteArray);
  for (var i = 0; i < staticAccountKeysLength; i++) {
    staticAccountKeys.add(SolAddress.uncheckBytes(
        byteArray.sublist(0, SolanaTransactionConstant.publicKeyLength)));
    byteArray.removeRange(0, SolanaTransactionConstant.publicKeyLength);
  }
  final recentBlockhash = SolAddress.uncheckBytes(
      byteArray.sublist(0, SolanaTransactionConstant.publicKeyLength));
  byteArray.removeRange(0, SolanaTransactionConstant.publicKeyLength);
  final instructionCount = SolanaTransactionUtils._decodeLength(byteArray);
  final compiledInstructions = <CompiledInstruction>[];
  for (var i = 0; i < instructionCount; i++) {
    final programIdIndex = byteArray.removeAt(0);
    final accountKeyIndexesLength =
        SolanaTransactionUtils._decodeLength(byteArray);
    final accountKeyIndexes = byteArray.sublist(0, accountKeyIndexesLength);
    byteArray.removeRange(0, accountKeyIndexesLength);
    final dataLength = SolanaTransactionUtils._decodeLength(byteArray);
    final data = byteArray.sublist(0, dataLength);
    byteArray.removeRange(0, dataLength);
    compiledInstructions.add(CompiledInstruction(
      programIdIndex: programIdIndex,
      accounts: accountKeyIndexes,
      data: data,
    ));
  }
  final addressTableLookupsCount =
      SolanaTransactionUtils._decodeLength(byteArray);
  final addressTableLookups = <AddressTableLookup>[];
  for (int i = 0; i < addressTableLookupsCount; i++) {
    final accountKey = SolAddress.uncheckBytes(
        byteArray.sublist(0, SolanaTransactionConstant.publicKeyLength));
    byteArray.removeRange(0, SolanaTransactionConstant.publicKeyLength);
    final writableIndexesLength =
        SolanaTransactionUtils._decodeLength(byteArray);
    final writableIndexes = byteArray.sublist(0, writableIndexesLength);
    byteArray.removeRange(0, writableIndexesLength);
    final readonlyIndexesLength =
        SolanaTransactionUtils._decodeLength(byteArray);
    final readonlyIndexes = byteArray.sublist(0, readonlyIndexesLength);
    byteArray.removeRange(0, readonlyIndexesLength);
    addressTableLookups.add(AddressTableLookup(
      accountKey: accountKey,
      writableIndexes: writableIndexes,
      readonlyIndexes: readonlyIndexes,
    ));
  }
  return MessageV0(
    header: header,
    accountKeys: staticAccountKeys,
    recentBlockhash: recentBlockhash,
    compiledInstructions: compiledInstructions,
    addressTableLookups: addressTableLookups,
  );
}