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,
    ];

    final newAuthorizedPublicKeys = <AuthorizedKey>[];
    for (final element in ownership.authorizedPublicKeys) {
      if (element.publicKey != null) {
        newAuthorizedPublicKeys.add(
          AuthorizedKey(
            encryptedSecretKey: element.encryptedSecretKey,
            publicKey: element.publicKey!.toUpperCase(),
          ),
        );
      }
    }

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

    for (final authorizedKey in newAuthorizedPublicKeys) {
      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 recipientsBuffers = Uint8List(0);

  if (data!.actionRecipients.isNotEmpty) {
    for (final recipient in data!.actionRecipients) {
      if (recipient.action == null && recipient.args == null) {
        recipientsBuffers = concatUint8List(<Uint8List>[
          recipientsBuffers,
          // 0 = unnamed action
          Uint8List.fromList([0]),
          hexToUint8List(recipient.address!),
        ]);
      } else {
        final serializedArgs = recipient.args!
            .map((arg) => typed_encoding.serialize(arg))
            .toList();
        recipientsBuffers = concatUint8List(
          <Uint8List>[
            recipientsBuffers,
            // 1 = named action
            Uint8List.fromList([1]),
            hexToUint8List(recipient.address!),
            toByteArray(recipient.action!.length),
            Uint8List.fromList(utf8.encode(recipient.action!)),
            Uint8List.fromList([serializedArgs.length]),
            ...serializedArgs,
          ],
        );
      }
    }
  }

  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!.actionRecipients.length));

  return concatUint8List(<Uint8List>[
    toByteArray(version, length: 4),
    Uint8List.fromList(hexToUint8List(address!.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,
    recipientsBuffers,
  ]);
}