previousSignaturePayload method

Uint8List previousSignaturePayload()

Generate the payload for the previous signature by encoding address, type and data

Implementation

Uint8List previousSignaturePayload() {
  final bufCodeSize = toByteArray(data!.code!.length, length: 4);
  // ignore: prefer_final_locals
  var contentSize = utf8.encode(data!.content!).length;
  final bufContentSize = toByteArray(contentSize, length: 4);

  var ownershipsBuffers = Uint8List(0);
  for (final ownership in data!.ownerships!) {
    final bufAuthKeyLength = Uint8List.fromList(
      toByteArray(ownership.authorizedPublicKeys!.length),
    );

    final authorizedKeysBuffer = <Uint8List>[
      Uint8List.fromList(<int>[bufAuthKeyLength.length]),
      bufAuthKeyLength
    ];

    for (final element in ownership.authorizedPublicKeys!) {
      if (element.publicKey != null) {
        element.publicKey = element.publicKey!.toUpperCase();
      }
    }

    ownership.authorizedPublicKeys!.sort(
      (AuthorizedKey a, AuthorizedKey b) =>
          a.publicKey!.compareTo(b.publicKey!),
    );

    for (final authorizedKey in ownership.authorizedPublicKeys!) {
      authorizedKeysBuffer
          .add(Uint8List.fromList(hexToUint8List(authorizedKey.publicKey!)));
      authorizedKeysBuffer.add(
        Uint8List.fromList(
          hexToUint8List(authorizedKey.encryptedSecretKey!),
        ),
      );
    }

    ownershipsBuffers = concatUint8List(<Uint8List>[
      ownershipsBuffers,
      toByteArray(
        Uint8List.fromList(hexToUint8List(ownership.secret!)).lengthInBytes,
        length: 4,
      ),
      Uint8List.fromList(hexToUint8List(ownership.secret!)),
      concatUint8List(authorizedKeysBuffer)
    ]);
  }

  var ucoTransfersBuffers = Uint8List(0);
  if (data!.ledger!.uco!.transfers!.isNotEmpty) {
    for (final ucoTransfer in data!.ledger!.uco!.transfers!) {
      ucoTransfersBuffers = concatUint8List(<Uint8List>[
        ucoTransfersBuffers,
        Uint8List.fromList(hexToUint8List(ucoTransfer.to!)),
        toByteArray(ucoTransfer.amount!, length: 8)
      ]);
    }
  }

  var tokenTransfersBuffers = Uint8List(0);
  if (data!.ledger!.token!.transfers!.isNotEmpty) {
    for (final tokenTransfer in data!.ledger!.token!.transfers!) {
      final bufTokenId =
          Uint8List.fromList(toByteArray(tokenTransfer.tokenId!));

      tokenTransfersBuffers = concatUint8List(<Uint8List>[
        tokenTransfersBuffers,
        Uint8List.fromList(hexToUint8List(tokenTransfer.tokenAddress!)),
        Uint8List.fromList(hexToUint8List(tokenTransfer.to!)),
        toByteArray(tokenTransfer.amount!, length: 8),
        Uint8List.fromList(<int>[bufTokenId.length]),
        bufTokenId
      ]);
    }
  }

  var recipients = Uint8List(0);
  for (final recipient in data!.recipients!) {
    recipients = concatUint8List(<Uint8List>[
      recipients,
      Uint8List.fromList(hexToUint8List(recipient))
    ]);
  }

  final bufOwnershipLength =
      Uint8List.fromList(toByteArray(data!.ownerships!.length));
  final bufUCOTransferLength =
      Uint8List.fromList(toByteArray(data!.ledger!.uco!.transfers!.length));
  final bufTokenTransferLength =
      Uint8List.fromList(toByteArray(data!.ledger!.token!.transfers!.length));
  final bufRecipientLength =
      Uint8List.fromList(toByteArray(data!.recipients!.length));

  return concatUint8List(<Uint8List>[
    toByteArray(version!, length: 4),
    Uint8List.fromList(hexToUint8List(address!)),
    Uint8List.fromList(<int>[txTypes[type]!]),
    bufCodeSize,
    Uint8List.fromList(utf8.encode(data!.code!)),
    bufContentSize,
    Uint8List.fromList(utf8.encode(data!.content!)),
    Uint8List.fromList(<int>[bufOwnershipLength.length]),
    bufOwnershipLength,
    ownershipsBuffers,
    Uint8List.fromList(<int>[bufUCOTransferLength.length]),
    bufUCOTransferLength,
    ucoTransfersBuffers,
    Uint8List.fromList(<int>[bufTokenTransferLength.length]),
    bufTokenTransferLength,
    tokenTransfersBuffers,
    Uint8List.fromList(<int>[bufRecipientLength.length]),
    bufRecipientLength,
    recipients
  ]);
}