serializeLegacy static method

List<int> serializeLegacy(
  1. Message message
)

serialize legacy Message to bytes

Implementation

static List<int> serializeLegacy(Message message) {
  final numKeys = message.accountKeys.length;
  final keyCount = SolanaTransactionUtils._encodeLength(numKeys);
  final List<Map<String, dynamic>> instructions =
      message.compiledInstructions.map((instruction) {
    final accounts = instruction.accounts;
    final programIdIndex = instruction.programIdIndex;
    final data = List<int>.from(instruction.data);
    final keyIndicesCount =
        SolanaTransactionUtils._encodeLength(accounts.length);
    final dataCount = SolanaTransactionUtils._encodeLength(data.length);

    return {
      'programIdIndex': programIdIndex,
      'keyIndicesCount': keyIndicesCount,
      'keyIndices': accounts,
      'dataLength': dataCount,
      'data': data,
    };
  }).toList();

  final instructionCount =
      SolanaTransactionUtils._encodeLength(instructions.length);
  List<int> instructionBuffer =
      List<int>.filled(SolanaTransactionConstant.packetDataSize, 0);
  instructionBuffer.setAll(0, instructionCount);
  int instructionBufferLength = instructionCount.length;
  for (var instruction in instructions) {
    final instructionLayout = LayoutConst.struct([
      LayoutConst.u8(property: 'programIdIndex'),
      LayoutConst.blob((instruction['keyIndicesCount'] as List).length,
          property: 'keyIndicesCount'),
      LayoutConst.array(LayoutConst.u8(property: 'keyIndex'),
          (instruction['keyIndices'] as List).length,
          property: 'keyIndices'),
      LayoutConst.blob((instruction['dataLength'] as List).length,
          property: 'dataLength'),
      LayoutConst.array(LayoutConst.u8(property: 'userdatum'),
          (instruction['data'] as List).length,
          property: 'data'),
    ]);

    final encode = instructionLayout.serialize(instruction);
    instructionBuffer.setAll(instructionBufferLength, encode);
    instructionBufferLength += encode.length;
  }
  instructionBuffer = instructionBuffer.sublist(0, instructionBufferLength);
  final signDataLayout = LayoutConst.struct([
    LayoutConst.blob(1, property: 'numRequiredSignatures'),
    LayoutConst.blob(1, property: 'numReadonlySignedAccounts'),
    LayoutConst.blob(1, property: 'numReadonlyUnsignedAccounts'),
    LayoutConst.blob(keyCount.length, property: 'keyCount'),
    LayoutConst.array(LayoutConst.blob(32, property: 'key'), numKeys,
        property: 'keys'),
    LayoutConst.blob(32, property: 'recentBlockhash'),
  ]);
  final transaction = {
    'numRequiredSignatures': <int>[message.header.numRequiredSignatures],
    'numReadonlySignedAccounts': <int>[
      message.header.numReadonlySignedAccounts
    ],
    'numReadonlyUnsignedAccounts': <int>[
      message.header.numReadonlyUnsignedAccounts
    ],
    'keyCount': keyCount,
    'keys': message.accountKeys.map((key) => key.toBytes()).toList(),
    'recentBlockhash': message.recentBlockhash.toBytes(),
  };
  final List<int> signedData = List<int>.filled(2048, 0);
  // final signData = LayoutByteWriter.filled(2048);
  final encode = signDataLayout.serialize(transaction);
  signedData.setAll(0, encode);
  signedData.setAll(encode.length, instructionBuffer);
  // signData.setAll(length, instructionBuffer.toBytes());

  return signedData.sublist(0, encode.length + instructionBufferLength);
}